def _largestContours(contours, convexityDefects): largestContours = [] for contour in contours: minX, minY, maxX, maxY = None, None, None, None contourDefects = convexityDefects.get(Helpers.hashable(contour), None) if contourDefects is not None: for i in range(contourDefects.shape[0]): s,e,f,d = contourDefects[i,0] defectPoint = tuple(contour[f][0]) if minX is None: minX, maxX = defectPoint[0], defectPoint[0] minY, maxY = defectPoint[1], defectPoint[1] largestContour = contour else: if defectPoint[0] < minX: minX = defectPoint[0] if defectPoint[0] > maxX: maxX = defectPoint[0] if defectPoint[1] < minY: minY = defectPoint[1] if defectPoint[1] > maxY: maxY = defectPoint[0] largestContours.append((abs(maxX - minX) * abs(maxY - minY), contour)) return sorted(largestContours, key=lambda x:x[0], reverse=True)
def _evaluateFingerGesture(contours, convexityDefects): depthsAndPoints = [] for contour in contours: contourDefects = convexityDefects.get(Helpers.hashable(contour), None) if contourDefects is not None: for i in range(contourDefects.shape[0]): start,end,depth_point,depth = contourDefects[i,0] depthsAndPoints.append((depth, tuple(contour[depth_point][0]))) if len(depthsAndPoints): depths = map(lambda x:x[0], depthsAndPoints) depthPoints = map(lambda x:x[1], depthsAndPoints) maxDepth = max(depths) centerOfMass = (int(sum(map(lambda x: x[0], depthPoints)) / float(len(depthPoints))), int(sum(map(lambda x: x[1], depthPoints)) / float(len(depthPoints)))) # fingerPoint = max(depthPoints, key=lambda x:((centerOfMass[0]-x[0])**2+(centerOfMass[1]-x[1])**2)**.5) fingerPoint = depthPoints[depths.index(maxDepth)] # fingerPoint = depthPoints[depthPoints.index(maxDepth)] if len(filter(lambda x: x>maxDepth/2, depths)) < 5: return ("GO", centerOfMass, fingerPoint) else: return ("STOP",)
def _contourConvexityDefects(contours): contourConvexityDefects = {} for contour in contours: hullIndices = cv2.convexHull(contour, returnPoints=False) if len(hullIndices)>3 and len(contour)>3: defects = cv2.convexityDefects(contour, hullIndices) contour.flags.writeable = False contourConvexityDefects[Helpers.hashable(contour)] = defects # pass return contourConvexityDefects
def _drawConvexityDefects(self, contours, convexityDefects): for contour in contours: contourDefects = convexityDefects.get(Helpers.hashable(contour), None) if contourDefects is not None: for i in range(contourDefects.shape[0]): s,e,f,d = contourDefects[i,0] defectPoint = tuple(contour[f][0]) start = tuple(contour[s][0]) end = tuple(contour[e][0]) cv2.circle(self.window.image,defectPoint,5,[255,255,255],-1) cv2.line(self.window.image, end, defectPoint, (255,0,0), 2)
def _contourConvexHulls(contours): contourConvexHulls = {} for contour in contours: hull = cv2.convexHull(contour) contourConvexHulls[Helpers.hashable(contour)] = hull return contourConvexHulls
def _drawConvexHull(self, contours, convexHulls): for contour in contours: cv2.drawContours(self.window.image, [convexHulls[Helpers.hashable(contour)]], -1, (0,0,255), 3)