Mercurial Hosting > traffic-intelligence
comparison python/cvutils.py @ 801:c5f98916297e
merged with dev branch
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 31 May 2016 17:07:23 -0400 |
parents | 0662c87a61c9 |
children | 52aa03260f03 |
comparison
equal
deleted
inserted
replaced
795:a34ec862371f | 801:c5f98916297e |
---|---|
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, nFrames = 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.CAP_PROP_FRAME_COUNT) |
227 if rawCount < 0: | 227 if rawCount < 0: |
228 rawCount = firstFrameNum+nFrames+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.CAP_PROP_POS_FRAMES, firstFrameNum) |
232 imgNum = 0 | 232 frameNum = firstFrameNum |
233 while imgNum<nFrames: | 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: |
237 ret, img = capture.read() | 237 ret, img = capture.read() |
238 i += 1 | 238 i += 1 |
239 if img.size>0: | 239 if img is not None and img.size>0: |
240 if saveImage: | 240 if saveImage: |
241 imgNumStr = format(firstFrameNum+imgNum, '0{}d'.format(nDigits)) | 241 frameNumStr = format(frameNum, '0{}d'.format(nDigits)) |
242 cv2.imwrite(outputPrefix+imgNumStr+'.png', img) | 242 cv2.imwrite(outputPrefix+frameNumStr+'.png', img) |
243 else: | 243 else: |
244 images.append(img) | 244 images.append(img) |
245 imgNum +=1 | 245 frameNum +=step |
246 if step > 1: | |
247 capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum) | |
246 capture.release() | 248 capture.release() |
247 else: | 249 else: |
248 print('Video capture for {} failed'.format(videoFilename)) | 250 print('Video capture for {} failed'.format(videoFilename)) |
249 return images | 251 return images |
250 | 252 |