facerec_from_webcam.py 2.7 KB

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