-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathvisual-test.py
95 lines (79 loc) · 2.77 KB
/
visual-test.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import matplotlib.pyplot as plt
from deepface import DeepFace
from deepface.commons.logger import Logger
logger = Logger()
# some models (e.g. Dlib) and detectors (e.g. retinaface) do not have test cases
# because they require to install huge packages
# this module is for local runs
model_names = [
"VGG-Face",
"Facenet",
"Facenet512",
"OpenFace",
"DeepFace",
"DeepID",
"Dlib",
"ArcFace",
"SFace",
]
detector_backends = [
"opencv",
"ssd",
"dlib",
"mtcnn",
# "mediapipe", # crashed in mac
"retinaface",
"yunet",
"yolov8",
]
# verification
for model_name in model_names:
obj = DeepFace.verify(
img1_path="dataset/img1.jpg", img2_path="dataset/img2.jpg", model_name=model_name
)
logger.info(obj)
logger.info("---------------------")
# represent
for model_name in model_names:
embedding_objs = DeepFace.represent(img_path="dataset/img1.jpg", model_name=model_name)
for embedding_obj in embedding_objs:
embedding = embedding_obj["embedding"]
logger.info(f"{model_name} produced {len(embedding)}D vector")
# find
dfs = DeepFace.find(
img_path="dataset/img1.jpg", db_path="dataset", model_name="Facenet", detector_backend="mtcnn"
)
for df in dfs:
logger.info(df)
# img_paths = ["dataset/img11.jpg", "dataset/img11_reflection.jpg", "dataset/couple.jpg"]
img_paths = ["dataset/img11.jpg"]
for img_path in img_paths:
# extract faces
for detector_backend in detector_backends:
face_objs = DeepFace.extract_faces(
img_path=img_path,
detector_backend=detector_backend,
align=True,
# expand_percentage=10,
# target_size=None,
)
for face_obj in face_objs:
face = face_obj["face"]
logger.info(detector_backend)
logger.info(face_obj["facial_area"])
logger.info(face_obj["confidence"])
# we know opencv sometimes cannot find eyes
if face_obj["facial_area"]["left_eye"] is not None:
assert isinstance(face_obj["facial_area"]["left_eye"], tuple)
assert isinstance(face_obj["facial_area"]["left_eye"][0], int)
assert isinstance(face_obj["facial_area"]["left_eye"][1], int)
if face_obj["facial_area"]["right_eye"] is not None:
assert isinstance(face_obj["facial_area"]["right_eye"], tuple)
assert isinstance(face_obj["facial_area"]["right_eye"][0], int)
assert isinstance(face_obj["facial_area"]["right_eye"][1], int)
assert isinstance(face_obj["confidence"], float)
assert face_obj["confidence"] <= 1
plt.imshow(face)
plt.axis("off")
plt.show()
logger.info("-----------")