Advertisement
Guest User

Untitled

a guest
Apr 13th, 2019
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. # USAGE
  2. # When encoding on laptop, desktop, or GPU (slower, more accurate):
  3. # python encode_faces.py --dataset dataset --encodings encodings.pickle --detection-method cnn
  4. # When encoding on Raspberry Pi (faster, more accurate):
  5. # python encode_faces.py --dataset dataset --encodings encodings.pickle --detection-method hog
  6.  
  7. # import the necessary packages
  8. from imutils import paths
  9. import face_recognition
  10. import argparse
  11. import pickle
  12. import cv2
  13. import os
  14.  
  15. # construct the argument parser and parse the arguments
  16. parser  = argparse.ArgumentParser()
  17. ap = parser.add_argument_group("required group")
  18. ap.add_argument("-i", "--dataset",required = True,
  19.     help="path to input directory of faces + images")
  20. ap.add_argument("-e", "--encodings",required = True,
  21.     help="path to serialized db of facial encodings")
  22. ap.add_argument("-d", "--detection-method", type=str, default="hog",
  23.     help="face detection model to use: either `hog` or `cnn`")
  24. args = vars(parser.parse_args([]))
  25.  
  26. # grab the paths to the input images in our dataset
  27. print("[INFO] quantifying faces...")
  28. imagePaths = list(paths.list_images(args["dataset"]))
  29.  
  30. # initialize the list of known encodings and known names
  31. knownEncodings = []
  32. knownNames = []
  33.  
  34. # loop over the image paths
  35. for (i, imagePath) in enumerate(imagePaths):
  36.     # extract the person name from the image path
  37.     print("[INFO] processing image {}/{}".format(i + 1,
  38.         len(imagePaths)))
  39.     name = imagePath.split(os.path.sep)[-2]
  40.  
  41.     # load the input image and convert it from RGB (OpenCV ordering)
  42.     # to dlib ordering (RGB)
  43.     image = cv2.imread(imagePath)
  44.     rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  45.  
  46.     # detect the (x, y)-coordinates of the bounding boxes
  47.     # corresponding to each face in the input image
  48.     boxes = face_recognition.face_locations(rgb,
  49.         model=args["detection_method"])
  50.  
  51.     # compute the facial embedding for the face
  52.     encodings = face_recognition.face_encodings(rgb, boxes)
  53.  
  54.     # loop over the encodings
  55.     for encoding in encodings:
  56.         # add each encoding + name to our set of known names and
  57.         # encodings
  58.         knownEncodings.append(encoding)
  59.         knownNames.append(name)
  60.  
  61. # dump the facial encodings + names to disk
  62. print("[INFO] serializing encodings...")
  63. data = {"encodings": knownEncodings, "names": knownNames}
  64. f = open(args["encodings"], "wb")
  65. f.write(pickle.dumps(data))
  66. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement