Advertisement
Guest User

Untitled

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