Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.py @ 429:2be846d36dec
reverted to stable OpenCV release 2.4.6
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 12 Nov 2013 00:38:47 -0500 |
| parents | 8fdbc13dad8b |
| children | 17185fe77316 |
comparison
equal
deleted
inserted
replaced
| 428:70accfa6692f | 429:2be846d36dec |
|---|---|
| 120 capture = cv2.VideoCapture(filename) | 120 capture = cv2.VideoCapture(filename) |
| 121 if capture.isOpened(): | 121 if capture.isOpened(): |
| 122 key = -1 | 122 key = -1 |
| 123 ret = True | 123 ret = True |
| 124 frameNum = firstFrameNum | 124 frameNum = firstFrameNum |
| 125 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum) | 125 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) |
| 126 while ret and not quitKey(key): | 126 while ret and not quitKey(key): |
| 127 ret, img = capture.read() | 127 ret, img = capture.read() |
| 128 if ret: | 128 if ret: |
| 129 if printFrames: | 129 if printFrames: |
| 130 print('frame {0}'.format(frameNum)) | 130 print('frame {0}'.format(frameNum)) |
| 131 frameNum+=1 | 131 frameNum+=1 |
| 132 if text != None: | 132 if text != None: |
| 133 cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) | 133 cv2.putText(img, text, (10,50), cv2.cv.CV_FONT_HERSHEY_PLAIN, 1, cvRed) |
| 134 cvImshow('frame', img, rescale) | 134 cvImshow('frame', img, rescale) |
| 135 key = cv2.waitKey(wait) | 135 key = cv2.waitKey(wait) |
| 136 cv2.destroyAllWindows() | 136 cv2.destroyAllWindows() |
| 137 | 137 |
| 138 def getImagesFromVideo(videoFilename, firstFrameNum = 0, nFrames = 1, saveImage = False, outputPrefix = 'image'): | 138 def getImagesFromVideo(videoFilename, firstFrameNum = 0, nFrames = 1, saveImage = False, outputPrefix = 'image'): |
| 139 '''Returns nFrames images from the video sequence''' | 139 '''Returns nFrames images from the video sequence''' |
| 140 from math import floor, log10 | 140 from math import floor, log10 |
| 141 images = [] | 141 images = [] |
| 142 capture = cv2.VideoCapture(videoFilename) | 142 capture = cv2.VideoCapture(videoFilename) |
| 143 if capture.isOpened(): | 143 if capture.isOpened(): |
| 144 nDigits = int(floor(log10(capture.get(cv2.CAP_PROP_FRAME_COUNT))))+1 | 144 nDigits = int(floor(log10(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))))+1 |
| 145 ret = False | 145 ret = False |
| 146 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum) | 146 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) |
| 147 imgNum = 0 | 147 imgNum = 0 |
| 148 while imgNum<nFrames: | 148 while imgNum<nFrames: |
| 149 ret, img = capture.read() | 149 ret, img = capture.read() |
| 150 i = 0 | 150 i = 0 |
| 151 while not ret and i<10: | 151 while not ret and i<10: |
| 164 return images | 164 return images |
| 165 | 165 |
| 166 def getFPS(videoFilename): | 166 def getFPS(videoFilename): |
| 167 capture = cv2.VideoCapture(videoFilename) | 167 capture = cv2.VideoCapture(videoFilename) |
| 168 if capture.isOpened(): | 168 if capture.isOpened(): |
| 169 fps = capture.get(cv2.CAP_PROP_FPS) | 169 fps = capture.get(cv2.cv.CV_CAP_PROP_FPS) |
| 170 capture.release() | 170 capture.release() |
| 171 return fps | 171 return fps |
| 172 else: | 172 else: |
| 173 print 'Cannot load file ' + videoFilename | 173 print 'Cannot load file ' + videoFilename |
| 174 return None | 174 return None |
| 203 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.): | 203 def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.): |
| 204 '''Displays the objects overlaid frame by frame over the video ''' | 204 '''Displays the objects overlaid frame by frame over the video ''' |
| 205 from moving import userTypeNames | 205 from moving import userTypeNames |
| 206 | 206 |
| 207 capture = cv2.VideoCapture(videoFilename) | 207 capture = cv2.VideoCapture(videoFilename) |
| 208 width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) | 208 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) |
| 209 height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) | 209 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) |
| 210 if capture.isOpened(): | 210 if capture.isOpened(): |
| 211 key = -1 | 211 key = -1 |
| 212 ret = True | 212 ret = True |
| 213 frameNum = firstFrameNum | 213 frameNum = firstFrameNum |
| 214 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum) | 214 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) |
| 215 if not lastFrameNumArg: | 215 if not lastFrameNumArg: |
| 216 from sys import maxint | 216 from sys import maxint |
| 217 lastFrameNum = maxint | 217 lastFrameNum = maxint |
| 218 else: | 218 else: |
| 219 lastFrameNum = lastFrameNumArg | 219 lastFrameNum = lastFrameNumArg |
| 237 imgcrop, yCropMin, yCropMax, xCropMin, xCropMax = imageBox(img, obj, frameNum, homography, width, height) | 237 imgcrop, yCropMin, yCropMax, xCropMin, xCropMax = imageBox(img, obj, frameNum, homography, width, height) |
| 238 cv2.rectangle(img, (xCropMin, yCropMin), (xCropMax, yCropMax), cvBlue, 1) | 238 cv2.rectangle(img, (xCropMin, yCropMin), (xCropMax, yCropMax), cvBlue, 1) |
| 239 objDescription = '{} '.format(obj.num) | 239 objDescription = '{} '.format(obj.num) |
| 240 if userTypeNames[obj.userType] != 'unknown': | 240 if userTypeNames[obj.userType] != 'unknown': |
| 241 objDescription += userTypeNames[obj.userType][0].upper() | 241 objDescription += userTypeNames[obj.userType][0].upper() |
| 242 cv2.putText(img, objDescription, obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) | 242 cv2.putText(img, objDescription, obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.cv.CV_FONT_HERSHEY_PLAIN, 1, cvRed) |
| 243 cvImshow('frame', img, rescale) | 243 cvImshow('frame', img, rescale) |
| 244 key = cv2.waitKey() | 244 key = cv2.waitKey() |
| 245 if saveKey(key): | 245 if saveKey(key): |
| 246 cv2.imwrite('image.png', img) | 246 cv2.imwrite('image.png', img) |
| 247 frameNum += 1 | 247 frameNum += 1 |
