-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.py
82 lines (70 loc) · 2.95 KB
/
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
import time
import cv2
from detector.cv_face_detector.model import CVFaceDetector
from detector.insight_detector.model import InsightDetector
from models.m1.model import M1FaceAntiSpoofing
from models.m2.model import M2FaceAntiSpoofing
from models.m3.model import M3FaceAntiSpoofing
from models.m4.model import M4FaceAntiSpoofing
from models.m5.model import M5FaceAntiSpoofing
from models.m6.model import M6FaceAntiSpoofing
from models.m7.model import M7FaceAntiSpoofing
from models.m8.model import M8FaceAntiSpoofing
import os
face_detector = InsightDetector()
spoof_detectors = [M8FaceAntiSpoofing()]
benchmark_dir = "benchmarks"
is_face = False
for spoof_detector in spoof_detectors:
print("Start ----------------------------- ", type(spoof_detector))
total_time = 0
all_count = 0
correct_count = 0
errors = []
for class_name in ["fake", "real"]:
class_path = os.path.join(benchmark_dir, class_name)
for image_name in os.listdir(class_path):
image_path = os.path.join(class_path, image_name)
bgr = cv2.imread(image_path)
if bgr is None:
continue
if not is_face:
face_bboxes = face_detector.get_face_bboxes(bgr)
else:
face_bboxes = [[0, 0, bgr.shape[1], bgr.shape[0]]]
if len(face_bboxes) == 0:
print("No face found " + image_name + " in class " + class_name)
for box in face_bboxes:
bbox = box["box"]
start_time = time.time()
crop = bgr[bbox[1]:bbox[3], bbox[0]:bbox[2], :]
if "crop" in box:
crop = box["crop"]
#cv2.imshow("crop", crop)
#cv2.waitKey(0)
real_score = spoof_detector.get_real_score(bgr, bbox)
total_time += time.time() - start_time
print("Real score for image name " + image_name + " in class " + class_name + " is: ",
real_score)
if class_name == "fake":
if real_score < 0.5:
is_correct = True
else:
is_correct = False
errors.append(real_score)
else:
if real_score >= 0.5:
is_correct = True
else:
is_correct = False
errors.append(1 - real_score)
if is_correct:
correct_count += 1
all_count += 1
print("Correct prediction: ", is_correct)
print("--- Average time for each face: ", total_time / all_count, " seconds")
print("--- Total count: ", all_count)
print("--- Correct count: ", correct_count)
print("--- Accuracy: ", correct_count/all_count)
print("--- Average error: ", (sum(errors) / len(errors)) * 100, "%")
print("End ----------------------------- ", type(spoof_detector))