1 '''
2 Created on Jul 1, 2009
3
4 This module contains tests for the face recognition algorithms.
5
6 @author: bolme
7 '''
8
9 import unittest
10
11 import pyvision as pv
12 import numpy as np
13
14
15
16
17 import os.path
18
20
22
23
24
25
26 fname = os.path.join(pv.__path__[0],'data','misc','lena.jpg')
27 im = pv.Image(fname,bw_annotate=True)
28
29
30
31 self.tile = im
32
34 '''meanStd Normalization: norm.mean() = 0.0 and norm.std() = 1.0....'''
35 ilog = None
36 if 'ilog' in globals().keys():
37 ilog = globals()['ilog']
38
39 norm = pv.meanStd(self.tile)
40
41 if ilog != None:
42 ilog.log(norm,label="meanStd_Normalization")
43
44 mat = norm.asMatrix2D()
45 self.assertAlmostEqual(mat.mean(),0.0,places=3)
46 self.assertAlmostEqual(mat.std(),1.0,places=3)
47
49 '''meanUnit Normalization: norm.mean() = 0.0 and ||norm|| = 1.0....'''
50 ilog = None
51 if 'ilog' in globals().keys():
52 ilog = globals()['ilog']
53
54 norm = pv.meanUnit(self.tile)
55
56 if ilog != None:
57 ilog.log(norm,label="meanUnit_Normalization")
58
59 mat = norm.asMatrix2D()
60 self.assertAlmostEqual(mat.mean(),0.0)
61 length = np.sqrt((mat**2).sum())
62 self.assertAlmostEqual(length,1.0,places=4)
63
65 '''unit Normalization: ||norm|| = 1.0 and dot(norm,im)/||im|| = 1.0.'''
66 ilog = None
67 if 'ilog' in globals().keys():
68 ilog = globals()['ilog']
69
70 norm = pv.unit(self.tile)
71
72 if ilog != None:
73 ilog.log(norm,label="unit_Normalization")
74
75 mat = norm.asMatrix2D()
76 length = np.sqrt((mat**2).sum())
77 self.assertAlmostEqual(length,1.0,places=3)
78
79 mat = norm.asMatrix2D()
80 mat = mat.flatten()
81 im = self.tile.asMatrix2D().flatten()
82 proj = np.dot(mat,im)
83 length = np.sqrt((im**2).sum())
84 self.assertAlmostEqual(proj/length,1.0,places=3)
85
87 '''bandPassFilter Normalization: ...................................'''
88 ilog = None
89 if 'ilog' in globals().keys():
90 ilog = globals()['ilog']
91
92 norm = pv.bandPassFilter(self.tile,10.0,4.0)
93
94 if ilog != None:
95 ilog.log(norm,label="bandPass_Normalization")
96
97 mat = norm.asMatrix2D()
98 self.assertAlmostEqual(mat.mean(),0.0,places=4)
99 self.assertAlmostEqual(mat.std(),12.090113839874826,places=3)
100
102 '''lowPassFilter Normalization: ....................................'''
103 ilog = None
104 if 'ilog' in globals().keys():
105 ilog = globals()['ilog']
106
107 norm = pv.lowPassFilter(self.tile,10.0)
108
109 if ilog != None:
110 ilog.log(norm,label="lowPass_Normalization")
111
112 mat = norm.asMatrix2D()
113 self.assertAlmostEqual(mat.mean(),123.69997406005859,places=3)
114 self.assertAlmostEqual(mat.std(),36.886999835117216,places=3)
115
117 '''highPassFilter Normalization: ...................................'''
118 ilog = None
119 if 'ilog' in globals().keys():
120 ilog = globals()['ilog']
121
122 norm = pv.highPassFilter(self.tile,10.0)
123
124 if ilog != None:
125 ilog.log(norm,label="highPass_Normalization")
126
127 mat = norm.asMatrix2D()
128 self.assertAlmostEqual(mat.mean(),0.0,places=4)
129 self.assertAlmostEqual(mat.std(),22.936873341661158,places=3)
130
132 '''highPassFilter Normalization: sigma = 1.5........................'''
133 ilog = None
134 if 'ilog' in globals().keys():
135 ilog = globals()['ilog']
136
137
138 norm = pv.highPassFilter(self.tile,1.5)
139
140 if ilog != None:
141 ilog.log(norm,label="veryHighPass_Normalization")
142
143 mat = norm.asMatrix2D()
144 self.assertAlmostEqual(mat.mean(),0.0,places=4)
145 self.assertAlmostEqual(mat.std(),8.0027218003238687,places=3)
146
148 '''selfQuotient Normalization: .....................................'''
149 ilog = None
150 if 'ilog' in globals().keys():
151 ilog = globals()['ilog']
152
153 norm = pv.selfQuotientImage(self.tile)
154
155 if ilog != None:
156 ilog.log(norm,label="selfQuotient_Normalization")
157
158 mat = norm.asMatrix2D()
159 self.assertAlmostEqual(mat.mean(),0.98861616849899292,places=3)
160 self.assertAlmostEqual(mat.std(),0.1647989569275968,places=3)
161
162
163
165
167 '''SURF Lena: ......................................................'''
168 ilog = None
169 if 'ilog' in globals().keys():
170 ilog = globals()['ilog']
171
172 filename = os.path.join(pv.__path__[0],'data','misc','lena.jpg')
173 im = pv.Image(filename)
174 timer = pv.Timer()
175 keypoints,descriptors = pv.surf(im)
176 timer.mark("LenaSurf")
177 if ilog != None:
178 ilog(timer,"SURFLena")
179 for each in keypoints:
180 im.annotateCircle(pv.Point(each[0][0],each[0][1]), each[2])
181 if ilog != None:
182 ilog(im,'SurfKeypoints')
183
184 self.assertEqual(len(keypoints),len(descriptors))
185 self.assertEqual(len(keypoints),774)
186
187
188
190 '''SURF Taz: .......................................................'''
191 ilog = None
192 if 'ilog' in globals().keys():
193 ilog = globals()['ilog']
194
195 filename = os.path.join(pv.__path__[0],'data','test','TAZ_0010.jpg')
196 im = pv.Image(filename)
197 timer = pv.Timer()
198 keypoints,descriptors = pv.surf(im)
199 timer.mark("TazSurf")
200 if ilog != None:
201 ilog(timer,"SURFTaz")
202 for each in keypoints:
203 im.annotateCircle(pv.Point(each[0][0],each[0][1]), each[2])
204 if ilog != None:
205 ilog(im,'SurfKeypoints')
206
207 self.assertEqual(len(keypoints),len(descriptors))
208 self.assertEqual(len(keypoints),367)
209
210
211
213
215 '''Initialize the tests'''
216
217
219 '''distance::boolToUbyte ...........................................'''
220 a = np.random.randint(2,size=16) > 0
221 b = pv.boolToUbyte(a)
222 c = pv.ubyteToBool(b)
223 d = pv.boolToUbyte(c)
224
225 self.assert_((a == c).sum() == 16)
226 self.assert_((b == d).sum() == 2)
227
228 a = np.random.randint(2,size=5000) > 0
229 b = pv.boolToUbyte(a)
230 c = pv.ubyteToBool(b)
231 d = pv.boolToUbyte(c)
232
233 self.assert_((a == c).sum() == 5000)
234 self.assert_((b == d).sum() == 625)
235
236
237
238
240 '''distance::hamming 1..............................................'''
241 a = np.random.randint(2,size=16) > 0
242 b = np.random.randint(2,size=16) > 0
243
244 bin_hamming = pv.hamming(a,b)
245
246 a = pv.boolToUbyte(a)
247 b = pv.boolToUbyte(b)
248
249 byte_hamming = pv.hamming(a,b)
250
251 self.assertEquals(bin_hamming,byte_hamming)
252
253
255 '''distance::hamming 2..............................................'''
256 a = np.random.randint(2,size=1769472) > 0
257 b = np.random.randint(2,size=1769472) > 0
258
259 bin_hamming = pv.hamming(a,b)
260
261 a = pv.boolToUbyte(a)
262 b = pv.boolToUbyte(b)
263
264 byte_hamming = pv.hamming(a,b)
265
266 self.assertEquals(bin_hamming,byte_hamming)
267
268
269
270
271
273 '''Run the face test suite.'''
274 pv.disableCommercialUseWarnings()
275
276 normalize_suite = unittest.TestLoader().loadTestsFromTestCase(_TestNormalize)
277 surf_suite = unittest.TestLoader().loadTestsFromTestCase(_TestSURF)
278 dist_suite = unittest.TestLoader().loadTestsFromTestCase(_TestDistance)
279
280
281
282 test_suites = [
283 normalize_suite,
284 surf_suite,
285 dist_suite
286 ]
287
288
289 pyvision_suite = unittest.TestSuite(test_suites)
290
291 unittest.TextTestRunner(verbosity=2).run(pyvision_suite)
292
293
294 if __name__ == '__main__':
295
296 unittest.main(testRunner = unittest.TextTestRunner(verbosity=2))
297