ret, frame = cap.read() if ret: faces, landmarks = face_detector.detect(frame, 0.8, scales=[1.0], do_flip=False) if len(faces) > 0: face_crops = [] face_boxes = [] for i in range(len(faces)): bbox = faces[i] bbox = (int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])) face_crop = utils.crop_face_loosely( bbox, frame, (config["model"]["im_width"], config["model"]["im_height"])) face_crop = cv2.resize(face_crop, (config["model"]["im_width"], config["model"]["im_height"])) face_box, _, _ = utils.get_loosen_bbox( bbox, frame, (config["model"]["im_width"], config["model"]["im_height"])) face_boxes.append(face_box) # Convert crop image to RGB color space face_crop = cv2.cvtColor(face_crop, cv2.COLOR_BGR2RGB) face_crops.append(face_crop) cv2.imshow("crop", face_crop)
print("Unable to connect to camera.") exit(-1) detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(face_landmark_path) frames = [] while cap.isOpened(): ret, frame = cap.read() if ret: face_rects = detector(frame, 0) if len(face_rects) > 0: shape = predictor(frame, face_rects[0]) shape = face_utils.shape_to_np(shape) face_crop = utils.crop_face_loosely(shape, frame, INPUT_SIZE) frames.append(face_crop) if len(frames) == 1: print(shape[30]) pred_cont_yaw, pred_cont_pitch, pred_cont_roll = net.test_online( frames) cv2_img = utils.draw_axis(frame, pred_cont_yaw, pred_cont_pitch, pred_cont_roll, tdx=shape[30][0], tdy=shape[30][1], size=100) cv2.imshow("cv2_img", cv2_img)
landmark = re_label(draw.copy()) # cv2.imshow("Label", draw) # cv2.waitKey(0) x_min = int(min(pt2d[0, :])) y_min = int(min(pt2d[1, :])) x_max = int(max(pt2d[0, :])) y_max = int(max(pt2d[1, :])) y_min = max(0, y_min - int(0.3 * (y_max - y_min))) # Extend face to top # Crop face bbox = (x_min, y_min, x_max, y_max) bbox_loosen, scale_x, scale_y = utils.get_loosen_bbox( bbox, img, (args.input_size, args.input_size)) crop = utils.crop_face_loosely(bbox, img, (args.input_size, args.input_size)) example["face_bbox"] = bbox_loosen # Adjust landmark points example["landmark"] = [] points = [] for l in range(len(landmark)): # Normalize landmark points x = float(landmark[l]['x'] - bbox_loosen[0]) * scale_x y = float(landmark[l]['y'] - bbox_loosen[1]) * scale_y x, y = utils.normalize_landmark_point( (x, y), (args.input_size, args.input_size)) example["landmark"].append([x, y])