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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python3
  2. import cv2
  3. import numpy as np
  4. import face_recognition
  5. import os
  6. import time
  7. import argparse
  8. def match(paths, cap, show):
  9. faces = []
  10. faceencs = []
  11. for path in paths:
  12. face = face_recognition.load_image_file(path)
  13. faces.append(face)
  14. encs = face_recognition.face_encodings(face)
  15. if len(encs) == 0:
  16. print("Warning: "+path+" has no face!")
  17. continue
  18. faceencs.append(encs[0])
  19. if len(faceencs) == 0:
  20. print("Warning: No valid faces!")
  21. matching = False
  22. while not matching:
  23. ret, frame = cap.read()
  24. if cv2.mean(frame)[0] < 30:
  25. continue
  26. scale = 0.25
  27. rgb_frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
  28. small_rgb_frame = cv2.resize(rgb_frame, (0, 0), fx=scale, fy=scale)
  29. framelocs = face_recognition.face_locations(small_rgb_frame)
  30. frameencs = face_recognition.face_encodings(small_rgb_frame, framelocs)
  31. # Loop through each face in this frame of video
  32. for (top, right, bottom, left), frameenc in zip(framelocs, frameencs):
  33. # See if the face is a match for the known face(s)
  34. dists = face_recognition.face_distance(faceencs, frameenc)
  35. dist = dists[0]
  36. for d in dists:
  37. if d < dist:
  38. dist = d
  39. print("Distance: "+str(dist))
  40. if show:
  41. cv2.rectangle(
  42. rgb_frame,
  43. (int(left / scale), int(top / scale)),
  44. (int(right / scale), int(bottom / scale)),
  45. (0, 0, 255), 2)
  46. # If a match was found in known_face_encodings, just use the first one.
  47. if dist <= 0.4:
  48. matching = True
  49. if show:
  50. cv2.imshow("frame", rgb_frame)
  51. if cv2.waitKey(1) & 0xFF == ord('q'):
  52. break
  53. # When everything done, release the capture
  54. cap.release()
  55. cv2.destroyAllWindows()
  56. if matching:
  57. print("Matches")
  58. exit(0)
  59. else:
  60. exit(1)
  61. def record(path, cap):
  62. def draw_face_rec(name, frame):
  63. rgb_frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
  64. framelocs = face_recognition.face_locations(rgb_frame)
  65. frameencs = face_recognition.face_encodings(rgb_frame, framelocs)
  66. for (top, right, bottom, left), frameenc in zip(framelocs, frameencs):
  67. cv2.rectangle(rgb_frame, (left, top), (right, bottom), (0, 0, 255), 2)
  68. cv2.imshow(name, rgb_frame)
  69. while True:
  70. ret, frame = cap.read()
  71. if cv2.mean(frame)[0] < 30:
  72. continue
  73. cv2.imshow("frame", frame)
  74. key = cv2.waitKey(1) & 0xFF
  75. if key == ord('q'):
  76. break
  77. elif key == ord('\r'):
  78. cv2.imshow("frame", frame)
  79. cv2.waitKey(1)
  80. draw_face_rec("frame", frame)
  81. while True:
  82. key = cv2.waitKey(0) & 0xFF
  83. if key == ord('\r'):
  84. cv2.imwrite(path, frame)
  85. return
  86. elif key == 27: # esc
  87. break
  88. parser = argparse.ArgumentParser()
  89. subs = parser.add_subparsers(dest="command", required=True)
  90. sub_match = subs.add_parser("match")
  91. sub_match.add_argument(
  92. "-d", "--device", type=int, default=0,
  93. help="the index of the video device")
  94. sub_match.add_argument(
  95. "-s", "--show", default=False, action="store_true",
  96. help="show what the camera sees")
  97. sub_match.add_argument(
  98. "faces", type=str, nargs="+",
  99. help="the source image file(s)")
  100. sub_record = subs.add_parser("record")
  101. sub_record.add_argument(
  102. "-d", "--device", type=int, default=0,
  103. help="the index of the video device")
  104. sub_record.add_argument(
  105. "face", type=str,
  106. help="the destination image file")
  107. args = parser.parse_args()
  108. if args.command == "match":
  109. match(args.faces, cv2.VideoCapture(args.device), args.show)
  110. elif args.command == "record":
  111. record(args.face, cv2.VideoCapture(args.device))