Guest User

Untitled

a guest
Jul 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. def run_face_recognition(database):
  2. """
  3. Start the face recognition via the webcam
  4. """
  5. # Open a handler for the camera
  6. video_capture = cv2.VideoCapture(CAMERA_DEVICE_ID)
  7.  
  8. # the face_recognitino library uses keys and values of your database separately
  9. known_face_encodings = list(database.values())
  10. known_face_names = list(database.keys())
  11.  
  12. while video_capture.isOpened():
  13. # Grab a single frame of video (and check if it went ok)
  14. ok, frame = video_capture.read()
  15. if not ok:
  16. logging.error("Could not read frame from camera. Stopping video capture.")
  17. break
  18.  
  19. # run detection and embedding models
  20. face_locations, face_encodings = get_face_embeddings_from_image(frame, convert_to_rgb=True)
  21.  
  22. # Loop through each face in this frame of video and see if there's a match
  23. for location, face_encoding in zip(face_locations, face_encodings):
  24.  
  25. # get the distances from this encoding to those of all reference images
  26. distances = face_recognition.face_distance(known_face_encodings, face_encoding)
  27.  
  28. # select the closest match (smallest distance) if it's below the threshold value
  29. if np.any(distances <= MAX_DISTANCE):
  30. best_match_idx = np.argmin(distances)
  31. name = known_face_names[best_match_idx]
  32. else:
  33. name = None
  34.  
  35. # put recognition info on the image
  36. paint_detected_face_on_image(frame, location, name)
  37.  
  38. # Display the resulting image
  39. cv2.imshow('Video', frame)
  40.  
  41. # Hit 'q' on the keyboard to quit!
  42. if cv2.waitKey(1) & 0xFF == ord('q'):
  43. break
  44.  
  45. # Release handle to the webcam
  46. video_capture.release()
  47. cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment