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.

78 lines
2.6 KiB

4 years ago
  1. # USAGE
  2. # python recognize_faces_image.py --encodings encodings.pickle --image examples/example_01.png
  3. # import the necessary packages
  4. import face_recognition
  5. import argparse
  6. import pickle
  7. import cv2
  8. # construct the argument parser and parse the arguments
  9. ap = argparse.ArgumentParser()
  10. ap.add_argument("-e", "--encodings", required=True,
  11. help="path to serialized db of facial encodings")
  12. ap.add_argument("-i", "--image", required=True,
  13. help="path to input image")
  14. ap.add_argument("-d", "--detection-method", type=str, default="cnn",
  15. help="face detection model to use: either `hog` or `cnn`")
  16. args = vars(ap.parse_args())
  17. # load the known faces and embeddings
  18. print("[INFO] loading encodings...")
  19. data = pickle.loads(open(args["encodings"], "rb").read())
  20. # load the input image and convert it from BGR to RGB
  21. image = cv2.imread(args["image"])
  22. rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  23. # detect the (x, y)-coordinates of the bounding boxes corresponding
  24. # to each face in the input image, then compute the facial embeddings
  25. # for each face
  26. print("[INFO] recognizing faces...")
  27. boxes = face_recognition.face_locations(rgb,
  28. model=args["detection_method"])
  29. encodings = face_recognition.face_encodings(rgb, boxes)
  30. # initialize the list of names for each face detected
  31. names = []
  32. # loop over the facial embeddings
  33. for encoding in encodings:
  34. # attempt to match each face in the input image to our known
  35. # encodings
  36. matches = face_recognition.compare_faces(data["encodings"],
  37. encoding)
  38. name = "Unknown"
  39. # check to see if we have found a match
  40. if True in matches:
  41. # find the indexes of all matched faces then initialize a
  42. # dictionary to count the total number of times each face
  43. # was matched
  44. matchedIdxs = [i for (i, b) in enumerate(matches) if b]
  45. counts = {}
  46. # loop over the matched indexes and maintain a count for
  47. # each recognized face face
  48. for i in matchedIdxs:
  49. name = data["names"][i]
  50. counts[name] = counts.get(name, 0) + 1
  51. # determine the recognized face with the largest number of
  52. # votes (note: in the event of an unlikely tie Python will
  53. # select first entry in the dictionary)
  54. name = max(counts, key=counts.get)
  55. # update the list of names
  56. names.append(name)
  57. # loop over the recognized faces
  58. for ((top, right, bottom, left), name) in zip(boxes, names):
  59. # draw the predicted face name on the image
  60. cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)
  61. y = top - 15 if top - 15 > 15 else top + 15
  62. cv2.putText(image, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
  63. 0.75, (0, 255, 0), 2)
  64. # show the output image
  65. cv2.imshow("Image", image)
  66. cv2.waitKey(0)