@@ -32,7 +32,7 @@ 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 ):
35
+ def test_image (image_to_check , known_names , known_face_encodings , tolerance = 0.6 ):
36
36
unknown_image = face_recognition .load_image_file (image_to_check )
37
37
38
38
# Scale down image if it's giant so things run a little faster
@@ -45,7 +45,7 @@ def test_image(image_to_check, known_names, known_face_encodings):
45
45
unknown_encodings = face_recognition .face_encodings (unknown_image )
46
46
47
47
for unknown_encoding in unknown_encodings :
48
- result = face_recognition .compare_faces (known_face_encodings , unknown_encoding )
48
+ result = face_recognition .compare_faces (known_face_encodings , unknown_encoding , tolerance = tolerance )
49
49
50
50
if True in result :
51
51
[print ("{},{}" .format (image_to_check , name )) for is_match , name in zip (result , known_names ) if is_match ]
@@ -57,7 +57,7 @@ def image_files_in_folder(folder):
57
57
return [os .path .join (folder , f ) for f in os .listdir (folder ) if re .match (r'.*\.(jpg|jpeg|png)' , f , flags = re .I )]
58
58
59
59
60
- def process_images_in_process_pool (images_to_check , known_names , known_face_encodings , number_of_cpus ):
60
+ def process_images_in_process_pool (images_to_check , known_names , known_face_encodings , number_of_cpus , tolerance ):
61
61
if number_of_cpus == - 1 :
62
62
processes = None
63
63
else :
@@ -69,7 +69,7 @@ def process_images_in_process_pool(images_to_check, known_names, known_face_enco
69
69
context = multiprocessing .get_context ("forkserver" )
70
70
71
71
pool = context .Pool (processes = processes )
72
- function_parameters = zip (images_to_check , itertools .repeat (known_names ), itertools .repeat (known_face_encodings ))
72
+ function_parameters = zip (images_to_check , itertools .repeat (known_names ), itertools .repeat (known_face_encodings ), itertools . repeat ( tolerance ) )
73
73
74
74
pool .starmap (test_image , function_parameters )
75
75
@@ -78,7 +78,8 @@ def process_images_in_process_pool(images_to_check, known_names, known_face_enco
78
78
@click .argument ('known_people_folder' )
79
79
@click .argument ('image_to_check' )
80
80
@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
- def main (known_people_folder , image_to_check , cpus ):
81
+ @click .option ('--tolerance' , default = 0.6 , help = 'Tolerance to use for face comparisons. Default is 0.6 and lower is more strict matching. Lower this if you get multiple matches for the same person. Try 0.54 for example.' )
82
+ def main (known_people_folder , image_to_check , cpus , tolerance ):
82
83
known_names , known_face_encodings = scan_known_people (known_people_folder )
83
84
84
85
# Multi-core processing only supported on Python 3.4 or greater
@@ -88,9 +89,9 @@ def main(known_people_folder, image_to_check, cpus):
88
89
89
90
if os .path .isdir (image_to_check ):
90
91
if cpus == 1 :
91
- [test_image (image_file , known_names , known_face_encodings ) for image_file in image_files_in_folder (image_to_check )]
92
+ [test_image (image_file , known_names , known_face_encodings , tolerance ) for image_file in image_files_in_folder (image_to_check )]
92
93
else :
93
- process_images_in_process_pool (image_files_in_folder (image_to_check ), known_names , known_face_encodings , cpus )
94
+ process_images_in_process_pool (image_files_in_folder (image_to_check ), known_names , known_face_encodings , cpus , tolerance )
94
95
else :
95
96
test_image (image_to_check , known_names , known_face_encodings )
96
97
0 commit comments