You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python
  2. import sys
  3. import cv2
  4. import numpy as np
  5. import face_recognition
  6. def record(path, dev):
  7. def draw_face_rec(name, frame):
  8. rgb_frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
  9. framelocs = face_recognition.face_locations(rgb_frame)
  10. frameencs = face_recognition.face_encodings(rgb_frame, framelocs)
  11. for (top, right, bottom, left), frameenc in zip(framelocs, frameencs):
  12. cv2.rectangle(rgb_frame, (left, top), (right, bottom), (0, 0, 255), 2)
  13. cv2.imshow(name, rgb_frame)
  14. if len(frameencs) == 0:
  15. return None
  16. else:
  17. return [ frameencs[0] ]
  18. cap = cv2.VideoCapture(dev)
  19. avg = 128
  20. while True:
  21. ret, frame = cap.read()
  22. mean = cv2.mean(frame)[0]
  23. avg = (avg + mean) / 2
  24. if mean < avg:
  25. continue
  26. cv2.imshow("frame", frame)
  27. key = cv2.waitKey(1) & 0xFF
  28. if key == ord('q'):
  29. break
  30. elif key == ord('\r'):
  31. cv2.imshow("frame", frame)
  32. cv2.waitKey(1)
  33. encs = draw_face_rec("frame", frame)
  34. if encs == None:
  35. continue
  36. while True:
  37. key = cv2.waitKey(0) & 0xFF
  38. if key == ord('\r'):
  39. np.save(path, encs[0])
  40. return
  41. elif key == 27: # esc
  42. break
  43. if len(sys.argv) != 2:
  44. print(f"Usage: {sys.argv[0]} <path>")
  45. sys.exit(1)
  46. record(sys.argv[1], 2)