print("Estimating pose...") person_pose_array, _ = pose_detector(img) res_img = cv2.addWeighted(img, 0.6, draw_person_pose(img, person_pose_array), 0.4, 0) # each person detected for person_pose in person_pose_array: unit_length = pose_detector.get_unit_length(person_pose) # face estimation print("Estimating face keypoints...") cropped_face_img, bbox = pose_detector.crop_face( img, person_pose, unit_length) if cropped_face_img is not None: face_keypoints = face_detector(cropped_face_img) res_img = draw_face_keypoints(res_img, face_keypoints, (bbox[0], bbox[1])) cv2.rectangle(res_img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (255, 255, 255), 1) # hands estimation print("Estimating hands keypoints...") hands = pose_detector.crop_hands(img, person_pose, unit_length) if hands["left"] is not None: hand_img = hands["left"]["img"] bbox = hands["left"]["bbox"] hand_keypoints = hand_detector(hand_img, hand_type="left") res_img = draw_hand_keypoints(res_img, hand_keypoints, (bbox[0], bbox[1])) cv2.rectangle(res_img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (255, 255, 255), 1)
# load model face_detector = FaceDetector("facenet", "models/facenet.npz", device=args.gpu) cascade = cv2.CascadeClassifier("models/haarcascade_frontalface_alt.xml") cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) while True: # get video frame ret, img = cap.read() if not ret: print("Failed to capture image") break # crop face gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) facerects = cascade.detectMultiScale(gray_img, scaleFactor=1.2, minNeighbors=3, minSize=(1, 1)) res_img = img.copy() if len(facerects) > 0: for facerect in facerects: cv2.rectangle(res_img, (facerect[0], facerect[1]), (facerect[0] + facerect[2], facerect[1] + facerect[3]), (255, 255, 255), 1) cropped_face, face_left_top = crop_face(img, facerect) face_keypoints = face_detector(cropped_face) res_img = draw_face_keypoints(res_img, face_keypoints, face_left_top) cv2.imshow("result", res_img) cv2.waitKey(1)
# load model cam_id = args.camera face_detector = FaceDetector("facenet", "models/facenet.npz", device=args.gpu) cascade = cv2.CascadeClassifier("models/haarcascade_frontalface_alt.xml") cap = cv2.VideoCapture(cam_id) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) while True: # get video frame ret, img = cap.read() if not ret: print("Failed to capture image") break # crop face gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) facerects = cascade.detectMultiScale(gray_img, scaleFactor=1.2, minNeighbors=3, minSize=(1, 1)) res_img = img.copy() if len(facerects) > 0: for facerect in facerects: cv2.rectangle(res_img, (facerect[0], facerect[1]), (facerect[0] + facerect[2], facerect[1] + facerect[3]), (255, 255, 255), 1) cropped_face, face_left_top = crop_face(img, facerect) face_keypoints = face_detector(cropped_face) res_img = draw_face_keypoints(res_img, face_keypoints, face_left_top) cv2.imshow("result", res_img) cv2.waitKey(1)
# inference print("Estimating pose...") person_pose_array, _ = pose_detector(img) res_img = cv2.addWeighted(img, 0.6, draw_person_pose(img, person_pose_array), 0.4, 0) # each person detected for person_pose in person_pose_array: unit_length = pose_detector.get_unit_length(person_pose) # face estimation print("Estimating face keypoints...") cropped_face_img, bbox = pose_detector.crop_face(img, person_pose, unit_length) if cropped_face_img is not None: face_keypoints = face_detector(cropped_face_img) res_img = draw_face_keypoints(res_img, face_keypoints, (bbox[0], bbox[1])) cv2.rectangle(res_img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (255, 255, 255), 1) # hands estimation print("Estimating hands keypoints...") hands = pose_detector.crop_hands(img, person_pose, unit_length) if hands["left"] is not None: hand_img = hands["left"]["img"] bbox = hands["left"]["bbox"] hand_keypoints = hand_detector(hand_img, hand_type="left") res_img = draw_hand_keypoints(res_img, hand_keypoints, (bbox[0], bbox[1])) cv2.rectangle(res_img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (255, 255, 255), 1) if hands["right"] is not None: hand_img = hands["right"]["img"] bbox = hands["right"]["bbox"]