def predict(): if request.method == "POST": flag = 0 r = request.data.decode('utf-8') _, encoded = r.split(",", 1) imgdata = base64.b64decode(encoded) im_arr = np.frombuffer(imgdata, dtype=np.uint8) # im_arr is one-dim Numpy array img = cv2.imdecode(im_arr, flags=cv2.IMREAD_COLOR) print(img.shape) # detector = dlib.get_frontal_face_detector() face_model = get_face_detector() predictor = dlib.shape_predictor('shape_68.dat') left = [36, 37, 38, 39, 40, 41] right = [42, 43, 44, 45, 46, 47] kernel = np.ones((9, 9), np.uint8) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) rects = find_faces(img, face_model) # rects = detector(gray, 1) for rect in rects: # print(type(rect[0])) rec = dlib.rectangle(int(rect[0]), int(rect[1]), int(rect[2]), int(rect[3])) shape = predictor(gray, rec) shape = shape_to_np(shape) mask = np.zeros(img.shape[:2], dtype=np.uint8) mask, end_points_left = eye_on_mask(mask, left, shape) mask, end_points_right = eye_on_mask(mask, right, shape) mask = cv2.dilate(mask, kernel, 5) eyes = cv2.bitwise_and(img, img, mask=mask) mask = (eyes == [0, 0, 0]).all(axis=2) eyes[mask] = [255, 255, 255] mid = (shape[42][0] + shape[39][0]) // 2 eyes_gray = cv2.cvtColor(eyes, cv2.COLOR_BGR2GRAY) # threshold = cv2.getTrackbarPos('threshold', 'image') _, thresh = cv2.threshold(eyes_gray, 90, 255, cv2.THRESH_BINARY) thresh = process_thresh(thresh) eyeball_pos_left = contouring(thresh[:, 0:mid], mid, end_points_left) eyeball_pos_right = contouring(thresh[:, mid:], mid, end_points_right, True) flag = print_eye_pos(eyeball_pos_left, eyeball_pos_right) op = dict() op['flag'] = json.dumps(flag) return jsonify(op)
""" rear_size = 1 rear_depth = 0 front_size = img.shape[1] front_depth = front_size * 2 val = [rear_size, rear_depth, front_size, front_depth] point_2d = get_2d_points(img, rotation_vector, translation_vector, camera_matrix, val) y = (point_2d[5] + point_2d[8]) // 2 x = point_2d[2] return (x, y) face_model = get_face_detector() landmark_model = get_landmark_model() cap = cv2.VideoCapture(0) ret, img = cap.read() size = img.shape font = cv2.FONT_HERSHEY_SIMPLEX # 3D model points. model_points = np.array([ (0.0, 0.0, 0.0), # Nose tip (0.0, -330.0, -65.0), # Chin (-225.0, 170.0, -135.0), # Left eye left corner (225.0, 170.0, -135.0), # Right eye right corne (-150.0, -150.0, -125.0), # Left Mouth corner (150.0, -150.0, -125.0) # Right mouth corner ])
def main(debug=False): # Load face detect model & face landmark model face_model = face_detector.get_face_detector() landmark_model = face_landmarks.get_landmark_model() cv2.namedWindow('debug_window') # Create camera instance for capture picture, speed up processing on windows platform by using cv2.CAP_DSHOW. if platform.system().lower() == 'windows': camera = cv2.VideoCapture(0 + cv2.CAP_DSHOW) else: camera = cv2.VideoCapture(0) _, sample_img = camera.read() # grab one frame, for getting resolution height, width, channel = sample_img.shape # resolution & color channel (1 or None = GrayScale, 3 = BGR colors) # 3D model points. model_points = np.array([ (0.0, 0.0, 0.0), # Nose tip (0.0, -330.0, -65.0), # Chin (-225.0, 170.0, -135.0), # Left eye left corner (225.0, 170.0, -135.0), # Right eye right corne (-150.0, -150.0, -125.0), # Left Mouth corner (150.0, -150.0, -125.0) # Right mouth corner ]) # Camera internals focal_length = width center = (width/2, height/2) camera_matrix = np.array([ [focal_length, 0, center[0]], [0, focal_length, center[1]], [0, 0, 1] ], dtype='double') # Lens distance coefficient: assuming no lens distortion dist_coefficient = np.zeros((4, 1)) while True: # start time stamp this frame time_start = datetime.now() # 1. capture image (1 frame) ret, frame = camera.read() if ret: # 2. detect faces faces = face_detector.find_faces(frame, face_model) if faces: if debug: face_detector.draw_faces(frame, faces) for face in faces: # 3. detect face landmarks try: landmarks = face_landmarks.detect_marks(frame, landmark_model, face) except cv2.error: # Skip this round if failed to detect landmarks. break if debug: face_landmarks.draw_marks(frame, landmarks, color=(0, 255, 0)) frame_points = np.array([ landmarks[30], # Nose tip landmarks[8], # Chin landmarks[36], # Left eye left corner landmarks[45], # Right eye right corne landmarks[48], # Left Mouth corner landmarks[54] # Right mouth corner ], dtype='double') # 4. get rotation & translation vector success, rotation_vector, translation_vector = cv2.solvePnP( model_points, frame_points, camera_matrix, dist_coefficient, flags=cv2.SOLVEPNP_UPNP ) if debug: print(' ' * 13, 'solvePnP:', success, rotation_vector.tolist(), translation_vector.tolist(), end='\r') # TODO: calculate iris, mouth and head's roll pitch yaw # TODO: create socket client, send calculated data to unity cv2.imshow('debug_window', frame) # end time stamp of this frame time_delta = datetime.now() - time_start # calculate Frame Per Second fps = 1e6 / time_delta.microseconds print(' FPS:', fps, end='\r') if cv2.waitKey(1) & 0xFF in (27, ord('q')): break if debug: cv2.destroyAllWindows() camera.release()
def get_head_position(File_name): face_model = get_face_detector() landmark_model = get_landmark_model() cap = cv2.VideoCapture(File_name) ret, img = cap.read() size = img.shape font = cv2.FONT_HERSHEY_SIMPLEX # 3D model points. model_points = np.array([ (0.0, 0.0, 0.0), # Nose tip (0.0, -330.0, -65.0), # Chin (-225.0, 170.0, -135.0), # Left eye left corner (225.0, 170.0, -135.0), # Right eye right corne (-150.0, -150.0, -125.0), # Left Mouth corner (150.0, -150.0, -125.0) # Right mouth corner ]) # Camera internals focal_length = size[1] center = (size[1] / 2, size[0] / 2) camera_matrix = np.array([[focal_length, 0, center[0]], [0, focal_length, center[1]], [0, 0, 1]], dtype="double") head_directions = [0] * 4 # 아래, 위, 오른쪽, 왼쪽 while True: ret, img = cap.read() if ret == True: faces = find_faces(img, face_model) for face in faces: marks = detect_marks(img, landmark_model, face) # mark_detector.draw_marks(img, marks, color=(0, 255, 0)) image_points = np.array( [ marks[30], # Nose tip marks[8], # Chin marks[36], # Left eye left corner marks[45], # Right eye right corne marks[48], # Left Mouth corner marks[54] # Right mouth corner ], dtype="double") dist_coeffs = np.zeros((4, 1)) # Assuming no lens distortion (success, rotation_vector, translation_vector) = cv2.solvePnP(model_points, image_points, camera_matrix, dist_coeffs, flags=cv2.SOLVEPNP_UPNP) # Project a 3D point (0, 0, 1000.0) onto the image plane. # We use this to draw a line sticking out of the nose (nose_end_point2D, jacobian) = cv2.projectPoints(np.array([ (0.0, 0.0, 1000.0) ]), rotation_vector, translation_vector, camera_matrix, dist_coeffs) for p in image_points: cv2.circle(img, (int(p[0]), int(p[1])), 3, (0, 0, 255), -1) p1 = (int(image_points[0][0]), int(image_points[0][1])) p2 = (int(nose_end_point2D[0][0][0]), int(nose_end_point2D[0][0][1])) x1, x2 = head_pose_points(img, rotation_vector, translation_vector, camera_matrix) cv2.line(img, p1, p2, (0, 255, 255), 2) cv2.line(img, tuple(x1), tuple(x2), (255, 255, 0), 2) # for (x, y) in marks: # cv2.circle(img, (x, y), 4, (255, 255, 0), -1) # cv2.putText(img, str(p1), p1, font, 1, (0, 255, 255), 1) try: m = (p2[1] - p1[1]) / (p2[0] - p1[0]) ang1 = int(math.degrees(math.atan(m))) except: ang1 = 90 try: m = (x2[1] - x1[1]) / (x2[0] - x1[0]) ang2 = int(math.degrees(math.atan(-1 / m))) except: ang2 = 90 # print('div by zero error') if ang1 >= 48: head_directions[0] += 1 # print('Head down') # cv2.putText(img, 'Head down', (30, 30), font, 2, (255, 255, 128), 3) elif ang1 <= -48: head_directions[1] += 1 # print('Head up') # cv2.putText(img, 'Head up', (30, 30), font, 2, (255, 255, 128), 3) if ang2 >= 48: head_directions[2] += 1 # print('Head right') # cv2.putText(img, 'Head right', (90, 30), font, 2, (255, 255, 128), 3) elif ang2 <= -48: head_directions[3] += 1 # print('Head left') # cv2.putText(img, 'Head left', (90, 30), font, 2, (255, 255, 128), 3) # cv2.putText(img, str(ang1), tuple(p1), font, 2, (128, 255, 255), 3) # cv2.putText(img, str(ang2), tuple(x1), font, 2, (255, 255, 128), 3) # cv2.imshow('img', img) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break cv2.destroyAllWindows() cap.release() return head_directions