Mercurial Hosting > traffic-intelligence
changeset 305:ca9131968bce
added sample to replay video
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 29 Mar 2013 21:20:12 -0400 |
parents | 20f9cd972dde |
children | 93d851d0d21e |
files | python/cvutils.py python/play-video.py |
diffstat | 2 files changed, 36 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/python/cvutils.py Fri Mar 29 16:56:23 2013 -0400 +++ b/python/cvutils.py Fri Mar 29 21:20:12 2013 -0400 @@ -24,6 +24,12 @@ cvGreen, cvBlue]) +def quitKey(key): + return chr(key&255)== 'q' or chr(key&255) == 'Q' + +def saveKey(key): + return chr(key&255) == 's' + def drawLines(filename, origins, destinations, w = 1, resultFilename='image.png'): '''Draws lines over the image ''' @@ -85,21 +91,24 @@ for i in range(0, last-1): cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color) - def playVideo(filename, firstFrameNum = 0): + def playVideo(filename, firstFrameNum = 0, frameRate = -1): '''Plays the video''' + wait = 5 + if frameRate > 0: + wait = int(round(1000./frameRate)) capture = cv2.VideoCapture(filename) if capture.isOpened(): key = -1 ret = True frameNum = firstFrameNum capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) - while ret and chr(key&255)!= 'q': + while ret and not quitKey(key): ret, img = capture.read() if ret: print('frame {0}'.format(frameNum)) frameNum+=1 cv2.imshow('frame', img) - key = cv2.waitKey(5) + key = cv2.waitKey(wait) def getImagesFromVideo(filename, nImages = 1, saveImage = False): '''Returns nImages images from the video sequence''' @@ -135,7 +144,7 @@ lastFrameNum = maxint else: lastFrameNum = lastFrameNumArg - while ret and chr(key&255)!= 'q' and chr(key&255)!= 'Q' and frameNum < lastFrameNum: + while ret and not quitKey(key) and frameNum < lastFrameNum: ret, img = capture.read() if ret: print('frame {0}'.format(frameNum)) @@ -150,7 +159,7 @@ cv2.putText(img, '{0}'.format(obj.num), obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) cv2.imshow('frame', img) key = cv2.waitKey() - if chr(key&255) == 's': + if saveKey(key): cv2.imwrite('image.png', img) frameNum += 1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/play-video.py Fri Mar 29 21:20:12 2013 -0400 @@ -0,0 +1,22 @@ +#! /usr/bin/env python + +import sys,getopt +import cvutils + +options, args = getopt.getopt(sys.argv[1:], 'hi:f:',['help', 'fps=']) +options = dict(options) +print options + +if '--help' in options.keys() or '-h' in options.keys() or len(sys.argv) == 1: + print('Usage: '+sys.argv[0]+' --help|-h -i video-filename [-f first_frame] [--fps frame_rate]') + sys.exit() + +firstFrameNum = 0 +if '-f' in options.keys(): + firstFrameNum = int(options['-f']) + +frameRate = -1 +if '--fps' in options.keys(): + frameRate = int(options['--fps']) + +cvutils.playVideo(options['-i'], firstFrameNum, frameRate)