comparison python/cvutils.py @ 807:52aa03260f03 opencv3

reversed all code to OpenCV 2.4.13
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 10 Jun 2016 15:26:19 -0400
parents 0662c87a61c9
children e73e7b644428
comparison
equal deleted inserted replaced
806:c6f497291fd8 807:52aa03260f03
147 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1) 147 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1)
148 148
149 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1): 149 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1):
150 '''Plays the video''' 150 '''Plays the video'''
151 windowName = 'frame' 151 windowName = 'frame'
152 wait = 5
152 if rescale == 1.: 153 if rescale == 1.:
153 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) 154 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
154 wait = 5
155 if frameRate > 0: 155 if frameRate > 0:
156 wait = int(round(1000./frameRate)) 156 wait = int(round(1000./frameRate))
157 if interactive: 157 if interactive:
158 wait = 0 158 wait = 0
159 capture = cv2.VideoCapture(filename) 159 capture = cv2.VideoCapture(filename)
160 if capture.isOpened(): 160 if capture.isOpened():
161 key = -1 161 key = -1
162 ret = True 162 ret = True
163 frameNum = firstFrameNum 163 frameNum = firstFrameNum
164 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum) 164 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum)
165 while ret and not quitKey(key): 165 while ret and not quitKey(key):
166 #ret, img = capture.read() 166 ret, img = capture.read()
167 for i in xrange(step):
168 ret, img = capture.read()
169 if ret: 167 if ret:
170 if printFrames: 168 if printFrames:
171 print('frame {0}'.format(frameNum)) 169 print('frame {0}'.format(frameNum))
172 frameNum+=step
173 if text is not None: 170 if text is not None:
174 cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) 171 cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed)
175 cvImshow(windowName, img, rescale) 172 cv2.imshow('frame', img)#cvImshow(windowName, img, rescale)
176 key = cv2.waitKey(wait) 173 key = cv2.waitKey(wait)
177 if saveKey(key): 174 if saveKey(key):
178 cv2.imwrite('image-{}.png'.format(frameNum), img) 175 cv2.imwrite('image-{}.png'.format(frameNum), img)
176 frameNum += step
177 if step > 1:
178 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum)
179 cv2.destroyAllWindows() 179 cv2.destroyAllWindows()
180 else: 180 else:
181 print('Video capture for {} failed'.format(filename)) 181 print('Video capture for {} failed'.format(filename))
182 182
183 def infoVideo(filename): 183 def infoVideo(filename):
184 '''Provides all available info on video ''' 184 '''Provides all available info on video '''
185 cvPropertyNames = {cv2.CAP_PROP_FORMAT: "format", 185 cvPropertyNames = {cv2.cv.CV_CAP_PROP_FORMAT: "format",
186 cv2.CAP_PROP_FOURCC: "codec (fourcc)", 186 cv2.cv.CV_CAP_PROP_FOURCC: "codec (fourcc)",
187 cv2.CAP_PROP_FPS: "fps", 187 cv2.cv.CV_CAP_PROP_FPS: "fps",
188 cv2.CAP_PROP_FRAME_COUNT: "number of frames", 188 cv2.cv.CV_CAP_PROP_FRAME_COUNT: "number of frames",
189 cv2.CAP_PROP_FRAME_HEIGHT: "heigh", 189 cv2.cv.CV_CAP_PROP_FRAME_HEIGHT: "heigh",
190 cv2.CAP_PROP_FRAME_WIDTH: "width", 190 cv2.cv.CV_CAP_PROP_FRAME_WIDTH: "width",
191 cv2.CAP_PROP_RECTIFICATION: "rectification", 191 cv2.cv.CV_CAP_PROP_RECTIFICATION: "rectification",
192 cv2.CAP_PROP_SATURATION: "saturation"} 192 cv2.cv.CV_CAP_PROP_SATURATION: "saturation"}
193 capture = cv2.VideoCapture(filename) 193 capture = cv2.VideoCapture(filename)
194 if capture.isOpened(): 194 if capture.isOpened():
195 for cvprop in [#cv2.CAP_PROP_BRIGHTNESS 195 for cvprop in [#cv2.cv.CV_CAP_PROP_BRIGHTNESS
196 #cv2.CAP_PROP_CONTRAST 196 #cv2.cv.CV_CAP_PROP_CONTRAST
197 #cv2.CAP_PROP_CONVERT_RGB 197 #cv2.cv.CV_CAP_PROP_CONVERT_RGB
198 #cv2.CAP_PROP_EXPOSURE 198 #cv2.cv.CV_CAP_PROP_EXPOSURE
199 cv2.CAP_PROP_FORMAT, 199 cv2.cv.CV_CAP_PROP_FORMAT,
200 cv2.CAP_PROP_FOURCC, 200 cv2.cv.CV_CAP_PROP_FOURCC,
201 cv2.CAP_PROP_FPS, 201 cv2.cv.CV_CAP_PROP_FPS,
202 cv2.CAP_PROP_FRAME_COUNT, 202 cv2.cv.CV_CAP_PROP_FRAME_COUNT,
203 cv2.CAP_PROP_FRAME_HEIGHT, 203 cv2.cv.CV_CAP_PROP_FRAME_HEIGHT,
204 cv2.CAP_PROP_FRAME_WIDTH, 204 cv2.cv.CV_CAP_PROP_FRAME_WIDTH,
205 #cv2.CAP_PROP_GAIN, 205 #cv2.cv.CV_CAP_PROP_GAIN,
206 #cv2.CAP_PROP_HUE 206 #cv2.cv.CV_CAP_PROP_HUE
207 #cv2.CAP_PROP_MODE 207 #cv2.cv.CV_CAP_PROP_MODE
208 #cv2.CAP_PROP_POS_AVI_RATIO 208 #cv2.cv.CV_CAP_PROP_POS_AVI_RATIO
209 #cv2.CAP_PROP_POS_FRAMES 209 #cv2.cv.CV_CAP_PROP_POS_FRAMES
210 #cv2.CAP_PROP_POS_MSEC 210 #cv2.cv.CV_CAP_PROP_POS_MSEC
211 #cv2.CAP_PROP_RECTIFICATION, 211 #cv2.cv.CV_CAP_PROP_RECTIFICATION,
212 #cv2.CAP_PROP_SATURATION 212 #cv2.cv.CV_CAP_PROP_SATURATION
213 ]: 213 ]:
214 prop = capture.get(cvprop) 214 prop = capture.get(cvprop)
215 if cvprop == cv2.CAP_PROP_FOURCC and prop > 0: 215 if cvprop == cv2.cv.CV_CAP_PROP_FOURCC and prop > 0:
216 prop = int2FOURCC(int(prop)) 216 prop = int2FOURCC(int(prop))
217 print('Video {}: {}'.format(cvPropertyNames[cvprop], prop)) 217 print('Video {}: {}'.format(cvPropertyNames[cvprop], prop))
218 else: 218 else:
219 print('Video capture for {} failed'.format(filename)) 219 print('Video capture for {} failed'.format(filename))
220 220
221 def getImagesFromVideo(videoFilename, firstFrameNum = 0, lastFrameNum = 1, step = 1, saveImage = False, outputPrefix = 'image'): 221 def getImagesFromVideo(videoFilename, firstFrameNum = 0, lastFrameNum = 1, step = 1, saveImage = False, outputPrefix = 'image'):
222 '''Returns nFrames images from the video sequence''' 222 '''Returns nFrames images from the video sequence'''
223 images = [] 223 images = []
224 capture = cv2.VideoCapture(videoFilename) 224 capture = cv2.VideoCapture(videoFilename)
225 if capture.isOpened(): 225 if capture.isOpened():
226 rawCount = capture.get(cv2.CAP_PROP_FRAME_COUNT) 226 rawCount = capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
227 if rawCount < 0: 227 if rawCount < 0:
228 rawCount = lastFrameNum+1 228 rawCount = lastFrameNum+1
229 nDigits = int(floor(log10(rawCount)))+1 229 nDigits = int(floor(log10(rawCount)))+1
230 ret = False 230 ret = False
231 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum) 231 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum)
232 frameNum = firstFrameNum 232 frameNum = firstFrameNum
233 while frameNum<=lastFrameNum and frameNum<rawCount: 233 while frameNum<=lastFrameNum and frameNum<rawCount:
234 ret, img = capture.read() 234 ret, img = capture.read()
235 i = 0 235 i = 0
236 while not ret and i<10: 236 while not ret and i<10:
242 cv2.imwrite(outputPrefix+frameNumStr+'.png', img) 242 cv2.imwrite(outputPrefix+frameNumStr+'.png', img)
243 else: 243 else:
244 images.append(img) 244 images.append(img)
245 frameNum +=step 245 frameNum +=step
246 if step > 1: 246 if step > 1:
247 capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum) 247 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum)
248 capture.release() 248 capture.release()
249 else: 249 else:
250 print('Video capture for {} failed'.format(videoFilename)) 250 print('Video capture for {} failed'.format(videoFilename))
251 return images 251 return images
252 252
253 def getFPS(videoFilename): 253 def getFPS(videoFilename):
254 capture = cv2.VideoCapture(videoFilename) 254 capture = cv2.VideoCapture(videoFilename)
255 if capture.isOpened(): 255 if capture.isOpened():
256 fps = capture.get(cv2.CAP_PROP_FPS) 256 fps = capture.get(cv2.cv.CV_CAP_PROP_FPS)
257 capture.release() 257 capture.release()
258 return fps 258 return fps
259 else: 259 else:
260 print('Video capture for {} failed'.format(videoFilename)) 260 print('Video capture for {} failed'.format(videoFilename))
261 return None 261 return None
289 289
290 290
291 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., annotations = [], gtMatches = {}, toMatches = {}): 291 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., annotations = [], gtMatches = {}, toMatches = {}):
292 '''Displays the objects overlaid frame by frame over the video ''' 292 '''Displays the objects overlaid frame by frame over the video '''
293 capture = cv2.VideoCapture(videoFilename) 293 capture = cv2.VideoCapture(videoFilename)
294 width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) 294 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
295 height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) 295 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
296 296
297 windowName = 'frame' 297 windowName = 'frame'
298 if rescale == 1.: 298 if rescale == 1.:
299 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) 299 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
300 300
302 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) 302 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients)
303 if capture.isOpened(): 303 if capture.isOpened():
304 key = -1 304 key = -1
305 ret = True 305 ret = True
306 frameNum = firstFrameNum 306 frameNum = firstFrameNum
307 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum) 307 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum)
308 if lastFrameNumArg is None: 308 if lastFrameNumArg is None:
309 lastFrameNum = maxint 309 lastFrameNum = maxint
310 else: 310 else:
311 lastFrameNum = lastFrameNumArg 311 lastFrameNum = lastFrameNumArg
312 nZerosFilename = int(ceil(log10(lastFrameNum))) 312 nZerosFilename = int(ceil(log10(lastFrameNum)))
362 key = cv2.waitKey() 362 key = cv2.waitKey()
363 if saveAllImages or saveKey(key): 363 if saveAllImages or saveKey(key):
364 cv2.imwrite('image-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img) 364 cv2.imwrite('image-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img)
365 frameNum += nFramesStep 365 frameNum += nFramesStep
366 if nFramesStep > 1: 366 if nFramesStep > 1:
367 capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum) 367 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum)
368 cv2.destroyAllWindows() 368 cv2.destroyAllWindows()
369 else: 369 else:
370 print('Cannot load file ' + videoFilename) 370 print('Cannot load file ' + videoFilename)
371 371
372 def computeHomographyFromPDTV(camera): 372 def computeHomographyFromPDTV(camera):
605 605
606 nImages = len(inputData) 606 nImages = len(inputData)
607 return array(inputData, dtype = float32), array([classLabel]*nImages) 607 return array(inputData, dtype = float32), array([classLabel]*nImages)
608 608
609 609
610 #########################
611 # running tests
612 #########################
613
614 if __name__ == "__main__":
615 import doctest
616 import unittest
617 suite = doctest.DocFileSuite('tests/cvutils.txt')
618 #suite = doctest.DocTestSuite()
619 unittest.TextTestRunner().run(suite)
620 #doctest.testmod()
621 #doctest.testfile("example.txt")