def face_detection(image, resize, video=False): if resize: image = imutils.resize(image, width=500) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # detect faces in the grayscale image rects = detector(gray, 1) # loop over the face detections for (i, rect) in enumerate(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) # convert dlib's rectangle to a OpenCV-style bounding box # [i.e., (x, y, w, h)], then draw the face bounding box (x, y, w, h) = face_utils.rect_to_bb(rect) # cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) # show the face number # cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10), # cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # loop over the (x, y)-coordinates for the facial landmarks # and draw them on the image if video: for (x, y) in shape: cv2.circle(image, (x, y), 1, (255, 0, 0), -1) else: image = visualize_facial_landmarks(image, shape) return image
face_img = np.zeros((64, 96)).astype(np.uint8) left_img = np.zeros((64, 96)).astype(np.uint8) rigt_img = np.zeros((64, 96)).astype(np.uint8) cur_status = None cur_direction = None cur_spacekey = None for (ii, rect_s) in enumerate(rects_small): tmp = np.array([rect_s.left(), rect_s.top(), rect_s.right(), rect_s.bottom()]) / scale tmp = tmp.astype(np.long) # get face rect rect = dlib.rectangle(tmp[0], tmp[1], tmp[2], tmp[3]) (x, y, w, h) = face_utils.rect_to_bb(rect) cv2.rectangle(frame_small, (int(x*scale), int(y*scale)), (int((x+w)*scale), int((y+h)*scale)), (0, 255, 0), 2) # get face landmarks shape = predictor(frame, rect) shape = face_utils.shape_to_np(shape) cur_status = face_utils.get_mouth_status(shape) for i, (x, y) in enumerate(shape): cv2.circle(frame_small, (int(x*scale), int(y*scale)), 1, (0, 0, 255), -1) # eye gaze estimation face_img, left_img, rigt_img, eye_lm, fc_c_world = \ pre_eye.WarpNCrop(frame[:,:,::-1], shape, inv_cameraMat, cam_new) y_result, eye_tensor, face_tensor = sess.run([y_conv, h_trans, face_h_trans], feed_dict={
# show the original input image and detect faces in the grayscale image if DEBUG: cv2.imshow("Input", image) cv2.waitKey(1) [rects, scores, tmp] = detector.run(gray, 2) if len(rects) > 0: # check if exist some detection ix = scores.index(max(scores)) rect = rects[ix] # extract the ROI of the *original* face, then align the face # using facial landmarks (x, y, w, h) = rect_to_bb(rect) faceAligned, pose = fa.align(image, gray, rect) # display the output images if DEBUG: faceOrig = imutils.resize(image[y:y + h, x:x + w], width=256) cv2.imshow("Original", faceOrig) cv2.imshow("Aligned", faceAligned) cv2.waitKey(1) if pose: path, file = os.path.split(image_path) if path != last_path: image_path_out = path.replace(dataset_path, output_path) last_path = path