Skip to content

Commit 7e454d7

Browse files
committed
Add face_distance.py example
1 parent 3a9fca9 commit 7e454d7

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ All the examples are available [here](https://github.com/ageitgey/face_recogniti
227227
* [Recognize faces in live video using your webcam - Faster Version (Requires OpenCV to be installed)](https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py)
228228
* [Recognize faces on a Raspberry Pi w/ camera](https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_on_raspberry_pi.py)
229229
* [Run a web service to recognize faces via HTTP (Requires Flask to be installed)](https://github.com/ageitgey/face_recognition/blob/master/examples/web_service_example.py)
230+
* [Compare faces by numeric face distance instead of only True/False matches](https://github.com/ageitgey/face_recognition/blob/master/examples/face_distance.py)
230231

231232
## How Face Recognition Works
232233

examples/face_distance.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import face_recognition
2+
3+
# Often instead of just checking if two faces match or not (True or False), it's helpful to see how similar they are.
4+
# You can do that by using the face_distance function.
5+
6+
# The model was trained in a way that faces with a distance of 0.6 or less should be a match. But if you want to
7+
# be more strict, you can look for a smaller face distance. For example, using a 0.55 cutoff would reduce false
8+
# positive matches at the risk of more false negatives.
9+
10+
# Note: This isn't exactly the same as a "percent match". The scale isn't linear. But you can assume that images with a
11+
# smaller distance are more similar to each other than ones with a larger distance.
12+
13+
# Load some images to compare against
14+
known_obama_image = face_recognition.load_image_file("obama.jpg")
15+
known_biden_image = face_recognition.load_image_file("biden.jpg")
16+
17+
# Get the face encodings for the known images
18+
obama_face_encoding = face_recognition.face_encodings(known_obama_image)[0]
19+
biden_face_encoding = face_recognition.face_encodings(known_biden_image)[0]
20+
21+
known_encodings = [
22+
obama_face_encoding,
23+
biden_face_encoding
24+
]
25+
26+
# Load a test image and get encondings for it
27+
image_to_test = face_recognition.load_image_file("obama2.jpg")
28+
image_to_test_encoding = face_recognition.face_encodings(image_to_test)[0]
29+
30+
# See how far apart the test image is from the known faces
31+
face_distances = face_recognition.face_distance(known_encodings, image_to_test_encoding)
32+
33+
for i, face_distance in enumerate(face_distances):
34+
print("The test image has a distance of {:.2} from known image #{}".format(face_distance, i))
35+
print("- With a normal cutoff of 0.6, would the test image match the known image? {}".format(face_distance < 0.6))
36+
print("- With a very strict cutoff of 0.5, would the test image match the known image? {}".format(face_distance < 0.5))
37+
print()

0 commit comments

Comments
 (0)