Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def run_face_recognition(database):
- """
- Start the face recognition via the webcam
- """
- # Open a handler for the camera
- video_capture = cv2.VideoCapture(CAMERA_DEVICE_ID)
- # the face_recognitino library uses keys and values of your database separately
- known_face_encodings = list(database.values())
- known_face_names = list(database.keys())
- while video_capture.isOpened():
- # Grab a single frame of video (and check if it went ok)
- ok, frame = video_capture.read()
- if not ok:
- logging.error("Could not read frame from camera. Stopping video capture.")
- break
- # run detection and embedding models
- face_locations, face_encodings = get_face_embeddings_from_image(frame, convert_to_rgb=True)
- # Loop through each face in this frame of video and see if there's a match
- for location, face_encoding in zip(face_locations, face_encodings):
- # get the distances from this encoding to those of all reference images
- distances = face_recognition.face_distance(known_face_encodings, face_encoding)
- # select the closest match (smallest distance) if it's below the threshold value
- if np.any(distances <= MAX_DISTANCE):
- best_match_idx = np.argmin(distances)
- name = known_face_names[best_match_idx]
- else:
- name = None
- # put recognition info on the image
- paint_detected_face_on_image(frame, location, name)
- # Display the resulting image
- cv2.imshow('Video', frame)
- # Hit 'q' on the keyboard to quit!
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- # Release handle to the webcam
- video_capture.release()
- cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment