verify_face.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. print(face_locations)
  56. no = len(face_locations)
  57. print("Number of faces detected: ", no)
  58. face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
  59. # Loop through each face in this frame of video
  60. for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
  61. start_time = time.perf_counter_ns()
  62. proba_list = clf.predict_proba([face_encoding])
  63. end_time = time.perf_counter_ns()
  64. process_time = end_time - start_time
  65. i = np.argmax(proba_list)
  66. proba = list(*proba_list)[i]
  67. name = dummy_data[i]["name"]
  68. print(name, "{:.2f}".format(proba), proba_list, process_time)
  69. # Draw a box around the face
  70. cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  71. # Draw a label with a name below the face
  72. cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
  73. if proba > threshold:
  74. font = cv2.FONT_HERSHEY_DUPLEX
  75. cv2.putText(frame, "{} {:.2f}".format(name, proba), (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
  76. # Display the resulting image
  77. cv2.imshow('Video', frame)
  78. # Hit 'q' on the keyboard to quit!
  79. if cv2.waitKey(1) & 0xFF == ord('q'):
  80. break
  81. # Release handle to the webcam
  82. video_capture.release()
  83. cv2.destroyAllWindows()