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.

132 lines
4.2 KiB

4 years ago
  1. #!/usr/bin/env python3
  2. # USAGE
  3. # python recognize_faces_video.py --encodings encodings.pickle
  4. # python recognize_faces_video.py --encodings encodings.pickle --output output/jurassic_park_trailer_output.avi --display 0
  5. # import the necessary packages
  6. from imutils.video import VideoStream
  7. import face_recognition
  8. import argparse
  9. import imutils
  10. import pickle
  11. import time
  12. import cv2
  13. # construct the argument parser and parse the arguments
  14. ap = argparse.ArgumentParser()
  15. ap.add_argument("-e", "--encodings", required=True,
  16. help="path to serialized db of facial encodings")
  17. ap.add_argument("-o", "--output", type=str,
  18. help="path to output video")
  19. ap.add_argument("-y", "--display", type=int, default=1,
  20. help="whether or not to display output frame to screen")
  21. ap.add_argument("-d", "--detection-method", type=str, default="cnn",
  22. help="face detection model to use: either `hog` or `cnn`")
  23. args = vars(ap.parse_args())
  24. # load the known faces and embeddings
  25. print("[INFO] loading encodings...")
  26. data = pickle.loads(open(args["encodings"], "rb").read())
  27. # initialize the video stream and pointer to output video file, then
  28. # allow the camera sensor to warm up
  29. print("[INFO] starting video stream...")
  30. vs = VideoStream(src=0).start()
  31. writer = None
  32. time.sleep(2.0)
  33. # loop over frames from the video file stream
  34. while True:
  35. try:
  36. # grab the frame from the threaded video stream
  37. frame = vs.read()
  38. # convert the input frame from BGR to RGB then resize it to have
  39. # a width of 750px (to speedup processing)
  40. rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  41. rgb = imutils.resize(frame, width=750)
  42. r = frame.shape[1] / float(rgb.shape[1])
  43. # detect the (x, y)-coordinates of the bounding boxes
  44. # corresponding to each face in the input frame, then compute
  45. # the facial embeddings for each face
  46. boxes = face_recognition.face_locations(rgb,
  47. model=args["detection_method"])
  48. encodings = face_recognition.face_encodings(rgb, boxes)
  49. names = []
  50. # loop over the facial embeddings
  51. for encoding in encodings:
  52. # attempt to match each face in the input image to our known
  53. # encodings
  54. matches = face_recognition.compare_faces(data["encodings"],
  55. encoding)
  56. name = "Unknown"
  57. # check to see if we have found a match
  58. if True in matches:
  59. # find the indexes of all matched faces then initialize a
  60. # dictionary to count the total number of times each face
  61. # was matched
  62. matchedIdxs = [i for (i, b) in enumerate(matches) if b]
  63. counts = {}
  64. # loop over the matched indexes and maintain a count for
  65. # each recognized face face
  66. for i in matchedIdxs:
  67. name = data["names"][i]
  68. counts[name] = counts.get(name, 0) + 1
  69. # determine the recognized face with the largest number
  70. # of votes (note: in the event of an unlikely tie Python
  71. # will select first entry in the dictionary)
  72. name = max(counts, key=counts.get)
  73. # update the list of names
  74. names.append(name)
  75. # loop over the recognized faces
  76. for ((top, right, bottom, left), name) in zip(boxes, names):
  77. # rescale the face coordinates
  78. top = int(top * r)
  79. right = int(right * r)
  80. bottom = int(bottom * r)
  81. left = int(left * r)
  82. # draw the predicted face name on the image
  83. cv2.rectangle(frame, (left, top), (right, bottom),
  84. (0, 255, 0), 2)
  85. y = top - 15 if top - 15 > 15 else top + 15
  86. cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
  87. 0.75, (0, 255, 0), 2)
  88. # if the video writer is None *AND* we are supposed to write
  89. # the output video to disk initialize the writer
  90. if writer is None and args["output"] is not None:
  91. fourcc = cv2.VideoWriter_fourcc(*"MJPG")
  92. writer = cv2.VideoWriter(args["output"], fourcc, 20,
  93. (frame.shape[1], frame.shape[0]), True)
  94. # if the writer is not None, write the frame with recognized
  95. # faces t odisk
  96. if writer is not None:
  97. writer.write(frame)
  98. # check to see if we are supposed to display the output frame to
  99. # the screen
  100. if args["display"] > 0:
  101. cv2.imshow("Frame", frame)
  102. key = cv2.waitKey(1) & 0xFF
  103. # if the `q` key was pressed, break from the loop
  104. if key == ord("q"):
  105. break
  106. except KeyboardInterrupt:
  107. break
  108. # do a bit of cleanup
  109. cv2.destroyAllWindows()
  110. vs.stop()
  111. # check to see if the video writer point needs to be released
  112. if writer is not None:
  113. writer.release()