-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
73 lines (61 loc) · 2.58 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
import time
import cv2
from detector.cv_face_detector.model import CVFaceDetector
#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
import os
face_detector = CVFaceDetector()
spoof_detectors = [M7FaceAntiSpoofing()]
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 not is_face:
face_bboxes = face_detector.get_face_bboxes(bgr)
else:
face_bboxes = [[0, 0, bgr.shape[1], bgr.shape[0]]]
for bbox in face_bboxes:
start_time = time.time()
crop = bgr[bbox[1]:bbox[3], bbox[0]:bbox[2], :]
# cv2.imshow("crop", crop)
# cv2.waitKey(1)
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))