# get a new pair of eye images if available eyes_consumed, eye_boxes = eyeDetector.consume_output(wait_needed) if eyes_consumed: frame, face_box, _, _ = q[ eyes_produced] # the corresponding frame and the face bounding box for this detection eyes = eyeDetector.preprocess_output(eye_boxes, face_image=helpers.crop( frame, face_box)) q[eyes_produced][2] = eyes eyes_produced += 1 # get a new face and the bounding box if available face_consumed, face_box = faceDetector.consume_output( confidence=args.confidence, wait=wait_needed) if face_consumed: face_img, face_box = faceDetector.preprocess_output( face_box, frame=q[faces_produced][0]) q[faces_produced][1] = face_box faces_produced += 1 eyeDetector.feed_input(face_img) headPoseEstimator.feed_input(face_img) frame = feed.read_next() # get the next frame from the input feed if frame is not None: # [original frame, face box, eyes, head pose] q.append([frame, None, None, None]) faceDetector.feed_input(frame) else: # When we reached the end of the input stream we have to wait for all frames to finish processing. # To avoid idle running the loop, blocking wait is used when no output is available from any model. wait_needed = not gaze_vector_consumed and not head_pose_consumed and not eyes_consumed and not face_consumed done = len(q) < 1
def main(args): fd_infer_time, ld_infer_time, hpe_infer_time, ge_infer_time = 0 ,0 ,0 ,0 start = time.time() face_detector = FaceDetector(args.model_fd, args.device_fd, args.ext_fd) fd_load_time = time.time() - start start = time.time() landmarks_detector = LandmarksDetector(args.model_ld, args.device_ld, args.ext_ld) ld_load_time = time.time() - start start = time.time() head_pose_estimator = HeadPoseEstimator(args.model_hpe, args.device_hpe, args.ext_hpe) hpe_load_time = time.time() - start start = time.time() gaze_estimator = GazeEstimator(args.model_ge, args.device_ge, args.ext_ge) ge_load_time = time.time() - start log.info("Models Loading...") log.info("Face detection load time :{:.4f}ms".format(fd_load_time)) log.info("Landmarks estimation load time :{:.4f}ms".format(ld_load_time)) log.info("Head pose estimation load time :{:.4f}ms".format(hpe_load_time)) log.info("Gaze estimation load time :{:.4f}ms".format(ge_load_time)) log.info('All Models loaded') mouse_controller = MouseController('high', 'fast') if args.input == 0: input_feeder = InputFeeder('cam', args.input) elif args.input.endswith('.jpg') or args.input.endswith('.bmp'): input_feeder = InputFeeder('image', args.input) else: input_feeder = InputFeeder('video', args.input) input_feeder.load_data() init_w = input_feeder.init_w init_h = input_feeder.init_h counter = 0 for flag, frame in input_feeder.next_batch(): if not flag: break counter +=1 key = cv2.waitKey(60) try: start = time.time() outputs = face_detector.predict(frame) face = face_detector.preprocess_output(frame, outputs, init_w, init_h) fd_infer_time += time.time() - start start = time.time() outputs = landmarks_detector.predict(face) left_eye, right_eye, real_landmraks = landmarks_detector.preprocess_output(face, outputs) ld_infer_time += time.time() - start start = time.time() outputs = head_pose_estimator.predict(face) head_pose_angles = head_pose_estimator.preprocess_output(outputs) hpe_infer_time += time.time() - start start = time.time() outputs = gaze_estimator.predict(left_eye, right_eye, head_pose_angles) gaze = gaze_estimator.preprocess_output(outputs) ge_infer_time += time.time() - start log.info("Face detection time :{:.4f}ms".format(fd_infer_time/counter)) log.info("Landmarks estimation time :{:.4f}ms".format(ld_infer_time/counter)) log.info("Head pose estimation time :{:.4f}ms".format(hpe_infer_time/counter)) log.info("Gaze estimation time :{:.4f}ms".format(ge_infer_time/counter)) if args.input != 0: drawer = Drawer(face, real_landmraks, head_pose_angles, gaze) drawer.draw_landmarks(20) drawer.draw_head_pose() drawer.draw_gazes() drawer.show() roll_cos = math.cos(head_pose_angles[2] * math.pi/180) roll_sin = math.sin(head_pose_angles[2] * math.pi/180) mouse_x = gaze[0] * roll_cos + gaze[0] * roll_sin mouse_y = gaze[1] * roll_cos + gaze[1] * roll_sin mouse_controller.move(mouse_x, mouse_y) except Exception as e: log.error(e) finally: if key == 27: break input_feeder.close()