def find_contours(self, thres): _, contours, hierarchy = cv2.findContours(thres.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # remove copy when cv2=3.2 is installed if len(contours) > 1: contours = [c for i, c in enumerate(contours) if hierarchy[0, i, 3] == -1] contours = [cv2.convexHull(c) for c in contours] if len(contours) > 1 and self._merge_mask is not None and np.any(self._merge_mask > 0): small_merge_mask = self._merge_mask[slice(*self.roi.value[0]), slice(*(self.roi.value[1] + 1)), 0] merge = [] other = [] for i in range(len(contours)): tmp = np.zeros_like(thres) cv2.drawContours(tmp, contours, i, (255), thickness=cv2.FILLED) cv2.bitwise_and(tmp, small_merge_mask, dst=tmp) if tmp.sum() > 0: merge.append(contours[i]) else: other.append(contours[i]) contours = ([cv2.convexHull(np.vstack(merge))] if len(merge) > 0 else []) + other contours = [c + self.roi.value[::-1, 0][None, None, :] for c in contours if len(c) >= self.min_contour_len.value] return contours
def dual_convex_hull(scan, debug_poly=None): """ Compute convex hull for left and right side of the scan (usefull if already in tunnel) """ heading = 0.0 # TODO pts_left, pts_right = [], [] for i, i_dist in enumerate(scan): if i_dist == 0 or i_dist >= 10000: continue angle = math.radians(270 * (i / len(scan)) - 135) + heading dist = i_dist/1000.0 x, y = dist * math.cos(angle), dist * math.sin(angle) if angle >= 0: # beware of heading modification, TODO autodetect pts_left.append((x, y)) else: pts_right.append((x, y)) hull_left = cv2.convexHull(np.array(pts_left, dtype=np.float32)) hull_right = cv2.convexHull(np.array(pts_right, dtype=np.float32)) if debug_poly is not None: debug_poly.append([p[0] for p in hull_left]) debug_poly[-1].append(hull_left[0][0]) # close polygon debug_poly.append([p[0] for p in hull_right]) debug_poly[-1].append(hull_right[0][0]) # close polygon
def draw_convex_hull(a, original): original = cv2.cvtColor(original, cv2.COLOR_GRAY2BGR) ret, b = cv2.threshold(a, 255, 255, cv2.THRESH_BINARY) contornos, jerarquia = cv2.findContours(a, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for n, cnt in enumerate(contornos): hull = cv2.convexHull(cnt) foo = cv2.convexHull(cnt, returnPoints=False) cv2.drawContours(original, contornos, n, (0, 35, 245)) if len(cnt) > 3 and len(foo) > 2: defectos = cv2.convexityDefects(cnt, foo) if defectos is not None: defectos = defectos.reshape(-1, 4) puntos = cnt.reshape(-1, 2) for d in defectos: if d[3] > 20: cv2.circle(original, tuple(puntos[d[0]]), 5, (255, 255, 0), 2) cv2.circle(original, tuple(puntos[d[1]]), 5, (255, 255, 0), 2) cv2.circle(original, tuple(puntos[d[2]]), 5, (0, 0, 255), 2) lista = numpy.reshape(hull, (1, -1, 2)) cv2.polylines(original, lista, True, (0, 255, 0), 3) center, radius = cv2.minEnclosingCircle(cnt) center = tuple(map(int, center)) radius = int(radius) cv2.circle(original, center, radius, (255, 0, 0), 3) cv2.imshow('Original', original)
def convexhullallcontours(contours): number = len(contours) status = numpy.zeros((number, 1)) for i, cnt1 in enumerate(contours): x = i if i != number - 1: for j, cnt2 in enumerate(contours[i + 1:]): x += 1 dist = arecontoursclose(cnt1, cnt2, distancethreshold=150) dist = True if dist is True: val = min(status[i], status[x]) status[x] = status[i] = val # ~ else: # ~ print ("Do nothing") maximum = int(status.max()) + 1 cont = None unified = [] unified2 = [] for i in xrange(maximum): pos = numpy.where(status == i)[0] if pos.size != 0: cont = numpy.vstack(contours[i] for i in pos) hull = cv2.convexHull(cont) hullindices = cv2.convexHull(cont, returnPoints=False) unified.append(hull) defects = cv2.convexityDefects(cont, hullindices) unified2.append(defects) return cont, unified, unified2
def gestureRecognizer(self): cv2.rectangle(self.img, (300, 300), (100, 100), (0, 255, 0), 0) crop_img = self.img[100:300, 100:300] grey = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY) value = (35, 35) blurred = cv2.GaussianBlur(grey, value, 0) _, thresh1 = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) cv2.imshow('Thresholded', thresh1) contours, hierarchy = cv2.findContours(thresh1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) max_area = -1 for i in range(len(contours)): cnt = contours[i] area = cv2.contourArea(cnt) if area > max_area: max_area = area ci = i cnt = contours[ci] x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(crop_img, (x, y), (x+w, y+h), (0, 0, 255), 0) hull = cv2.convexHull(cnt) drawing = np.zeros(crop_img.shape, np.uint8) cv2.drawContours(drawing, [cnt], 0, (0, 255, 0), 0) cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 0) hull = cv2.convexHull(cnt, returnPoints=False) defects = cv2.convexityDefects(cnt, hull) count_defects = 0 cv2.drawContours(thresh1, contours, -1, (0, 255, 0), 3) for i in range(defects.shape[0]): s, e, f, d = defects[i, 0] start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) a = math.sqrt((end[0] - start[0])**2 + (end[1] - start[1])**2) b = math.sqrt((far[0] - start[0])**2 + (far[1] - start[1])**2) c = math.sqrt((end[0] - far[0])**2 + (end[1] - far[1])**2) angle = math.acos((b**2 + c**2 - a**2)/(2*b*c)) * 57 if angle <= 90: count_defects += 1 cv2.circle(crop_img, far, 1, [0, 0, 255], -1) cv2.line(crop_img, start, end, [0, 255, 0], 2) # start actions when number of fingers is recognized if count_defects == 2: cv2.putText(self.img, "Volume Down", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) self.volumeDown(1) elif count_defects == 3: cv2.putText(self.img, "Volume Up", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, 2) self.volumeUp(1) elif count_defects == 4: cv2.putText(self.img, "Play/Pause", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) self.pauseAndStartVideo(2) else: cv2.putText(self.img, "Finger Control", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, 2) cv2.imshow('Gesture', self.img) all_img = np.hstack((drawing, crop_img)) # Display the resulting frame cv2.imshow('Contours', all_img)
def compute_shape_metrics(labels): reader = csv.reader(open(labels)) results = None for row in reader: if row[0] == "IMG": continue name = row[0] if (row[1] == " metal_lines" or row[1]==" metal_tree"): continue buf = 10 x = int(row[2]) y = int(row[3]) x_size = int(row[4])/2 + buf y_size = int(row[5])/2 + buf # Load Images img = cv2.imread(name+".jpg") # Color Space Conversion lab = cv2.cvtColor(img,cv2.COLOR_BGR2LAB) win = img[y-y_size:y+y_size,x-x_size:x+x_size] lab_win = lab[y-y_size:y+y_size,x-x_size:x+x_size] mid = (float(np.max(lab_win[...,1]))+float(np.min(lab_win[...,1])))/2. thresh = cv2.threshold(lab_win[...,1],mid,255,cv2.THRESH_BINARY)[1] # Contours contours, hier = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) largest_area = 0 largest_contour = 0 for i in contours: area = cv2.contourArea(i) if area > largest_area: largest_area = area largest_contour = i # Hulls hull = cv2.convexHull(largest_contour,returnPoints=False) hull_pts = cv2.convexHull(largest_contour) # Perimeter Difference perimeter_ratio = cv2.arcLength(hull_pts,True)/cv2.arcLength(largest_contour,True) # Area Difference area_ratio = cv2.contourArea(largest_contour)/cv2.contourArea(hull_pts) # Max Convexity Defect defects = cv2.convexityDefects(largest_contour,hull) depth = np.max(defects[...,-1])/256.0 rect = cv2.boundingRect(hull_pts) defect_ratio = depth/np.max(rect[2:]) # Combined Score print perimeter_ratio,area_ratio,defect_ratio,np.sum(defects[...,-1]),row[-1],row[1] #print perimeter_ratio,area_ratio,np.max(defects[...,-1]),np.sum(defects[...,-1]),row[-1],row[1] if results == None: results = np.array([perimeter_ratio,area_ratio,defect_ratio,]) #results = np.array([perimeter_ratio,area_ratio,defect_ratio,np.sum(defects[...,-1])]) #results = np.array([perimeter_ratio,area_ratio,np.max(defects[...,-1]),np.sum(defects[...,-1])]) else: results = np.vstack((results,np.array([perimeter_ratio,area_ratio,defect_ratio]))) #results = np.vstack((results,np.array([perimeter_ratio,area_ratio,defect_ratio,np.sum(defects[...,-1])]))) #results = np.vstack((results,np.array([perimeter_ratio,area_ratio,np.max(defects[...,-1]),np.sum(defects[...,-1])]))) return results
def track(self, frame, hist): tracking_frame = self.apply_hist_mask(frame, hist) tracking_frame = cv2.morphologyEx(tracking_frame, cv2.MORPH_OPEN, np.ones((19, 19), np.uint8)) tracking_frame = cv2.morphologyEx(tracking_frame, cv2.MORPH_CLOSE, np.ones((51, 11), np.uint8)) cv2.GaussianBlur(tracking_frame, (17, 17), 0, tracking_frame) gray = cv2.cvtColor(tracking_frame, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 0, 255, 0) _, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if contours is not None and len(contours) > 0: max_contour = self.calc_max_contour(contours) hull = cv2.convexHull(max_contour) cv2.drawContours(tracking_frame, [hull], 0, (0, 0, 255), 0) cv2.drawContours(tracking_frame, max_contour, -1, (0, 255, 0), 3) centroid = self.calc_centroid(max_contour) (x, y), radius = cv2.minEnclosingCircle(max_contour) cv2.circle(tracking_frame, (int(x), int(y)), int(radius), (0, 255, 0), 2) self.hand_center = (int(x), int(y)) self.hand_radius = radius defects = self.calc_defects(max_contour) if centroid is not None and defects is not None and len(defects) > 0: farthest_point = self.calc_farthest_point(defects, max_contour, centroid) if farthest_point is not None: cv2.circle(tracking_frame, farthest_point, 5, [0, 0, 255], -1) defects = cv2.convexityDefects(max_contour, cv2.convexHull(max_contour, returnPoints=False)) count_defects = 0 for i in range(defects.shape[0]): s, e, f, d = defects[i, 0] start = tuple(max_contour[s][0]) end = tuple(max_contour[e][0]) far = tuple(max_contour[f][0]) a = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2) b = math.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2) c = math.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2) angle = math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) * 57 if angle <= 90: count_defects += 1 cv2.circle(tracking_frame, far, 1, [0, 0, 255], -1) #dist = cv2.pointPolygonTest(max_contour,far,True) cv2.line(tracking_frame, start, end, [0, 255, 0], 2) self.fingers = count_defects return tracking_frame
def get_defects(self, cnt, drawing): defects = None hull = cv2.convexHull(cnt) cv2.drawContours(drawing, [cnt], 0, (0, 255, 0), 0) cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 0) hull = cv2.convexHull(cnt, returnPoints=False) # For finding defects if hull.size > 2: defects = cv2.convexityDefects(cnt, hull) return defects
def gesture_recog(): while(cap.isOpened()): ret, img = cap.read() cv2.rectangle(img,(500,500),(0,0),(0,255,0),0) crop_img = img[0:500, 0:500] grey = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY) value = (35, 35) blurred = cv2.GaussianBlur(grey, value, 0) _, thresh1 = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) cv2.imshow('Thresholded', thresh1) contours, hierarchy = cv2.findContours(thresh1.copy(),cv2.RETR_TREE, \ cv2.CHAIN_APPROX_NONE) max_area = -1 for i in range(len(contours)): cnt=contours[i] area = cv2.contourArea(cnt) if(area>max_area): max_area=area ci=i cnt=contours[ci] x,y,w,h = cv2.boundingRect(cnt) cv2.rectangle(crop_img,(x,y),(x+w,y+h),(0,0,255),0) hull = cv2.convexHull(cnt) drawing = np.zeros(crop_img.shape,np.uint8) cv2.drawContours(drawing,[cnt],0,(0,255,0),0) cv2.drawContours(drawing,[hull],0,(0,0,255),0) hull = cv2.convexHull(cnt,returnPoints = False) defects = cv2.convexityDefects(cnt,hull) count_defects = 0 cv2.drawContours(thresh1, contours, -1, (0,255,0), 3) for i in range(defects.shape[0]): s,e,f,d = defects[i,0] start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) a = math.sqrt((end[0] - start[0])**2 + (end[1] - start[1])**2) b = math.sqrt((far[0] - start[0])**2 + (far[1] - start[1])**2) c = math.sqrt((end[0] - far[0])**2 + (end[1] - far[1])**2) angle = math.acos((b**2 + c**2 - a**2)/(2*b*c)) * 57 if angle <= 90: count_defects += 1 cv2.circle(crop_img,far,1,[0,0,255],-1) cv2.line(crop_img,start,end,[0,255,0],2) frame = ET.SubElement(video, "frame") ET.SubElement(frame, "time").text = time.strftime("%H:%M:%S") ET.SubElement(frame, "fingers").text = "{}".format((count_defects+1)) print "Fingers : {}".format(count_defects+1) cv2.imshow('Gesture', img) all_img = np.hstack((drawing, crop_img)) cv2.imshow('Contours', all_img) k = cv2.waitKey(10) if k == 27: break
def DetectVisPup(im, avgPup, initPupPos, binNum = 2**6-1, kHistMed = 3, convHullTH = 0.7, closePtsTH = 5): ellCenter, ellVertices, deg = None, None, None negIm = 1 - im ## Article stuff dont work very well ## #hist = cv2.calcHist([negIm],[0],None,[binNum],[0,binNum]) hist,bins = np.histogram(negIm.ravel(),binNum,[0,1]) medHist = signal.medfilt(hist, kHistMed) #locExtrm = signal.argrelextrema(medHist, np.less) locExtrm = signal.argrelmin(hist) if locExtrm[0].tolist() == []: return None, None, None pupTH = np.max(locExtrm[0][-2]/np.float64(binNum)) - 0.05 ## #pupTH = avgPup pupBW = np.zeros((im.shape[0], im.shape[1]), 'uint8') pupBW[im<avgPup]=255 _, contours, hierarchy = cv2.findContours(pupBW, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) if contours == []: print("No contours") return None, None, None contours = [cv2.convexHull(contour) for contour in contours] contoursArea = np.asarray([cv2.contourArea(contour) for contour in contours]) maxContourArea = np.max(contoursArea) maxContourInd = np.nonzero(maxContourArea==contoursArea)[0][0] maxContour = contours[maxContourInd] if maxContourArea < np.sum(contoursArea) * convHullTH: mmnts = [cv2.moments(c) for c in contours] CM = [(int(M['m10']/M['m00']), int(M['m01']/M['m00'])) if M['m00']!=0 else (np.nan, np.nan) for M in mmnts ] maxCM = CM[maxContourInd] distCM = [np.sqrt((maxCM[0] - otherCM[0])**2 + (maxCM[1] - otherCM[1])**2) for otherCM in CM] _, radius = maxExtentLargstBlob = cv2.minEnclosingCircle(contours[maxContourInd]) nearByContInd = np.nonzero([dCM < radius/2 for dCM in distCM])[0] maxContour = cv2.convexHull(np.vstack(np.asarray([contours[ii] for ii in nearByContInd]))).squeeze() #newContours = [contours[ii] for ii in range(len(contours)) if not ii in nearByContInd] #newContours.append(largeContour) ## sparse up contour points #contPntDist = distance.squareform(distance.pdist(np.asarray(maxContour).squeeze())) #tooClosePntsInd = np.where(contPntDist < closePtsTH) ##contDist = distance.squareform(distance.pdist(np.asarray(maxContour).squeeze())) #for i in range(len(contPntDist)): # for j in range(len(contPntDist)): # if i < j and : ## ## remove colinear points minRow = np.min(maxContour[:, 0]) maxRow = np.max(maxContour[:, 0]) ## ## Elipse fit if len(maxContour)>5: ellCenter, ellVertices, deg = cv2.fitEllipse(maxContour) return ellCenter, ellVertices, deg
def count_fingers(img): img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Otsu's thresholding after Gaussian filtering img = cv2.GaussianBlur(img, (5, 5), 0) ret, mask = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) cv2.imshow("Threshold", mask) (_, cnts, _) = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) list_far = [] list_end = [] if cnts: areas = [cv2.contourArea(c) for c in cnts] max_index = np.argmax(areas) cnt = cnts[max_index] M = cv2.moments(cnt) cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) hull1 = cv2.convexHull(cnt) hull2 = cv2.convexHull(cnt, returnPoints=False) try: defects = cv2.convexityDefects(cnt, hull2) except Exception, e: defects = None print e counter = 0 if defects is not None: for i in range(defects.shape[0]): s, e, f, d = defects[i, 0] # start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) if d < 20000: continue if far[1] >= (cy+40): continue diff1 = abs(end[0]-far[0]) if diff1 > 100: continue cv2.line(img, end, far, (0, 0, 0), 2, 8) cv2.imshow("hand", img) cv2.waitKey(1) list_far.append(far) list_end.append(end) counter += 1
def get_feature_mask(aligned_landmarks_68, size, padding=0, dilation=30): """ Return the face feature mask """ # pylint: disable=no-member logger.trace("aligned_landmarks_68: %s, size: %s, padding: %s, dilation: %s", aligned_landmarks_68, size, padding, dilation) scale = size - 2 * padding translation = padding pad_mat = np.matrix([[scale, 0.0, translation], [0.0, scale, translation]]) aligned_landmarks_68 = np.expand_dims(aligned_landmarks_68, axis=1) aligned_landmarks_68 = cv2.transform(aligned_landmarks_68, pad_mat, aligned_landmarks_68.shape) aligned_landmarks_68 = np.squeeze(aligned_landmarks_68) (l_start, l_end) = FACIAL_LANDMARKS_IDXS["left_eye"] (r_start, r_end) = FACIAL_LANDMARKS_IDXS["right_eye"] (m_start, m_end) = FACIAL_LANDMARKS_IDXS["mouth"] (n_start, n_end) = FACIAL_LANDMARKS_IDXS["nose"] (lb_start, lb_end) = FACIAL_LANDMARKS_IDXS["left_eyebrow"] (rb_start, rb_end) = FACIAL_LANDMARKS_IDXS["right_eyebrow"] (c_start, c_end) = FACIAL_LANDMARKS_IDXS["chin"] l_eye_points = aligned_landmarks_68[l_start:l_end].tolist() l_brow_points = aligned_landmarks_68[lb_start:lb_end].tolist() r_eye_points = aligned_landmarks_68[r_start:r_end].tolist() r_brow_points = aligned_landmarks_68[rb_start:rb_end].tolist() nose_points = aligned_landmarks_68[n_start:n_end].tolist() chin_points = aligned_landmarks_68[c_start:c_end].tolist() mouth_points = aligned_landmarks_68[m_start:m_end].tolist() l_eye_points = l_eye_points + l_brow_points r_eye_points = r_eye_points + r_brow_points mouth_points = mouth_points + nose_points + chin_points l_eye_hull = cv2.convexHull(np.array(l_eye_points).reshape( (-1, 2)).astype(int)).flatten().reshape((-1, 2)) r_eye_hull = cv2.convexHull(np.array(r_eye_points).reshape( (-1, 2)).astype(int)).flatten().reshape((-1, 2)) mouth_hull = cv2.convexHull(np.array(mouth_points).reshape( (-1, 2)).astype(int)).flatten().reshape((-1, 2)) mask = np.zeros((size, size, 3), dtype=float) cv2.fillConvexPoly(mask, l_eye_hull, (1, 1, 1)) cv2.fillConvexPoly(mask, r_eye_hull, (1, 1, 1)) cv2.fillConvexPoly(mask, mouth_hull, (1, 1, 1)) if dilation > 0: kernel = np.ones((dilation, dilation), np.uint8) mask = cv2.dilate(mask, kernel, iterations=1) logger.trace("Returning: %s", mask) return mask
def convexity_2(contour,img=None): if img is not None: hull = cv2.convexHull(contour, returnPoints=1) cv2.drawContours(img, hull, -1, (255,0,0)) hull = cv2.convexHull(contour, returnPoints=0) if len(hull)>12: res = cv2.convexityDefects(contour, hull) # return: start_index, end_index, farthest_pt_index, fixpt_depth) if res is None: return False if len(res)>2: return True return False
def process_mask(mask): # Find Contours contours, hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) hull = 0 for i in xrange(len(contours)): temp_hull = cv2.convexHull(contours[i]) if i == 0: hull = temp_hull else: hull = np.concatenate((hull,temp_hull)) hull = cv2.convexHull(hull) return hull
def process(im, haarc=None,silent=False): if haarc is None: haarc = haar.haarInit(im + '/../haar/cascade.xml') img_ref = im img, rect = findROI(img_ref, haarc) imb = extractBinary(img) imb_contours = imb.copy() vect = None img_tr = np.copy(img_ref) if not silent: debugThresh(img) if rect is None: img_ref = cv2.cvtColor(img_ref,cv2.COLOR_GRAY2BGR) return img_ref, img_ref, None contours, _ = cv2.findContours(imb_contours, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) if contours: contour = bestContourAsInt(contours) hull = cv2.convexHull(contour, returnPoints=False).astype('int') defects = cv2.convexityDefects(contour, hull) hull_points = [tuple(p[0]) for p in cv2.convexHull(contour, returnPoints=True)] contour_points = [tuple(p[0]) for p in contour] hull_refined, defects_points = refineHullDefects(hull_points, defects, contour, 2500) features = packFeatures(contour, hull_points, defects_points, hull_refined, rect) img = drawResult(img, features) img_ref = cv2.cvtColor(img_ref,cv2.COLOR_GRAY2BGR) (x,y,w,h) = rect img_ref[y:y+h, x:x+w] = img cv2.rectangle(img_ref, (x,y), (x+w,y+h), (255,0,0)) img_tr[y:y+h, x:x+w] = imb else: img_ref = cv2.cvtColor(img_ref,cv2.COLOR_GRAY2BGR) img_tr = imb densityVect = zoning(imb) img_tr = cv2.cvtColor(img_tr,cv2.COLOR_GRAY2BGR) return img_ref, img_tr,densityVect
def start(self): edged = self.filter(self.image) cnts = self.find_contours(edged.copy()) passed = [] conts = cv2.drawContours(self.image.copy(), cnts, -1, (255, 255, 0, 255), 2) for ind, cnt in enumerate(cnts): hull = cv2.convexHull(cnt, returnPoints=True) perim = cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(hull, 21, True) # * perim, True) sides = len(approx) formats = [hull, perim, approx, sides] if self.sides_check(sides): print("%d passed with %d sides" % (ind, sides)) conts = cv2.drawContours(conts, [approx], -1, (0, 0, 255, 255), 4) passed.append([ind, formats]) u_passed_list = [] for con in passed: ind = con[0] warp = self.warp_rect(con[1][2]) blur = cv2.medianBlur(warp, 3) canny = cv2.Canny(blur, 300, 500) cv2.imshow("can " + str(ind), canny) contour = self.find_contours(canny, True)[0] # Get largest contour match = cv2.matchShapes(self.s_cnt[0], contour, 3, 1) u_passed_list.append([ind, match]) warp = cv2.drawContours(warp, [contour], -1, (0, 0, 255, 255), 4) warp = cv2.putText(warp, str(ind), (45, 45), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 1, (255, 255, 255, 255), 3, 8) cv2.imshow("warp " + str(ind), warp) low_high = sorted(u_passed_list, key=lambda x: x[1]) lowest = low_high[0][0] print("Selecting contour %d" % lowest) hull = cv2.convexHull(cnts[lowest], returnPoints=True) m = cv2.moments(hull) centered = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00'])) print("Selected object center %d : %d" % centered) conts = cv2.putText(conts, str("Target"), (centered[0] - 40, centered[1] - 30), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 1, (255, 255, 255, 255), 1, 8) conts = cv2.putText(conts, str("%d : %d" % centered), (centered[0] - 50, centered[1] + 60), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 1, (255, 255, 255, 255), 1, 8) cv2.imshow("source", self.image) cv2.imshow("edged", edged) cv2.imshow("contours", conts) self.wait_destroy()
def evaluate(image): #copy the image as to not destroy it #image = image.copy() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (11, 11), 0) #cv2.imshow("Image", image) # The first thing we are going to do is apply edge detection to # the image to reveal the outlines of the coins edged = cv2.Canny(blurred, 30, 150) #cv2.imshow("Edges", edged) # Find contours in the edged image. (_,cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #sort them largest to smallest cnts = sorted(cnts, key = cv2.contourArea, reverse = True) #calculate the peremiter #if there are no visible contours in the image, return a fitness of 0 if len(cnts) == 0: return 0 perimeter = cv2.arcLength(cnts[0], True) #find the convexHull cnt = cnts[0] hull = cv2.convexHull(cnt,returnPoints = False) hull_for_len = cv2.convexHull(cnt,returnPoints = True) defects = cv2.convexityDefects(cnt,hull) #calculate the length of the convexHull convexHull = cv2.arcLength(hull_for_len, True) #Draw Convex Hull on image for i in range(defects.shape[0]): s,e,f,d = defects[i,0] start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) cv2.line(image,start,end,[255,0,0],2) #Draw Perimeter on image cv2.drawContours(image, cnts, 0, (0, 255, 0), 2) #calulate fitness fitness = perimeter / convexHull #Return (fitness,drawnImage) return (fitness,image)
def WessisGesturRecognition(croppedData): grey = cv2.cvtColor(croppedData, cv2.COLOR_BGR2GRAY) value = (35, 35) blurred = cv2.GaussianBlur(grey, value, 0) _, thresh1 = cv2.threshold(blurred, 127, 255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) contours, hierarchy = cv2.findContours(thresh1.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) max_area = -1 for i in range(len(contours)): cnt=contours[i] area = cv2.contourArea(cnt) if(area > max_area): max_area=area ci=i cnt=contours[ci] x,y,w,h = cv2.boundingRect(cnt) cv2.rectangle(croppedData,(x,y),(x+w,y+h),(0,0,255),0) hull = cv2.convexHull(cnt) drawing = np.zeros(croppedData.shape,np.uint8) cv2.drawContours(drawing,[cnt],0,(0,255,0),0) cv2.drawContours(drawing,[hull],0,(0,0,255),0) hull = cv2.convexHull(cnt,returnPoints = False) defects = cv2.convexityDefects(cnt,hull) count_defects = 0 for i in range(defects.shape[0]): s,e,f,d = defects[i,0] start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) a = math.sqrt((end[0] - start[0])**2 + (end[1] - start[1])**2) b = math.sqrt((far[0] - start[0])**2 + (far[1] - start[1])**2) c = math.sqrt((end[0] - far[0])**2 + (end[1] - far[1])**2) angle = math.acos((b**2 + c**2 - a**2)/(2*b*c)) * 57 if angle <= 45: count_defects += 1 cv2.circle(croppedData,far,1,[0,0,255],-1) elif angle >= 180 and angle >= 90: cv2.putText(croppedData, str, (30,100), cv2.FONT_HERSHEY_DUPLEX, 1.6, 1.6) cv2.line(croppedData, start, end, [0, 0, 0], 2) if count_defects == 1: str = "Two fingers up" elif count_defects == 2: str = "Three fingers up" elif count_defects == 3: str = "Four fingers up" elif count_defects == 4: str= "\"Hi hi...\" " else: str = "Recognizing Hand Gesture..." print(str)
def finger_count(img): hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) # define range of blue color in HSV lower = np.array([20,100,100]) upper = np.array([40,255,255]) mask = cv2.inRange(hsv, lower, upper) # Bitwise-AND mask and original image res = cv2.bitwise_and(img,img, mask= mask) res = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(res,0,255,cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(thresh.copy(),cv2.cv.CV_RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) max_area = 0 ci = 0 defects = None for i in range(len(contours)): cnt=contours[i] area = cv2.contourArea(cnt) if(area>max_area): max_area=area ci=i if ci != 0: cnt=contours[ci] M = cv2.moments(cnt) cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) cv2.circle(img,(cx,cy),8,[255,0,0],-1) hull = cv2.convexHull(cnt) drawing = np.zeros(img.shape,np.uint8) cv2.drawContours(drawing,[cnt],0,(0,255,0),2) cv2.drawContours(drawing,[hull],0,(0,0,255),2) hull = cv2.convexHull(cnt,returnPoints = False) try: defects = cv2.convexityDefects(cnt,hull) except Exception, e: print e mind=0 maxd=0 i=0 prev_start = (0,0) cntr = 0
def count_fingers(hand_frame): hand_frame = cv2.cvtColor(hand_frame,cv2.COLOR_BGR2GRAY) # Otsu's thresholding after Gaussian filtering hand_frame = cv2.GaussianBlur(hand_frame,(5,5),0) ret,mask = cv2.threshold(hand_frame,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) (cnts,_)=cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) list_far=[] list_end=[] if cnts: areas = [cv2.contourArea(c) for c in cnts] max_index = np.argmax(areas) cnt=cnts[max_index] M = cv2.moments(cnt) cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) hull1 = cv2.convexHull(cnt) hull2 = cv2.convexHull(cnt,returnPoints = False) try: defects = cv2.convexityDefects(cnt,hull2) except Exception, e: defects = None print e counter = 0 if defects is not None: for i in range(defects.shape[0]): s,e,f,d = defects[i,0] start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) if d<20000: continue if far[1] >= (cy+40): continue else: pass list_far.append(far) list_end.append(end) counter +=1
def __calcConvexHull(self,m,c): #try: CH = cv2.convexHull(c) #ConvexArea = cv2.contourArea(CH) #Area = self.__calcArea(m,c) #Solidity = Area/ConvexArea return {'ConvexHull':CH} #{'ConvexHull':CH,'ConvexArea':ConvexArea,'Solidity':Solidity}
def convexhull(self, gray): mask = np.zeros(gray.shape, dtype=np.uint8) cnt, _ = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for c in cnt: hull = cv2.convexHull(c) cv2.drawContours(mask, [hull], -1, 255, -1) return mask
def build_correspondance(self, visible_markers,camera_calibration,min_marker_perimeter): """ - use all visible markers - fit a convex quadrangle around it - use quadrangle verts to establish perpective transform - map all markers into surface space - build up list of found markers and their uv coords """ all_verts = [m['verts'] for m in visible_markers if m['perimeter']>=min_marker_perimeter] if not all_verts: return all_verts = np.array(all_verts) all_verts.shape = (-1,1,2) # [vert,vert,vert,vert,vert...] with vert = [[r,c]] # all_verts_undistorted_normalized centered in img center flipped in y and range [-1,1] all_verts_undistorted_normalized = cv2.undistortPoints(all_verts, camera_calibration['camera_matrix'],camera_calibration['dist_coefs']) hull = cv2.convexHull(all_verts_undistorted_normalized,clockwise=False) #simplify until we have excatly 4 verts if hull.shape[0]>4: new_hull = cv2.approxPolyDP(hull,epsilon=1,closed=True) if new_hull.shape[0]>=4: hull = new_hull if hull.shape[0]>4: curvature = abs(GetAnglesPolyline(hull,closed=True)) most_acute_4_threshold = sorted(curvature)[3] hull = hull[curvature<=most_acute_4_threshold] # all_verts_undistorted_normalized space is flipped in y. # we need to change the order of the hull vertecies hull = hull[[1,0,3,2],:,:] # now we need to roll the hull verts until we have the right orientation: # all_verts_undistorted_normalized space has its origin at the image center. # adding 1 to the coordinates puts the origin at the top left. distance_to_top_left = np.sqrt((hull[:,:,0]+1)**2+(hull[:,:,1]+1)**2) bot_left_idx = np.argmin(distance_to_top_left)+1 hull = np.roll(hull,-bot_left_idx,axis=0) #based on these 4 verts we calculate the transformations into a 0,0 1,1 square space m_from_undistored_norm_space = m_verts_from_screen(hull) self.detected = True # map the markers vertices in to the surface space (one can think of these as texture coordinates u,v) marker_uv_coords = cv2.perspectiveTransform(all_verts_undistorted_normalized,m_from_undistored_norm_space) marker_uv_coords.shape = (-1,4,1,2) #[marker,marker...] marker = [ [[r,c]],[[r,c]] ] # build up a dict of discovered markers. Each with a history of uv coordinates for m,uv in zip (visible_markers,marker_uv_coords): try: self.markers[m['id']].add_uv_coords(uv) except KeyError: self.markers[m['id']] = Support_Marker(m['id']) self.markers[m['id']].add_uv_coords(uv) #average collection of uv correspondences accros detected markers self.build_up_status = sum([len(m.collected_uv_coords) for m in self.markers.values()])/float(len(self.markers)) if self.build_up_status >= self.required_build_up: self.finalize_correnspondance()
def fill_ratio(self, mat, contour, threshed): fill_mask = np.zeros(mat.shape[:2], dtype=np.uint8) cv2.drawContours(fill_mask, [contour], -1, 255, thickness=-1) fill_masked = cv2.bitwise_and(threshed, threshed, mask=fill_mask) hull_area = cv2.contourArea(cv2.convexHull(contour)) fill = np.sum(fill_masked) / 255 / hull_area return fill
def detect_possible_buttons(image, source_image): contours, hierarchy = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) buttons = [] for cnt in contours: if 850 < cv2.contourArea(cnt) < 3000: # remove small and large areas like noise etc hull = cv2.convexHull(cnt) # find the convex hull of contour hull = cv2.approxPolyDP(hull, 0.1 * cv2.arcLength(hull, True), True) min = [10000.0, 10000.0] max = [0.0, 0.0] #print '%d,%d' % (point[0][0], point[0][1]) if len(hull) == 4: margin = 2 for point in hull: x = point[0][0] y = point[0][1] if x < min[0]: min[0] = x - margin if y < min[1]: min[1] = y - margin if x > max[0]: max[0] = x + margin if y > max[1]: max[1] = y + margin points = [[min[0], min[1]], [max[0], min[1]], [max[0], max[1]], [min[0], max[1]]] points = np.array(points,np.int0) button = {'image': source_image[min[0]:max[0], min[1]:max[1]], 'x': 0.5*(max[0]+min[0]), 'y': 0.5*(max[1]+min[1]), 'w': max[0]-min[0], 'h': max[1]-min[1]} buttons.append(button) cv2.polylines(source_image, [points], True, (255, 0, 0), 2) cv2.drawContours(source_image, [hull], 0, (0, 255, 0), 1) return buttons
def get_info(self, shred, contour, name): area = cv2.contourArea(contour) hull = cv2.convexHull(contour) hull_area = cv2.contourArea(hull) solidity = float(area) / hull_area # Also top and bottom points on the contour topmost = map(int, contour[contour[:, :, 1].argmin()][0]) bottommost = map(int, contour[contour[:, :, 1].argmax()][0]) tags = [] if solidity < 0.75: tags.append("Suspicious shape") width_mm = self.sheet.px_to_mm(shred.shape[0]) height_mm = self.sheet.px_to_mm(shred.shape[1]) ratio = shred.shape[0] / float(shred.shape[1]) return { "area": area, "ratio": ratio, "solidity": solidity, "topmost": topmost, "bottommost": bottommost, "width_mm": width_mm, "height_mm": height_mm }, tags
def get_convex_hull_properties_in_slice(segment): cnt = np.array(segment.get_mid_slice_contour()) cnt_len = cnt.shape[0] cnt = cnt.reshape((cnt_len,1,2)) if len(cnt)>3: hull = cv2.convexHull(cnt,returnPoints = False) segment.feature_dict["mid_slice_convex_hull_indices"] = [x[0] for x in hull] try: defects = cv2.convexityDefects(cnt,hull) if defects is not None: segment.feature_dict["mid_slice_convexity_deffects"] = [[i for i in defects[k][0]] for k in xrange(len(defects))] else: segment.feature_dict["mid_slice_convexity_deffects"] = [[]] except: segment.feature_dict["mid_slice_convexity_deffects"] = [[]] else: segment.feature_dict["mid_slice_convex_hull_indices"] = [] segment.feature_dict["mid_slice_convexity_deffects"] = [[]]
def get_image_mask(self, image, new_face, landmarks, mat, image_size): face_mask = numpy.zeros(image.shape,dtype=float) if 'rect' in self.mask_type: face_src = numpy.ones(new_face.shape,dtype=float) cv2.warpAffine( face_src, mat, image_size, face_mask, cv2.WARP_INVERSE_MAP | cv2.INTER_CUBIC, cv2.BORDER_TRANSPARENT ) hull_mask = numpy.zeros(image.shape,dtype=float) if 'hull' in self.mask_type: hull = cv2.convexHull( numpy.array( landmarks ).reshape((-1,2)).astype(int) ).flatten().reshape( (-1,2) ) cv2.fillConvexPoly( hull_mask,hull,(1,1,1) ) if self.mask_type == 'rect': image_mask = face_mask elif self.mask_type == 'facehull': image_mask = hull_mask else: image_mask = ((face_mask*hull_mask)) if self.erosion_kernel is not None: if self.erosion_kernel_size > 0: image_mask = cv2.erode(image_mask,self.erosion_kernel,iterations = 1) elif self.erosion_kernel_size < 0: dilation_kernel = abs(self.erosion_kernel) image_mask = cv2.dilate(image_mask,dilation_kernel,iterations = 1) if self.blur_size!=0: image_mask = cv2.blur(image_mask,(self.blur_size,self.blur_size)) return image_mask
def InitFromContour(self, contour): # Prefilter on bounding rect because moments take some time self.recX, self.recY, self.recW, self.recH = cv2.boundingRect(contour) if self.recH < config['hand']['minimumHeight'] or self.recH > config['hand']['maximumHeight'] or self.recW < \ config['hand']['minimumWidth'] or self.recW > config['hand']['maximumWidth']: return False self.handContour = contour self.properties['foundHand'] = True # Get general info self.moments = cv2.moments(contour) self.hull = cv2.convexHull(self.handContour, returnPoints = False) self.defects = cv2.convexityDefects(self.handContour, self.hull) _,radius = cv2.minEnclosingCircle(self.handContour) self.radius = int(radius / 1.2) self.centerX = int(self.moments['m10'] / self.moments['m00']) self.centerY = int(self.moments['m01'] / self.moments['m00']) self.properties['centerX'] = self.centerX self.properties['centerY'] = self.centerY self.InitGestures() return True
def execute(self, image): image = cv2.cvtColor(image, cv2.cv.CV_BGR2HSV) h, _, _ = cv2.split(image) image[h < self.hue_min] *= 0 image[h > self.hue_max] *= 0 #image[image > 0] = 255 gray = cv2.cvtColor(image, cv.CV_BGR2GRAY) cnt, _ = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) image *= 0 area, c = self.detect_biggest_area(gray) if self._calibrate_hue and c is not None and area > self.area_min: self.hue_min += 1 #self.notify_observers() elif self._calibrate_hue and self.hue_min > 0: print self.hue_min self.notify_observers() self._calibrate_hue = False self.calibrate_closed_hand(area) self.calibrate_extended_hand(area) self.calibrate_normal_hand(area) if c is not None and area >= self.area_min: hull = cv2.convexHull(c) cv2.drawContours(image, [hull],-1, (255,255,255), -1) self.notify_output_observers(str(self.calc_return_value(area)) + "\n") else: self.notify_output_observers('0\n') return image
def convex_hull(contour): hull = cv2.convexHull(contour, returnPoints=1) return hull
area_l = cv.contourArea(c_l) if area_l > 500: M_l = cv.moments(c_l) if (M_l["m00"] == 0): M_l["m00"] = 1 x_l = int(M_l["m10"] / M_l["m00"]) y_l = int(M_l["m01"] / M_l["m00"]) ## generacion del rectangulo en la imagen rect_l = cv.minAreaRect(c_l) box_l = cv.boxPoints(rect_l) box_l = np.int0(box_l) ## generacion dle centroide cv.circle(procesamiento_l, (x_l, y_l), 7, (0, 255, 0), -1) font = cv.FONT_HERSHEY_SIMPLEX contorno_limpio_l = cv.convexHull(c_l) x_real_l = x_l y_real_l = y_l cv.drawContours(procesamiento_l, [contorno_limpio_l], 0, (0, 0, 0), 3) cv.drawContours(procesamiento_l, [box_l], 0, (0, 0, 255), 2) cv.circle(procesamiento_l, (tuple(box_l[0][:])), 5, (255, 0, 0), -1) cv.circle(procesamiento_l, (tuple(box_l[2][:])), 5, (0, 255, 0), -1) #print(box_l) entrada_l = np.matrix([[x_real_l], [y_real_l], [0], [1]]) sistema_real_l = transpo @ entrada_l
def getmask(x, p): mask = np.zeros(x.shape[:2]) p = [[[int(e[0]), int(e[1])]] for e in p] hull = cv2.convexHull(np.array(p), False) cv2.fillPoly(mask, [hull], 1) return mask
img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) mask = np.zeros_like(img_gray) faces = detector(img_gray) for face in faces: landmarks = predictor(img_gray, face) landmarks_points = [] for n in range(0, 68): x = landmarks.part(n).x y = landmarks.part(n).y landmarks_points.append((x, y)) #cv2.circle(frame, (x, y), 3, (0, 0, 255), -1) points = np.array(landmarks_points, np.int32) # Convex HULL convexhull = cv2.convexHull(points) #cv2.polylines(frame, [convexhull], True, (255, 0, 0), 3) #cv2.fillConvexPoly(mask, convexhull, 255) #frame = cv2.bitwise_and(frame, frame, mask=mask) # rectangle rect = cv2.boundingRect(convexhull) (x, y, w, h) = rect #cv2.rectangle(frame, (x,y), (x+w, y+h), (0,255,0)) mask[y:y + h, x:x + w] = 255 frame = cv2.bitwise_and(frame, frame, mask=mask) mask_frame = frame[y:y + h, x:x + w] cv2.imshow("Image", mask_frame) k = cv2.waitKey(33) if k == ord('s'):
c.area = cv2.contourArea(tem) if c.area > maxArea: maxArea = c.area c_i = i for cnt in contours: try: M = cv2.moments(cnt) global cx, cy cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) except: pass cv2.setMouseCallback('draw_obj', d.CallBackFunction) res = contours[c_i] hull = cv2.convexHull(res) compare = (cx, cy) #print(d.return_compare(compare)) draw_Object = np.zeros(img.shape, np.uint8) cv2.drawContours(draw_Object, [res], 0, (0, 255, 0), 2) cv2.drawContours(draw_Object, [hull], 0, (255, 255, 0), 2) cv2.circle(draw_Object, (cx, cy), 10, (0, 0, 255), -1) MAX_y = draw_Object.shape[0] MAX_x = draw_Object.shape[1] d.draw____(draw_Object, MAX_x, MAX_y) calc, cnt = d.calculate(res, draw_Object) #print('counter: ' + str(counter)) if c.trigger_switch == True: if counter % 2 == 0: k = d.return_compare(compare)
# Find contours contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(crop_image, contours, 0, (0, 0, 255), 6) #print("number of contours : ",len(contours)) try: # Find contour with maximum area contour = max(contours, key=lambda x: cv2.contourArea(x)) # Create bounding rectangle around the contour x, y, w, h = cv2.boundingRect(contour) cv2.rectangle(crop_image, (x, y), (x + w, y + h), (0, 0, 255), 0) # Find convex hull hull = cv2.convexHull(contour) # Draw contour drawing = np.zeros(crop_image.shape, np.uint8) cv2.drawContours(drawing, [contour], 0, (0, 255, 0), 0) cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 0) # Find convexity defects hull = cv2.convexHull(contour, returnPoints=False) defects = cv2.convexityDefects(contour, hull) # Use cosine rule to find angle of the far point from the start and end point i.e. the convex points (the finger # tips) for all defects count_defects = 0 bol = False
def draw_convex_hull(im, points, color): points = points.astype(numpy.int32) points = cv2.convexHull(points) cv2.fillConvexPoly(im, points, color=color)
shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) # extract the left and right eye coordinates, then use the # coordinates to compute the eye aspect ratio for both eyes leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) # average the eye aspect ratio together for both eyes ear = (leftEAR + rightEAR) / 2.0 # compute the convex hull for the left and right eye, then # visualize each of the eyes leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) # check to see if the eye aspect ratio is below the blink # threshold, and if so, increment the blink frame counter if ear < EYE_AR_THRESH: COUNTER += 1 # if the eyes were closed for a sufficient number of # then sound the alarm if COUNTER >= EYE_AR_CONSEC_FRAMES: # if the alarm is not on, turn it on if not ALARM_ON: ALARM_ON = True
def get_cnt(thresh): cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) for (i, c) in enumerate(cnts): try: # compute the area of the contour along with the bounding box # to compute the aspect ratio area = cv2.contourArea(c) (x, y, w, h) = cv2.boundingRect(c) # compute the aspect ratio of the contour, which is simply the width # divided by the height of the bounding box aspectRatio = w / float(h) # use the area of the contour and the bounding box area to compute # the extent extent = area / float(w * h) # compute the convex hull of the contour, then use the area of the # original contour and the area of the convex hull to compute the # solidity hull = cv2.convexHull(c) hullArea = cv2.contourArea(hull) solidity = area / float(hullArea) angle = -1 if len(c) > 4: (x, y), (MA, ma), angle = cv2.fitEllipse(c) equi_diameter = np.sqrt(4 * area / np.pi) # visualize the original contours and the convex hull and initialize # the name of the shape # cv2.drawContours(hullImage, [hull], -1, 255, -1) # cv2.drawContours(image, [c], -1, (240, 0, 159), 3) shape = "" if area < 2000: continue # O aspect ratio is square if 0.9 <= aspectRatio <= 1.1: shape = "o" # I aspect ratio is long elif aspectRatio >= 2.0: shape = "i" # Angle magic elif angle > 160 or angle < 40 or area < 3000: shape = "t" elif angle >= 115: shape = "z" elif angle >= 100: shape = "j" elif angle >= 60: shape = "l" elif angle > 50: shape = "s" else: shape = "?" return shape # show the contour properties # print("{}, {:.2f}, {:.2f}, {:.2f}, {:.2f} , {:.2f}, {:.2f}" # .format(shape, aspectRatio, extent, solidity, angle, equi_diameter, area)) except: # print("Error") return "?" return "?"
# reigon1.drawBoundary(frame) cv2.imshow('fore', mask) _, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) # get the contour with the greatest area # max_area = -1 # ci = -1 for i in range(len(contours)): cnt = contours[i] area = cv2.contourArea(cnt) if area > 45: hull = cv2.convexHull(cnt) cv2.drawContours(frame, [hull], 0, (0, 255, 0), 2) # if(area>max_area): # max_area=area # ci=i # if(ci != -1): # cnt=contours[ci] # # cv2.imshow('mask',mask) # # Draw a rectangle in the center # frame = cv2.rectangle(frame,params.start,params.end,(0,255,0),1) # if(ci != -1):
#draw the ROI's on a picture # mask = np.zeros(d.shape, np.uint8) # for roi in rois: # cv2.drawContours(mask, [roi[0]], 0, 255, -1) #cv2.putText(pic, str(d[roi[2][1],roi[2][0]])[:4], (roi[2][0],roi[2][1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0)) #thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR) #pic = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) # pic = cv2.bitwise_and(c, c, mask=mask) if (hand != None): mask = np.zeros(d.shape, np.uint8) cv2.drawContours(mask, [hand], 0, 255, -1) pic = cv2.bitwise_and(c, c, mask=mask) #get the convex hull hull = cv2.convexHull(hand) cv2.drawContours(pic, [hull], 0, (0, 0, 255), 2) #get a bounding rectangle rect = cv2.minAreaRect(hand) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(pic, [box], 0, (0, 255, 0), 2) #get the convexity defects #this doesn't work on the depth map - it's too noisy, so commenting out # poly = cv2.approxPolyDP(roi[0], 0.001*cv2.arcLength(roi[0], True), True) # hull_idx = cv2.convexHull(poly, returnPoints=False) # defects = cv2.convexityDefects(poly, hull_idx) # for i in range(defects.shape[0]): # s,e,f,d = defects[i,0]
def count(thresholded, segmented): # find the convex hull of the segmented hand region chull = cv2.convexHull(segmented) # find the most extreme points in the convex hull extreme_top = tuple(chull[chull[:, :, 1].argmin()][0]) extreme_bottom = tuple(chull[chull[:, :, 1].argmax()][0]) extreme_left = tuple(chull[chull[:, :, 0].argmin()][0]) extreme_right = tuple(chull[chull[:, :, 0].argmax()][0]) # find the center of the palm cX = int((extreme_left[0] + extreme_right[0]) / 2) # problem arm # y_distanc = (abs(int(extreme_top[1] - extreme_bottom[1]) / abs(int(extreme_left[0] - extreme_right[0])))) # x_distanc = ( abs(int(extreme_left[0] - extreme_right[0])) / abs(int(extreme_top[1] - extreme_bottom[1]))) # if y_distanc > 2 * x_distanc: # cY = int(extreme_left[0] + extreme_right[0] / 2) # else: cY = int(extreme_top[1] + extreme_bottom[1] / 2) # find the maximum euclidean distance between the center of the palm # and the most extreme points of the convex hull distance = pairwise.euclidean_distances( [(cX, cY)], Y=[extreme_left, extreme_right, extreme_top, extreme_bottom])[0] maximum_distance = distance[distance.argmax()] # calculate the radius of the circle with 80% of the max euclidean distance obtained radius = int(0.8 * maximum_distance) # find the circumference of the circle circumference = (2 * np.pi * radius) # take out the circular region of interest which has # the palm and the fingers circular_roi = np.zeros(thresholded.shape[:2], dtype="uint8") # draw the circular ROI cv2.circle(circular_roi, (cX, cY), radius, 255, 1) # take bit-wise AND between thresholded hand using the circular ROI as the mask # which gives the cuts obtained using mask on the thresholded hand image circular_roi = cv2.bitwise_and(thresholded, thresholded, mask=circular_roi) # compute the contours in the circular ROI cnts = cv2.findContours(circular_roi.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0] # initalize the finger count count = 0 # loop through the contours found for c in cnts: # compute the bounding box of the contour (x, y, w, h) = cv2.boundingRect(c) # increment the count of fingers only if - # 1. The contour region is not the wrist (bottom area) # 2. The number of points along the contour does not exceed # 25% of the circumference of the circular ROI if ((cY + (cY * 0.25)) > (y + h)) and ((circumference * 0.25) > c.shape[0]): count += 1 return count
def Classify_Pieces(img_b, piece_cnt, edge_num, avgPC_W, avgPC_H): """ Once all pieces are fully visable this function will classify them all Input: Binarised puzzle layout image, Colour puzzle layout image with blacked out background, Piece contours Output: True if edge_num edges are found, Array containing piece objects """ ##### Max piece width [Calibration Values] max_piece_width = avgPC_W * 2 # 240 for hulk puzzle # 230 for hoarse puzzle ##### Angle deviation on line [Calibration Values] ang_dev = 0.15 # Rad # Define piece object array Piece_C = np.empty(0, dtype=object) # Copy images img_b_PC = np.copy(img_b) # Get image dimensions rows_PC, cols_PC, chan_PC = img_b_PC.shape ##### To check if all edges were found [Calibration Values] # edge_num = 20 #defined in parameters edge_count = 0 # For all valid piece contours print len(piece_cnt) for i, cnt in enumerate(piece_cnt): # Center location in overall image M = cv2.moments(cnt) cx_original = int(M["m10"] / M["m00"]) cy_original = int(M["m01"] / M["m00"]) # Find bound rectangle points x, y, w, h = cv2.boundingRect(cnt) if w % 2 != 0: w += 1 if h % 2 != 0: h += 1 # Create max_piece_width by max_piece_width image with puzzle piece at its center img_piece = np.zeros((max_piece_width, max_piece_width, 3), np.uint8) img_piece[max_piece_width / 2 - h / 2:max_piece_width / 2 + h / 2, max_piece_width / 2 - w / 2:max_piece_width / 2 + w / 2] = img_b_PC[y:y + h, x:x + w] #showimg(img_piece) # Find new piece image contour for re-centering img_piece_bin = Binarise(img_piece) piece_cnt = cv2.findContours(img_piece_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0] # Use only largest contour max_area = 0 m_a_index = 0 for h, cnt in enumerate(piece_cnt): area = int(np.ceil(cv2.contourArea(cnt))) if area > max_area: max_area = area m_a_index = h # True center of piece M = cv2.moments(piece_cnt[m_a_index]) cx = int(M["m10"] / M["m00"]) cy = int(M["m01"] / M["m00"]) # Offset from true center c_off_x = max_piece_width / 2 - cx c_off_y = max_piece_width / 2 - cy # Re-center image M = np.float32([[1, 0, c_off_x], [0, 1, c_off_y]]) img_piece = cv2.warpAffine(img_piece, M, (max_piece_width, max_piece_width)) # Set new piece image center cx = max_piece_width / 2 cy = max_piece_width / 2 # Binarise piece image and apply Canny edge detection img_piece_bin = Binarise(img_piece) img_piece_edge = cv2.Canny(img_piece_bin, 50, 150, apertureSize=3) #showimg(img_piece_edge) # Find all Hough lines in edge piece image minLineLength = avgPC_W / 4 maxLineGap = avgPC_W / 5 lines = cv2.HoughLines(img_piece_edge, 1, np.pi / 180, minLineLength, maxLineGap) try: tmp = len(lines) # might be no line in a puzle piece A_1 = [] A_2 = [] A_3 = [] A_4 = [] # For all lines for rho, theta in lines[0]: # Add all lines that are within ang_dev radians from one another if np.shape(A_1)[0] == 0: # A_1 is empty A_1.append(theta) elif abs( (sum(A_1) / len(A_1)) - theta ) <= ang_dev: # new line is extension of the previous lines A_1.append(theta) elif np.shape( A_2 )[0] == 0: # find a new line, not aligned with previous lines A_2.append(theta) elif abs((sum(A_2) / len(A_2)) - theta ) <= ang_dev: # new line, extension of previous lines A_2.append(theta) elif np.shape(A_3)[0] == 0: A_3.append(theta) elif abs((sum(A_3) / len(A_3)) - theta) <= ang_dev: A_3.append(theta) elif np.shape(A_4)[0] == 0: A_4.append(theta) elif abs((sum(A_4) / len(A_4)) - theta) <= ang_dev: A_4.append(theta) # Use edge with the most lines if len(A_1) >= len(A_2) and len(A_1) >= len(A_3) and len( A_1) >= len(A_4): A = sum(A_1) / len(A_1) elif len(A_2) >= len(A_3) and len(A_2) >= len(A_4): A = sum(A_2) / len(A_2) elif len(A_3) >= len(A_4): A = sum(A_3) / len(A_3) else: A = sum(A_4) / len(A_4) # A is the theta(averaged) of the edge with the most lines except: rect = cv2.minAreaRect(cnt) #cv2.drawContours(img_piece,[rect],0,(0,0,255),2) #showimg(img_piece) A = rect[2] print 'exception: angle: ' + float((A * 180 / np.pi) % 90) # Convert angle to degrees (<90) A_C = (A * 180 / np.pi) % 90 # Rotate piece image and binarised image M = cv2.getRotationMatrix2D((max_piece_width / 2, max_piece_width / 2), A_C, 1) img_piece = cv2.warpAffine(img_piece, M, (max_piece_width, max_piece_width)) # Find new piece contour defects img_piece_bin = Binarise(img_piece) piece_cnt = cv2.findContours(img_piece_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0] # Use only largest contour max_area = 0 m_a_index = 0 for h, cnt in enumerate(piece_cnt): area = int(np.ceil(cv2.contourArea(cnt))) if area > max_area: max_area = area m_a_index = h # Find piece contour defects hull = cv2.convexHull(piece_cnt[m_a_index], returnPoints=False) defects = cv2.convexityDefects(piece_cnt[m_a_index], hull) top_found_bool = False right_found_bool = False bottom_found_bool = False left_found_bool = False ######### Indent\tab boundary defect distances [Calibration Values] indent_min_d = avgPC_W / 4 # 9500 For hulk Puzzle # 6500 For hoarse puzzle tab_max_d = avgPC_W / 1.5 defect_line_max_d = avgPC_W / 6 # 30 # 25 Previous value ######### Tab width from (cx, cy) to consider [Calibration Values] tab_w_off = max_piece_width / 10 # Indent/tab matrix it_class_C = np.zeros(4, dtype=int) if DEBUG_IMAGE: img_piece_debug = np.copy(img_piece) # Search for indents for search in range(defects.shape[0]): s, e, f, d = defects[search, 0] if s < cnt.shape[0] and e < cnt.shape[0] and f < cnt.shape[ 0]: # defect within piece start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) # If defect is far enough from fit contour if d > indent_min_d: # Indent to top if not top_found_bool and cy > far[1] and np.abs( cx - far[0]) < np.abs(cy - far[1]): top_found_bool = True it_class_C[0] = -1 if DEBUG_IMAGE: cv2.line(img_piece_debug, start, end, [255, 0, 255], 2) cv2.circle(img_piece_debug, far, 3, [0, 0, 255], -1) cv2.circle(img_piece_debug, far, 1, [255, 0, 255], -1) # Indent to right elif not right_found_bool and cx < far[0] and np.abs( cx - far[0]) > np.abs(cy - far[1]): right_found_bool = True it_class_C[1] = -1 if DEBUG_IMAGE: cv2.line(img_piece_debug, start, end, [255, 0, 255], 2) cv2.circle(img_piece_debug, far, 3, [0, 0, 255], -1) cv2.circle(img_piece_debug, far, 1, [255, 0, 255], -1) # Indent to bottom elif not bottom_found_bool and cy < far[1] and np.abs( cx - far[0]) < np.abs(cy - far[1]): bottom_found_bool = True it_class_C[2] = -1 if DEBUG_IMAGE: cv2.line(img_piece_debug, start, end, [255, 0, 255], 2) cv2.circle(img_piece_debug, far, 3, [0, 0, 255], -1) cv2.circle(img_piece_debug, far, 1, [255, 0, 255], -1) # Indent to left elif not left_found_bool and cx > far[0] and np.abs( cx - far[0]) > np.abs(cy - far[1]): left_found_bool = True it_class_C[3] = -1 if DEBUG_IMAGE: cv2.line(img_piece_debug, start, end, [255, 0, 255], 2) cv2.circle(img_piece_debug, far, 3, [0, 0, 255], -1) cv2.circle(img_piece_debug, far, 1, [255, 0, 255], -1) # Search for tabs for search in range(defects.shape[0]): s, e, f, d = defects[search, 0] if s < cnt.shape[0] and e < cnt.shape[0] and f < cnt.shape[0]: start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) if d < tab_max_d and np.sqrt( (start[0] - end[0])**2 + (start[1] - end[1])**2) < defect_line_max_d: # Tab to top if not top_found_bool and cy > far[1] and np.abs( cx - far[0]) < np.abs(cy - far[1]) and np.abs( cx - far[0]) <= tab_w_off: top_found_bool = True it_class_C[0] = 1 if DEBUG_IMAGE: cv2.line(img_piece_debug, start, end, [0, 255, 0], 2) cv2.circle(img_piece_debug, far, 3, [255, 0, 0], -1) cv2.circle(img_piece_debug, far, 1, [0, 255, 0], -1) # Tab to right elif not right_found_bool and cx < far[0] and np.abs( cx - far[0]) > np.abs(cy - far[1]) and np.abs( cy - far[1]) <= tab_w_off: right_found_bool = True it_class_C[1] = 1 if DEBUG_IMAGE: cv2.line(img_piece_debug, start, end, [0, 255, 0], 2) cv2.circle(img_piece_debug, far, 3, [255, 0, 0], -1) cv2.circle(img_piece_debug, far, 1, [0, 255, 0], -1) # Tab to bottom elif not bottom_found_bool and cy < far[1] and np.abs( cx - far[0]) < np.abs(cy - far[1]) and np.abs( cx - far[0]) <= tab_w_off: bottom_found_bool = True it_class_C[2] = 1 if DEBUG_IMAGE: cv2.line(img_piece_debug, start, end, [0, 255, 0], 2) cv2.circle(img_piece_debug, far, 3, [255, 0, 0], -1) cv2.circle(img_piece_debug, far, 1, [0, 255, 0], -1) # Tab to left elif not left_found_bool and cx > far[0] and np.abs( cx - far[0]) > np.abs(cy - far[1]) and np.abs( cy - far[1]) <= tab_w_off: left_found_bool = True it_class_C[3] = 1 if DEBUG_IMAGE: cv2.line(img_piece_debug, start, end, [0, 255, 0], 2) cv2.circle(img_piece_debug, far, 3, [255, 0, 0], -1) cv2.circle(img_piece_debug, far, 1, [0, 255, 0], -1) # showimg(img_piece_debug) if not top_found_bool: # Edge to top edge_count += 1 top_found_bool = True if not right_found_bool: # Edge to right edge_count += 1 right_found_bool = True if not bottom_found_bool: # Edge to bottom edge_count += 1 bottom_found_bool = True if not left_found_bool: # Edge to left edge_count += 1 left_found_bool = True print 'edge_count' + str(edge_count) if DEBUG_IMAGE: cv2.circle(img_piece_debug, tuple([max_piece_width / 2, max_piece_width / 2]), 3, [0, 0, 0], -1) cv2.circle(img_piece_debug, tuple([max_piece_width / 2, max_piece_width / 2]), 1, [255, 255, 255], -1) cwd = os.getcwd() # Save piece image piece_file_name_C = cwd + "/Cpieces/p_" + str(i) + ".png" cv2.imwrite(piece_file_name_C, img_piece) # Create piece object piece_center_location_C = tuple([cx_original, cy_original]) Piece_C = np.append( Piece_C, C.Puzzle_Piece(i, piece_center_location_C, it_class_C, A_C, piece_file_name_C)) if DEBUG_IMAGE: cv2.circle(img_piece_debug, tuple([max_piece_width / 2, max_piece_width / 2]), 3, [0, 0, 0], -1) cv2.circle(img_piece_debug, tuple([max_piece_width / 2, max_piece_width / 2]), 1, [0, 165, 255], -1) if DEBUG_IMAGE: cv2.imshow("Debug [Piece" + str(i) + "]", img_piece_debug) return edge_count == edge_num, Piece_C
def run_detector(): # define two constants, one for the eye aspect ratio to indicate # blink and then a second constant for the number of consecutive # frames the eye must be below the threshold for to set off the # alarm EYE_AR_THRESH = 0.3 EYE_AR_CONSEC_FRAMES = 48 # initialize the frame counter as well as a boolean used to # indicate if the alarm is going off COUNTER = 0 ALARM_ON = False # initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor print("[INFO] loading facial landmark predictor...") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # grab the indexes of the facial landmarks for the left and # grab the indexes of the facial landmarks for the left and # right eye, respectively (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] # start the video stream thread print("[INFO] starting video stream thread...") # vs = VideoStream(src=args["webcam"]).start() # # time.sleep(1.0) cam = cv2.VideoCapture(1 + cv2.CAP_DSHOW) # Current time in millis start_time = int(round(time.time() * 1000)) #time 30 seconds later in millis stop_time = int(round(time.time() * 1000)) + 5000 # loop over frames from the video stream while True: # grab the frame from the threaded video file stream, resize # it, and convert it to grayscale # channels) ret, frame = cam.read() frame = imutils.resize(frame, width=450) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # detect faces in the grayscale frame rects = detector(gray, 0) # loop over the face detections for rect in rects: # determine the facial landmarks for the face region, then # convert the facial landmark (x, y)-coordinates to a NumPy # array shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) # extract the left and right eye coordinates, then use the # coordinates to compute the eye aspect ratio for both eyes leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) # average the eye aspect ratio together for both eyes ear = (leftEAR + rightEAR) / 2.0 # compute the convex hull for the left and right eye, then # visualize each of the eyes leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) # check to see if the eye aspect ratio is below the blink # threshold, and if so, increment the blink frame counter if ear < EYE_AR_THRESH: COUNTER += 1 # if the eyes were closed for a sufficient number of # then sound the alarm if COUNTER >= EYE_AR_CONSEC_FRAMES: # if the alarm is not on, turn it on if not ALARM_ON: ALARM_ON = True # check to see if an alarm file was supplied, # and if so, start a thread to have the alarm # sound played in the background t = Thread(target=sound_alarm, args=("alarm.wav", )) t.deamon = True t.start() # draw an alarm on the frame cv2.putText(frame, "DROWSINESS ALERT!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # otherwise, the eye aspect ratio is not below the blink # threshold, so reset the counter and alarm else: COUNTER = 0 ALARM_ON = False # draw the computed eye aspect ratio on the frame to help # with debugging and setting the correct eye aspect ratio # thresholds and frame counters cv2.putText(frame, "EAR: {:.2f}".format(ear), (300, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # show the frame cv2.imshow("Frame", frame) cv2.waitKey(1) & 0xFF # Time to run detector for # Currently set to 1 seconds for testing start_time = int(round(time.time() * 1000)) if start_time > stop_time: break # do a bit of cleanup cv2.destroyAllWindows() cam.release()
cv2.CHAIN_APPROX_NONE) Area = [] # to save the area hull = [] # to save the calculated coordinates # Calculate the Area of the contours and save them in a list for I in range(len(contours)): Area.append(cv2.contourArea(contours[I])) Max = max(Area) # to isolate the larger region # Find the largest area of the contour, among them for j in range(len(Area)): if Area[j] == Max: cnt = contours[j] # Extract the coordinates of the outer, surrounding the contour hull = (cv2.convexHull(cnt)) # Perform the contour approximation with given outer to obatin a outline Con = cv2.approxPolyDP(hull, 0.01 * cv2.arcLength(hull, True), True) #cv2.approxPlyDP(curve,epsilon,closed,approxCurve) : to find the arc length of the contour #cv2.arcLength(curve,closed): #curve: Input vector of 2D points #closed : Flag indicating whether the curve is closed or not #epsilon: accouracy parameter: ,0.01*cv2.arcLength(hull,True) #curve : Input vector of a 2D Point #approx Curve : result of the approximation #closed:if True, the aproximated curve is closed == first and last vertices are connected # using the index of the contour, find the circle # Reset the previous mask A = Mask.copy()
rects = detector(image, 1) bg = np.zeros((480, 640, 3), dtype=np.uint8) a, b, c = bg.shape for (i, rect) in enumerate(rects): shape = predictor(image, rect) shape = face_utils.shape_to_np(shape) leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] mouth = shape[mStart:mEnd] Mouth = cv2.convexHull(mouth) leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) Mouth = antimirror(b, Mouth) leftEyeHull = antimirror(b, leftEyeHull) rightEyeHul = antimirror(b, rightEyeHull) x1 = Thread(target=cv2.drawContours, args=(bg, [Mouth], 0, nr, -1)) x2 = Thread(target=cv2.drawContours, args=(bg, [leftEyeHull], 0, nr, -1)) x3 = Thread(target=cv2.drawContours,
def draw_convex_hull(im, points, color): points = cv2.convexHull(points) cv2.fillConvexPoly(im, points, color=color)
txt_path2 = "E:/data_ceshi/sswap/pointsjpg.txt" img1 = cv2.imread(filename1) img2 = cv2.imread(filename2) img1Warped = np.copy(img2) # Read array of corresponding points points1 = readPoints(txt_path1) print(len(points1)) points2 = readPoints(txt_path2) print(len(points2)) # Find convex hull hull1 = [] hull2 = [] img_corp = img1.copy() hullIndex = cv2.convexHull(np.array(points2), returnPoints=False) # find convexHull hullIndex1 = cv2.convexHull(np.array(points1)) for i in range(len(hullIndex1)): cv2.line(img_corp, tuple(hullIndex1[i][0]), tuple(hullIndex1[(i + 1) % len(hullIndex1)][0]), (255, 0, 0), 2) #cv2.circle(img_corp,i,2,(205,0,0),2) img_point = img1.copy() for i in points1: cv2.circle(img_point, tuple(i), 2, (0, 255, 0), 5) fillbox = np.hstack((img1, img_point, img_corp)) cv2.imwrite("E:/data_ceshi/sswap/fillbox.png", fillbox) for i in range(0, len(hullIndex)):
def build_correspondance(self, visible_markers): """ - use all visible markers - fit a convex quadrangle around it - use quadrangle verts to establish perpective transform - map all markers into surface space - build up list of found markers and their uv coords """ if visible_markers == []: self.m_to_screen = None self.m_from_screen = None self.detected = False return all_verts = np.array([[m['verts_norm'] for m in visible_markers]]) all_verts.shape = ( -1, 1, 2) # [vert,vert,vert,vert,vert...] with vert = [[r,c]] hull = cv2.convexHull(all_verts, clockwise=False) #simplify until we have excatly 4 verts if hull.shape[0] > 4: new_hull = cv2.approxPolyDP(hull, epsilon=1, closed=True) if new_hull.shape[0] >= 4: hull = new_hull if hull.shape[0] > 4: curvature = abs(GetAnglesPolyline(hull, closed=True)) most_acute_4_threshold = sorted(curvature)[3] hull = hull[curvature <= most_acute_4_threshold] #now we need to roll the hull verts until we have the right orientation: distance_to_origin = np.sqrt(hull[:, :, 0]**2 + hull[:, :, 1]**2) top_left_idx = np.argmin(distance_to_origin) hull = np.roll(hull, -top_left_idx, axis=0) #based on these 4 verts we calculate the transformations into a 0,0 1,1 square space self.m_to_screen = m_verts_to_screen(hull) self.m_from_screen = m_verts_from_screen(hull) self.detected = True # map the markers vertices in to the surface space (one can think of these as texture coordinates u,v) marker_uv_coords = cv2.perspectiveTransform(all_verts, self.m_from_screen) marker_uv_coords.shape = ( -1, 4, 1, 2) #[marker,marker...] marker = [ [[r,c]],[[r,c]] ] # build up a dict of discovered markers. Each with a history of uv coordinates for m, uv in zip(visible_markers, marker_uv_coords): try: self.markers[m['id']].add_uv_coords(uv) except KeyError: self.markers[m['id']] = Support_Marker(m['id']) self.markers[m['id']].add_uv_coords(uv) #average collection of uv correspondences accros detected markers self.build_up_status = sum( [len(m.collected_uv_coords) for m in self.markers.values()]) / float(len(self.markers)) if self.build_up_status >= self.required_build_up: self.finalize_correnspondance() self.defined = True
def count(image, thresholded, segmented): # find the convex hull of the segmented hand region # which is the maximum contour with respect to area chull = cv2.convexHull(segmented) # find the most extreme points in the convex hull extreme_top = tuple(chull[chull[:, :, 1].argmin()][0]) extreme_bottom = tuple(chull[chull[:, :, 1].argmax()][0]) extreme_left = tuple(chull[chull[:, :, 0].argmin()][0]) extreme_right = tuple(chull[chull[:, :, 0].argmax()][0]) # find the center of the palm cX = int((extreme_left[0] + extreme_right[0]) / 2) cY = int((extreme_top[1] + extreme_bottom[1]) / 2) # find the maximum euclidean distance between the center of the palm # and the most extreme points of the convex hull distances = pairwise.euclidean_distances( [(cX, cY)], Y=[extreme_left, extreme_right, extreme_top, extreme_bottom])[0] max_distance = distances[distances.argmax()] # calculate the radius of the circle with 80% of the max euclidean distance obtained radius = int(0.8 * max_distance) # find the circumference of the circle circumference = (2 * np.pi * radius) # initialize circular_roi with same shape as thresholded image circular_roi = np.zeros(thresholded.shape[:2], dtype="uint8") # draw the circular ROI with radius and center point of convex hull calculated above cv2.circle(circular_roi, (cX, cY), radius, 255, 1) # take bit-wise AND between thresholded hand using the circular ROI as the mask # which gives the cuts obtained using mask on the thresholded hand image circular_roi = cv2.bitwise_and(thresholded, thresholded, mask=circular_roi) # compute the contours in the circular ROI (_, cnts, _) = cv2.findContours(circular_roi.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) count = 0 # approach 1 - eliminating wrist #cntsSorted = sorted(cnts, key=lambda x: cv2.contourArea(x)) #print(len(cntsSorted[1:])) # gives the count of fingers # approach 2 - eliminating wrist # loop through the contours found for i, c in enumerate(cnts): # compute the bounding box of the contour (x, y, w, h) = cv2.boundingRect(c) # increment the count of fingers only if - # 1. The contour region is not the wrist (bottom area) # 2. The number of points along the contour does not exceed # 25% of the circumference of the circular ROI if ((cY + (cY * 0.25)) > (y + h)) and ((circumference * 0.25) > c.shape[0]): count += 1 return count
images = np.array(images) nonzero = [] for i in range((len(images) - 1)): mask = cv2.absdiff(images[i], images[i + 1]) # mask = cv2.adaptiveThreshold(mask, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \ # cv2.THRESH_BINARY, 11, 2) mask = cv2.adaptiveThreshold(mask, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 101, 0) cnts, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) plot_img = normal_images[i] hierarchy = hierarchy[0] for c in cnts: c = cv2.convexHull(c) area = cv2.contourArea(c) perimeter = cv2.arcLength(c, True) epsilon = 0.01 * cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, epsilon, True) if 8 < len(approx) < 15 and 512 < area < 952 and 83 < perimeter < 109: print("Area = {} | Points = {} | Perimeter = {}".format( area, len(approx), perimeter)) M = cv2.moments(c) cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) cv2.drawContours(plot_img, [c], -1, (0, 255, 255), 1)
landmarks = predictor(img_gray, face) landmarks_points = [] for n in range(36, 41): x = landmarks.part(n).x y = landmarks.part(n).y landmarks_points.append((x, y)) for n in range(42, 47): x = landmarks.part(n).x y = landmarks.part(n).y landmarks_points.append((x, y)) # cv2.circle(img, (x, y), 3, (0, 0, 255), -1) # landmarks_points = np.array([(landmarks.part(36).x, landmarks.part(36).y),(landmarks.part(37).x, landmarks.part(37).y),(landmarks.part(38).x, landmarks.part(38).y),(landmarks.part(39).x, landmarks.part(39).y),(landmarks.part(40).x, landmarks.part(40).y),(landmarks.part(41).x, landmarks.part(41).y),(landmarks.part(42).x, landmarks.part(42).y),(landmarks.part(43).x, landmarks.part(43).y),(landmarks.part(44).x, landmarks.part(44).y),(landmarks.part(45).x, landmarks.part(45).y),(landmarks.part(46).x, landmarks.part(46).y),(landmarks.part(47).x, landmarks.part(47).y)],np.int32) points = np.array(landmarks_points, np.int32) convexhull = cv2.convexHull(points) # cv2.polylines(img, [convexhull], True, (255, 0, 0), 3) cv2.fillConvexPoly(mask, convexhull, 255) face_image_1 = cv2.bitwise_and(img, img, mask=mask) # Delaunay triangulation rect = cv2.boundingRect(convexhull) subdiv = cv2.Subdiv2D(rect) subdiv.insert(landmarks_points) triangles = subdiv.getTriangleList() triangles = np.array(triangles, dtype=np.int32) indexes_triangles = [] for t in triangles: pt1 = (t[0], t[1])
def findTape(contours, image, centerX, centerY): screenHeight, screenWidth, channels = image.shape; #Seen vision targets (correct angle, adjacent to each other) targets = [] if len(contours) >= 2: #Sort contours by area size (biggest to smallest) cntsSorted = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True) biggestCnts = [] for cnt in cntsSorted: # Get moments of contour; mainly for centroid M = cv2.moments(cnt) # Get convex hull (bounding polygon on contour) hull = cv2.convexHull(cnt) # Calculate Contour area cntArea = cv2.contourArea(cnt) # calculate area of convex hull hullArea = cv2.contourArea(hull) # Filters contours based off of size if (checkContours(cntArea, hullArea)): ### MOSTLY DRAWING CODE, BUT CALCULATES IMPORTANT INFO ### # Gets the centeroids of contour if M["m00"] != 0: cx = int(M["m10"] / M["m00"]) cy = int(M["m01"] / M["m00"]) else: cx, cy = 0, 0 if(len(biggestCnts) < 13): #### CALCULATES ROTATION OF CONTOUR BY FITTING ELLIPSE ########## rotation = getEllipseRotation(image, cnt) # Calculates yaw of contour (horizontal position in degrees) yaw = calculateYaw(cx, centerX, H_FOCAL_LENGTH) # Calculates yaw of contour (horizontal position in degrees) pitch = calculatePitch(cy, centerY, V_FOCAL_LENGTH) ##### DRAWS CONTOUR###### # Gets rotated bounding rectangle of contour rect = cv2.minAreaRect(cnt) # Creates box around that rectangle box = cv2.boxPoints(rect) # Not exactly sure box = np.int0(box) # Draws rotated rectangle cv2.drawContours(image, [box], 0, (23, 184, 80), 3) # Calculates yaw of contour (horizontal position in degrees) yaw = calculateYaw(cx, centerX, H_FOCAL_LENGTH) # Calculates yaw of contour (horizontal position in degrees) pitch = calculatePitch(cy, centerY, V_FOCAL_LENGTH) # Draws a vertical white line passing through center of contour cv2.line(image, (cx, screenHeight), (cx, 0), (255, 255, 255)) # Draws a white circle at center of contour cv2.circle(image, (cx, cy), 6, (255, 255, 255)) # Draws the contours cv2.drawContours(image, [cnt], 0, (23, 184, 80), 1) # Gets the (x, y) and radius of the enclosing circle of contour (x, y), radius = cv2.minEnclosingCircle(cnt) # Rounds center of enclosing circle center = (int(x), int(y)) # Rounds radius of enclosning circle radius = int(radius) # Makes bounding rectangle of contour rx, ry, rw, rh = cv2.boundingRect(cnt) boundingRect = cv2.boundingRect(cnt) # Draws countour of bounding rectangle and enclosing circle in green cv2.rectangle(image, (rx, ry), (rx + rw, ry + rh), (23, 184, 80), 1) cv2.circle(image, center, radius, (23, 184, 80), 1) # Appends important info to array if [cx, cy, rotation, cnt] not in biggestCnts: biggestCnts.append([cx, cy, rotation, cnt]) # Sorts array based on coordinates (leftmost to rightmost) to make sure contours are adjacent biggestCnts = sorted(biggestCnts, key=lambda x: x[0]) # Target Checking for i in range(len(biggestCnts) - 1): #Rotation of two adjacent contours tilt1 = biggestCnts[i][2] tilt2 = biggestCnts[i + 1][2] #x coords of contours cx1 = biggestCnts[i][0] cx2 = biggestCnts[i + 1][0] cy1 = biggestCnts[i][1] cy2 = biggestCnts[i + 1][1] # If contour angles are opposite if (np.sign(tilt1) != np.sign(tilt2)): centerOfTarget = math.floor((cx1 + cx2) / 2) #ellipse negative tilt means rotated to right #Note: if using rotated rect (min area rectangle) # negative tilt means rotated to left # If left contour rotation is tilted to the left then skip iteration if (tilt1 > 0): if (cx1 < cx2): continue # If left contour rotation is tilted to the left then skip iteration if (tilt2 > 0): if (cx2 < cx1): continue #Angle from center of camera to target (what you should pass into gyro) yawToTarget = calculateYaw(centerOfTarget, centerX, H_FOCAL_LENGTH) #Make sure no duplicates, then append if [centerOfTarget, yawToTarget] not in targets: targets.append([centerOfTarget, yawToTarget]) #Check if there are targets seen if (len(targets) > 0): # pushes that it sees vision target to network tables networkTable.putBoolean("tapeDetected", True) #Sorts targets based on x coords to break any angle tie targets.sort(key=lambda x: math.fabs(x[0])) finalTarget = min(targets, key=lambda x: math.fabs(x[1])) # Puts the yaw on screen #Draws yaw of target + line where center of target is cv2.putText(image, "Yaw: " + str(finalTarget[1]), (40, 40), cv2.FONT_HERSHEY_COMPLEX, .6, (255, 255, 255)) cv2.line(image, (finalTarget[0], screenHeight), (finalTarget[0], 0), (255, 0, 0), 2) currentAngleError = finalTarget[1] # pushes vision target angle to network tables networkTable.putNumber("tapeYaw", currentAngleError) else: # pushes that it deosn't see vision target to network tables networkTable.putBoolean("tapeDetected", False) cv2.line(image, (round(centerX), screenHeight), (round(centerX), 0), (255, 255, 255), 2) return image
def findFace(l, s, f, v, a): #Create HOG face detector from dlib class face_detector = dlib.get_frontal_face_detector() landmark_predictor_model = "shape_predictor_68_face_landmarks.dat" face_pose_predictor = dlib.shape_predictor(landmark_predictor_model) video_cap = cv2.VideoCapture(0) #image from video (default webcam) #if no one is in the frame last = True (leftStart, leftEnd) = facial_locations["left_eye"] (rightStart, rightEnd) = facial_locations["right_eye"] EYE_THRESH = 0.3 EYE_FRAMES = 3 COUNTER = 0 TOTAL = 0 while True: #capture frame by frame #returns return code(tells us if we have run out of frames) #frame = one frame ret, frame = video_cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) detected_faces = face_detector(gray, 1) if (v): print("Found {} faces in this frame.".format(len(detected_faces))) if (len(detected_faces) == 0): last = True # Loop through all faces found for i, face_rect in enumerate(detected_faces): if (v): print( "- Face #{} found at Left: {} Top: {} Right: {} Bottom: {}" .format(i, face_rect.left(), face_rect.top(), face_rect.right(), face_rect.bottom())) # Draw a box around each face we found cv2.rectangle(frame, (face_rect.left(), face_rect.top()), (face_rect.right(), face_rect.bottom()), (0, 255, 0), 2) crop = frame[face_rect.top():face_rect.bottom(), face_rect.left():face_rect.right()] #Generate and store landmarks face_landmarks = face_pose_predictor(frame, face_rect) points = store_landmarks(face_landmarks) if (a): #Collect facial feature coordinates leftEye = points[leftStart:leftEnd] rightEye = points[rightStart:rightEnd] lear = eye_aspect_ratio(leftEye) rear = eye_aspect_ratio(rightEye) aear = (lear + rear) / 2.0 leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) if aear < EYE_THRESH: COUNTER += 1 else: if COUNTER >= EYE_FRAMES: TOTAL += 1 COUNTER = 0 cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) #If user wants to save faces, save into file #TODO: given filename save if (s and last): last = False cv2.imwrite("face.jpg", crop) #If user wants to show landmarks on screen if (l): frame = show_landmarks(frame, face_landmarks, points, v) #Display info cv2.putText(frame, 'SEARCHING FOR FACES...', topLeft, font, fontScale, fontColor, lineType) cv2.imshow('Video', frame) # Close when 'q' key is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break video_cap.release() cv2.destroyAllWindows()
mouth_open = False # This variable will be used below to help know if the mouth has been closed after yawning while (True): # While loop helps taking multiple frames from detection and keep the program on until told to shut ret, frame = cap.read() # ret stores the boolean value returned from read method. If yes, then the camera is on and face/faces are being detected distance = decision(frame) # Creates a variable that stores the distance value between two lips returned by the function created above if(ret == True): # If ret is True, the program moves forward landmarks = get_landmarks(frame) if(landmarks.all()!=[0]): # This checks if the landmarks created are not null l1=[] # New list is created to store boundary cordinates of the entire lips for k in range(48,60): # 48-60 in DLIB that defines the lips of a face l1.append(landmarks[k]) # Appends the coridnates of the lips using landmakrs function created above l2 = np.asarray(l1) lips = cv2.convexHull(l2) # convexHull is a mathematic form of tracing a curve by cordinates cv2.drawContours(frame, [lips], -1, (0, 255, 0), 1) # Draws countours on the lips for asthetic purposes if (distance > 35): # If distance is more than 35, we change the boolean value of mouth_open to True, it acts as a flag mouth_open = True if (distance < 20) and mouth_open: # Now condition is if the mouth is now closed (based on distance betweent the two lips) and that the mouth was previously open yawns = yawns + 1 # Increment the yawn count mouth_open = False # Flag the mouth_open to false again so it can be used again cv2.putText(frame,"Yawn Count: "+str(yawns),(50,100),fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale=1.0,color=(0,255,255)) # This shows a text on given cordinates on the image itelf cv2.imshow("Subject Yawn Count",frame) # This streams the video being captured on the screen if cv2.waitKey(1) == 27: # Waits for ESCAPE key to be pressed to break out of the code break else:
def findBall(contours, image, centerX, centerY): screenHeight, screenWidth, channels = image.shape; #Seen vision targets (correct angle, adjacent to each other) cargo = [] if len(contours) > 0: #Sort contours by area size (biggest to smallest) cntsSorted = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True) biggestCargo = [] for cnt in cntsSorted: x, y, w, h = cv2.boundingRect(cnt) aspect_ratio = float(w) / h # Get moments of contour; mainly for centroid M = cv2.moments(cnt) # Get convex hull (bounding polygon on contour) hull = cv2.convexHull(cnt) # Calculate Contour area cntArea = cv2.contourArea(cnt) # Filters contours based off of size if (checkBall(cntArea, aspect_ratio)): ### MOSTLY DRAWING CODE, BUT CALCULATES IMPORTANT INFO ### # Gets the centeroids of contour if M["m00"] != 0: cx = int(M["m10"] / M["m00"]) cy = int(M["m01"] / M["m00"]) else: cx, cy = 0, 0 if(len(biggestCargo) < 3): ##### DRAWS CONTOUR###### # Gets rotated bounding rectangle of contour rect = cv2.minAreaRect(cnt) # Creates box around that rectangle box = cv2.boxPoints(rect) # Not exactly sure box = np.int0(box) # Draws rotated rectangle cv2.drawContours(image, [box], 0, (23, 184, 80), 3) # Draws a vertical white line passing through center of contour cv2.line(image, (cx, screenHeight), (cx, 0), (255, 255, 255)) # Draws a white circle at center of contour cv2.circle(image, (cx, cy), 6, (255, 255, 255)) # Draws the contours cv2.drawContours(image, [cnt], 0, (23, 184, 80), 1) # Gets the (x, y) and radius of the enclosing circle of contour (x, y), radius = cv2.minEnclosingCircle(cnt) # Rounds center of enclosing circle center = (int(x), int(y)) # Rounds radius of enclosning circle radius = int(radius) # Makes bounding rectangle of contour rx, ry, rw, rh = cv2.boundingRect(cnt) # Draws countour of bounding rectangle and enclosing circle in green cv2.rectangle(image, (rx, ry), (rx + rw, ry + rh), (23, 184, 80), 1) cv2.circle(image, center, radius, (23, 184, 80), 1) # Appends important info to array if [cx, cy, cnt] not in biggestCargo: biggestCargo.append([cx, cy, cnt]) # Check if there are cargo seen if (len(biggestCargo) > 0): #pushes that it sees cargo tables networkTable.putBoolean("cargoDetected", True) # Sorts targets based on x coords to break any angle tie biggestCargo.sort(key=lambda x: math.fabs(x[0])) closestCargo = min(biggestCargo, key=lambda x: (math.fabs(x[0] - centerX))) xCoord = closestCargo[0] finalTarget = calculateYaw(xCoord, centerX, H_FOCAL_LENGTH) print("Yaw: " + str(finalTarget)) # Puts the yaw on screen # Draws yaw of target + line where center of target is cv2.putText(image, "Yaw: " + str(finalTarget), (40, 40), cv2.FONT_HERSHEY_COMPLEX, .6, (255, 255, 255)) cv2.line(image, (xCoord, screenHeight), (xCoord, 0), (255, 0, 0), 2) currentAngleError = finalTarget #pushes cargo angle to network tables networkTable.putNumber("cargoYaw", currentAngleError) else: #pushes that it doesn't see cargo to network tables networkTable.putBoolean("cargoDetected", False) cv2.line(image, (round(centerX), screenHeight), (round(centerX), 0), (255, 255, 255), 2) return image
# print(contours) max_area = 100 ci = 0 for i in range(len(contours)): cnt = contours[i] area = cv2.contourArea(cnt) if (area > max_area): max_area = area ci = i #Largest area contour cnts = contours[ci] #Find convex hull hull = cv2.convexHull(cnts) #Find convex defects hull2 = cv2.convexHull(cnts, returnPoints=False) defects = cv2.convexityDefects(cnts, hull2) #Get defect points and draw them in the original image FarDefect = [] for i in range(defects.shape[0]): s, e, f, d = defects[i, 0] start = tuple(cnts[s][0]) end = tuple(cnts[e][0]) far = tuple(cnts[f][0]) FarDefect.append(far) cv2.line(frame, start, end, [0, 255, 0], 1)
def main(): global COUNTER global TOTAL shape_predictor_path = '../shape_predictor_68_face_landmarks.dat' video_path = '../blink_test.mp4' detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(shape_predictor_path) print('Load detector successfully') (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] vs = FileVideoStream(video_path).start() file_stream = True while True: if file_stream and not vs.more(): break frame = vs.read() frame = imutils.resize(frame, width=450) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # detect faces in the grayscale frame rects = detector(gray, 0) for rect in rects: # determine the facial landmarks for the face region, then # convert the facial landmark (x, y)-coordinates to a NumPy # array shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) # extract the left and right eye coordinates, then use the # coordinates to compute the eye aspect ratio for both eyes leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) # average the eye aspect ratio together for both eyes ear = (leftEAR + rightEAR) / 2.0 leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) if ear < EYE_AR_THRESH: COUNTER += 1 else: if COUNTER >= EYE_AR_CONSEC_FRAMES: TOTAL += 1 COUNTER = 0 cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.putText(frame, "EAR: {:.2f}".format(ear), (300, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break cv2.destroyAllWindows() vs.stop()
# blurring the image mask = cv2.GaussianBlur(mask, (5, 5), 100) # find contours contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # find contour of max area(hand) cnt = max(contours, key=lambda x: cv2.contourArea(x)) # approx the contour a little epsilon = 0.0005 * cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, epsilon, True) # making convex hull hull = cv2.convexHull(cnt) # define area of hull and area of hand areahull = cv2.contourArea(hull) areacnt = cv2.contourArea(cnt) # find the percentage of area not covered by hand in convex hull arearatio = ((areahull - areacnt) / areacnt) * 100 # find the defects in convex hull with respect to hand hull = cv2.convexHull(approx, returnPoints=False) defects = cv2.convexityDefects(approx, hull) # l = no. of defects l = 0
def jaggedlySilhoutteReward(self, goodDiffConvexTarget = 600): # gives the ratio of the distance in leght between the perimeters # of the silhoutte contours and the convex perimenters # find bigger external contour grayConv = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) #ImageCopy = self.image.copy() # get the outer silhouette and max one shape only Imgblurred = cv2.GaussianBlur(grayConv, (5, 5), 0) ret,thresh = cv2.threshold(Imgblurred, 1, 255, cv2.THRESH_BINARY) ing2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # single clear silhoutte contours = sorted(contours, key = cv2.contourArea, reverse = True) # calculate its perimeter perimeterExternalCnt = cv2.arcLength(contours[0], True) # calculate convexHull hull = cv2.convexHull(contours[0]) #calculate perimenter convexHull perimeterHull = cv2.arcLength(hull, True) # draw contours # ============================================================================= # cv2.drawContours(self.image, [hull], -1, (0,255,0), 1) # ============================================================================= # determine the ratio diff = abs(perimeterExternalCnt - perimeterHull) # extract the score distFromTargetvalue = abs(goodDiffConvexTarget - diff) distFromTarget = distFromTargetvalue / goodDiffConvexTarget if diff > goodDiffConvexTarget: # ============================================================================= # cv2.putText(self.image, "jaggedly {}".format(1), (20, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (250, 250, 250), 1) # cv2.putText(self.image, "diff {}".format( diff), (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (250, 250, 250), 1) # # cv2.imshow('convex', self.image) # cv2.waitKey() # cv2.destroyAllWindows() # ============================================================================= return 1 else: # ============================================================================= # cv2.putText(self.image, "Jaggedly {}".format(1 - distFromTarget), (20, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (250, 250, 250), 1) # cv2.putText(self.image, "diff {}".format( diff), (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (250, 250, 250), 1) # # cv2.imshow('convex', self.image) # cv2.waitKey() # cv2.destroyAllWindows() # ============================================================================= return 1 - distFromTarget