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.py 3.1 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
  1. import face_recognition
  2. import cv2
  3. import numpy as np
  4. # This is a super simple (but slow) example of running face recognition on live video from your webcam.
  5. # There's a second example that's a little more complicated but runs faster.
  6. # PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
  7. # OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
  8. # specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
  9. # Get a reference to webcam #0 (the default one)
  10. video_capture = cv2.VideoCapture(0)
  11. # Load a sample picture and learn how to recognize it.
  12. obama_image = face_recognition.load_image_file("obama.jpg")
  13. obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
  14. # Load a second sample picture and learn how to recognize it.
  15. biden_image = face_recognition.load_image_file("biden.jpg")
  16. biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
  17. # Create arrays of known face encodings and their names
  18. known_face_encodings = [
  19. obama_face_encoding,
  20. biden_face_encoding
  21. ]
  22. known_face_names = [
  23. "Barack Obama",
  24. "Joe Biden"
  25. ]
  26. while True:
  27. # Grab a single frame of video
  28. ret, frame = video_capture.read()
  29. # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
  30. rgb_frame = frame[:, :, ::-1]
  31. # Find all the faces and face enqcodings in the frame of video
  32. face_locations = face_recognition.face_locations(rgb_frame)
  33. face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
  34. # Loop through each face in this frame of video
  35. for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
  36. # See if the face is a match for the known face(s)
  37. matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
  38. name = "Unknown"
  39. # If a match was found in known_face_encodings, just use the first one.
  40. # if True in matches:
  41. # first_match_index = matches.index(True)
  42. # name = known_face_names[first_match_index]
  43. # Or instead, use the known face with the smallest distance to the new face
  44. face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
  45. best_match_index = np.argmin(face_distances)
  46. if matches[best_match_index]:
  47. name = known_face_names[best_match_index]
  48. # Draw a box around the face
  49. cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  50. # Draw a label with a name below the face
  51. cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
  52. font = cv2.FONT_HERSHEY_DUPLEX
  53. cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
  54. # Display the resulting image
  55. cv2.imshow('Video', frame)
  56. # Hit 'q' on the keyboard to quit!
  57. if cv2.waitKey(1) & 0xFF == ord('q'):
  58. break
  59. # Release handle to the webcam
  60. video_capture.release()
  61. cv2.destroyAllWindows()
Tip!

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

Comments

Loading...