-
Notifications
You must be signed in to change notification settings - Fork 1
/
faceencodings.py
55 lines (31 loc) · 1.26 KB
/
faceencodings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from imutils import paths
import face_recognition
import argparse
import pickle
import cv2
import os
ap = argparse.ArgumentParser()
ap.add_argument("-i","--dataset",required=True,help="path to input directory of face+images")
ap.add_argument("-e","--encodings",required=True,help="path to serialized db of facial encodings")
ap.add_argument("-d","--detection-method",type=str,default="hog",help="face detection model to use: either 'hog' or 'cnn' ")
args = vars(ap.parse_args())
print("Encoding faces........")
imagePaths = list(paths.list_images(args["dataset"]))
knownEncodings = []
knownNames = []
name="Unknown"
for (i,imagePath) in enumerate(imagePaths):
print("processing image {}/{} ".format(i+1,len(imagePaths)))
name = imagePath.split(os.path.sep)[-2]
image = cv2.imread(imagePath)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
boxes = face_recognition.face_locations(rgb,model=args["detection_method"])
encodings = face_recognition.face_encodings(rgb,boxes)
for encoding in encodings:
knownEncodings.append(encoding)
knownNames.append(name)
print("Serializing encodings.....")
data = {"encodings" : knownEncodings , "names" : knownNames}
f = open(args["encodings"], "wb")
f.write(pickle.dumps(data))
f.close