(self, size=(250, 250, 250), doHandSize=True)
return numpy.array((0, 0, 0), numpy.float), size
Detect the hand as closest object to camera
        :param size: bounding box size
        :return: center of mass of hand
Detect the hand as closest object to camera
        :param size: bounding box size
        :return: center of mass of hand
Detect the hand as closest object to camera : param size : bounding box size : return : center of mass of hand
def detect(self, size=(250, 250, 250), doHandSize=True):
        """
        Detect the hand as closest object to camera
        :param size: bounding box size
        :return: center of mass of hand
        """

        steps = 65
        dz = (self.maxDepth - self.minDepth)/float(steps)
        for i in range(5,steps):
            part = self.dpt.copy()
            part[part < i*dz + self.minDepth] = 0
            part[part > (i+1)*dz + self.minDepth] = 0
            part[part != 0] = 10  # set to something
            #plt.imshow(part);  plt.show()
            ret, thresh = cv2.threshold(part, 1, 255, cv2.THRESH_BINARY)
            thresh = thresh.astype(dtype=numpy.uint8)
            #opencv2
            contours, hierarchy=cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            #opencv3
            #im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

            for c in range(len(contours)):
                if cv2.contourArea(contours[c]) > 200:

                    # centroid
                    M = cv2.moments(contours[c])
                    cx = int(numpy.rint(M['m10']/M['m00']))
                    cy = int(numpy.rint(M['m01']/M['m00']))

                    # crop
                    xstart = int(max(cx-100, 0))
                    xend = int(min(cx+100, self.dpt.shape[1]-1))
                    ystart = int(max(cy-100, 0))
                    yend = int(min(cy+100, self.dpt.shape[0]-1))

                    cropped = self.dpt[ystart:yend, xstart:xend].copy()
                    cropped[cropped < i*dz + self.minDepth] = 0.
                    cropped[cropped > (i+1)*dz + self.minDepth] = 0.
                    com = self.calculateCoM(cropped)
                    if numpy.allclose(com, 0.):
                        com[2] = cropped[cropped.shape[0]//2, cropped.shape[1]//2]
                    com[0] += xstart
                    com[1] += ystart

                    # refine iteratively
                    com = self.refineCoMIterative(com, 5, size)
                    zstart = com[2] - size[2] / 2.
                    zend = com[2] + size[2] / 2.

                    if doHandSize is True:
                        # refined contour for size estimation
                        part_ref = self.dpt.copy()
                        part_ref[part_ref < zstart] = 0
                        part_ref[part_ref > zend] = 0
                        part_ref[part_ref != 0] = 10  # set to something
                        ret, thresh_ref = cv2.threshold(part_ref, 1, 255, cv2.THRESH_BINARY)
                        contours_ref, _ = cv2.findContours(thresh_ref.astype(dtype=numpy.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
                        # find the largest contour
                        areas = [cv2.contourArea(cc) for cc in contours_ref]
                        c_max = numpy.argmax(areas)

                        # final result
                        return com, self.estimateHandsize(contours_ref[c_max], com, size)
                    else:
                        return com, size

        # no appropriate hand detected
        return numpy.array((0, 0, 0), numpy.float), size
def detect ( self , size = ( 250 , 250 , 250 ) , doHandSize = True ) : steps = 65 dz = ( self . maxDepth - self . minDepth ) / float ( steps ) for i in range ( 5 , steps ) : part = self . dpt . copy ( ) part [ part < i * dz + self . minDepth ] = 0 part [ part > ( i + 1 ) * dz + self . minDepth ] = 0 part [ part != 0 ] = 10 # set to something #plt.imshow(part);  plt.show() ret , thresh = cv2 . threshold ( part , 1 , 255 , cv2 . THRESH_BINARY ) thresh = thresh . astype ( dtype = numpy . uint8 ) #opencv2 contours , hierarchy = cv2 . findContours ( thresh , cv2 . RETR_TREE , cv2 . CHAIN_APPROX_SIMPLE ) #opencv3 #im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for c in range ( len ( contours ) ) : if cv2 . contourArea ( contours [ c ] ) > 200 : # centroid M = cv2 . moments ( contours [ c ] ) cx = int ( numpy . rint ( M [ 'm10' ] / M [ 'm00' ] ) ) cy = int ( numpy . rint ( M [ 'm01' ] / M [ 'm00' ] ) ) # crop xstart = int ( max ( cx - 100 , 0 ) ) xend = int ( min ( cx + 100 , self . dpt . shape [ 1 ] - 1 ) ) ystart = int ( max ( cy - 100 , 0 ) ) yend = int ( min ( cy + 100 , self . dpt . shape [ 0 ] - 1 ) ) cropped = self . dpt [ ystart : yend , xstart : xend ] . copy ( ) cropped [ cropped < i * dz + self . minDepth ] = 0. cropped [ cropped > ( i + 1 ) * dz + self . minDepth ] = 0. com = self . calculateCoM ( cropped ) if numpy . allclose ( com , 0. ) : com [ 2 ] = cropped [ cropped . shape [ 0 ] // 2 , cropped . shape [ 1 ] // 2 ] com [ 0 ] += xstart com [ 1 ] += ystart # refine iteratively com = self . refineCoMIterative ( com , 5 , size ) zstart = com [ 2 ] - size [ 2 ] / 2. zend = com [ 2 ] + size [ 2 ] / 2. if doHandSize is True : # refined contour for size estimation part_ref = self . dpt . copy ( ) part_ref [ part_ref < zstart ] = 0 part_ref [ part_ref > zend ] = 0 part_ref [ part_ref != 0 ] = 10 # set to something ret , thresh_ref = cv2 . threshold ( part_ref , 1 , 255 , cv2 . THRESH_BINARY ) contours_ref , _ = cv2 . findContours ( thresh_ref . astype ( dtype = numpy . uint8 ) , cv2 . RETR_TREE , cv2 . CHAIN_APPROX_SIMPLE ) # find the largest contour areas = [ cv2 . contourArea ( cc ) for cc in contours_ref ] c_max = numpy . argmax ( areas ) # final result return com , self . estimateHandsize ( contours_ref [ c_max ] , com , size ) else : return com , size # no appropriate hand detected return numpy . array ( ( 0 , 0 , 0 ) , numpy . float ) , size