From bd3489186877da093cdebaa0123141b6ebaf9ba2 Mon Sep 17 00:00:00 2001 From: Adam Geitgey Date: Fri, 29 Dec 2017 16:40:14 -0800 Subject: [PATCH] Fix #271 - convert BGR to RBG when using OpenCV VideoCapture --- examples/facerec_from_video_file.py | 7 +++++-- examples/facerec_from_webcam.py | 7 +++++-- examples/facerec_from_webcam_faster.py | 7 +++++-- examples/find_faces_in_batches.py | 3 +++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/examples/facerec_from_video_file.py b/examples/facerec_from_video_file.py index d143f30..4c1f4ee 100644 --- a/examples/facerec_from_video_file.py +++ b/examples/facerec_from_video_file.py @@ -42,9 +42,12 @@ while True: if not ret: break + # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses) + rgb_frame = frame[:, :, ::-1] + # Find all the faces and face encodings in the current frame of video - face_locations = face_recognition.face_locations(frame) - face_encodings = face_recognition.face_encodings(frame, face_locations) + face_locations = face_recognition.face_locations(rgb_frame) + face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) face_names = [] for face_encoding in face_encodings: diff --git a/examples/facerec_from_webcam.py b/examples/facerec_from_webcam.py index b5b2ded..8225e2b 100644 --- a/examples/facerec_from_webcam.py +++ b/examples/facerec_from_webcam.py @@ -19,9 +19,12 @@ while True: # Grab a single frame of video ret, frame = video_capture.read() + # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses) + rgb_frame = frame[:, :, ::-1] + # Find all the faces and face enqcodings in the frame of video - face_locations = face_recognition.face_locations(frame) - face_encodings = face_recognition.face_encodings(frame, face_locations) + face_locations = face_recognition.face_locations(rgb_frame) + face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) # Loop through each face in this frame of video for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): diff --git a/examples/facerec_from_webcam_faster.py b/examples/facerec_from_webcam_faster.py index acc7dea..3472358 100644 --- a/examples/facerec_from_webcam_faster.py +++ b/examples/facerec_from_webcam_faster.py @@ -30,11 +30,14 @@ while True: # Resize frame of video to 1/4 size for faster face recognition processing small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) + # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses) + rgb_small_frame = small_frame[:, :, ::-1] + # Only process every other frame of video to save time if process_this_frame: # Find all the faces and face encodings in the current frame of video - face_locations = face_recognition.face_locations(small_frame) - face_encodings = face_recognition.face_encodings(small_frame, face_locations) + face_locations = face_recognition.face_locations(rgb_small_frame) + face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations) face_names = [] for face_encoding in face_encodings: diff --git a/examples/find_faces_in_batches.py b/examples/find_faces_in_batches.py index e180024..dd30149 100644 --- a/examples/find_faces_in_batches.py +++ b/examples/find_faces_in_batches.py @@ -28,6 +28,9 @@ while video_capture.isOpened(): if not ret: break + # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses) + frame = frame[:, :, ::-1] + # Save each frame of the video to a list frame_count += 1 frames.append(frame) -- GitLab