Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

benchmark.py 2.4 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...