-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsave_known_face.py
48 lines (38 loc) · 1.9 KB
/
save_known_face.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
# This python script can be used to save new face in face_encodings dir.
# If you want to save a new face encoding
# python save_known_face.py -f image.jpg -n "ibrahim"
# OR
# python save_known_face.py -f image.png -n "ibrahim"
import cv2
import numpy as np
import dlib
import argparse
def main(file, person_name, detect_faces, predictor):
# Grab a single frame from WebCam
frame = cv2.imread(file)
# Find all the faces and face enqcodings in the frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detect_faces.detectMultiScale(gray, 1.3,5)
face_locations = []
for (x,y,w,h) in faces:
face_locations.append(dlib.rectangle(x, y, x+w, y+h))
# If there is atleast 1 face then do face recognition
if (len(face_locations) > 0 and len(face_locations) < 2):
for face_location in face_locations:
face_encoding = predictor(frame, face_location)
face_descriptor = face_rec.compute_face_descriptor(frame, face_encoding, 1)
# Save encoding in a numpy file
np.save("face_encodings/"+person_name, np.array(face_descriptor))
else:
print("Either no face or more then one face detected. Please check the image file again")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", type=str, help="give file for to extract face from it", required=True)
parser.add_argument("-n", "--name", type=str, help="give name for the person", required=True)
args = parser.parse_args()
file = args.file
person_name = args.name
detect_faces = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
face_rec = dlib.face_recognition_model_v1('dlib_face_recognition_resnet_model_v1.dat')
main(file, person_name, detect_faces, predictor)