verify_face.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import face_recognition
  2. import cv2
  3. import numpy as np
  4. import joblib
  5. import time
  6. # This is a super simple (but slow) example of running face recognition on live video from your webcam.
  7. # There's a second example that's a little more complicatedq but runs faster.
  8. # PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
  9. # OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
  10. # specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
  11. # Get a reference to webcam #0 (the default one)
  12. video_capture = cv2.VideoCapture(0)
  13. clf = joblib.load('saved_model_2.pkl')
  14. classes = clf.classes_
  15. threshold = 0.9
  16. dummy_data = [
  17. {
  18. "name": "Bayu",
  19. "address": "299 St Louis Road Oak Forest, IL 60452",
  20. "nik": "1000076456784631"
  21. },
  22. {
  23. "name": "Dio",
  24. "address": "22 Whitemarsh St. Mansfield, MA 02048",
  25. "nik": "1000024792887549"
  26. },
  27. {
  28. "name": "Hadi",
  29. "address": "643 Honey Creek Dr. Milledgeville, GA 31061",
  30. "nik": "1000038502830420"
  31. },
  32. {
  33. "name": "Kevin",
  34. "address": "881 Cooper Ave. Hummelstown, PA 17036",
  35. "nik": "1000045356476664"
  36. },
  37. {
  38. "name": "Matrix",
  39. "address": "580 Glenwood Dr. Garner, NC 27529",
  40. "nik": "1000023452134598"
  41. },
  42. {
  43. "name": "Surya",
  44. "address": "909 South St Paul Street Hopewell, VA 23860",
  45. "nik": "1000075656784734"
  46. },
  47. ]
  48. while True:
  49. # Grab a single frame of video
  50. ret, frame = video_capture.read()
  51. # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
  52. rgb_frame = frame[:, :, ::-1]
  53. # Find all the faces and face enqcodings in the frame of video
  54. face_locations = face_recognition.face_locations(rgb_frame)
  55. no = len(face_locations)
  56. print("Number of faces detected: ", no)
  57. face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
  58. # Loop through each face in this frame of video
  59. for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
  60. start_time = time.perf_counter_ns()
  61. proba_list = clf.predict_proba([face_encoding])
  62. end_time = time.perf_counter_ns()
  63. process_time = end_time - start_time
  64. i = np.argmax(proba_list)
  65. proba = list(*proba_list)[i]
  66. name = dummy_data[i]["name"]
  67. print(name, "{:.2f}".format(proba), proba_list, process_time)
  68. # Draw a box around the face
  69. cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  70. # Draw a label with a name below the face
  71. cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
  72. if proba > threshold:
  73. font = cv2.FONT_HERSHEY_DUPLEX
  74. cv2.putText(frame, "{} {:.2f}".format(name, proba), (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
  75. # Display the resulting image
  76. cv2.imshow('Video', frame)
  77. # Hit 'q' on the keyboard to quit!
  78. if cv2.waitKey(1) & 0xFF == ord('q'):
  79. break
  80. # Release handle to the webcam
  81. video_capture.release()
  82. cv2.destroyAllWindows()