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.

facematcher.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python3
  2. import cv2
  3. import numpy as np
  4. import face_recognition
  5. import os
  6. import sys
  7. import time
  8. def match(path, cap):
  9. face = face_recognition.load_image_file(path)
  10. faceencs = face_recognition.face_encodings(face)
  11. if len(faceencs) == 0:
  12. print("Oh noes, bad face!")
  13. exit(1)
  14. faceenc = faceencs[0]
  15. matching = False
  16. while not matching:
  17. ret, frame = cap.read()
  18. if cv2.mean(frame)[0] < 30:
  19. continue
  20. scale = 0.25
  21. rgb_frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
  22. small_rgb_frame = cv2.resize(rgb_frame, (0, 0), fx=scale, fy=scale)
  23. framelocs = face_recognition.face_locations(small_rgb_frame)
  24. frameencs = face_recognition.face_encodings(small_rgb_frame, framelocs)
  25. # Loop through each face in this frame of video
  26. for (top, right, bottom, left), frameenc in zip(framelocs, frameencs):
  27. # See if the face is a match for the known face(s)
  28. dist = face_recognition.face_distance([faceenc], frameenc)[0]
  29. print("Distance: "+str(dist))
  30. cv2.rectangle(
  31. rgb_frame,
  32. (int(left / scale), int(top / scale)),
  33. (int(right / scale), int(bottom / scale)),
  34. (0, 0, 255), 2)
  35. # If a match was found in known_face_encodings, just use the first one.
  36. if dist <= 0.35:
  37. matching = True
  38. cv2.imshow("frame", rgb_frame)
  39. if cv2.waitKey(1) & 0xFF == ord('q'):
  40. break
  41. # When everything done, release the capture
  42. cap.release()
  43. cv2.destroyAllWindows()
  44. if matching:
  45. print("Matches")
  46. exit(0)
  47. else:
  48. exit(1)
  49. def record(path, cap):
  50. def draw_face_rec(name, frame):
  51. rgb_frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
  52. framelocs = face_recognition.face_locations(rgb_frame)
  53. frameencs = face_recognition.face_encodings(rgb_frame, framelocs)
  54. for (top, right, bottom, left), frameenc in zip(framelocs, frameencs):
  55. cv2.rectangle(rgb_frame, (left, top), (right, bottom), (0, 0, 255), 2)
  56. cv2.imshow(name, rgb_frame)
  57. while True:
  58. ret, frame = cap.read()
  59. if cv2.mean(frame)[0] < 30:
  60. continue
  61. cv2.imshow("frame", frame)
  62. key = cv2.waitKey(1) & 0xFF
  63. if key == ord('q'):
  64. break
  65. elif key == ord('\r'):
  66. cv2.imshow("frame", frame)
  67. cv2.waitKey(1)
  68. draw_face_rec("frame", frame)
  69. while True:
  70. key = cv2.waitKey(0) & 0xFF
  71. if key == ord('\r'):
  72. cv2.imwrite(path, frame)
  73. return
  74. elif key == 27: # esc
  75. break
  76. def usage(argv0):
  77. print("Usage: "+argv0+" match <path> [cam ID]")
  78. print(" "+argv0+" record <path> [cam ID]")
  79. if len(sys.argv) < 2:
  80. usage(sys.argv[0])
  81. exit(1)
  82. if sys.argv[1] == "match":
  83. if len(sys.argv) == 3:
  84. capid = 2
  85. elif len(sys.argv) == 4:
  86. capid = int(sys.argv[3])
  87. else:
  88. usage(sys.argv[0])
  89. exit(1)
  90. match(sys.argv[2], cv2.VideoCapture(capid))
  91. elif sys.argv[1] == "record":
  92. if len(sys.argv) == 3:
  93. capid = 2
  94. elif len(sys.argv) == 4:
  95. capid = int(sys.argv[3])
  96. else:
  97. usage(sys.argv[0])
  98. exit(1)
  99. record(sys.argv[2], cv2.VideoCapture(capid))
  100. else:
  101. usage(sys.argv[0])
  102. exit(1)