Mercurial Hosting > traffic-intelligence
comparison python/cvutils.py @ 385:1917db662aa7
added rescaling options to scripts play-video and display-trajectories
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 22 Jul 2013 18:33:47 -0400 |
parents | 6da9cf5609aa |
children | eaf7765221d9 |
comparison
equal
deleted
inserted
replaced
384:6da9cf5609aa | 385:1917db662aa7 |
---|---|
98 if lastCoordinate != None and lastCoordinate >=0: | 98 if lastCoordinate != None and lastCoordinate >=0: |
99 last = min(positions.length()-1, lastCoordinate) | 99 last = min(positions.length()-1, lastCoordinate) |
100 for i in range(0, last-1): | 100 for i in range(0, last-1): |
101 cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color) | 101 cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color) |
102 | 102 |
103 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None): | 103 def cvImshow(windowName, img, rescale = 1.0): |
104 'Rescales the image (in particular if too large)' | |
105 from cv2 import resize | |
106 if rescale != 1.: | |
107 size = (int(round(img.shape[1]*rescale)), int(round(img.shape[0]*rescale))) | |
108 resizedImg = resize(img, size) | |
109 cv2.imshow(windowName, resizedImg) | |
110 else: | |
111 cv2.imshow(windowName, img) | |
112 | |
113 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1.): | |
104 '''Plays the video''' | 114 '''Plays the video''' |
105 wait = 5 | 115 wait = 5 |
106 if frameRate > 0: | 116 if frameRate > 0: |
107 wait = int(round(1000./frameRate)) | 117 wait = int(round(1000./frameRate)) |
108 if interactive: | 118 if interactive: |
119 if printFrames: | 129 if printFrames: |
120 print('frame {0}'.format(frameNum)) | 130 print('frame {0}'.format(frameNum)) |
121 frameNum+=1 | 131 frameNum+=1 |
122 if text != None: | 132 if text != None: |
123 cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) | 133 cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) |
124 cv2.imshow('frame', img) | 134 cvImshow('frame', img, rescale) |
125 key = cv2.waitKey(wait) | 135 key = cv2.waitKey(wait) |
126 cv2.destroyAllWindows() | 136 cv2.destroyAllWindows() |
127 | 137 |
128 def getImagesFromVideo(filename, nImages = 1, saveImage = False): | 138 def getImagesFromVideo(filename, nImages = 1, saveImage = False): |
129 '''Returns nImages images from the video sequence''' | 139 '''Returns nImages images from the video sequence''' |
144 cv2.imwrite('image{0:04d}.png'.format(numImg), img) | 154 cv2.imwrite('image{0:04d}.png'.format(numImg), img) |
145 else: | 155 else: |
146 images.append(img) | 156 images.append(img) |
147 return images | 157 return images |
148 | 158 |
149 def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True): | 159 def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.): |
150 '''Displays the objects overlaid frame by frame over the video ''' | 160 '''Displays the objects overlaid frame by frame over the video ''' |
151 capture = cv2.VideoCapture(videoFilename) | 161 capture = cv2.VideoCapture(videoFilename) |
152 if capture.isOpened(): | 162 if capture.isOpened(): |
153 key = -1 | 163 key = -1 |
154 ret = True | 164 ret = True |
171 obj.projectedPositions = obj.positions.project(homography) | 181 obj.projectedPositions = obj.positions.project(homography) |
172 else: | 182 else: |
173 obj.projectedPositions = obj.positions | 183 obj.projectedPositions = obj.positions |
174 draw(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant()) | 184 draw(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant()) |
175 cv2.putText(img, '{0}'.format(obj.num), obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) | 185 cv2.putText(img, '{0}'.format(obj.num), obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) |
176 cv2.imshow('frame', img) | 186 cvImshow('frame', img, rescale) |
177 key = cv2.waitKey() | 187 key = cv2.waitKey() |
178 if saveKey(key): | 188 if saveKey(key): |
179 cv2.imwrite('image.png', img) | 189 cv2.imwrite('image.png', img) |
180 frameNum += 1 | 190 frameNum += 1 |
181 cv2.destroyAllWindows() | 191 cv2.destroyAllWindows() |