verify_face_2.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import face_recognition
  2. import cv2
  3. import numpy as np
  4. import joblib
  5. import time
  6. import os
  7. clf = joblib.load('saved_model.pkl')
  8. classes = clf.classes_
  9. threshold = 0.65
  10. test_image = face_recognition.load_image_file(os.path.join('application_data', 'input_image', 'input_image2.jpg'))
  11. while True:
  12. rgb_frame = test_image[:, :, ::-1]
  13. # Find all the faces and face enqcodings in the frame of video
  14. face_locations = face_recognition.face_locations(rgb_frame)
  15. no = len(face_locations)
  16. print("Number of faces detected: ", no)
  17. face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
  18. # Loop through each face in this frame of video
  19. for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
  20. start_time = time.perf_counter_ns()
  21. proba_list = clf.predict_proba([face_encoding])
  22. end_time = time.perf_counter_ns()
  23. process_time = end_time - start_time
  24. i = np.argmax(proba_list)
  25. proba = list(*proba_list)[i]
  26. name = classes[i]
  27. print(name, "{:.2f}".format(proba), proba_list, process_time)
  28. # Draw a box around the face
  29. cv2.rectangle(test_image, (left, top), (right, bottom), (0, 0, 255), 2)
  30. # Draw a label with a name below the face
  31. cv2.rectangle(test_image, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
  32. if(proba > threshold):
  33. font = cv2.FONT_HERSHEY_DUPLEX
  34. cv2.putText(test_image, "{} {:.2f}".format(name, proba), (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
  35. # Display the resulting image
  36. cv2.imshow('Video', test_image)
  37. # Hit 'q' on the keyboard to quit!
  38. if cv2.waitKey(1) & 0xFF == ord('q'):
  39. break
  40. # Release handle to the webcam
  41. cv2.destroyAllWindows()