Skip to content

Commit 93d04bd

Browse files
committed
Add benchmark.py to examples
1 parent 06242f2 commit 93d04bd

6 files changed

+78
-0
lines changed

HISTORY.rst

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ History
66

77
* Added --show-distance to cli
88
* Fixed a bug where --tolerance was ignored in cli if testing a single image
9+
* Added benchmark.py to examples
910

1011

1112
0.2.1 (2017-06-03)

examples/benchmark.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import timeit
2+
3+
# Note: This example is only tested with Python 3 (not Python 2)
4+
5+
# This is a very simple benchmark to give you an idea of how fast each step of face recognition will run on your system.
6+
# Notice that face detection gets very slow at large image sizes. So you might consider running face detection on a
7+
# scaled down version of your image and then running face encodings on the the full size image.
8+
9+
TEST_IMAGES = [
10+
"obama-240p.jpg",
11+
"obama-480p.jpg",
12+
"obama-720p.jpg",
13+
"obama-1080p.jpg"
14+
]
15+
16+
17+
def run_test(setup, test, iterations_per_test=5, tests_to_run=10):
18+
fastest_execution = min(timeit.Timer(test, setup=setup).repeat(tests_to_run, iterations_per_test))
19+
execution_time = fastest_execution / iterations_per_test
20+
fps = 1.0 / execution_time
21+
return execution_time, fps
22+
23+
24+
setup_locate_faces = """
25+
import face_recognition
26+
27+
image = face_recognition.load_image_file("{}")
28+
"""
29+
30+
test_locate_faces = """
31+
face_locations = face_recognition.face_locations(image)
32+
"""
33+
34+
setup_face_landmarks = """
35+
import face_recognition
36+
37+
image = face_recognition.load_image_file("{}")
38+
face_locations = face_recognition.face_locations(image)
39+
"""
40+
41+
test_face_landmarks = """
42+
landmarks = face_recognition.face_landmarks(image, face_locations=face_locations)[0]
43+
"""
44+
45+
setup_encode_face = """
46+
import face_recognition
47+
48+
image = face_recognition.load_image_file("{}")
49+
face_locations = face_recognition.face_locations(image)
50+
"""
51+
52+
test_encode_face = """
53+
encoding = face_recognition.face_encodings(image, known_face_locations=face_locations)[0]
54+
"""
55+
56+
setup_end_to_end = """
57+
import face_recognition
58+
59+
image = face_recognition.load_image_file("{}")
60+
"""
61+
62+
test_end_to_end = """
63+
encoding = face_recognition.face_encodings(image)[0]
64+
"""
65+
66+
print("Benchmarks (Note: All benchmarks are only using a single CPU core)")
67+
print()
68+
69+
for image in TEST_IMAGES:
70+
size = image.split("-")[1].split(".")[0]
71+
print("Timings at {}:".format(size))
72+
73+
print(" - Face locations: {:.4f}s ({:.2f} fps)".format(*run_test(setup_locate_faces.format(image), test_locate_faces)))
74+
print(" - Face landmarks: {:.4f}s ({:.2f} fps)".format(*run_test(setup_face_landmarks.format(image), test_face_landmarks)))
75+
print(" - Encode face (inc. landmarks): {:.4f}s ({:.2f} fps)".format(*run_test(setup_encode_face.format(image), test_encode_face)))
76+
print(" - End-to-end: {:.4f}s ({:.2f} fps)".format(*run_test(setup_end_to_end.format(image), test_end_to_end)))
77+
print()

examples/obama-1080p.jpg

378 KB
Loading

examples/obama-240p.jpg

36.4 KB
Loading

examples/obama-480p.jpg

100 KB
Loading

examples/obama-720p.jpg

197 KB
Loading

0 commit comments

Comments
 (0)