Skip to content

Commit 23d2d65

Browse files
dedztbhageitgey
authored andcommitted
Use the best match for better accuracy in examples
1 parent f9f95ba commit 23d2d65

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

examples/facerec_from_webcam.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import face_recognition
22
import cv2
3+
import numpy as np
34

45
# This is a super simple (but slow) example of running face recognition on live video from your webcam.
56
# There's a second example that's a little more complicated but runs faster.
@@ -48,9 +49,15 @@
4849
name = "Unknown"
4950

5051
# If a match was found in known_face_encodings, just use the first one.
51-
if True in matches:
52-
first_match_index = matches.index(True)
53-
name = known_face_names[first_match_index]
52+
# if True in matches:
53+
# first_match_index = matches.index(True)
54+
# name = known_face_names[first_match_index]
55+
56+
# Or instead, use the known face with the smallest distance to the new face
57+
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
58+
best_match_index = np.argmin(face_distances)
59+
if matches[best_match_index]:
60+
name = known_face_names[best_match_index]
5461

5562
# Draw a box around the face
5663
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

examples/facerec_from_webcam_faster.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import face_recognition
22
import cv2
3+
import numpy as np
34

45
# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
56
# other example, but it includes some basic performance tweaks to make things run a lot faster:
@@ -59,10 +60,16 @@
5960
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
6061
name = "Unknown"
6162

62-
# If a match was found in known_face_encodings, just use the first one.
63-
if True in matches:
64-
first_match_index = matches.index(True)
65-
name = known_face_names[first_match_index]
63+
# # If a match was found in known_face_encodings, just use the first one.
64+
# if True in matches:
65+
# first_match_index = matches.index(True)
66+
# name = known_face_names[first_match_index]
67+
68+
# Or instead, use the known face with the smallest distance to the new face
69+
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
70+
best_match_index = np.argmin(face_distances)
71+
if matches[best_match_index]:
72+
name = known_face_names[best_match_index]
6673

6774
face_names.append(name)
6875

examples/identify_and_draw_boxes_on_faces.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import face_recognition
22
from PIL import Image, ImageDraw
3+
import numpy as np
34

45
# This is an example of running face recognition on a single image
56
# and drawing a box around each person that was identified.
@@ -43,9 +44,15 @@
4344
name = "Unknown"
4445

4546
# If a match was found in known_face_encodings, just use the first one.
46-
if True in matches:
47-
first_match_index = matches.index(True)
48-
name = known_face_names[first_match_index]
47+
# if True in matches:
48+
# first_match_index = matches.index(True)
49+
# name = known_face_names[first_match_index]
50+
51+
# Or instead, use the known face with the smallest distance to the new face
52+
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
53+
best_match_index = np.argmin(face_distances)
54+
if matches[best_match_index]:
55+
name = known_face_names[best_match_index]
4956

5057
# Draw a box around the face using the Pillow module
5158
draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))

0 commit comments

Comments
 (0)