facerec_from_webcam_faster.py 3.6 KB

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