1 '''
2 Copyright David S. Bolme
3
4 Created on Nov 5, 2010
5
6 @author: bolme
7 '''
8 import unittest
9
10 import pyvision as pv
11 import numpy as np
12 import os.path
13 import cv
14
15 DATA_DIR = os.path.join(pv.__path__[0],'data','test')
16 SYNC_VIDEO = 'video_sync.mov'
17 SYNC_FRAMES = ['video_sync_0001.jpg', 'video_sync_0002.jpg', 'video_sync_0003.jpg', 'video_sync_0004.jpg', 'video_sync_0005.jpg',]
18
19 BUGS_VIDEO = os.path.join(pv.__path__[0],'data','test','BugsSample.m4v')
20 TAZ_VIDEO = os.path.join(pv.__path__[0],'data','test','TazSample.m4v')
21 TOYCAR_VIDEO = os.path.join(pv.__path__[0],'data','test','toy_car.m4v')
22
24
26
27 self.im = pv.Image(os.path.join(pv.__path__[0],"data","nonface","NONFACE_46.jpg"))
28 self.pil = self.im.asPIL()
29 self.mat = self.im.asMatrix2D()
30 assert self.mat.shape[0] == 640
31 assert self.mat.shape[1] == 480
32 self.mat3d = self.im.asMatrix3D()
33 assert self.mat3d.shape[0] == 3
34 assert self.mat3d.shape[1] == 640
35 assert self.mat3d.shape[2] == 480
36 self.opencv = self.im.asOpenCV()
37
39 w,h = self.im.size
40 data_buffer = self.im.toBufferGray(8)
41 self.assertEqual(len(data_buffer),w*h)
42 data_buffer = self.im.toBufferGray(32)
43 self.assertEqual(len(data_buffer),4*w*h)
44 data_buffer = self.im.toBufferGray(64)
45 self.assertEqual(len(data_buffer),8*w*h)
46
48 im = pv.Image(self.mat3d)
49 w,h = im.size
50 data_buffer = im.toBufferGray(8)
51 self.assertEqual(len(data_buffer),w*h)
52 data_buffer = im.toBufferGray(32)
53 self.assertEqual(len(data_buffer),4*w*h)
54 data_buffer = im.toBufferGray(64)
55 self.assertEqual(len(data_buffer),8*w*h)
56
58 im = pv.Image(self.mat)
59 w,h = im.size
60 data_buffer = im.toBufferGray(8)
61 self.assertEqual(len(data_buffer),w*h)
62 data_buffer = im.toBufferGray(32)
63 self.assertEqual(len(data_buffer),4*w*h)
64 data_buffer = im.toBufferGray(64)
65 self.assertEqual(len(data_buffer),8*w*h)
66
68 im = self.im
69 pil = im.asPIL().convert('L')
70 pil = pil.resize((180,120))
71 im = pv.Image(pil)
72 mat = im.asMatrix2D()
73 for i in range(im.width):
74 for j in range(im.height):
75 self.assertAlmostEqual(pil.getpixel((i,j)),mat[i,j])
76
78 im = pv.Image(self.mat[:180,:120])
79 pil = im.asPIL()
80 mat = im.asMatrix2D()
81 for i in range(im.width):
82 for j in range(im.height):
83 self.assertAlmostEqual(pil.getpixel((i,j)),mat[i,j])
84
86 pil = self.im.asPIL().resize((180,120))
87 im = pv.Image(pil)
88 mat = im.asMatrix3D()
89 for i in range(im.width):
90 for j in range(im.height):
91 for c in range(3):
92 self.assertAlmostEqual(pil.getpixel((i,j))[c],mat[c,i,j])
93
95 im = pv.Image(self.mat3d[:,:180,:120])
96 pil = self.im.asPIL()
97 mat = im.asMatrix3D()
98 for i in range(im.width):
99 for j in range(im.height):
100 for c in range(3):
101 self.assertAlmostEqual(pil.getpixel((i,j))[c],mat[c,i,j])
102
104 pil = self.im.asPIL().resize((180,120))
105 im = pv.Image(pil)
106 cv = im.asOpenCV()
107
108
109
110
111
112
113 for i in range(im.width):
114 for j in range(im.height):
115 for c in range(3):
116 self.assertAlmostEqual(pil.getpixel((i,j))[c],ord(cv.tostring()[i*3+j*im.width*3+2-c]))
117
119 pil = self.im.asPIL().resize((180,120))
120 im = pv.Image(pil)
121 cv = im.asOpenCV()
122 pil = pv.Image(cv).asPIL()
123
124 for i in range(im.width):
125 for j in range(im.height):
126 for c in range(3):
127 self.assertAlmostEqual(pil.getpixel((i,j))[c],ord(cv.tostring()[i*3+j*im.width*3+2-c]))
128
135
136
137
138
139
140
141
142
143
144
145
146
148 pil = self.im.asPIL().resize((180,120))
149 im = pv.Image(pil)
150 cvim = im.asOpenCV()
151 data_buffer = im.toBufferRGB(8)
152
153 for i in range(im.width):
154 for j in range(im.height):
155 for c in range(3):
156 self.assertAlmostEqual(ord(data_buffer[i*3+j*im.width*3+c]),ord(cvim.tostring()[i*3+j*im.width*3+2-c]))
157
160
162 r,c = 10,20
163 cvmat = cv.CreateMat(r,c,cv.CV_32F)
164 for i in range(r):
165 for j in range(c):
166 cvmat[i,j] = i*j
167 mat = pv.OpenCVToNumpy(cvmat)
168 self.assert_(mat.shape == (r,c))
169 for i in range(r):
170 for j in range(c):
171 self.assert_(mat[i,j] == cvmat[i,j])
172
173
175 im = pv.Image(pv.LENA)
176 im = im.resize((512,400))
177 cv_im = im.asOpenCV()
178 mat = im.asMatrix3D()
179 cv_32 = cv.CreateImage(cv.GetSize(cv_im),cv.IPL_DEPTH_32F,3)
180 cv.Convert(cv_im,cv_32)
181
182 for x in range(50):
183 for y in range(50):
184 for c in range(3):
185 self.assertAlmostEqual(cv_im[y,x][2-c],mat[c,x,y])
186 self.assertAlmostEqual(cv_im[y,x][2-c],cv_32[y,x][2-c])
187
188 _ = pv.Image(cv_32)
189
191 r,c = 10,20
192 mat = np.zeros((r,c),dtype=np.float32)
193 for i in range(r):
194 for j in range(c):
195 mat[i,j] = i*j
196 cvmat = pv.NumpyToOpenCV(mat)
197 self.assert_(mat.shape == (r,c))
198 for i in range(r):
199 for j in range(c):
200 self.assert_(mat[i,j] == cvmat[i,j])
201
203 rect = pv.Rect(-3, -2, 35, 70)
204 imcrop = self.im.crop(rect)
205 cropSize = imcrop.size
206
207 self.assertEquals((35,70), cropSize)
208
209 rect = pv.Rect(620, 460, 35, 70)
210 imcrop = self.im.crop(rect)
211 cropSize = imcrop.size
212
213 self.assertEquals((35,70), cropSize)
214
216 im = pv.Image(pv.BABOON)
217 hsv = im.asHSV()
218 im = pv.Image(hsv)
219
220
221 im = pv.Image(os.path.join(pv.__path__[0],"data","misc","baboon_bw.jpg"))
222 self.assertRaises(Exception, im.asHSV)
223
224
225
226
228 '''Tests for the video class.'''
229
230
232 """Video Sync Test"""
233
234
235
236 ilog = None
237 video_path = os.path.join(DATA_DIR,SYNC_VIDEO)
238 video = pv.Video(video_path)
239
240 frame_num = 0
241 for frame_name in SYNC_FRAMES:
242 frame_path = os.path.join(DATA_DIR,frame_name)
243 ffmpeg_frame = pv.Image(frame_path)
244 opencv_frame = video.next()
245
246
247 diff = ffmpeg_frame.asMatrix3D() - opencv_frame.asMatrix3D()
248 diff_max = max(abs(diff.max()),abs(diff.min()))
249 self.assert_(diff_max < 30.0)
250 diff = pv.Image(diff)
251 if ilog != None:
252
253 ilog(ffmpeg_frame,"ffmpeg_%04d"%frame_num)
254 ilog(opencv_frame,"opencv_%04d"%frame_num)
255 ilog(diff,"diff_%04d"%frame_num)
256 frame_num += 1
257
258
259 self.assertRaises(StopIteration, video.next)
260
261 if ilog != None:
262 ilog.show()
263
265 """Frame Count Test"""
266 video_path = os.path.join(DATA_DIR,SYNC_VIDEO)
267 video = pv.Video(video_path)
268
269 count = 0
270 for _ in video:
271
272 count += 1
273
274 self.assertEquals(count,5)
275
277 """Frame Count Test"""
278 video_path = os.path.join(DATA_DIR,SYNC_VIDEO)
279 video = pv.FFMPEGVideo(video_path)
280
281 count = 0
282 for _ in video:
283
284 count += 1
285
286 self.assertEquals(count,5)
287
289
290 ilog = None
291
292 video = pv.FFMPEGVideo(BUGS_VIDEO)
293
294 i = 0
295 for frame in video:
296
297 if ilog != None:
298 print "Processing Frame:",i
299
300
301 ilog(frame,format='jpg')
302
303 i += 1
304
305 if ilog != None:
306 ilog.show()
307
308
309
310
311
312
315
316 if __name__ == "__main__":
317
318 test()
319