Cannot crop frame based on face_location

Open
#579 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
opencv, python

Research direction

Start with the live-webcam example around face_recognition.face_locations, the frame slice, and cv2.imwrite, and run the reproduction against the reported versions. Compare the face_location values with the image dimensions and verify the saved crop can be opened; the issue is done when the detected face is saved as a non-empty image.

Written by the indexing model from the issue text.

Description

  • face_recognition version: 1.2.2 (most current)
  • Python version: 3.6.5
  • Operating System: MacOS
Description

When getting the location of faces in a live webcam feed I have been trying to crop the image to just display the detected face. However, when I used the rectangle coordinates to perform a crop on the image and save it to the disk, the jpg cannot be opened because it is empty.
screen shot 2018-08-05 at 10 06 58 pm
Here is a screenshot to show you that drawing a rectangle is working.

What I Did
import cv2
import face_recognition
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-d", "--detection-method", type=str, default="hog", help="face detection model to use: either `hog` (light) or `cnn` (heavy)")
args = vars(ap.parse_args())

cap = cv2.VideoCapture(0)

print("[WELCOME] Welcome to the face dataset creator!")
print("[INFO] Attempting to recognize face...")
faces_recognized = 0
while(True):
    ret, frame = cap.read()
    if not ret:
        print("[WARNING] Error retrieving frame from web-cam")
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    face_location_array = face_recognition.face_locations(rgb_frame, model=args['detection_method'])
    if face_location_array:     #  prevent manipulation of null variable
        y1 = face_location_array[0][0]
        x1 = face_location_array[0][1]
        end_coordinate_y = face_location_array[0][2]
        end_coordinate_x = face_location_array[0][3]
        faces_recognized += 1
        print("[%i] Face recognized..." % faces_recognized)
        cv2.rectangle(frame, (x1, y1), (end_coordinate_x, end_coordinate_y), (0, 0, 255), 2)
        cropped_face = frame[x1:end_coordinate_y, y1:end_coordinate_x]
        cv2.imwrite("faceimage.jpg", cropped_face)

    cv2.imshow('face detector', frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
Dominant language
Python
Stars
56.8k
Forks
13.7k
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ageitgey/face_recognition

All issues in ageitgey/face_recognition

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.