Skip to content

Commit bd34891

Browse files
committed
Fix ageitgey#271 - convert BGR to RBG when using OpenCV VideoCapture
1 parent 7d3cec3 commit bd34891

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

examples/facerec_from_video_file.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@
4242
if not ret:
4343
break
4444

45+
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
46+
rgb_frame = frame[:, :, ::-1]
47+
4548
# Find all the faces and face encodings in the current frame of video
46-
face_locations = face_recognition.face_locations(frame)
47-
face_encodings = face_recognition.face_encodings(frame, face_locations)
49+
face_locations = face_recognition.face_locations(rgb_frame)
50+
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
4851

4952
face_names = []
5053
for face_encoding in face_encodings:

examples/facerec_from_webcam.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
# Grab a single frame of video
2020
ret, frame = video_capture.read()
2121

22+
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
23+
rgb_frame = frame[:, :, ::-1]
24+
2225
# Find all the faces and face enqcodings in the frame of video
23-
face_locations = face_recognition.face_locations(frame)
24-
face_encodings = face_recognition.face_encodings(frame, face_locations)
26+
face_locations = face_recognition.face_locations(rgb_frame)
27+
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
2528

2629
# Loop through each face in this frame of video
2730
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):

examples/facerec_from_webcam_faster.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030
# Resize frame of video to 1/4 size for faster face recognition processing
3131
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
3232

33+
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
34+
rgb_small_frame = small_frame[:, :, ::-1]
35+
3336
# Only process every other frame of video to save time
3437
if process_this_frame:
3538
# Find all the faces and face encodings in the current frame of video
36-
face_locations = face_recognition.face_locations(small_frame)
37-
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
39+
face_locations = face_recognition.face_locations(rgb_small_frame)
40+
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
3841

3942
face_names = []
4043
for face_encoding in face_encodings:

examples/find_faces_in_batches.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
if not ret:
2929
break
3030

31+
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
32+
frame = frame[:, :, ::-1]
33+
3134
# Save each frame of the video to a list
3235
frame_count += 1
3336
frames.append(frame)

0 commit comments

Comments
 (0)