-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognize.py
76 lines (64 loc) · 2.6 KB
/
recognize.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import itertools
import logging
import pathlib
from typing import List
import face_recognition
from config import IMAGE_EXTENSIONS
from util import g_path, logger
def filter_dir(dir):
yield from filter(lambda x: x.suffix in IMAGE_EXTENSIONS, g_path(dir).iterdir())
class FaceRec:
def __init__(self, gakki_dir):
self.logger = logging.getLogger(self.__class__.__name__)
self.known_faces = []
for file_path in filter_dir(gakki_dir): # type:pathlib.Path
try:
gakki_face_encoding = face_recognition.face_encodings(face_recognition.load_image_file(file_path))
self.logger.info("found:{:>3}, file_name:{}".format(len(gakki_face_encoding), file_path))
self.known_faces.extend(gakki_face_encoding[0:min(1, len(gakki_face_encoding))])
except Exception as e:
self.logger.exception(e)
continue
def recon(self, path):
"""
result compare result
:param path:
:return: is gakki: True, no face match: False, no face detected: 2
"""
for unknown_face in face_recognition.face_encodings(face_recognition.load_image_file(path)):
votes = face_recognition.compare_faces(self.known_faces, unknown_face, tolerance=0.45) # type: List
result = votes.count(True) > 0
self.logger.info("{}:{}".format(votes, result))
if result:
return result
else:
return 2
def select_gakki():
all_dir = "img"
gakki_dir = "gakki_img"
not_gakki_dir = "not_gakki_img"
no_face_dir = "no_face_detected"
fr = FaceRec("know")
clock = itertools.count()
for file_path in filter_dir(all_dir): # type:pathlib.Path
logging.info("recognizing {}: {}".format(next(clock), file_path))
ok_path = g_path(gakki_dir, file_path.name)
fail_path = g_path(not_gakki_dir, file_path.name)
no_face_path = g_path(no_face_dir, file_path.name)
if ok_path.exists() or fail_path.exists() or no_face_path.exists():
# already compared continue
continue
try:
result = fr.recon(file_path)
if result is True:
ok_path.symlink_to(file_path.absolute())
elif result == 2:
no_face_path.symlink_to(file_path.absolute())
else:
fail_path.symlink_to(file_path.absolute())
except Exception as e:
logger.exception(e)
continue
if __name__ == "__main__":
# FaceRec("know").recon("know/<strong>gakki<strong>853.jpeg")
select_gakki()