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.

131 lines
4.2 KiB

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