Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.py @ 381:387cc0142211
script to replay event annotations
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 19 Jul 2013 11:58:35 -0400 |
| parents | 22ddb8f85495 |
| children | 6da9cf5609aa |
comparison
equal
deleted
inserted
replaced
| 380:adfd4f70ee1d | 381:387cc0142211 |
|---|---|
| 96 if lastCoordinate != None and lastCoordinate >=0: | 96 if lastCoordinate != None and lastCoordinate >=0: |
| 97 last = min(positions.length()-1, lastCoordinate) | 97 last = min(positions.length()-1, lastCoordinate) |
| 98 for i in range(0, last-1): | 98 for i in range(0, last-1): |
| 99 cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color) | 99 cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color) |
| 100 | 100 |
| 101 def playVideo(filename, firstFrameNum = 0, frameRate = -1): | 101 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None): |
| 102 '''Plays the video''' | 102 '''Plays the video''' |
| 103 wait = 5 | 103 wait = 5 |
| 104 if frameRate > 0: | 104 if frameRate > 0: |
| 105 wait = int(round(1000./frameRate)) | 105 wait = int(round(1000./frameRate)) |
| 106 if interactive: | |
| 107 wait = 0 | |
| 106 capture = cv2.VideoCapture(filename) | 108 capture = cv2.VideoCapture(filename) |
| 107 if capture.isOpened(): | 109 if capture.isOpened(): |
| 108 key = -1 | 110 key = -1 |
| 109 ret = True | 111 ret = True |
| 110 frameNum = firstFrameNum | 112 frameNum = firstFrameNum |
| 111 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) | 113 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) |
| 112 while ret and not quitKey(key): | 114 while ret and not quitKey(key): |
| 113 ret, img = capture.read() | 115 ret, img = capture.read() |
| 114 if ret: | 116 if ret: |
| 115 print('frame {0}'.format(frameNum)) | 117 if printFrames: |
| 118 print('frame {0}'.format(frameNum)) | |
| 116 frameNum+=1 | 119 frameNum+=1 |
| 120 if text != None: | |
| 121 cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) | |
| 117 cv2.imshow('frame', img) | 122 cv2.imshow('frame', img) |
| 118 key = cv2.waitKey(wait) | 123 key = cv2.waitKey(wait) |
| 119 cv2.destroyAllWindows() | 124 cv2.destroyAllWindows() |
| 120 | 125 |
| 121 def getImagesFromVideo(filename, nImages = 1, saveImage = False): | 126 def getImagesFromVideo(filename, nImages = 1, saveImage = False): |
| 137 cv2.imwrite('image{0:04d}.png'.format(numImg), img) | 142 cv2.imwrite('image{0:04d}.png'.format(numImg), img) |
| 138 else: | 143 else: |
| 139 images.append(img) | 144 images.append(img) |
| 140 return images | 145 return images |
| 141 | 146 |
| 142 def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0, lastFrameNumArg = None): | 147 def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True): |
| 143 '''Displays the objects overlaid frame by frame over the video ''' | 148 '''Displays the objects overlaid frame by frame over the video ''' |
| 144 capture = cv2.VideoCapture(videoFilename) | 149 capture = cv2.VideoCapture(videoFilename) |
| 145 if capture.isOpened(): | 150 if capture.isOpened(): |
| 146 key = -1 | 151 key = -1 |
| 147 ret = True | 152 ret = True |
| 153 else: | 158 else: |
| 154 lastFrameNum = lastFrameNumArg | 159 lastFrameNum = lastFrameNumArg |
| 155 while ret and not quitKey(key) and frameNum < lastFrameNum: | 160 while ret and not quitKey(key) and frameNum < lastFrameNum: |
| 156 ret, img = capture.read() | 161 ret, img = capture.read() |
| 157 if ret: | 162 if ret: |
| 158 print('frame {0}'.format(frameNum)) | 163 if printFrames: |
| 164 print('frame {0}'.format(frameNum)) | |
| 159 for obj in objects: | 165 for obj in objects: |
| 160 if obj.existsAtInstant(frameNum): | 166 if obj.existsAtInstant(frameNum): |
| 161 if not hasattr(obj, 'projectedPositions'): | 167 if not hasattr(obj, 'projectedPositions'): |
| 162 if homography != None: | 168 if homography != None: |
| 163 obj.projectedPositions = obj.positions.project(homography) | 169 obj.projectedPositions = obj.positions.project(homography) |
