Cannot crop frame based on face_location
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
- Domain
- computer-vision
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.

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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ageitgey/face_recognition
-
CLI image-folder regex missing end-anchor picks up non-image files (foo.jpg.bak, notes.jpegdoc) Open
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
ageitgey/face_recognition#1678 ·
-
Honest-Feedback Open
Difficulty 4/5 3-5 days Newbie friendliness 45/100
ageitgey/face_recognition#1669 · 3 comments ·
-
AiSoul Open
Difficulty 5/5 Over a week Newbie friendliness 20/100
ageitgey/face_recognition#1668 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 55/100
ageitgey/face_recognition#1666 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 76/100
ageitgey/face_recognition#1665 · 2 comments ·
All issues in ageitgey/face_recognition
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
bancolombia/sentinel#23 ·
-
test md OpenCI
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100