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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# standard imports
import contextlib
import io
import os
import unittest
# external imports
import requests
import PIL.Image
# bsie imports
from bsie.utils import errors
# objects to test
from bsie.reader.face import FaceExtract
## code ##
def fetch(source, target):
target = os.path.join(os.path.dirname(__file__), target)
if not os.path.exists(target):
with open(target, 'wb') as ofile:
ans = requests.get(source)
ofile.write(ans.content)
class TestFaceExtract(unittest.TestCase):
def setUp(self):
# download test image w/o face
fetch('https://www.bsfs.io/testdata/Quiejoore1ahxa9jahma.jpg', 'faces-noface.jpg')
# download test image w/ face
fetch('https://www.bsfs.io/testdata/ONekai7Ohphooch3aege.jpg', 'faces-ivan.jpg')
def test_essentials(self):
# repr respects min_face_prob
self.assertEqual(repr(FaceExtract(min_face_prob=1.0)), 'FaceExtract(1.0)')
self.assertEqual(repr(FaceExtract(min_face_prob=0.5)), 'FaceExtract(0.5)')
# repr respects type
class Foo(FaceExtract): pass
self.assertEqual(repr(Foo(min_face_prob=0.5)), 'Foo(0.5)')
# comparison respects type
class Foo(): pass
self.assertNotEqual(FaceExtract(), 1234)
self.assertNotEqual(hash(FaceExtract()), hash(1234))
self.assertNotEqual(FaceExtract(), 'hello')
self.assertNotEqual(hash(FaceExtract()), hash('hello'))
self.assertNotEqual(FaceExtract(), Foo())
self.assertNotEqual(hash(FaceExtract()), hash(Foo()))
# comparison respects constructor arguments (except cuda_device)
self.assertEqual(FaceExtract(), FaceExtract())
self.assertEqual(hash(FaceExtract()), hash(FaceExtract()))
self.assertNotEqual(FaceExtract(), FaceExtract(target_size=10))
self.assertNotEqual(hash(FaceExtract()), hash(FaceExtract(target_size=10)))
self.assertNotEqual(FaceExtract(), FaceExtract(min_face_size=10))
self.assertNotEqual(hash(FaceExtract()), hash(FaceExtract(min_face_size=10)))
self.assertNotEqual(FaceExtract(), FaceExtract(min_face_prob=1.))
self.assertNotEqual(hash(FaceExtract()), hash(FaceExtract(min_face_prob=1.)))
self.assertNotEqual(FaceExtract(), FaceExtract(ext_face_size=100))
self.assertNotEqual(hash(FaceExtract()), hash(FaceExtract(ext_face_size=100)))
self.assertNotEqual(FaceExtract(), FaceExtract(thresholds=[0.1,0.1,0.1]))
self.assertNotEqual(hash(FaceExtract()), hash(FaceExtract(thresholds=[0.1,0.1,0.1])))
self.assertNotEqual(FaceExtract(), FaceExtract(factor=1.))
self.assertNotEqual(hash(FaceExtract()), hash(FaceExtract(factor=1.)))
# comparison ignores cuda_device
self.assertEqual(FaceExtract(), FaceExtract(cuda_device='cuda:123'))
self.assertEqual(hash(FaceExtract()), hash(FaceExtract(cuda_device='cuda:123')))
def test_preprocess(self):
testpath = os.path.join(os.path.dirname(__file__), 'faces-noface.jpg')
with PIL.Image.open(testpath) as img:
self.assertEqual(img.size, (199, 148))
# landscape, downscale, no rotation
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 100, None)
self.assertEqual(img.size, (100, 74))
self.assertEqual(denorm((10,10)), (10*1.99, 10*2.0))
# landscape, upscale, no rotation
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 398, None)
self.assertEqual(img.size, (398, 296))
self.assertEqual(denorm((10,10)), (10*0.5, 10*0.5))
# landscape, downscale, 90cw
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 100, 90)
self.assertEqual(img.size, (74, 100))
self.assertEqual(denorm((10,10)), (10.0*1.99, 64*2.0))
# landscape, upscale, 90cw
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 398, 90)
self.assertEqual(img.size, (296, 398))
self.assertEqual(denorm((10,10)), (10*0.5, 286*0.5))
# landscape, downscale, 90ccw
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 100, 270)
self.assertEqual(img.size, (74, 100))
self.assertEqual(denorm((10,10)), (90*1.99, 10*2.0))
# landscape, upscale, 90ccw
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 398, 270)
self.assertEqual(img.size, (296, 398))
self.assertEqual(denorm((10,10)), (388*0.5, 10*0.5))
# landscape, downscale, 180
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 100, 180)
self.assertEqual(img.size, (100, 74))
self.assertEqual(denorm((10,10)), (90*1.99, 64*2.0))
# landscape, upscale, 180
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 398, 180)
self.assertEqual(img.size, (398, 296))
self.assertEqual(denorm((10,10)), (388*0.5, 286*0.5))
# portrait, downscale, no rotation
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath).rotate(90, expand=True), 100, None)
self.assertEqual(img.size, (74, 100))
self.assertEqual(denorm((10,10)), (10*2.0, 10*1.99))
# portrait, upscale, no rotation
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath).rotate(90, expand=True), 398, None)
self.assertEqual(img.size, (296, 398))
self.assertEqual(denorm((10,10)), (10*0.5, 10*0.5))
# portrait, downscale, 90cw
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath).rotate(90, expand=True), 100, 90)
self.assertEqual(img.size, (100, 74))
self.assertEqual(denorm((10,10)), (10.0*2.0, 90*1.99))
# portrait, upscale, 90cw
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath).rotate(90, expand=True), 398, 90)
self.assertEqual(img.size, (398, 296))
self.assertEqual(denorm((10,10)), (10*0.5, 388*0.5))
# portrait, downscale, 90ccw
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath).rotate(90, expand=True), 100, 270)
self.assertEqual(img.size, (100, 74))
self.assertEqual(denorm((10,10)), (64*2.0, 10*1.99))
# portrait, upscale, 90ccw
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath).rotate(90, expand=True), 398, 270)
self.assertEqual(img.size, (398, 296))
self.assertEqual(denorm((10,10)), (286*0.5, 10*0.5))
# portrait, downscale, 180
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath).rotate(90, expand=True), 100, 180)
self.assertEqual(img.size, (74, 100))
self.assertEqual(denorm((10,10)), (64*2.0, 90*1.99))
# portrait, upscale, 180
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath).rotate(90, expand=True), 398, 180)
self.assertEqual(img.size, (296, 398))
self.assertEqual(denorm((10,10)), (286*0.5, 388*0.5))
# square image
testpath = os.path.join(os.path.dirname(__file__), 'faces-ivan.jpg')
with PIL.Image.open(testpath) as img:
self.assertEqual(img.size, (561, 561))
# square, downscale, no rotation
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 51, None)
self.assertEqual(img.size, (51, 51))
self.assertEqual(denorm((10,10)), (10*11, 10*11))
# square, upscale, no rotation
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 1122, None)
self.assertEqual(img.size, (1122, 1122))
self.assertEqual(denorm((10,10)), (10*0.5, 10*0.5))
# square, downscale, 90cw
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 51, 90)
self.assertEqual(img.size, (51, 51))
self.assertEqual(denorm((10,10)), (10.0*11, 41*11))
# square, upscale, 90cw
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 1122, 90)
self.assertEqual(img.size, (1122, 1122))
self.assertEqual(denorm((10,10)), (10*0.5, 1112*0.5))
# square, downscale, 90ccw
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 51, 270)
self.assertEqual(img.size, (51, 51))
self.assertEqual(denorm((10,10)), (41*11, 10*11))
# square, upscale, 90ccw
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 1122, 270)
self.assertEqual(img.size, (1122, 1122))
self.assertEqual(denorm((10,10)), (1112*0.5, 10*0.5))
# square, downscale, 180
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 51, 180)
self.assertEqual(img.size, (51, 51))
self.assertEqual(denorm((10,10)), (41*11, 41*11))
# square, upscale, 180
img, denorm = FaceExtract.preprocess(PIL.Image.open(testpath), 1122, 180)
self.assertEqual(img.size, (1122, 1122))
self.assertEqual(denorm((10,10)), (1112*0.5, 1112*0.5))
def test_call(self):
with contextlib.redirect_stderr(io.StringIO()): # NOTE: hide warnings from facenet_pytorch
rdr = FaceExtract()
# discards non-image files
self.assertRaises(errors.UnsupportedFileFormatError, rdr,
__file__)
# raises on invalid image
self.assertRaises(errors.UnsupportedFileFormatError, rdr,
os.path.join(os.path.dirname(__file__), 'testimage_exif_corrupted.jpg'))
# raises on missing file
self.assertRaises(errors.ReaderError, rdr,
os.path.join(os.path.dirname(__file__), 'invalid.jpg'))
# may return empty list
self.assertListEqual(FaceExtract(min_face_prob=1)(
os.path.join(os.path.dirname(__file__), 'faces-noface.jpg')), [])
self.assertListEqual(FaceExtract(min_face_prob=1)(
os.path.join(os.path.dirname(__file__), 'faces-ivan.jpg')), [])
# returns faces
faces = rdr(os.path.join(os.path.dirname(__file__), 'faces-ivan.jpg'))
# check if face was detected
self.assertEqual(len(faces), 1)
# check ucid
self.assertSetEqual({f['ucid'] for f in faces}, {
'926dc1684dd453aa2c3c8daf1c82ecf918514ef0de416b6b842235c23bec32ee',
})
# check embedding
for face in faces:
self.assertEqual(face['embedding'].shape, (512, ))
# check bbox
self.assertAlmostEqual(faces[0]['x'], 275.8, 2)
self.assertAlmostEqual(faces[0]['y'], 91.67, 2)
self.assertAlmostEqual(faces[0]['width'], 50.5, 2)
self.assertAlmostEqual(faces[0]['height'], 65.42, 2)
# FIXME: RuntimeError
# FIXME: ValueError
## main ##
if __name__ == '__main__':
unittest.main()
## EOF ##
|