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

facerec_from_webcam_faster.py 3.9 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  1. import face_recognition
  2. import cv2
  3. import numpy as np
  4. # This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
  5. # other example, but it includes some basic performance tweaks to make things run a lot faster:
  6. # 1. Process each video frame at 1/4 resolution (though still display it at full resolution)
  7. # 2. Only detect faces in every other frame of video.
  8. # PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
  9. # OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
  10. # specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
  11. # Get a reference to webcam #0 (the default one)
  12. video_capture = cv2.VideoCapture(0)
  13. # Load a sample picture and learn how to recognize it.
  14. obama_image = face_recognition.load_image_file("obama.jpg")
  15. obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
  16. # Load a second sample picture and learn how to recognize it.
  17. biden_image = face_recognition.load_image_file("biden.jpg")
  18. biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
  19. # Create arrays of known face encodings and their names
  20. known_face_encodings = [
  21. obama_face_encoding,
  22. biden_face_encoding
  23. ]
  24. known_face_names = [
  25. "Barack Obama",
  26. "Joe Biden"
  27. ]
  28. # Initialize some variables
  29. face_locations = []
  30. face_encodings = []
  31. face_names = []
  32. process_this_frame = True
  33. while True:
  34. # Grab a single frame of video
  35. ret, frame = video_capture.read()
  36. # Resize frame of video to 1/4 size for faster face recognition processing
  37. small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
  38. # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
  39. rgb_small_frame = small_frame[:, :, ::-1]
  40. # Only process every other frame of video to save time
  41. if process_this_frame:
  42. # Find all the faces and face encodings in the current frame of video
  43. face_locations = face_recognition.face_locations(rgb_small_frame)
  44. face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
  45. face_names = []
  46. for face_encoding in face_encodings:
  47. # See if the face is a match for the known face(s)
  48. matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
  49. name = "Unknown"
  50. # # 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]
  54. # Or instead, use the known face with the smallest distance to the new face
  55. face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
  56. best_match_index = np.argmin(face_distances)
  57. if matches[best_match_index]:
  58. name = known_face_names[best_match_index]
  59. face_names.append(name)
  60. process_this_frame = not process_this_frame
  61. # Display the results
  62. for (top, right, bottom, left), name in zip(face_locations, face_names):
  63. # Scale back up face locations since the frame we detected in was scaled to 1/4 size
  64. top *= 4
  65. right *= 4
  66. bottom *= 4
  67. left *= 4
  68. # Draw a box around the face
  69. cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  70. # Draw a label with a name below the face
  71. cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
  72. font = cv2.FONT_HERSHEY_DUPLEX
  73. cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
  74. # Display the resulting image
  75. cv2.imshow('Video', frame)
  76. # Hit 'q' on the keyboard to quit!
  77. if cv2.waitKey(1) & 0xFF == ord('q'):
  78. break
  79. # Release handle to the webcam
  80. video_capture.release()
  81. cv2.destroyAllWindows()
Tip!

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

Comments

Loading...