-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_face_verify.py
More file actions
164 lines (127 loc) · 5.14 KB
/
Copy pathtest_face_verify.py
File metadata and controls
164 lines (127 loc) · 5.14 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
----------------------------------------------------------------------
Authors: Stephan Nell
----------------------------------------------------------------------
Unit tests for the Face Verify
All the images used are from public domain and are copyright free
----------------------------------------------------------------------
"""
import pytest
import cv2
import os
from hutts_verification.verification.face_verify import FaceVerify
from hutts_verification.image_preprocessing.blur_manager import BlurManager
# Constants path to trained data for Shape Predictor.
SHAPE_PREDICTOR_PATH = "{base}/../../main/python/hutts_verification/image_preprocessing/trained_data/shape_predictor_face_landmarks.dat".format(
base=os.path.abspath(os.path.dirname(__file__)))
FACE_RECOGNITION_PATH = "{base}/../../main/python/hutts_verification/image_preprocessing/trained_data/dlib_face_recognition_resnet_model_v1.dat".format(
base=os.path.abspath(os.path.dirname(__file__)))
TEMPLATE_DIR = "{base_path}/../../main/python/hutts_verification/image_preprocessing/templates/".format(
base_path=os.path.abspath(os.path.dirname(__file__)))
test_image_colour = cv2.imread(TEMPLATE_DIR + "temp_flag.jpg")
thanks_obama = cv2.imread(TEMPLATE_DIR + "obama.jpg")
# Soap Joe official example for Fraud Detection for South African ID books
soap_joe = cv2.imread(TEMPLATE_DIR + "soapJoeExample.jpg")
def test_face_verify_constructor():
"""
Test to see if constructor without path passed
"""
with pytest.raises(TypeError):
FaceVerify()
def test_face_verify_constructor_2():
"""
Test to see if constructor with one path passed and other omitted
"""
with pytest.raises(TypeError):
FaceVerify(SHAPE_PREDICTOR_PATH)
def test_face_verify_constructor_3():
"""
Test to see if constructor one path and one incorrect type
"""
with pytest.raises(TypeError):
FaceVerify(SHAPE_PREDICTOR_PATH, 1)
def test_face_verify_constructor_4():
"""
Test to see if constructor one path and one incorrect type
"""
with pytest.raises(TypeError):
FaceVerify(1, FACE_RECOGNITION_PATH)
def test_face_verify_constructor_5():
"""
Test to see if constructor both paths incorrect
"""
with pytest.raises(TypeError):
FaceVerify(1, FACE_RECOGNITION_PATH)
def test_face_verify_constructor_6():
"""
Test to see if constructor both paths correct
"""
FaceVerify(SHAPE_PREDICTOR_PATH, FACE_RECOGNITION_PATH)
def test_face_verify():
"""
Test with no value provided
"""
face_verf = FaceVerify(SHAPE_PREDICTOR_PATH, FACE_RECOGNITION_PATH)
with pytest.raises(TypeError):
face_verf.verify()
def test_face_verify_2():
"""
Test with one correct value provided
"""
face_verf = FaceVerify(SHAPE_PREDICTOR_PATH, FACE_RECOGNITION_PATH)
with pytest.raises(TypeError):
face_verf.verify(test_image_colour)
def test_face_verify_3():
"""
Test with one correct value provided
"""
face_verf = FaceVerify(SHAPE_PREDICTOR_PATH, FACE_RECOGNITION_PATH)
with pytest.raises(TypeError):
face_verf.verify(test_image_colour)
def test_face_verify_4():
"""
Test with correct value provided but with no face
"""
face_verf = FaceVerify(SHAPE_PREDICTOR_PATH, FACE_RECOGNITION_PATH)
with pytest.raises(ValueError):
face_verf.verify(test_image_colour, test_image_colour)
def test_face_verify_5():
"""
Test with one correct value provided but only one face is correct
"""
face_verf = FaceVerify(SHAPE_PREDICTOR_PATH, FACE_RECOGNITION_PATH)
with pytest.raises(ValueError):
face_verf.verify(face1=thanks_obama, face2=test_image_colour)
def test_face_verify_6():
"""
Test with one correct value provided with both faces correct must be 100% since it is the same face
NOTE: these values may differ from Python-OpenCV from PyPi and building OpenCV from source
"""
face_verf = FaceVerify(SHAPE_PREDICTOR_PATH, FACE_RECOGNITION_PATH)
match, percentage = face_verf.verify(thanks_obama, thanks_obama)
assert match
assert percentage > 94
def test_face_verify_7():
"""
Provides different faces to verification module.
Face should return a low result percentage.
NOTE: these values may differ from Python-OpenCV from PyPi and building OpenCV from source
"""
face_verf = FaceVerify(SHAPE_PREDICTOR_PATH, FACE_RECOGNITION_PATH)
match, percentage = face_verf.verify(thanks_obama, soap_joe)
assert not match
assert percentage < 40
def test_face_verify_8():
"""
Test with the same face for both images.
One of the images will be slightly damage.
The damage test is to establish if the face verification can compensate for lower quality images.
NOTE: these values may differ from Python-OpenCV from PyPi and building OpenCV from source
"""
face_verf = FaceVerify(SHAPE_PREDICTOR_PATH, FACE_RECOGNITION_PATH)
blur_manager = BlurManager("median", [7])
damaged_obama = thanks_obama.copy()
damaged_obama = blur_manager.apply(damaged_obama)
match, percentage = face_verf.verify(thanks_obama, damaged_obama)
assert match
assert percentage > 85