@@ -32,7 +32,14 @@ def scan_known_people(known_people_folder):
32
32
return known_names , known_face_encodings
33
33
34
34
35
- def test_image (image_to_check , known_names , known_face_encodings , tolerance = 0.6 ):
35
+ def print_result (filename , name , distance , show_distance = False ):
36
+ if show_distance :
37
+ print ("{},{},{}" .format (filename , name , distance ))
38
+ else :
39
+ print ("{},{}" .format (filename , name ))
40
+
41
+
42
+ def test_image (image_to_check , known_names , known_face_encodings , tolerance = 0.6 , show_distance = False ):
36
43
unknown_image = face_recognition .load_image_file (image_to_check )
37
44
38
45
# Scale down image if it's giant so things run a little faster
@@ -45,19 +52,20 @@ def test_image(image_to_check, known_names, known_face_encodings, tolerance=0.6)
45
52
unknown_encodings = face_recognition .face_encodings (unknown_image )
46
53
47
54
for unknown_encoding in unknown_encodings :
48
- result = face_recognition .compare_faces (known_face_encodings , unknown_encoding , tolerance = tolerance )
55
+ distances = face_recognition .face_distance (known_face_encodings , unknown_encoding )
56
+ result = list (distances <= tolerance )
49
57
50
58
if True in result :
51
- [print ( "{},{}" . format ( image_to_check , name )) for is_match , name in zip (result , known_names ) if is_match ]
59
+ [print_result ( image_to_check , name , distance , show_distance ) for is_match , name , distance in zip (result , known_names , distances ) if is_match ]
52
60
else :
53
- print ( "{}, unknown_person". format ( image_to_check ) )
61
+ print_result ( image_to_check , " unknown_person", None , show_distance )
54
62
55
63
56
64
def image_files_in_folder (folder ):
57
65
return [os .path .join (folder , f ) for f in os .listdir (folder ) if re .match (r'.*\.(jpg|jpeg|png)' , f , flags = re .I )]
58
66
59
67
60
- def process_images_in_process_pool (images_to_check , known_names , known_face_encodings , number_of_cpus , tolerance ):
68
+ def process_images_in_process_pool (images_to_check , known_names , known_face_encodings , number_of_cpus , tolerance , show_distance ):
61
69
if number_of_cpus == - 1 :
62
70
processes = None
63
71
else :
@@ -69,7 +77,14 @@ def process_images_in_process_pool(images_to_check, known_names, known_face_enco
69
77
context = multiprocessing .get_context ("forkserver" )
70
78
71
79
pool = context .Pool (processes = processes )
72
- function_parameters = zip (images_to_check , itertools .repeat (known_names ), itertools .repeat (known_face_encodings ), itertools .repeat (tolerance ))
80
+
81
+ function_parameters = zip (
82
+ images_to_check ,
83
+ itertools .repeat (known_names ),
84
+ itertools .repeat (known_face_encodings ),
85
+ itertools .repeat (tolerance ),
86
+ itertools .repeat (show_distance )
87
+ )
73
88
74
89
pool .starmap (test_image , function_parameters )
75
90
@@ -79,7 +94,8 @@ def process_images_in_process_pool(images_to_check, known_names, known_face_enco
79
94
@click .argument ('image_to_check' )
80
95
@click .option ('--cpus' , default = 1 , help = 'number of CPU cores to use in parallel (can speed up processing lots of images). -1 means "use all in system"' )
81
96
@click .option ('--tolerance' , default = 0.6 , help = 'Tolerance for face comparisons. Default is 0.6. Lower this if you get multiple matches for the same person.' )
82
- def main (known_people_folder , image_to_check , cpus , tolerance ):
97
+ @click .option ('--show-distance' , default = False , type = bool , help = 'Output face distance. Useful for tweaking tolerance setting.' )
98
+ def main (known_people_folder , image_to_check , cpus , tolerance , show_distance ):
83
99
known_names , known_face_encodings = scan_known_people (known_people_folder )
84
100
85
101
# Multi-core processing only supported on Python 3.4 or greater
@@ -89,11 +105,11 @@ def main(known_people_folder, image_to_check, cpus, tolerance):
89
105
90
106
if os .path .isdir (image_to_check ):
91
107
if cpus == 1 :
92
- [test_image (image_file , known_names , known_face_encodings , tolerance ) for image_file in image_files_in_folder (image_to_check )]
108
+ [test_image (image_file , known_names , known_face_encodings , tolerance , show_distance ) for image_file in image_files_in_folder (image_to_check )]
93
109
else :
94
- process_images_in_process_pool (image_files_in_folder (image_to_check ), known_names , known_face_encodings , cpus , tolerance )
110
+ process_images_in_process_pool (image_files_in_folder (image_to_check ), known_names , known_face_encodings , cpus , tolerance , show_distance )
95
111
else :
96
- test_image (image_to_check , known_names , known_face_encodings )
112
+ test_image (image_to_check , known_names , known_face_encodings , tolerance , show_distance )
97
113
98
114
99
115
if __name__ == "__main__" :
0 commit comments