1
+ #How to call example
2
+ #python get_face_vector.py -i test2.bmp
3
+
4
+ import optparse
5
+ import dlib
6
+ import pickle
7
+ from skimage import io
8
+ detector = dlib .get_frontal_face_detector ()
9
+
10
+ def getAllFaceBoundingBoxes (rgbImg ):
11
+ assert rgbImg is not None
12
+ try :
13
+ return detector (rgbImg , 1 )
14
+ except Exception as e :
15
+ print ("Warning: {}" .format (e ))
16
+ return []
17
+
18
+ if __name__ == '__main__' :
19
+ parser = optparse .OptionParser ()
20
+ parser .add_option ("-i" , "--image" , dest = "image" , default = 'test.bmp' )
21
+ sp = dlib .shape_predictor ('shape_predictor_5_face_landmarks.dat' )
22
+ facerec = dlib .face_recognition_model_v1 ('dlib_face_recognition_resnet_model_v1.dat' )
23
+ options , _ = parser .parse_args ()
24
+ f = options .image
25
+ img = io .imread (f )
26
+ dets = getAllFaceBoundingBoxes (img )
27
+ # print("Number of faces detected: {}".format(len(dets)))
28
+ # for i, d in enumerate(dets):
29
+ # shape = sp(img, d)
30
+ # v = facerec.compute_face_descriptor(img, shape);
31
+
32
+
33
+ faces = dlib .full_object_detections ()
34
+ for detection in dets :
35
+ faces .append (sp (img , detection ))
36
+
37
+ images = dlib .get_face_chips (img , faces , size = 150 , padding = 0.25 )
38
+
39
+ img = images [0 ]
40
+ dets = getAllFaceBoundingBoxes (img )
41
+ for i , d in enumerate (dets ):
42
+ shape = sp (img , d )
43
+ v = facerec .compute_face_descriptor (img , shape );
44
+
45
+ with open ('face.vec' , 'wb' ) as handle :
46
+ pickle .dump (v , handle )
0 commit comments