def get_landmarks(argv): line = argv[1] left = argv[2] top = argv[3] right = argv[2]+argv[4] bottom = argv[3]+argv[5] rect = dlib.rectangle(int(left),int(top),int(right), int(bottom)) predictor_path = 'shape_predictor_68_face_landmarks.dat' # http://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(predictor_path) lmarks = [] bboxes = [] #for i,line in enumerate(line_arr): # print('%d/%d'%(i,len(line_arr))) img = io.imread(line) dets = detector(img, 0) if len(dets) == 0: rect = dlib.rectangle(0,0,img.shape[0], img.shape[1]) else: rect = dets[0] shape = predictor(img, rect) xy = _shape_to_np(shape) np.savetxt('landmarks.txt', xy) lmarks.append(xy) bboxes.append(rect) lmarks = np.vstack(lmarks) bboxes = np.asarray(bboxes) print lmarks return lmarks
def get_points(image, bounds = None): bounds = get_bounds(image) if bounds is None else bounds dif_x, dif_y = 0, 0 scale = 10 offsets = [[-scale, -scale], [-scale, scale], [scale, scale], [scale, -scale]] points = [] for i in range(0, 68): points.append([0.0, 0.0]) origin_rect = dlib.rectangle(bounds[0], bounds[1], bounds[2], bounds[3]) origin_shape = predictor(image, origin_rect) for o in offsets: rect = dlib.rectangle(bounds[0] + o[0], bounds[1] + o[1], bounds[2] + o[0], bounds[3] + o[1]) shape = predictor(image, rect) for i in range(0, shape.num_parts): p = shape.part(i) points[i][0] += float(p.x) / 4 points[i][1] += float(p.y) / 4 p2 = [] for i in range(0, 68): p = points[i] dif_x += float(p[0] - origin_shape.part(i).x) / float(68) dif_y += float(p[1] - origin_shape.part(i).y) / float(68) p2.append((int(p[0]), int(p[1]))) print(dif_x, dif_y) return p2, bounds
def eye_region(shape): pointsl = np.ndarray((6,1,2), dtype=np.float32) pointsr = np.ndarray((6,1,2), dtype=np.float32) for i in range(36,42): pointsl[i-37,0] = (shape.part(i).x, shape.part(i).y) for i in range(42,48): pointsr[i-43,0] = (shape.part(i).x, shape.part(i).y) rectl = cv2.boundingRect(pointsl) rectr = cv2.boundingRect(pointsr) xl = rectl[0] + rectl[2]/2.0 yl = rectl[1] + rectl[3]/2.0 xr = rectr[0] + rectr[2]/2.0 yr = rectr[1] + rectl[3]/2.0 w = (xr - xl) * 3 / 10 h = (xr - xl) / 10 rectl = dlib.rectangle(int(xl-w),int(yl-h), int(xl+w),int(yl+h)) rectr = dlib.rectangle(int(xr-w),int(yr-h), int(xr+w),int(yr+h)) return rectl,rectr
def CT_run(self, img, img_old, img_last): """ CT_run: ------- When called, it will // CT.update // the Correlation Tracker once. if self.UPDATED, it will call // CT.start_track //. """ if self.UPDATED: if self.UPDATE_ASSIGN: self.CT.start_track(img_old, dlib.rectangle(*self.CT_box_update)) else: self.CT.start_track(img_last, dlib.rectangle(*self.CT_box_update)) self._CT_turn_new_to_old() # turn new to old self.CT.update(img) # get current position and update // CT_box // rect = self.CT.get_position() self.CT_box = [int(rect.left()), int(rect.top()), \ int(rect.right()), int(rect.bottom())] if self.UPDATED: pass # _CT_turn_new_to_old() self.UPDATED = False return
def returnKey(self, event): #self._sliding_window((50,100),0) #print self.img_num/float(20*3600) #save rectangle position self.rectangle_frame_pairs[self.img_num] = self._get_coord_rectangle() #print image_id #print self._get_coord_rectangle() #print self.rectangle_frame_pairs # set the current position of the rectangle for tracking if (self.flag == 0): #print coords_rectangle coords_relative = self._get_coord_rectangle() #proveri uste ednas koordhinatite self.tracker.start_track(self.images_raw[self.img_num], dlib.rectangle(coords_relative[0],coords_relative[1],coords_relative[2],coords_relative[3])) #self.tracker.start_track(self.images_raw[0], dlib.rectangle(170, 200, 240, 240)) self.flag = 1 self.img_num = self.img_num + 1 if self.img_num >= len(self.images): self.img_num = 0 self._change_image() else: #self.new_img = ImageTk.PhotoImage(Image.open(os.path.join(self.video_folder, "{0}.jpg".format(self.img_num)))) #self.img = io.imread(os.path.join(self.video_folder, "{0}.jpg".format(self.img_num))) #self.im_from_array = Image.fromarray(self.img) #self.new_img = ImageTk.PhotoImage(image = self.im_from_array) #self.canvas.itemconfig(self.img_id, image = self.new_img) # you can refactor this code coords_relative = self._get_coord_rectangle() #update filter #self.prev_tracker = tracker self.tracker.update(self.images_raw[self.img_num], dlib.rectangle(coords_relative[0],coords_relative[1],coords_relative[2],coords_relative[3])) # update rectangle (overlay it) rel_position = self.tracker.get_position() curr_position = self._get_coord_rectangle() #print (rel_position.left()) # refactor this code as well self.canvas.move(self.polygon_id, -curr_position[0]+rel_position.left(), -curr_position[1]+rel_position.top()) self.img_num = self.img_num + 1 if self.img_num >= len(self.images): self.img_num = 0 self._change_image()
def serv(self, inp_q, res_q, continue_evt, reload_val, mdl_update_pth): while True: try: continue_evt.wait() if reload_val.value: self.classify = svm_load_model(mdl_update_pth) reload_val.value = 0 cli_name, data_buffer, mode = inp_q.get(timeout=2) bbs_filt, labels, probs = [], [], [] if mode: # 1 is strong mode bbs_filt_np, feats = data_buffer bbs_filt_np = bbs_filt_np.astype(np.int64) for bbs_np in bbs_filt_np: bbs_filt.append(dlib.rectangle(bbs_np[0], bbs_np[1], bbs_np[2], bbs_np[3])) else: nparr = np.fromstring(data_buffer, dtype=np.uint8) img_np = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR) faces, bbs_filt = self.faceDetAlign(img_np) feats = self.featureExtract(faces) if len(feats): labels, probs = self.runSVM(feats.tolist(), self.classify) res_q.put((cli_name, (bbs_filt, labels, probs, feats))) inp_q.task_done() except (Queue.Empty, KeyboardInterrupt): # print 'timeout in face' pass
def start_tracking(): global updated global i, img_count global updtbox, oldbox, crtbox while i <= img_count: # get a new frame img = cv2.imread(imdb_path+'/%04d.jpg'%i) # update the tracker if updated: # tracker.start_track() tracker.start_track(img, dlib.rectangle(*updtbox)) oldbox = updtbox updated = False # post a new frame trd_post = Thread(target=postIMG) trd_post.start() else: # tracker.update() tracker.update(img) rect = tracker.get_position() pt1 = [int(rect.left()), int(rect.top())] pt2 = [int(rect.right()),int(rect.bottom())] crtbox = pt1 + pt2 f.write(str(crtbox)+'\n') if i%10 == 0: print 'frame',i,'returns',crtbox if showimg: showIMG(img, crtbox, 2000) # next frame i +=1
def get_landmarks(self, im): # im 就是检测到的人脸 # det = self.detector(im) # if det: # return np.matrix([[p.x, p.y] for p in self.predictor(im, det[0]).parts()]) return np.matrix([[p.x, p.y] for p in self.predictor(im, dlib.rectangle(0, 0, im.shape[0], im.shape[1])).parts()])
def getNormalizedLandmarks(img, predictor, d, fronter = None, win2 = None): shape = predictor(img, d) landmarks = list(map(lambda p: (p.x, p.y), shape.parts())) npLandmarks = np.float32(landmarks) if NORM_MODE == 0: npLandmarkIndices = np.array(landmarkIndices) H = cv2.getAffineTransform(npLandmarks[npLandmarkIndices], MINMAX_TEMPLATE[npLandmarkIndices]) normLM = cv2.transform(np.asarray([npLandmarks]),H)[0,:,:] return normLM,shape else: assert fronter is not None thumbnail = fronter.frontalizeImage(img,d,npLandmarks) #thumbnail = imgEnhance(thumbnail) cut = thumbnail.shape[0]/5 thumbnail = thumbnail[cut+5:thumbnail.shape[0]-cut-5,cut+10:thumbnail.shape[1]-cut-10,:].copy() newShape = predictor(thumbnail, dlib.rectangle(0,0,thumbnail.shape[0],thumbnail.shape[1])) if win2 is not None: win2.clear_overlay() win2.set_image(thumbnail) win2.add_overlay(newShape) #dlib.hit_enter_to_continue() landmarks = list(map(lambda p: (float(p.x)/thumbnail.shape[0], float(p.y)/thumbnail.shape[1]), newShape.parts())) npLandmarks = np.float32(landmarks) normLM = npLandmarks return normLM,shape,thumbnail
def fit(self, image): """Summary. Parameters ---------- image : TYPE Description Returns ------- name : TYPE Description """ bbox = self.localize(image) if bbox is not None: face = rectangle( bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]) shape = self.model(image, face) xs = [p.x for p in shape.parts()] ys = [p.y for p in shape.parts()] x = np.min(xs) y = np.min(ys) w = np.max(xs) - np.min(xs) h = np.max(ys) - np.min(ys) return { 'pts': zip(xs, ys), 'xs': xs, 'ys': ys, 'pts-bbox': [x, y, w, h], 'face-bbox': bbox, 'nose': (shape.parts()[30].x, shape.parts()[30].y) } else: return None
def get_landmarks(line_arr): predictor_path = 'shape_predictor_68_face_landmarks.dat' # http://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(predictor_path) lmarks = [] bboxes = [] for i,line in enumerate(line_arr): print('%d/%d'%(i,len(line_arr))) img = io.imread(line) dets = detector(img, 0) if len(dets) == 0: rect = dlib.rectangle(0,0,img.shape[0], img.shape[1]) else: rect = dets[0] shape = predictor(img, rect) xy = _shape_to_np(shape) lmarks.append(xy) bboxes.append(rect) lmarks = np.vstack(lmarks) bboxes = np.asarray(bboxes) return lmarks,bboxes
def get_emotions(img): #face = detector(img, 1)[0] emotions = {} emotions[0] = [0] emotions[1] = [0] emotions[2] = [0] emotions[3] = [0] emotions[4] = [0] faces = get_faces(img) if faces is None: return emotions for face in faces: face_area = rotate_image.cut(image, face) face_area = rotate_image.rotate_face(face_area) dim = face_area.shape[0] face2 = dlib.rectangle(0, 0, dim, dim) face_vector = generate_feature_vector(face_area, face2) test_x = np.array([test_feature_vector]) test_y = pipeline.predict(test_x) #array.append(test_y) emotions[test_y] = 1 return emotions
def get_position(self): track_rect = self.track_window roi_x1, roi_y1 = track_rect[0], track_rect[1] roi_x2 = track_rect[0] + track_rect[2] roi_y2 = track_rect[1] + track_rect[3] ret = dlib.rectangle(roi_x1, roi_y1, roi_x2, roi_y2) return ret
def run(self): while True: ret, self.frame = self.cam.read() vis = self.frame.copy() hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.))) if self.selection: x0, y0, x1, y1 = self.selection self.tracker.start_track(self.frame, dlib.rectangle(int(x0),int(y0),int(x1),int(y1))) if self.tracking_state == 1: self.selection = None self.tracker.update(self.frame) track_box = self.tracker.get_position() pt1 = (track_box.left(), track_box.top()) pt2 = (track_box.right(), track_box.bottom()) cv2.rectangle(vis, pt1, pt2, (0, 255, 0), 2) cv2.imshow('camshift', vis) ch = 0xFF & cv2.waitKey(5) if ch == 27: break if ch == ord('b'): self.show_backproj = not self.show_backproj cv2.destroyAllWindows()
def detect_faces(self, image): """ Detect faces in rgb image """ self.detected_faces = None detected_faces = detect_face(image, **self.kwargs) self.detected_faces = [dlib.rectangle(int(face[0]), int(face[1]), int(face[2]), int(face[3])) for face in detected_faces]
def download(person, url, bb): imgName = os.path.basename(url) rawPersonPath = os.path.join(args.raw, person) rawImgPath = os.path.join(rawPersonPath, imgName) alignedPersonPath = os.path.join(args.aligned, person) alignedImgPath = os.path.join(alignedPersonPath, hashlib.md5(imgName).hexdigest()+".png") mkdirP(rawPersonPath) mkdirP(alignedPersonPath) if not os.path.isfile(rawImgPath): urlF = urllib2.urlopen(url) with open(rawImgPath, 'wb') as f: f.write(urlF.read()) if not os.path.isfile(alignedImgPath): bgr = cv2.imread(rawImgPath) if bgr is None: return rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB) dlibBB = dlib.rectangle(*bb) outRgb = align.align(96, rgb, bb=dlibBB, landmarkIndices=landmarkIndices) if outRgb is not None: outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR) cv2.imwrite(alignedImgPath, outBgr)
def getRep(self, img, rgb=True): if len(img.shape) < 3: # if grayscale, convert to 3 channel image rgbImg = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB) else: # otherwise convert to rgb if not rgb: rgbImg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) else: rgbImg = img if rgbImg is None: print 'No image' return None # get bounding box of the face in the image bb = self.align.getLargestFaceBoundingBox(rgbImg) if bb is None: # print 'WARNING: Could not find largest bounding box, using ROI as is' bb = rectangle(0, 0, rgbImg.shape[1], rgbImg.shape[0]) # return None # align the face within the bounding box alignedFace = self.align.align(self.imgDim, rgbImg, bb, landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE) if alignedFace is None: print 'No aligned face' return None rep = self.net.forward(alignedFace) return rep
def set_predetected(self, width, height): """ Set a dlib rectangle for predetected faces """ # Predetected_face is used for sort tool. # Landmarks should not be extracted again from predetected faces, # because face data is lost, resulting in a large variance # against extract from original image self.detected_faces = [dlib.rectangle(0, 0, width, height)]
def rotate_image(img): ## Get size of resized image dim = img.shape[0] ## Create rectangle size of resized image (to place blank face) face2 = dlib.rectangle(0, 0, dim, dim) ## Input image points img_face_shape = predictor(img, face2) img_points = np.matrix([[p.x, p.y] for p in img_face_shape.parts()]) ## Empty image to base points empty_img = np.empty([dim,dim], dtype=np.uint8) empty_img.fill(255) empty_face_shape = predictor(empty_img,face2) base_points = np.matrix([[p.x, p.y] for p in empty_face_shape.parts()]) ## Perform transformation M = transformation_from_points(base_points[ALIGN_POINTS], img_points[ALIGN_POINTS]) warped_img = warp_im(img, M, img.shape) return warped_img
def lip_region(shape): points = np.ndarray((12,1,2), dtype=np.float32) index = 0 ''' for i in range(65,68): points[index,0] = (shape.part(i).x, shape.part(i).y) index += 1 for i in range(55,60): points[index,0] = (shape.part(i).x, shape.part(i).y) index += 1 ''' for i in range(55,65): points[index,0] = (shape.part(i).x, shape.part(i).y) index += 1 rect = cv2.boundingRect(points) xl = rect[0] yl = rect[1] xr = rect[0]+rect[2] yr = rect[1]+rect[3] ''' rect = dlib.rectangle(int(xl-w),int(yl-h), int(xl+w),int(yl+h)) ''' rect = dlib.rectangle(int(xl),int(yl),int(xr),int(yr)) return rect
def check_if_new(det, objs): for obj_id in objs: ob = objs[obj_id] pos_before = ob["tracker"].get_position() rect_before = dlib.rectangle(long(pos_before.left()), long(pos_before.top()), long(pos_before.right()), long(pos_before.bottom())) rect_det = dlib.rectangle(long(det.left()), long(det.top()), long(det.right()), long(det.bottom())) area_before = rect_before.area() print "!!! {0} {1}".format(rect_det, rect_before) intersection = rect_before.intersect(det) inters_area = intersection.area() print "area before {0} intersection {1}".format(area_before, inters_area) inters_ratio = float(inters_area) / float(area_before) print "intersection ratio: {0}".format(inters_ratio) if inters_ratio > INTERSECTION_EQ_RATIO: ob["confirmed"] = True return False return True
def create_tracker(frame, roi): # tracker = camshiftTracker() # using mean shift for now tracker = meanshiftTracker() # (roi_x1, roi_y1, roi_x2, roi_y2) = roi tracker.start_track(frame, dlib.rectangle(roi_x1, roi_y1, roi_x2, roi_y2)) return tracker
def _css_to_rect(css): """ Convert a tuple in (top, right, bottom, left) order to a dlib `rect` object :param css: plain tuple representation of the rect in (top, right, bottom, left) order :return: a dlib `rect` object """ return dlib.rectangle(css[3], css[0], css[1], css[2])
def start_track(self, frame, bbox): self.bbox = bbox self.init_region = frame.raw_img[bbox.left: bbox.right, bbox.top: bbox.bottom] self.label = bbox.label super(Tracker, self).start_track( frame.raw_img, dlib.rectangle(bbox.left, bbox.top, bbox.right, bbox.bottom) )
def test_rect2bb(self): aligner = GFAlign(None) rect = dlib.rectangle(left=2, right=10, top=4, bottom=14) (x, y, w, h) = aligner.rect2BoundingBox(rect) self.assertEqual(x, 2) self.assertEqual(y, 4) self.assertEqual(w, 8) self.assertEqual(h, 10)
def align_face_dlib(image, face_box, landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE): align = AlignDlib('../models/dlib/shape_predictor_68_face_landmarks.dat') assert isinstance(face_box, tuple) face_rect = dlib.rectangle(*face_box) landmarks = align.findLandmarks(image, face_rect) alignedFace = align.align(EXPECT_SIZE, image, face_rect, landmarks=landmarks, landmarkIndices=landmarkIndices) return alignedFace, landmarks
def create_tracker(frame, roi, use_dlib=False): if use_dlib: tracker = dlib.correlation_tracker() else: tracker = meanshiftTracker() (roi_x1, roi_y1, roi_x2, roi_y2) = roi LOG.debug('create tracker received: {}'.format(roi)) tracker.start_track(frame, dlib.rectangle(roi_x1, roi_y1, roi_x2, roi_y2)) return tracker
def detect_faces(self, images): """ Detect faces in images """ self.detected_faces = None for current_image in images: detected_faces = detect_face(current_image, **self.kwargs) self.detected_faces = [dlib.rectangle(int(face[0]), int(face[1]), int(face[2]), int(face[3])) for face in detected_faces] if self.detected_faces: break
def represent_image(self, item): face_path = item[1] face = io.imread(FACE_SAVE_DIR + '/' + face_path) height = face.shape[0] width = face.shape[1] face_box = dlib.rectangle(left=0, top=0, right=width - 1, bottom=height - 1) embedding = self.represent(face, face_box).tolist() owner_id = item[0] return owner_id, embedding
def main(): symbol = lightened_moon_feature(num_classes=40, use_fuse=True) devs = mx.cpu() if args.gpus is None else [mx.gpu(int(i)) for i in args.gpus.split(',')] _, arg_params, aux_params = mx.model.load_checkpoint(args.model_load_prefix, args.model_load_epoch) detector = dlib.get_frontal_face_detector() face_cascade = cv2.CascadeClassifier(args.opencv) # read img and drat face rect img = cv2.imread(args.img) faces = detector(img, 1) gray = np.zeros(img.shape[0:2]) if len(faces) == 0: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) opencv_faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in opencv_faces: faces.append(dlib.rectangle(int(x), int(y), int(x+w), int(y+h))) max_face = faces[0] if len(faces) > 0: max_face = max(faces, key=lambda rect: rect.width() * rect.height()) for f in faces: if f == max_face: cv2.rectangle(img, (f.left(), f.top()), (f.right(), f.bottom()), (0,0,255), 2) else: cv2.rectangle(img, (f.left(), f.top()), (f.right(), f.bottom()), (255,0,0), 2) cv2.imwrite(args.img.replace('jpg', 'png'), img) # crop face area gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) pad = [0.25, 0.25, 0.25, 0.25] if args.pad is None else args.pad left = int(max(0, max_face.left() - max_face.width()*float(pad[0]))) top = int(max(0, max_face.top() - max_face.height()*float(pad[1]))) right = int(min(gray.shape[1], max_face.right() + max_face.width()*float(pad[2]))) bottom = int(min(gray.shape[0], max_face.bottom()+max_face.height()*float(pad[3]))) gray = gray[left:right, top:bottom] gray = cv2.resize(gray, (args.size, args.size))/255.0 img = np.expand_dims(np.expand_dims(gray, axis=0), axis=0) # get pred arg_params['data'] = mx.nd.array(img, devs) exector = symbol.bind(devs, arg_params ,args_grad=None, grad_req="null", aux_states=aux_params) exector.forward(is_train=False) exector.outputs[0].wait_to_read() output = exector.outputs[0].asnumpy() text = ["5_o_Clock_Shadow","Arched_Eyebrows","Attractive","Bags_Under_Eyes","Bald", "Bangs","Big_Lips","Big_Nose", "Black_Hair","Blond_Hair","Blurry","Brown_Hair","Bushy_Eyebrows","Chubby","Double_Chin","Eyeglasses","Goatee", "Gray_Hair", "Heavy_Makeup","High_Cheekbones","Male","Mouth_Slightly_Open","Mustache","Narrow_Eyes","No_Beard", "Oval_Face","Pale_Skin","Pointy_Nose","Receding_Hairline","Rosy_Cheeks","Sideburns","Smiling","Straight_Hair", "Wavy_Hair","Wearing_Earrings","Wearing_Hat","Wearing_Lipstick","Wearing_Necklace","Wearing_Necktie","Young"] pred = np.ones(40) print("attribution is:") for i in range(40): print text[i].rjust(20)+" : \t", if output[0][i] < 0: pred[i] = -1 print "No" else: pred[i] = 1 print "Yes"
def detectAndTrackLargestFace(): #Open the first webcame device capture = cv2.VideoCapture(0) #Create two opencv named windows cv2.namedWindow("base-image", cv2.WINDOW_AUTOSIZE) cv2.namedWindow("result-image", cv2.WINDOW_AUTOSIZE) #Position the windows next to eachother cv2.moveWindow("base-image",0,100) cv2.moveWindow("result-image",400,100) #Start qthe window thread for the two windows we are using #cv2.startWindowThread() #Create the tracker we will use tracker = dlib.correlation_tracker() #The variable we use to keep track of the fact whether we are #currently using the dlib tracker trackingFace = 0 #The color of the rectangle we draw around the face rectangleColor = (0,165,255) try: while True: #Retrieve the latest image from the webcam rc,fullSizeBaseImage = capture.read() #Resize the image to 320x240 baseImage = cv2.resize( fullSizeBaseImage, ( 320, 240)) #Check if a key was pressed and if it was Q, then destroy all #opencv windows and exit the application pressedKey = cv2.waitKey(2) if pressedKey == ord('Q'): cv2.destroyAllWindows() exit(0) #Result image is the image we will show the user, which is a #combination of the original image from the webcam and the #overlayed rectangle for the largest face resultImage = baseImage.copy() #If we are not tracking a face, then try to detect one if not trackingFace: #For the face detection, we need to make use of a gray #colored image so we will convert the baseImage to a #gray-based image gray = cv2.cvtColor(baseImage, cv2.COLOR_BGR2GRAY) #Now use the haar cascade detector to find all faces #in the image faces = faceCascade.detectMultiScale(gray, 1.3, 5) #In the console we can show that only now we are #using the detector for a face print("Using the cascade detector to detect face") #For now, we are only interested in the 'largest' #face, and we determine this based on the largest #area of the found rectangle. First initialize the #required variables to 0 maxArea = 0 x = 0 y = 0 w = 0 h = 0 #Loop over all faces and check if the area for this #face is the largest so far #We need to convert it to int here because of the #requirement of the dlib tracker. If we omit the cast to #int here, you will get cast errors since the detector #returns numpy.int32 and the tracker requires an int for (_x,_y,_w,_h) in faces: if _w*_h > maxArea: x = int(_x) y = int(_y) w = int(_w) h = int(_h) maxArea = w*h x1=x-20 y1=y-30 x2=x+w+20 y2=y+h+30 if len(faces)>0: print("Face is being captured") roi = resultImage.copy() roi = roi[y1:y2,x1:x2] #roi=cv2.resize( roi,( OUTPUT_SIZE_WIDTH, OUTPUT_SIZE_HEIGHT)) cv2.imwrite("Face.jpg",roi) print("Face is copied") #time.sleep(1) try: age,gender,facialHair_beard,facialHair_moustache,facialHair_sideburns,makeup_eyeMakeup,makeup_lipMakeup=CallMicrosoftAPI.call_mico_api("C:/Users/mg75/Desktop/target marketing using facial detection/Face.jpg") print(age,gender,facialHair_beard,facialHair_moustache) #select_num = randint(1,3) if gender=="male" and (age>=20 and age<30) and (facialHair_beard>0.1 and facialHair_beard<=0.7): VideoDisplay.videoDisplay("C:/Users/mg75/Desktop/target marketing using facial detection/videos/Face_latest.mp4") elif gender=="male" and age>=35: VideoDisplay.videoDisplay("C:/Users/mg75/Desktop/target marketing using facial detection/videos/Beer15.mp4") elif gender=="female" and (age>=20 and age<30) and (makeup_eyeMakeup==True or makeup_lipMakeup==True): VideoDisplay.videoDisplay("C:/Users/mg75/Desktop/target marketing using facial detection/Makeup15.mp4") elif gender=="female" and age>=30: VideoDisplay.videoDisplay("C:/Users/mg75/Desktop/target marketing using facial detection/videos/Mother15.mp4") else: VideoDisplay.videoDisplay("C:/Users/mg75/Desktop/target marketing using facial detection/videos/Flowers15.mp4") except: print("Face is not detected properly") #If one or more faces are found, initialize the tracker #on the largest face in the picture if maxArea > 0 : #Initialize the tracker tracker.start_track(baseImage, dlib.rectangle( x-10, y-20, x+w+10, y+h+20)) #Set the indicator variable such that we know the #tracker is tracking a region in the image trackingFace = 1 #Check if the tracker is actively tracking a region in the image if trackingFace: #Update the tracker and request information about the #quality of the tracking update trackingQuality = tracker.update( baseImage ) #If the tracking quality is good enough, determine the #updated position of the tracked region and draw the #rectangle if trackingQuality >= 8.75: tracked_position = tracker.get_position() t_x = int(tracked_position.left()) t_y = int(tracked_position.top()) t_w = int(tracked_position.width()) t_h = int(tracked_position.height()) cv2.rectangle(resultImage, (t_x, t_y), (t_x + t_w , t_y + t_h), rectangleColor ,2) else: #If the quality of the tracking update is not #sufficient (e.g. the tracked region moved out of the #screen) we stop the tracking of the face and in the #next loop we will find the largest face in the image #again trackingFace = 0 #Since we want to show something larger on the screen than the #original 320x240, we resize the image again # #Note that it would also be possible to keep the large version #of the baseimage and make the result image a copy of this large #base image and use the scaling factor to draw the rectangle #at the right coordinates. largeResult = cv2.resize(resultImage, (OUTPUT_SIZE_WIDTH,OUTPUT_SIZE_HEIGHT)) #Finally, we want to show the images on the screen #cv2.rectangle(baseImage, (t_x, t_y), # (t_x + t_w , t_y + t_h), # rectangleColor ,2) cv2.imshow("base-image", baseImage) cv2.imshow("result-image", largeResult) #save the file #cv2.imwrite("test.png",fullSizeBaseImage) print(trackingFace) #To ensure we can also deal with the user pressing Ctrl-C in the console #we have to check for the KeyboardInterrupt exception and destroy #all opencv windows and exit the application except KeyboardInterrupt as e: cv2.destroyAllWindows() exit(0)
rect: an instance of dlib.rectangle, the face region Returns: the rect in the original image """ <<<<<<< HEAD left = int(rect.left() / oriSize[0] * imShape[1]) right = int(rect.right() / oriSize[0] * imShape[1]) top = int(rect.top() / oriSize[1] * imShape[0]) bottom = int(rect.bottom() / oriSize[1] * imShape[0]) ======= left = int(round(rect.left() / oriSize[0] * imShape[1])) right = int(round(rect.right() / oriSize[0] * imShape[1])) top = int(round(rect.top() / oriSize[1] * imShape[0])) bottom = int(round(rect.bottom() / oriSize[1] * imShape[0])) >>>>>>> 8d65bf9dd02952114a8c4a1e39aed0ab305d258a return dlib.rectangle(left, top, right, bottom) class Detector: """ Detect and calculate ppg signal roiRatio: a positive number, the roi gets bigger as it increases smoothRatio: a real number between 0 and 1, the landmarks get stabler as it increases """ detectSize = 500 clipSize = 540 roiRatio = 5 rectSmoothRatio = 0.98 rectDistThres = 4 markSmoothRatio = 0.95 <<<<<<< HEAD markDistThres = 0.2
visualise=True) hog_features.append(features) hog_images.append(hog_image) elif GET_HOG_FEATURES: features, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1), visualise=True) hog_features.append(features) hog_images.append(hog_image) if GET_LANDMARKS: scipy.misc.imsave('temp.jpg', image) image2 = cv2.imread('temp.jpg') face_rects = [ dlib.rectangle(left=1, top=1, right=47, bottom=47) ] face_landmarks = get_landmarks(image2, face_rects) landmarks.append(face_landmarks) labels_list.append( get_new_label(labels[i], one_hot_encoding=ONE_HOT_ENCODING)) nb_images_per_label[get_new_label(labels[i])] += 1 except Exception as e: print("error in image: " + str(i) + " - " + str(e)) np.save(OUTPUT_FOLDER_NAME + '/' + category + '/images.npy', images) if ONE_HOT_ENCODING: np.save(OUTPUT_FOLDER_NAME + '/' + category + '/labels.npy', labels_list) else:
h, w = image.shape[:2] except: break k += 1 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale( gray, scaleFactor=1.05, minNeighbors=5, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE ) for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) dlib_rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h)) print dlib_rect detected_landmarks = predictor(image, dlib_rect).parts() landmarks = np.matrix([[p.x, p.y] for p in detected_landmarks]) for idx, point in enumerate(landmarks): pos = (point[0, 0], point[0, 1]) cv2.putText(image, str(idx), pos, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.4, color=(0, 0, 255)) cv2.circle(image, pos, 3, color=(0, 255, 255))
detector_idxs[i], boxes[i], confidences[i])) # Finally, note that you don't have to use the XML based input to # train_simple_object_detector(). If you have already loaded your training # images and bounding boxes for the objects then you can call it as shown # below. # You just need to put your images into a list. images = [ io.imread(faces_folder + '/2008_002506.jpg'), io.imread(faces_folder + '/2009_004587.jpg') ] # Then for each image you make a list of rectangles which give the pixel # locations of the edges of the boxes. boxes_img1 = ([ dlib.rectangle(left=329, top=78, right=437, bottom=186), dlib.rectangle(left=224, top=95, right=314, bottom=185), dlib.rectangle(left=125, top=65, right=214, bottom=155) ]) boxes_img2 = ([ dlib.rectangle(left=154, top=46, right=228, bottom=121), dlib.rectangle(left=266, top=280, right=328, bottom=342) ]) # And then you aggregate those lists of boxes into one big list and then call # train_simple_object_detector(). boxes = [boxes_img1, boxes_img2] detector2 = dlib.train_simple_object_detector(images, boxes, options) # We could save this detector to disk by uncommenting the following. #detector2.save('detector2.svm')
def run(): # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-p", "--prototxt", required=False, help="path to Caffe 'deploy' prototxt file") ap.add_argument("-m", "--model", required=True, help="path to Caffe pre-trained model") ap.add_argument("-i", "--input", type=str, help="path to optional input video file") ap.add_argument("-o", "--output", type=str, help="path to optional output video file") # confidence default 0.4 ap.add_argument("-c", "--confidence", type=float, default=0.4, help="minimum probability to filter weak detections") ap.add_argument("-s", "--skip-frames", type=int, default=30, help="# of skip frames between detections") args = vars(ap.parse_args()) # initialize the list of class labels MobileNet SSD was trained to # detect CLASSES = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] # load our serialized model from disk net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) # if a video path was not supplied, grab a reference to the ip camera if not args.get("input", False): print("[INFO] Starting the live stream..") #vs = cv2.VideoCapture(cv2.CAP_V4L2) vs = VideoStream(cv2.CAP_V4L2).start() time.sleep(2.0) # otherwise, grab a reference to the video file else: print("[INFO] Starting the video..") vs = cv2.VideoCapture(args["input"]) # initialize the video writer (we'll instantiate later if need be) writer = None # initialize the frame dimensions (we'll set them as soon as we read # the first frame from the video) W = None H = None # instantiate our centroid tracker, then initialize a list to store # each of our dlib correlation trackers, followed by a dictionary to # map each unique object ID to a TrackableObject ct = CentroidTracker(maxDisappeared=40, maxDistance=50) trackers = [] trackableObjects = {} # initialize the total number of frames processed thus far, along # with the total number of objects that have moved either up or down totalFrames = 0 totalDown = 0 totalUp = 0 x = [] empty = [] empty1 = [] # start the frames per second throughput estimator fps = FPS().start() if config.Thread: vs = thread.ThreadingClass(cv2.CAP_V4L2) # loop over frames from the video stream while True: # grab the next frame and handle if we are reading from either # VideoCapture or VideoStream frame = vs.read() frame = frame[1] if args.get("input", False) else frame # if we are viewing a video and we did not grab a frame then we # have reached the end of the video if args["input"] is not None and frame is None: break # resize the frame to have a maximum width of 500 pixels (the # less data we have, the faster we can process it), then convert # the frame from BGR to RGB for dlib frame = imutils.resize(frame, width=400) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # if the frame dimensions are empty, set them if W is None or H is None: (H, W) = frame.shape[:2] # if we are supposed to be writing a video to disk, initialize # the writer if args["output"] is not None and writer is None: fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = cv2.VideoWriter(args["output"], fourcc, 30, (W, H), True) # initialize the current status along with our list of bounding # box rectangles returned by either (1) our object detector or # (2) the correlation trackers status = "Waiting" rects = [] # check to see if we should run a more computationally expensive # object detection method to aid our tracker if totalFrames % args["skip_frames"] == 0: # set the status and initialize our new set of object trackers status = "Detecting" trackers = [] # convert the frame to a blob and pass the blob through the # network and obtain the detections blob = cv2.dnn.blobFromImage(frame, 0.007843, (W, H), 127.5) net.setInput(blob) detections = net.forward() # loop over the detections for i in np.arange(0, detections.shape[2]): # extract the confidence (i.e., probability) associated # with the prediction confidence = detections[0, 0, i, 2] # filter out weak detections by requiring a minimum # confidence if confidence > args["confidence"]: # extract the index of the class label from the # detections list idx = int(detections[0, 0, i, 1]) # if the class label is not a person, ignore it if CLASSES[idx] != "person": continue # compute the (x, y)-coordinates of the bounding box # for the object box = detections[0, 0, i, 3:7] * np.array([W, H, W, H]) (startX, startY, endX, endY) = box.astype("int") # construct a dlib rectangle object from the bounding # box coordinates and then start the dlib correlation # tracker tracker = dlib.correlation_tracker() rect = dlib.rectangle(startX, startY, endX, endY) tracker.start_track(rgb, rect) # add the tracker to our list of trackers so we can # utilize it during skip frames trackers.append(tracker) # otherwise, we should utilize our object *trackers* rather than # object *detectors* to obtain a higher frame processing throughput else: # loop over the trackers for tracker in trackers: # set the status of our system to be 'tracking' rather # than 'waiting' or 'detecting' status = "Tracking" # update the tracker and grab the updated position tracker.update(rgb) pos = tracker.get_position() # unpack the position object startX = int(pos.left()) startY = int(pos.top()) endX = int(pos.right()) endY = int(pos.bottom()) # add the bounding box coordinates to the rectangles list rects.append((startX, startY, endX, endY)) # draw a horizontal line in the center of the frame -- once an # object crosses this line we will determine whether they were # moving 'up' or 'down' cv2.line(frame, (0, H // 2), (W, H // 2), (153, 204, 102), 3) cv2.putText(frame, "-Prediction border - Entrance-", (10, H - ((i * 20) + 200)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1) # use the centroid tracker to associate the (1) old object # centroids with (2) the newly computed object centroids objects = ct.update(rects) # loop over the tracked objects for (objectID, centroid) in objects.items(): # check to see if a trackable object exists for the current # object ID to = trackableObjects.get(objectID, None) # if there is no existing trackable object, create one if to is None: to = TrackableObject(objectID, centroid) # otherwise, there is a trackable object so we can utilize it # to determine direction else: # the difference between the y-coordinate of the *current* # centroid and the mean of *previous* centroids will tell # us in which direction the object is moving (negative for # 'up' and positive for 'down') y = [c[1] for c in to.centroids] direction = centroid[1] - np.mean(y) to.centroids.append(centroid) # check to see if the object has been counted or not if not to.counted: # if the direction is negative (indicating the object # is moving up) AND the centroid is above the center # line, count the object if direction < 0 and centroid[1] < H // 2: totalUp += 1 empty.append(totalUp) to.counted = True # if the direction is positive (indicating the object # is moving down) AND the centroid is below the # center line, count the object elif direction > 0 and centroid[1] > H // 2: totalDown += 1 empty1.append(totalDown) #print(empty1[-1]) x = [] # compute the sum of total people inside x.append(len(empty1) - len(empty)) #print("Total people inside:", x) # if the people limit exceeds over threshold, send an email alert if sum(x) >= 2: cv2.putText(frame, "-ALERT: People limit exceeded-", (10, frame.shape[0] - 80), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 2) to.counted = True # store the trackable object in our dictionary trackableObjects[objectID] = to # draw both the ID of the object and the centroid of the # object on the output frame text = "ID {}".format(objectID) cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cv2.circle(frame, (centroid[0], centroid[1]), 4, (255, 255, 255), -1) # construct a tuple of information we will be displaying on the # info = [ # ("exit", totalUp), # ("Enter", totalDown), # ("Status", status), # ] info2 = [ ("Total People: ", x), ] # Display the output # for (i, (k, v)) in enumerate(info): # text = "{}: {}".format(k, v) # cv2.putText(frame, text, (10, H - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2) for (i, (k, v)) in enumerate(info2): text = "{}: {}".format(k, v) # cv2.putText(frame, text, (265, H - ((i * 20) + 60)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 0), 2) #Red cv2.putText(frame, text, (15, H - ((i * 20) + 50)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 0), 2) #Red # Initiate a simple log to save data at end of the day if config.Log: datetimee = [datetime.datetime.now()] d = [datetimee, empty1, empty, x] export_data = zip_longest(*d, fillvalue='') with open('Log.csv', 'w', newline='') as myfile: wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) wr.writerow(("End Time", "In", "Out", "Total Inside")) wr.writerows(export_data) # show the output frame cv2.imshow("Real-Time Monitoring/Analysis Window", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # increment the total number of frames processed thus far and # then update the FPS counter totalFrames += 1 fps.update() if config.Timer: # Automatic timer to stop the live stream. Set to 8 hours (28800s). t1 = time.time() num_seconds = (t1 - t0) if num_seconds > 28800: break # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # # if we are not using a video file, stop the camera video stream # if not args.get("input", False): # vs.stop() # # # otherwise, release the video file pointer # else: # vs.release() # close any open windows cv2.destroyAllWindows()
import cv2 from classes.car_crash_game import CarCrashGame from classes.body_detect import detect_body from classes.car_crash_game import body_pos tracker = dlib.correlation_tracker() # dlib correlation tracker initialisation cap = cv2.VideoCapture(0) # capture the video using opencv video capture CarCrashGame = CarCrashGame(cap, tracker) hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) while cap.isOpened(): ret, img = cap.read() body_detected, body_pos_detect = detect_body(img, body_pos, hog) if body_detected: tracker.start_track(img, dlib.rectangle(body_pos_detect[0], body_pos_detect[1], \ body_pos_detect[2], body_pos_detect[3])) pos = tracker.get_position() track_pos_prev = [(pos.left() + pos.right()) / 2., (pos.top() + pos.bottom()) / 2.] # we start the game if there is a hand detected CarCrashGame.start_game(track_pos_prev) while True: pos = tracker.get_position() track_pos_prev = [(pos.left() + pos.right()) / 2., (pos.top() + pos.bottom()) / 2.] CarCrashGame.update(track_pos_prev) k = cv2.waitKey(1) if k == 27: break
def detectAndTrackLargestFace(): #Open the first webcame device capture = VideoStream(src=0).start() time.sleep(3) #Create two opencv named windows if dev: cv2.namedWindow("base-image", cv2.WINDOW_AUTOSIZE) cv2.namedWindow("result-image", cv2.WINDOW_AUTOSIZE) #Position the windows next to eachother cv2.moveWindow("base-image", 0, 100) cv2.moveWindow("result-image", 400, 100) #Start the window thread for the two windows we are using cv2.startWindowThread() #Create the tracker we will use tracker = dlib.correlation_tracker() #The variable we use to keep track of the fact whether we are #currently using the dlib tracker trackingFace = 0 #The color of the rectangle we draw around the face rectangleColor = (0, 165, 255) #Set threshold border left_x = 100 right_x = 230 top_y = 50 bottom_y = 140 try: while True: #Retrieve the latest image from the webcam fullSizeBaseImage = capture.read() #Wait for camera to be ready if fullSizeBaseImage is None: time.sleep(0.1) continue #Resize the image to 320x240 baseImage = cv2.resize(fullSizeBaseImage, (320, 240)) #Check if a key was pressed and if it was Q, then destroy all #opencv windows and exit the application pressedKey = cv2.waitKey(2) if pressedKey == ord('Q'): cv2.destroyAllWindows() GPIO.cleanup() exit(0) #Result image is the image we will show the user, which is a #combination of the original image from the webcam and the #overlayed rectangle for the largest face resultImage = baseImage.copy() #If we are not tracking a face, then try to detect one if not trackingFace: GPIO.output(LED, GPIO.LOW) GPIO.output(LEFT, GPIO.LOW) GPIO.output(RIGHT, GPIO.LOW) GPIO.output(UP, GPIO.LOW) GPIO.output(DOWN, GPIO.LOW) #For the face detection, we need to make use of a gray #colored image so we will convert the baseImage to a #gray-based image gray = cv2.cvtColor(baseImage, cv2.COLOR_BGR2GRAY) #Now use the haar cascade detector to find all faces #in the image faces = faceCascade.detectMultiScale(gray, 1.3, 5) #For now, we are only interested in the 'largest' #face, and we determine this based on the largest #area of the found rectangle. First initialize the #required variables to 0 maxArea = 0 x = 0 y = 0 w = 0 h = 0 #Loop over all faces and check if the area for this #face is the largest so far #We need to convert it to int here because of the #requirement of the dlib tracker. If we omit the cast to #int here, you will get cast errors since the detector #returns numpy.int32 and the tracker requires an int for (_x, _y, _w, _h) in faces: if _w * _h > maxArea: x = int(_x) y = int(_y) w = int(_w) h = int(_h) maxArea = w * h #If one or more faces are found, initialize the tracker #on the largest face in the picture if maxArea > 0: #Initialize the tracker tracker.start_track( baseImage, dlib.rectangle(x - 10, y - 20, x + w + 10, y + h + 20)) #Set the indicator variable such that we know the #tracker is tracking a region in the image trackingFace = 1 #Check if the tracker is actively tracking a region in the image if trackingFace: #Update the tracker and request information about the #quality of the tracking update trackingQuality = tracker.update(baseImage) #If the tracking quality is good enough, determine the #updated position of the tracked region and draw the #rectangle if trackingQuality >= 7: tracked_position = tracker.get_position() t_x = int(tracked_position.left()) t_y = int(tracked_position.top()) t_w = int(tracked_position.width()) t_h = int(tracked_position.height()) cv2.rectangle(resultImage, (t_x, t_y), (t_x + t_w, t_y + t_h), rectangleColor, 2) GPIO.output(LED, GPIO.HIGH) #If the center of face out of border, will send trigger #to the arduino to track the face. pos_x = t_x + t_w / 2 pos_y = t_y + t_h / 2 print(pos_x, pos_y) if pos_x > right_x: GPIO.output(RIGHT, GPIO.HIGH) GPIO.output(LEFT, GPIO.LOW) elif pos_x < left_x: GPIO.output(LEFT, GPIO.HIGH) GPIO.output(RIGHT, GPIO.LOW) else: GPIO.output(RIGHT, GPIO.LOW) GPIO.output(LEFT, GPIO.LOW) if pos_y < top_y: GPIO.output(DOWN, GPIO.HIGH) GPIO.output(UP, GPIO.LOW) elif pos_y > bottom_y: GPIO.output(UP, GPIO.HIGH) GPIO.output(DOWN, GPIO.LOW) else: GPIO.output(UP, GPIO.LOW) GPIO.output(DOWN, GPIO.LOW) else: #If the quality of the tracking update is not #sufficient (e.g. the tracked region moved out of the #screen) we stop the tracking of the face and in the #next loop we will find the largest face in the image #again trackingFace = 0 #Since we want to show something larger on the screen than the #original 320x240, we resize the image again # #Note that it would also be possible to keep the large version #of the baseimage and make the result image a copy of this large #base image and use the scaling factor to draw the rectangle #at the right coordinates. largeResult = cv2.resize(resultImage, (OUTPUT_SIZE_WIDTH, OUTPUT_SIZE_HEIGHT)) #Finally, we want to show the images on the screen if dev: cv2.imshow("base-image", baseImage) cv2.imshow("result-image", largeResult) #To ensure we can also deal with the user pressing Ctrl-C in the console #we have to check for the KeyboardInterrupt exception and destroy #all opencv windows and exit the application except KeyboardInterrupt as e: cv2.destroyAllWindows() GPIO.cleanup() exit(0)
d2=dist.euclidean(a1,a3) ratio=d2/d1 print(ratio)""" #print("Number of faces detected: ",len(faceRects)) # List to store landmarks of all detected faces landmarksAll = [] # Loop over all detected face rectangles for i in range(0, len(faceRects)): newRect = dlib.rectangle(int(faceRects[i].left()),int(faceRects[i].top()), int(faceRects[i].right()),int(faceRects[i].bottom())) # For every face rectangle, run landmarkDetector landmarks = landmarkDetector(img, newRect) # Print number of landmarks # if i==0: #print("Number of landmarks",len(landmarks.parts())) # Store landmarks for current face landmarksAll.append(landmarks) # Draw landmarks on face
idx = int(label["id"]) - 1 label = "{}: {:.2f}".format(label["name"], score) cv2.rectangle(output, (startX, startY), (endX, endY), COLORS[idx], 2) y = startY - 10 if startY - 10 > 10 else startY + 10 cv2.putText(output, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.3, COLORS[idx], 1) ''' # constrói um objeto de retângulo dlib a partir das # coordenadas da caixa delimitadora e inicie o rastreador # de correlação dlib. tracker = dlib.correlation_tracker() # Salva em rect a lista de coordenadas apenas no momento # que detecta. Esses dados servirão para o rastreador # acompanhar a posição atual e seguir em frente. rect = dlib.rectangle(startX, startY, endX, endY) tracker.start_track(frame, rect) # Adiciona o rastreador na lista de rastreadores para que # possamos utilizá-los durante o salto de frames. trackers.append(tracker) # Caso contrário, deveríamos utilizar nossos objetos *rastreadores* # em vez de objetos *detectores* para obter uma maior taxa de # transferência de processamento de frames. else: # Loop sobre a lista de objetos rastreados for tracker in trackers: # Ajusta o status do sistema para ser 'rastreado' em vez # de 'esperar' ou 'detectar'.
n_frames_per_batch = math.ceil(10 * fps) number_roi = 7 # Number of region of interests within a frame filter_size = 3 # Padding filter size frames_subset = [] prediction = [[0]] model.predict(np.zeros((49, 200, 3))[None, :, :, :]) while True: start = timeit.default_timer() ret_val, original_img = cam.read() img = original_img.copy() result = fd.detect_face(img) try: top, bottom, left, right = result["bbox_indices"] rect = dlib.rectangle(left, top, right, bottom) landmarks = fp.get_face_landmarks(img, rect) # Remove eye regions left_eye = fp.facial_landmarks_indices["generic"]["left_eye"] right_eye = fp.facial_landmarks_indices["generic"]["right_eye"] right_hull = cv2.convexHull(landmarks[right_eye[0]:right_eye[1]]) left_hull = cv2.convexHull(landmarks[left_eye[0]:left_eye[1]]) cv2.drawContours(img, [right_hull], -1, color=(0, 0, 0), thickness=cv2.FILLED) cv2.drawContours(img, [left_hull], -1,
def main(input_path): global new_coords cap = cv2.VideoCapture(input_path) if not cap.isOpened(): print("Video capture not opened. Exiting program\n") exit(-1) # Initialize the correlation tracker. tracker = dlib.correlation_tracker() # Create a named window and attach a mouse event handler cv2.namedWindow("Video") cv2.setMouseCallback("Video", mouse_event_handler) while True: t_start = time.time() ret, frame = cap.read() if frame is None: break frame = cv2.resize(frame, (WIDTH, HEIGHT)) # TODO resize coordinates so that processing is done with lower res than output window out_img = frame.copy() # Dlib uses RGB and CV2 uses BGR rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Start tracking when a new bounding box is defined if new_coords and len(mouse_coords) == 2: print("Initializing tracker") # Order the coordinates so that the first element is to the left and second element to the right if mouse_coords[0] > mouse_coords[1]: mouse_coords.reverse() # dlib requires the top left and bottom right, generate them if needed if mouse_coords[0][1] > mouse_coords[1][1]: top_left = (mouse_coords[0][0], mouse_coords[1][1]) bottom_right = (mouse_coords[1][0], mouse_coords[0][1]) else: top_left = mouse_coords[0] bottom_right = mouse_coords[1] cv2.rectangle(out_img, top_left, bottom_right, (0, 255, 0), 2) rect = dlib.rectangle(top_left[0], top_left[1], bottom_right[0], bottom_right[1]) tracker.start_track(rgb, rect) new_coords = False if not new_coords and len(mouse_coords) == 2: # Perform tracking print("Tracking...") tracker.update(rgb) track_pos = tracker.get_position() x1 = round(track_pos.left()) x2 = round(track_pos.right()) y1 = round(track_pos.top()) y2 = round(track_pos.bottom()) cv2.rectangle(out_img, (x1, y1), (x2, y2), (0, 0, 255), 2) cv2.imshow("Video", out_img) key = cv2.waitKey(1) & 0xFF if key == ord('q') or key == ord('Q'): print("Exiting program.\n") # Release resources cap.release() cv2.destroyAllWindows() break if LIMIT_FREQ: t_end = time.time() t_diff = t_end - t_start if t_diff < 1 / FREQ: time.sleep(1 / FREQ - t_diff)
def img2sticker_kf(img_orig, img_sticker, detector_hog, landmark_predictor): # preprocess img_rgb = cv2.cvtColor(img_orig, cv2.COLOR_BGR2RGB) # detector img_rgb_vga = cv2.resize(img_rgb, (640, 360)) dlib_rects = detector_hog(img_rgb_vga, 0) if len(dlib_rects) < 1: return img_orig # tracker if len(dlib_rects) == 1: bbox = dlib_rects[0] list_input = [(bbox.left(), bbox.top()), (bbox.right(), bbox.bottom())] np_estimate = np.array(box_tracker.process(list_input)) np_est_points = np_estimate.reshape(2, 3)[:, :2].astype(int) l, t, r, b = np_est_points.flatten() # print (l,t,r,b) if (b - t) * (r - l) > 100: dlib_rects[0] = dlib.rectangle(left=l, top=t, right=r, bottom=b) # landmark list_landmarks = [] for dlib_rect in dlib_rects: points = landmark_predictor(img_rgb_vga, dlib_rect) list_points = list(map(lambda p: (p.x, p.y), points.parts())) list_landmarks.append(list_points) # head coord for dlib_rect, landmark in zip(dlib_rects, list_landmarks): x = landmark[58][0] # mouth y = landmark[58][1] w = dlib_rect.width() h = dlib_rect.height() h = h // 2 x, y, w, h = [ele * 2 for ele in [x, y, w, h]] break # sticker img_sticker = cv2.resize(img_sticker, (w, h), interpolation=cv2.INTER_NEAREST) if flg_nose_tracker == True: list_input = [(x, y)] np_estimate = np.array(nose_tracker.process(list_input)) np_est_points = np_estimate.reshape(1, 3)[:, :2].astype(int) x_tmp, y_tmp = np_est_points.flatten() if x_tmp * y_tmp != 0: x = x_tmp y = y_tmp refined_x = x - w // 2 - dlib_rect.width() refined_y = y - h // 2 - dlib_rect.height() # print ('(x,y) : (%d,%d)'%(refined_x, refined_y)) if refined_y < 0: img_sticker = img_sticker[-refined_y:] refined_y = 0 if refined_x < 0: img_sticker = img_sticker[:, -refined_x:] refined_x = 0 elif refined_x + img_sticker.shape[1] >= img_orig.shape[1]: img_sticker = img_sticker[:, :-(img_sticker.shape[1] + refined_x - img_orig.shape[1])] img_bgr = img_orig.copy() sticker_area = img_bgr[refined_y:refined_y + img_sticker.shape[0], refined_x:refined_x + img_sticker.shape[1]] img_bgr[refined_y:refined_y+img_sticker.shape[0], refined_x:refined_x+img_sticker.shape[1]] = \ cv2.addWeighted(sticker_area, 1.0, img_sticker, 0.5, 0) return img_bgr
def get_frame(self): success, image = self.video.read() # We are using Motion JPEG, but OpenCV defaults to capture raw images, # so we must encode it into JPEG in order to correctly display the # video stream. image = enhance_image(image) image = adjust_gamma(image) image = resize(image, width=900) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) fidsToDelete = [] for fid in self.faceTrackers.keys(): trackingQuality = self.faceTrackers[fid].update(gray) # If the tracking quality is not good enough, we must delete # this tracker if trackingQuality < 7: fidsToDelete.append(fid) for fid in fidsToDelete: print("Removing fid " + str(fid) + " from list of trackers") self.faceTrackers.pop(fid, None) if (self.totalFrame % 30) == 0: blur = compute_blurrness(image) #if blur < 70: # print('blurness: ', blur) # ret, jpeg = cv2.imencode('.jpg', image) # return jpeg.tobytes() #faces = detector(gray, 0) faces = face_recognition.face_locations(gray) for face in faces: #bb = face_utils.rect_to_bb(face) #(_x, _y, _w, _h) = bb (top, right, bottom, left) = face (_x, _y, _w, _h) = (left, top, right - left, bottom - top) x = int(_x) y = int(_y) w = int(_w) h = int(_h) # calculate the centerpoint x_bar = x + 0.5 * w y_bar = y + 0.5 * h # Variable holding information which faceid we # matched with matchedFid = None # Now loop over all the trackers and check if the # centerpoint of the face is within the box of a # tracker for fid in self.faceTrackers.keys(): tracked_position = self.faceTrackers[fid].get_position() t_x = int(tracked_position.left()) t_y = int(tracked_position.top()) t_w = int(tracked_position.width()) t_h = int(tracked_position.height()) # calculate the centerpoint t_x_bar = t_x + 0.5 * t_w t_y_bar = t_y + 0.5 * t_h # check if the centerpoint of the face is within the # rectangleof a tracker region. Also, the centerpoint # of the tracker region must be within the region # detected as a face. If both of these conditions hold # we have a match if ((t_x <= x_bar <= (t_x + t_w)) and (t_y <= y_bar <= (t_y + t_h)) and (x <= t_x_bar <= (x + w)) and (y <= t_y_bar <= (y + h))): matchedFid = fid # If no matched fid, then we will check if it is one of our # existing ids, otherwise we create a new ID and encoding. if matchedFid is None: print("Creating new tracker " + str(self.currentFaceID)) encoding = face_recognition.face_encodings(image, [face], num_jitters=10) response = self.compare_faces(encoding) print('Response is:', response) if (response is None) or (response == 'unknown'): # Create and store the tracker tracker = dlib.correlation_tracker() tracker.start_track( gray, dlib.rectangle(x - 10, y - 20, x + w + 10, y + h + 20)) self.faceTrackers[self.currentFaceID] = tracker self.faceNames[self.currentFaceID] = "Person " \ + str(self.currentFaceID) self.encodings[self.currentFaceID] = encoding self.currentFaceID += 1 else: # Create and store the tracker tracker = dlib.correlation_tracker() tracker.start_track( gray, dlib.rectangle(x - 10, y - 20, x + w + 10, y + h + 20)) self.faceTrackers[response] = tracker self.faceNames[response] = "Person " + str(response) print('Find existed person: ', response) # Keep tracking for fid in self.faceTrackers.keys(): tracked_position = self.faceTrackers[fid].get_position() t_x = int(tracked_position.left()) t_y = int(tracked_position.top()) t_w = int(tracked_position.width()) t_h = int(tracked_position.height()) cv2.rectangle(image, (t_x, t_y), (t_x + t_w, t_y + t_h), rectangleColor, 2) if fid in self.faceNames.keys(): cv2.putText(image, self.faceNames[fid], (int(t_x + t_w / 2), int(t_y)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) else: cv2.putText(image, "Detecting...", (int(t_x + t_w / 2), int(t_y)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) text = "total frames{}".format(self.totalFrame) cv2.putText(image, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) ret, jpeg = cv2.imencode('.jpg', image) self.totalFrame += 1 return jpeg.tobytes()
def gen(): """Video streaming generator function.""" print('Inicio de contador de personas.') print('Iniciando captura de video de la cámara ...') #cap = cv2.VideoCapture(LINK_CAMARA, cv2.CAP_FFMPEG) #cap = cv2.VideoCapture(LINK_CAMARA) cap = cv2.VideoCapture('768x576.avi') category_index = label_map_util.create_category_index_from_labelmap( LABEL_MAP, use_display_name=True) print('Iniciando detección de personas ...') model = detection_model labels = LABEL_MAP roi_position = 0.6 threshold = 0.1 # Valor original 0.5 x_axis = True skip_frames = 20 show = True counter = [0, 0, 0, 0] # left, right, up, down total_frames = 0 #ct = CentroidTracker(maxDisappeared=40, maxDistance=50) #ct = CentroidTracker(maxDisappeared=20, maxDistance=20) ct = CentroidTracker(maxDisappeared=5, maxDistance=10) trackers = [] trackableObjects = {} while cap.isOpened(): ret, image_np = cap.read() if not ret: print('Error. No se pudo recuperar captura de video de la cámara.') break height, width, _ = image_np.shape rgb = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB) status = "Waiting" rects = [] if total_frames % skip_frames == 0: status = "Detecting" trackers = [] # Actual detection. output_dict = run_inference_for_single_image(model, image_np) for i, (y_min, x_min, y_max, x_max) in enumerate(output_dict['detection_boxes']): if output_dict['detection_scores'][i] > threshold and ( labels == None or category_index[output_dict['detection_classes'] [i]]['name'] in labels): tracker = dlib.correlation_tracker() rect = dlib.rectangle(int(x_min * width), int(y_min * height), int(x_max * width), int(y_max * height)) tracker.start_track(rgb, rect) trackers.append(tracker) else: status = "Tracking" for tracker in trackers: # update the tracker and grab the updated position tracker.update(rgb) pos = tracker.get_position() # unpack the position object x_min, y_min, x_max, y_max = int(pos.left()), int( pos.top()), int(pos.right()), int(pos.bottom()) # add the bounding box coordinates to the rectangles list rects.append((x_min, y_min, x_max, y_max)) objects = ct.update(rects) for (objectID, centroid) in objects.items(): to = trackableObjects.get(objectID, None) if to is None: to = TrackableObject(objectID, centroid) else: if x_axis and not to.counted: x = [c[0] for c in to.centroids] direction = centroid[0] - np.mean(x) if centroid[0] > roi_position * width and direction > 0: counter[1] += 1 to.counted = True elif centroid[0] < roi_position * width and direction < 0: counter[0] += 1 to.counted = True elif not x_axis and not to.counted: y = [c[1] for c in to.centroids] direction = centroid[1] - np.mean(y) if centroid[1] > roi_position * height and direction > 0: counter[3] += 1 to.counted = True elif centroid[1] < roi_position * height and direction < 0: counter[2] += 1 to.counted = True to.centroids.append(centroid) trackableObjects[objectID] = to text = "ID {}".format(objectID) cv2.putText(image_np, text, (centroid[0] - 10, centroid[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cv2.circle(image_np, (centroid[0], centroid[1]), 4, (255, 255, 255), -1) # Original # Draw ROI line if x_axis: cv2.line(image_np, (int(roi_position * width), 0), (int(roi_position * width), height), (0xFF, 0, 0), 5) else: cv2.line(image_np, (0, int(roi_position * height)), (width, int(roi_position * height)), (0xFF, 0, 0), 5) # display count and status font = cv2.FONT_HERSHEY_SIMPLEX # Original if x_axis: cv2.putText(image_np, f'Left: {counter[0]}; Right: {counter[1]}', (10, 35), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX) else: cv2.putText(image_np, f'Up: {counter[2]}; Down: {counter[3]}', (10, 35), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX) cv2.putText(image_np, 'Status: ' + status, (10, 70), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX) if show: img = cv2.resize(image_np, (0, 0), fx=0.5, fy=0.5) frame = cv2.imencode('.jpg', img)[1].tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') time.sleep(0.1) total_frames += 1
def start_tracking(self, image, roi): tracker = self.tracker rect = dlib.rectangle(roi) tracker.start_track(image, rect) self.track_started = True
idx = int(detections[0, 0, i, 1]) # if the class label is not a person, ignore it if CLASSES[idx] != "person": continue # compute the (x, y)-coordinates of the bounding box # for the object box = detections[0, 0, i, 3:7] * np.array([W, H, W, H]) (startX, startY, endX, endY) = box.astype("int") # construct a dlib rectangle object from the bounding # box coordinates and then start the dlib correlation # tracker tracker = dlib.correlation_tracker() rect = dlib.rectangle(int(startX), int(startY), int(endX), int(endY)) tracker.start_track(rgb, rect) # add the tracker to our list of trackers so we can # utilize it during skip frames trackers.append(tracker) # otherwise, we should utilize our object *trackers* rather than # object *detectors* to obtain a higher frame processing throughput else: # loop over the trackers for tracker in trackers: # set the status of our system to be 'tracking' rather # than 'waiting' or 'detecting' status = "Tracking"
proba = preds[j] # Compare this vector to source class vectors to verify it is actual belong to this class match_class_idx = (labels == j) match_class_idx = np.where(match_class_idx)[0] selected_idx = np.random.choice(match_class_idx, comparing_num) compare_embeddings = embeddings[selected_idx] # Calculate cosine similarity cos_similarity = CosineSimilarity(embedding, compare_embeddings) if cos_similarity < cosine_threshold and proba > proba_threshold: name = le.classes_[j] text = "{}".format(name) # print("Recognized: {} <{:.2f}>".format(name, proba*100)) # Start tracking tracker = dlib.correlation_tracker() rect = dlib.rectangle(int(box[0]), int(box[1]), int(box[2]), int(box[3])) tracker.start_track(rgb, rect) trackers.append(tracker) texts.append(text) y = bbox[1] - 10 if bbox[1] - 10 > 10 else bbox[1] + 10 cv2.putText(frame, text, (bbox[0], y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 2) else: for tracker, text in zip(trackers, texts): pos = tracker.get_position() # unpack the position object startX = int(pos.left())
# Capture frame-by-frame ret, frame = camera.read() frame = cv2.flip(frame, 1) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Face Detection cycle if detect_flag: detections = face_detector(frame, 1) if not detections: continue ## Choose first detected face shape = detections[0] (init_det_x, init_det_y, face_width, face_height) = rect_to_bb(shape) detect_flag = False predict_flag = True # Update detected face location predicted_rectangle = dlib.rectangle(init_det_x, init_det_y, shape.right(), shape.bottom()) reset_shape = True old_face_det_x = 0 old_face_det_y = 0 # Face Detection cycle if predict_flag: # Face landmark prediction facelan = face_predictor(gray, predicted_rectangle) land_coor = frg.shape_to_np(facelan) # Visualize the landmarks # for (x, y) in land_coor: # cv2.circle(frame, (x, y), 3, (0, 0, 255), -1) # Update face location based on the predicted face landmarks (new_face_det_x, new_face_det_y) = np.mean(land_coor, axis=0) if reset_shape:
def _get_face_feat(self, img_x, box): rec = dlib.rectangle(*box) shape = self.sp(img_x, rec) feat = self.facerec.compute_face_descriptor(img_x, shape) return feat
try: dets = detector(img) except: print("finished!") #flag = False for i, eye in enumerate(dets): left = eye.left() top = eye.top() right = eye.right() bottom = eye.bottom() #控制最多检测数两个瞳孔 if i >= 2: break track[i].start_track(img, dlib.rectangle(left, top, right, bottom)) n += 1 for i in range(2): box_predict = track[i].get_position() # 得到目标的位置 center_x = int(box_predict.left() + box_predict.right()) // 2 center_y = int(box_predict.top() + box_predict.bottom()) // 2 radiu = int( max(abs(box_predict.left() - box_predict.right()), abs(box_predict.top() - box_predict.bottom())) // 2) # print(center_x, center_y, radiu) cv2.circle(img, (center_x, center_y), radiu, (55, 255, 155), 3) #cv2.rectangle(img, (int(box_predict.left()), int(box_predict.top())), # (int(box_predict.right()), int(box_predict.bottom())), (93, 93, 223), 2) # 用矩形框标注出来 try: cv2.imshow("image", img)
def func(image_path,index): #path to frontal face detector cascade_path = "/Users/steveash/Downloads/opencv-4.0.0-alpha/data/haarcascades/haarcascade_frontalface_default.xml" #path to landmarks predictor predictor_path= "/Users/steveash/Desktop/learning/shape_predictor_68_face_landmarks.dat" # Create the haar cascade faceCascade = cv2.CascadeClassifier(cascade_path) # create the landmark predictor predictor = dlib.shape_predictor(predictor_path) # Read the image image = cv2.imread(image_path) # convert the image to grayscale #gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow("img",image) # Detect faces in the image faces = faceCascade.detectMultiScale( image, scaleFactor=1.05, minNeighbors=5, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE) print("Found {0} faces!".format(len(faces))) #to break the code, if the face is not found if len(faces) != 1: return 0 for (x, y, w, h) in faces: # Draw a rectangle around the faces cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) # Converting the OpenCV rectangle coordinates to Dlib rectangle for further use dlib_rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h)) print(dlib_rect) # extracting feature from the recognized frontal face detected_landmarks = predictor(image, dlib_rect).parts() # numpy matrix of 68 coordinates landmarks = np.matrix([[p.x, p.y] for p in detected_landmarks]) # copying the image so we can see side-by-side image_copy = image.copy() for idx, point in enumerate(landmarks): # iterating over coordinates of each of the 68 facial landmarks pos = (point[0, 0], point[0, 1]) # typecasting of tuple pos into list for classification, training-testing mylist[index].append(list(pos)) # for showing the landmarks in the image, idx = 0 to 67 cv2.putText(image_copy, str(idx), pos, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.4, color=(0, 0, 255)) # draw points on the landmark positions cv2.circle(image_copy, pos, 3, color=(0, 255, 255)) #print(landmarks) #small1 = cv2.resize(image, (0,0), fx=0.5, fy=0.5) #small2 = cv2.resize(image_copy, (0,0), fx=0.5, fy=0.5) #print(mylist) cv2.imshow("Faces found", image) cv2.imshow("Landmarks found", image_copy) cv2.waitKey(500) return 1
idx = int(detections[0, 0, i, 1]) # get the index from detected object # 1 wenne i kiyana object eke wargaya class eke position eka if (idx == 15): # filter only humans box = detections[0, 0, i, 3:7] * np.array( [w, h, w, h]) ##3:7 wenne i kiyana object eke x1=3,y1=4,x2=5,y2=6 points 4 print box.astype("int") (startX, startY, endX, endY) = box.astype("int") coordinates.append((startX, startY, endX, endY)) cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2) if key == ord("t"): coor=track_func(frame) tracker = [dlib.correlation_tracker() for _ in xrange(len(coor))] # Provide the tracker the initial position of the object [tracker[i].start_track(frame, dlib.rectangle(*rect)) for i, rect in enumerate(coor)] for ii in xrange(len(tracker)): tracker[ii].update(frame) # Get the position of th object, draw a # bounding box around it and display it. rect = tracker[ii].get_position() pt1 = (int(rect.left()), int(rect.top())) pt2 = (int(rect.right()), int(rect.bottom())) cv2.rectangle(frame, pt1, pt2, (255, 255, 255), 3) label = str(ii) # Get the position of the object, draw a # bounding box around it and display it. y = pt1[1] - 15 if pt1[1] - 15 > 15 else pt1[1] + 15 cv2.putText(frame, label, (pt1[0], y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[15], 2)
def runComputationallyTaskingAlgoIfBasicAlgoFails(self): # check to see if we should run a more computationally expensive # object detection method to aid our tracker if self.totalFrames % self.conf["track_object"] == 0: # initialize our new set of object trackers self.trackers = [] # convert the frame to a blob and pass the blob through the # network and obtain the detections blob = cv2.dnn.blobFromImage(frame, size=(300, 300), ddepth=cv2.CV_8U) self.net.setInput(blob, scalefactor=1.0 / 127.5, mean=[127.5, 127.5, 127.5]) detections = self.net.forward() # loop over the detections for i in np.arange(0, detections.shape[2]): # extract the confidence (i.e., probability) associated # with the prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` # is greater than the minimum confidence if confidence > self.conf["confidence"]: # extract the index of the class label from the # detections list idx = int(detections[0, 0, i, 1]) # if the class label is not a car, ignore it if self.CLASSES[idx] != "car": continue # compute the (x, y)-coordinates of the bounding box # for the object box = detections[0, 0, i, 3:7] * np.array( [self.W, self.H, self.W, self.H]) (startX, startY, endX, endY) = box.astype("int") # construct a dlib rectangle object from the bounding # box coordinates and then start the dlib correlation # tracker tracker = dlib.correlation_tracker() rect = dlib.rectangle(startX, startY, endX, endY) tracker.start_track(self.rgb, rect) # add the tracker to our list of trackers so we can # utilize it during skip frames self.trackers.append(tracker) # otherwise, we should utilize our object *trackers* rather than # object *detectors* to obtain a higher frame processing # throughput else: # loop over the trackers for tracker in self.trackers: # update the tracker and grab the updated position tracker.update(self.rgb) pos = tracker.get_position() # unpack the position object startX = int(pos.left()) startY = int(pos.top()) endX = int(pos.right()) endY = int(pos.bottom()) # add the bounding box coordinates to the rectangles list self.rects.append((startX, startY, endX, endY))
from skimage import io import cv2 import imutils # Path to the video frames video_folder = '/Users/Alex/Desktop/HumanDetection/video/Video1.mov' cap = cv2.VideoCapture(video_folder) # Create the correlation tracker - the object needs to be initialized # before it can be used tracker = dlib.correlation_tracker() win = dlib.image_window() (grabbed, img) = cap.read() frame1 = imutils.resize(img, width=500) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) tracker.start_track(gray, dlib.rectangle(342, 136, 356, 177)) # We will track the frames as we load them from the video try: while True: (grabbed, img) = cap.read() img = imutils.resize(img, width=500) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) # We need to initialize the tracker on the first frame # if k == 0: # Start a track on the juice box. If you look at the first frame you # will see that the juice box is contained within the bounding # box (74, 67, 112, 153). # tracker.start_track(img, dlib.rectangle(74, 67, 112, 153))# (342, 141), (356, 174) # else:
def start_tracking(self): i = 0 for f in range(self.frames_count): timer = cv2.getTickCount() ret, self.frame = self.cap.read() if not ret: print("End!") break print("Processing Frame {}".format(i)) img_raw = self.frame image = cv2.resize(img_raw.copy(), self.video_size, interpolation=cv2.INTER_CUBIC) if i == 0: #只有在第一帧时才需要框选目标 while (True): img_first = image.copy() if self.track_window: cv2.rectangle( img_first, (self.track_window[0], self.track_window[1]), (self.track_window[2], self.track_window[3]), self.box_color, 1) elif self.selection: cv2.rectangle(img_first, (self.selection[0], self.selection[1]), (self.selection[2], self.selection[3]), self.box_color, 1) cv2.imshow(self.windowName, img_first) if cv2.waitKey(self.speed) == 13: #Enter开始追踪 break if self.tracker_type == 'Dlib_Tracker': self.tracker.start_track( image, dlib.rectangle(self.track_window[0], self.track_window[1], self.track_window[2], self.track_window[3])) elif self.tracker_type == 'CamShift': tracker_box = (self.track_window[0], self.track_window[1], self.track_window[2] - self.track_window[0], self.track_window[3] - self.track_window[1]) roi = image[self.track_window[1]:self.track_window[3], self.track_window[0]:self.track_window[2]] hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)), np.array((180., 255., 255.))) roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180]) cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX) term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) elif self.tracker_type == 'Template_Matching': ''' 1.平方差匹配 method = CV_TM_SQDIFF 2.标准平方差匹配 method = CV_TM_SQDIFF_NORMED 3.相关匹配 method = CV_TM_CCORR 4.标准相关匹配 method = CV_TM_CCORR_NORMED 5.相关匹配 method = CV_TM_CCOEFF 6.标准相关匹配 method = CV_TM_CCOEFF_NORMED cv2.matchTemplate()方法严格要求模板与背景为同一数据类型(CV_8U or CV_32F) ''' method = cv2.TM_CCOEFF_NORMED template = image[self.track_window[1]:self.track_window[3], self.track_window[0]:self.track_window[2]] template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY) template = template.astype(np.float32) else: #OpenCV预置的五种追踪器 ret = self.tracker.init( image, (self.track_window[0], self.track_window[1], self.track_window[2] - self.track_window[0], self.track_window[3] - self.track_window[1])) #框选完目标后,第一帧结束就开始追踪目标 if self.tracker_type == 'Dlib_Tracker': self.tracker.update(image) tracker_box = self.tracker.get_position() x, y, w, h = tracker_box.left(), tracker_box.top( ), tracker_box.width(), tracker_box.height() elif self.tracker_type == 'CamShift': hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1) ret, tracker_box = cv2.CamShift(dst, tracker_box, term_crit) x, y, w, h = tracker_box elif self.tracker_type == 'Template_Matching': gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = gray.astype(np.float32) res = cv2.matchTemplate(gray, template, method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) w, h = template.shape[::-1] #平方差匹配CV_TM_SQDIFF与标准平方差匹配TM_SQDIFF_NORMED最佳匹配为最小值 0,匹配值越大匹配越差,其余则相反 if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: x = min_loc[0] y = min_loc[1] else: x = max_loc[0] y = max_loc[1] else: #OpenCV预置的五种追踪器 ret, tracker_box = self.tracker.update(image) x, y, w, h = tracker_box self.drawing(image, x, y, w, h, timer) cv2.imshow(self.windowName, image) if cv2.waitKey(self.speed) == 27: #Esc结束 break i += 1 if i == self.frames_count: cv2.imwrite('Video/track_result.jpg', image) cv2.destroyAllWindows()
def trackMultipleObjects(): rectangleColor = (0, 255, 0) frameCounter = 0 currentCarID = 0 fps = 0 carTracker = {} carNumbers = {} carLocation1 = {} carLocation2 = {} speed = [None] * 1000 # Write output to video file out = cv2.VideoWriter('outpy.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (WIDTH, HEIGHT)) while True: start_time = time.time() rc, image = video.read() if type(image) == type(None): break image = cv2.resize(image, (WIDTH, HEIGHT)) resultImage = image.copy() frameCounter = frameCounter + 1 carIDtoDelete = [] for carID in carTracker.keys(): trackingQuality = carTracker[carID].update(image) if trackingQuality < 7: carIDtoDelete.append(carID) for carID in carIDtoDelete: # print ('Removing carID ' + str(carID) + ' from list of trackers.') # print ('Removing carID ' + str(carID) + ' previous location.') # print ('Removing carID ' + str(carID) + ' current location.') carTracker.pop(carID, None) carLocation1.pop(carID, None) carLocation2.pop(carID, None) if not (frameCounter % 10): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cars = carCascade.detectMultiScale(gray, 1.1, 13, 18, (24, 24)) for (_x, _y, _w, _h) in cars: x = int(_x) y = int(_y) w = int(_w) h = int(_h) x_bar = x + 0.5 * w y_bar = y + 0.5 * h matchCarID = None for carID in carTracker.keys(): trackedPosition = carTracker[carID].get_position() t_x = int(trackedPosition.left()) t_y = int(trackedPosition.top()) t_w = int(trackedPosition.width()) t_h = int(trackedPosition.height()) t_x_bar = t_x + 0.5 * t_w t_y_bar = t_y + 0.5 * t_h if ((t_x <= x_bar <= (t_x + t_w)) and (t_y <= y_bar <= (t_y + t_h)) and (x <= t_x_bar <= (x + w)) and (y <= t_y_bar <= (y + h))): matchCarID = carID if matchCarID is None: # print ('Creating new tracker ' + str(currentCarID)) tracker = dlib.correlation_tracker() tracker.start_track(image, dlib.rectangle(x, y, x + w, y + h)) carTracker[currentCarID] = tracker carLocation1[currentCarID] = [x, y, w, h] currentCarID = currentCarID + 1 #cv2.line(resultImage,(0,480),(1280,480),(255,0,0),5) for carID in carTracker.keys(): trackedPosition = carTracker[carID].get_position() t_x = int(trackedPosition.left()) t_y = int(trackedPosition.top()) t_w = int(trackedPosition.width()) t_h = int(trackedPosition.height()) cv2.rectangle(resultImage, (t_x, t_y), (t_x + t_w, t_y + t_h), rectangleColor, 4) # speed estimation carLocation2[carID] = [t_x, t_y, t_w, t_h] end_time = time.time() if not (end_time == start_time): fps = 1.0 / (end_time - start_time) #cv2.putText(resultImage, 'FPS: ' + str(int(fps)), (620, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) for i in carLocation1.keys(): if frameCounter % 1 == 0: [x1, y1, w1, h1] = carLocation1[i] [x2, y2, w2, h2] = carLocation2[i] # print 'previous location: ' + str(carLocation1[i]) + ', current location: ' + str(carLocation2[i]) carLocation1[i] = [x2, y2, w2, h2] # print 'new previous location: ' + str(carLocation1[i]) if [x1, y1, w1, h1] != [x2, y2, w2, h2]: if (speed[i] == None or speed[i] == 0) and y1 >= 275 and y1 <= 285: speed[i] = estimateSpeed([x1, y1, w1, h1], [x2, y2, w2, h2]) #if y1 > 275 and y1 < 285: if speed[i] != None and y1 >= 180: hell_yeah = int(speed[i]) if (hell_yeah >= LIMIT): cv2.putText( resultImage, str(int(speed[i])) + " km/hr LIMIT CROSSED", (int(x1 + w1 / 2), int(y1 - 5)), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2) else: cv2.putText(resultImage, str(int(speed[i])) + " km/hr", (int(x1 + w1 / 2), int(y1 - 5)), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2) #print ('CarID ' + str(i) + ': speed is ' + str("%.2f" % round(speed[i], 0)) + ' km/h.\n') #else: # cv2.putText(resultImage, "Far Object", (int(x1 + w1/2), int(y1)),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) #print ('CarID ' + str(i) + ' Location1: ' + str(carLocation1[i]) + ' Location2: ' + str(carLocation2[i]) + ' speed is ' + str("%.2f" % round(speed[i], 0)) + ' km/h.\n') cv2.imshow('result', resultImage) # Write the frame into the file 'output.avi' #out.write(resultImage) if cv2.waitKey(33) == 27: break cv2.destroyAllWindows()
#del current_locations[j] print('removed duplicate person') #break for k in sorted(del_indexs, key=int, reverse=True): del current_locations[k] print('total new people: ', len(current_locations)) # Add a new tracker to a new people for current_location in current_locations: if isHuman(current_location.coor): tracker = dlib.correlation_tracker() cv2.rectangle(frame,(int(current_location.coor.P1.X), int(current_location.coor.P1.Y)),(int(current_location.coor.P2.X), int(current_location.coor.P2.Y)),(144,144,255),3) tracker.start_track(frame, dlib.rectangle(current_location.coor.P1.X,current_location.coor.P1.Y,current_location.coor.P2.X,current_location.coor.P2.Y)) trackers.append(tracker) tracking_locations.append(convertToTracking(tracker)) print('total trackers: ', len(trackers)) print('total trackering: ', len(tracking_locations)) # Clean up tracker trackers, tracking_locations = cleanUpTracker(trackers, tracking_locations) #Clean up tracking if iteration_no and iteration_no % CHECK_POINT == 0: trackers, tracking_locations = cleanUpTracking(trackers, tracking_locations, iteration_no) count, count_out = counting(tracking_locations, count, count_out) cv2.line(frame,(int(line_in.P1.X), int(line_in.P1.Y)),(int(line_in.P2.X), int(line_in.P2.Y)),(255,0,0),5)
def get_landmarks(im): rects = cascade.detectMultiScale(im, 1.3,5) x,y,w,h =rects[0] rect=dlib.rectangle(x,y,x+w,y+h) return numpy.matrix([[p.x, p.y] for p in predictor(im, rect).parts()])
def main(): global cam, turret #start cam cam = Camera.Capture(cfg) cam.daemon = True cam.start() i = 0 while (i < 50): #allow 5sec for startup i += 1 sleep(0.1) if cam.rawframe != None: break frame = cam.getFrame() #need during init #start turret turret = Turret.Targetting(cfg) turret.daemon = True turret.recenter() turret.start() #start controller cmdlistener = Controller.Listener(cfg) cmdlistener.startlisteners() key = "" #display displaytext = "" #for writing message in window if os.environ.get('DISPLAY') and int(cfg['camera']['display']): display = True print 'display found' else: display = False print 'no display' if display: cv2.namedWindow("display", cv2.cv.CV_WINDOW_AUTOSIZE) cv2.setMouseCallback("display", on_click, 0) #tracking functions track = Tracker.Tracking(cfg, display) tracker = None #dlib object tracking #motion detection avgframe = np.float32(frame) avgtimer = threading.Event() #TODO: put this in Timer thread? #speak speak = Speak.Speak(cfg) speak.say("ready") cam.resetFPS() while (1): #capture frame,position framexy = turret.xy frame = cam.getFrame() if display: #default display displayframe = frame #targetting color (hsv) if track.mode == 4: cx, cy, displayframe = track.targetHsv(frame) if cx: turret.sendTarget(turret.coordToPulse((cx, cy)), framexy) #targetting object (dlib) if track.mode == 5: tracker.update(frame) rect = tracker.get_position() cx = (rect.right() + rect.left()) / 2 cy = (rect.top() + rect.bottom()) / 2 turret.sendTarget(turret.coordToPulse((cx, cy)), framexy) if display: pt1 = (int(rect.left()), int(rect.top())) pt2 = (int(rect.right()), int(rect.bottom())) cv2.rectangle(displayframe, pt1, pt2, (255, 255, 255), 3) #detect motionhsv, motionobj elif track.mode == 2 or track.mode == 3: if avgtimer.isSet(): #learn image for motion detection cv2.accumulateWeighted(frame, avgframe, 0.8) resframe = cv2.convertScaleAbs(avgframe) else: cnt, motionmask = track.getMotionContour( frame, resframe, track.areathresholdobject if track.mode == 3 else track.areathresholdcolor) if display: displayframe = cv2.cvtColor(motionmask.copy(), cv2.COLOR_GRAY2RGB) if not cnt == None: #motionhsv if track.mode == 2: #mask for mean track.bgrtarget = cv2.mean(frame, motionmask) framehsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) track.hsvtarget = cv2.mean(framehsv, motionmask) track.setColorRange() turret.armed = True displaytext = "Targetting " + Speak.BGRname( track.bgrtarget) speak.say(displaytext) track.mode = 4 #hsvtarget cam.resetFPS() #motionobj if track.mode == 3: #object points x, y, w, h = cv2.boundingRect(cnt) if x > 3 and y > 3: # and x+w<cam.w-3 and y+h<cam.h-3: #object inside frame #TODO: which sides? points = (x, y, x + w, y + h) tracker = dlib.correlation_tracker() tracker.start_track(frame, dlib.rectangle(*points)) displaytext = "Targetting object" speak.say(displaytext) track.mode = 5 #dlib cam.resetFPS() else: cv2.rectangle(displayframe, (x, y), (x + w, y + h), (0, 0, 255), 2) #face elif track.mode == 1: detector = dlib.get_frontal_face_detector() faces = detector(frame) best_area = 0 best_face = None for d in faces: area = (d.left() - d.right()) * (d.top() - d.bottom()) if area > best_area: best_area = area best_face = d if not best_face == None: #face points points = (best_face.left(), best_face.top(), best_face.right(), best_face.bottom()) tracker = dlib.correlation_tracker() tracker.start_track(frame, dlib.rectangle(*points)) displaytext = "Targetting human" speak.say(displaytext) track.mode = 5 #dlib #if face, go for shirt color # xs = int(x + (x/2)) #shirt # ys = int(y+(h*1.7)) #shirt # framecenter = frame[ys-track.sampleradius:ys+track.sampleradius, xs-track.sampleradius:xs+track.sampleradius] # framecenterhsv = cv2.cvtColor(framecenter, cv2.COLOR_BGR2HSV) # track.hsvtarget = [int(cv2.mean(framecenterhsv)[0]), int(cv2.mean(framecenterhsv)[1]), int(cv2.mean(framecenterhsv)[2])] # track.setColorRange() # track.bgrtarget = [int(cv2.mean(framecenter)[0]), int(cv2.mean(framecenter)[1]), int(cv2.mean(framecenter)[2])] # speak.say("Hey, you in the " + Speak.BGRname(track.bgrtarget) + " shirt") # track.mode = 4 #hsvtarget #display frame if display: if displaytext: textsize = 0.003 * cam.w cv2.putText(displayframe, displaytext, (5, cam.h - 5), cv2.FONT_HERSHEY_SIMPLEX, textsize, (0, 255, 255)) cv2.imshow('display', displayframe) cv2.waitKey(1) # Command Handler -------------------------- if cmdlistener.cmdsent.isSet(): key = cmdlistener.cmd cmdlistener.reset() #quit/restart if key == "?" or key == "h": f = open( os.path.dirname(os.path.abspath(__file__)) + '/help.txt', 'r') print(f.read()) elif key == "q" or key == "r": turret.quit() cam.quit() cmdlistener.quit() cfg.write() if display: cv2.destroyAllWindows() if key == "q": speak.say("quitting. bye") return 0 if key == "r": speak.say("restarting") return 1 #--- Targeting --- elif key == " ": #reset all speak.say("Reset") displaytext = "" turret.armed = False track.mode = 0 turret.recenter() cam.resetFPS() elif key == "t": #sample center of image sampleradius = 0.02 * (cam.w / float(cfg['camera']['scaledown'])) framecenter = frame[(cam.h / 2) - sampleradius:(cam.h / 2) + sampleradius, (cam.w / 2) - sampleradius:(cam.w / 2) + sampleradius] framecenterhsv = cv2.cvtColor(framecenter, cv2.COLOR_BGR2HSV) track.hsvtarget = [ int(cv2.mean(framecenterhsv)[0]), int(cv2.mean(framecenterhsv)[1]), int(cv2.mean(framecenterhsv)[2]) ] track.setColorRange() track.bgrtarget = [ int(cv2.mean(framecenter)[0]), int(cv2.mean(framecenter)[1]), int(cv2.mean(framecenter)[2]) ] displaytext = "Targetting " + Speak.BGRname(track.bgrtarget) speak.say(displaytext) track.mode = 4 cam.resetFPS() elif key == "1": #start face detect displaytext = "Seeking humans." speak.say(displaytext) track.mode = 1 #face cam.resetFPS() #track.faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") elif key == "2" or key == "3": #start motion detect: 2:hsv or 3:dlib displaytext = "Motion " + ("by color" if key == "2" else "by object") speak.say(displaytext) Timer.Countdown(5, avgtimer).thread.start() sleep(.1) #timer set track.mode = int(key) #motiondetect #--- Controls --- elif key == "j": #joke/taunt speak.taunt() elif key == "g": #test fire turret.fire() #--- Settings --- elif key == "i": #fps print(cam.getFPS()) cam.resetFPS() elif key == "p": #toggle armed turret.armed = not turret.armed if turret.armed: speak.say("armed") else: speak.say("disarmed") elif key == "n": #toggle noisy if speak.quiet: speak.quiet = 0 speak.say("talking") else: speak.say("quiet") sleep(.1) speak.quiet = 1 cfg['speak']['quiet'] = speak.quiet elif key == "+": #volume speak.volume(.1) speak.say("check") elif key == "-": speak.volume(-.1) speak.say("check") elif key == "y": #restart speak speak = Speak.Speak() speak.say("ok") elif key == "w": #pause display cam.resetFPS() if not display and os.environ.get('DISPLAY') and int( cfg['camera']['display']): display = True print("- display resumed -") else: display = False print("- display paused -") #--- Tolerance --- else: adjustpercent = 0.10 #step +/- percent for manual key adjustments adjust = lambda val, sign: val * (1 + (sign * adjustpercent)) if key == "l" or key == "m": #target sensitivity turret.firesensitivity = adjust(turret.firesensitivity, 1 if key == "l" else -1) cfg['tolerance'][ 'firesensitivity'] = turret.firesensitivity if key == "a" or key == "z": #threshhold area track.areathresholdcolor = adjust(track.areathresholdcolor, 1 if key == "a" else -1) cfg['tolerance']['colortrack'][ 'areathreshold'] = track.areathresholdcolor if key == "s" or key == "x": #threshhold area track.areathresholdobject = adjust( track.areathresholdobject, 1 if key == "s" else -1) cfg['tolerance']['objecttrack'][ 'areathreshold'] = track.areathresholdobject print "Area(Color):", track.areathresholdcolor, "Area(Object):", track.areathresholdobject, "Trigger:", turret.firesensitivity