Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.py @ 482:f6415f012640
adding functionalities (save images directly to display trajectories to create movies
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 02 Apr 2014 16:12:24 -0400 |
| parents | d337bffd7283 |
| children | 343cfd185ca6 |
comparison
equal
deleted
inserted
replaced
| 481:b6ad86ee7033 | 482:f6415f012640 |
|---|---|
| 200 else: | 200 else: |
| 201 imgcrop = [] | 201 imgcrop = [] |
| 202 return imgcrop, yCropMin, yCropMax, xCropMin, xCropMax | 202 return imgcrop, yCropMin, yCropMax, xCropMin, xCropMax |
| 203 | 203 |
| 204 | 204 |
| 205 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1): | 205 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False): |
| 206 '''Displays the objects overlaid frame by frame over the video ''' | 206 '''Displays the objects overlaid frame by frame over the video ''' |
| 207 from moving import userTypeNames | 207 from moving import userTypeNames |
| 208 | 208 from math import ceil, log10 |
| 209 capture = cv2.VideoCapture(videoFilename) | 209 capture = cv2.VideoCapture(videoFilename) |
| 210 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) | 210 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) |
| 211 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) | 211 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) |
| 212 if capture.isOpened(): | 212 if capture.isOpened(): |
| 213 key = -1 | 213 key = -1 |
| 214 ret = True | 214 ret = True |
| 215 frameNum = firstFrameNum | 215 frameNum = firstFrameNum |
| 216 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) | 216 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) |
| 217 if not lastFrameNumArg: | 217 if lastFrameNumArg == None: |
| 218 from sys import maxint | 218 from sys import maxint |
| 219 lastFrameNum = maxint | 219 lastFrameNum = maxint |
| 220 else: | 220 else: |
| 221 lastFrameNum = lastFrameNumArg | 221 lastFrameNum = lastFrameNumArg |
| 222 nZerosFilename = int(ceil(log10(lastFrameNum))) | |
| 222 while ret and not quitKey(key) and frameNum < lastFrameNum: | 223 while ret and not quitKey(key) and frameNum < lastFrameNum: |
| 223 ret, img = capture.read() | 224 ret, img = capture.read() |
| 224 if ret: | 225 if ret: |
| 225 if printFrames: | 226 if printFrames: |
| 226 print('frame {0}'.format(frameNum)) | 227 print('frame {0}'.format(frameNum)) |
| 240 cv2.rectangle(img, (xCropMin, yCropMin), (xCropMax, yCropMax), cvBlue, 1) | 241 cv2.rectangle(img, (xCropMin, yCropMin), (xCropMax, yCropMax), cvBlue, 1) |
| 241 objDescription = '{} '.format(obj.num) | 242 objDescription = '{} '.format(obj.num) |
| 242 if userTypeNames[obj.userType] != 'unknown': | 243 if userTypeNames[obj.userType] != 'unknown': |
| 243 objDescription += userTypeNames[obj.userType][0].upper() | 244 objDescription += userTypeNames[obj.userType][0].upper() |
| 244 cv2.putText(img, objDescription, obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.cv.CV_FONT_HERSHEY_PLAIN, 1, cvRed) | 245 cv2.putText(img, objDescription, obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.cv.CV_FONT_HERSHEY_PLAIN, 1, cvRed) |
| 245 cvImshow('frame', img, rescale) | 246 if not saveAllImages: |
| 246 key = cv2.waitKey() | 247 cvImshow('frame', img, rescale) |
| 247 if saveKey(key): | 248 key = cv2.waitKey() |
| 248 cv2.imwrite('image.png', img) | 249 if saveAllImages or saveKey(key): |
| 250 cv2.imwrite('image-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img) | |
| 249 frameNum += nFramesStep | 251 frameNum += nFramesStep |
| 250 if nFramesStep > 1: | 252 if nFramesStep > 1: |
| 251 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) | 253 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) |
| 252 cv2.destroyAllWindows() | 254 cv2.destroyAllWindows() |
| 253 else: | 255 else: |
