Mercurial Hosting > traffic-intelligence
comparison python/cvutils.py @ 821:26daf35180ad
finished modification and demo script to replay synchronized video (with same frame rate)
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 22 Jun 2016 16:36:12 -0400 |
parents | e73e7b644428 |
children | 2faabcbde2c4 |
comparison
equal
deleted
inserted
replaced
820:e73e7b644428 | 821:26daf35180ad |
---|---|
144 newCameraMatrix = deepcopy(intrinsicCameraMatrix) | 144 newCameraMatrix = deepcopy(intrinsicCameraMatrix) |
145 newCameraMatrix[0,2] = newImgSize[0]/2. | 145 newCameraMatrix[0,2] = newImgSize[0]/2. |
146 newCameraMatrix[1,2] = newImgSize[1]/2. | 146 newCameraMatrix[1,2] = newImgSize[1]/2. |
147 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1) | 147 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1) |
148 | 148 |
149 def playVideo(filenames, windowNames = None, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1): | 149 def playVideo(filenames, windowNames = None, firstFrameNums = None, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1): |
150 '''Plays the video(s)''' | 150 '''Plays the video(s)''' |
151 if len(filenames) == 0: | |
152 print('Empty filename list') | |
153 return | |
151 if windowNames is None: | 154 if windowNames is None: |
152 windowNames = ['frame{}'.format(i) for i in xrange(len(filenames))] | 155 windowNames = ['frame{}'.format(i) for i in xrange(len(filenames))] |
153 wait = 5 | 156 wait = 5 |
154 if rescale == 1.: | 157 if rescale == 1.: |
155 for windowName in windowNames: | 158 for windowName in windowNames: |
160 wait = 0 | 163 wait = 0 |
161 captures = [cv2.VideoCapture(fn) for fn in filenames] | 164 captures = [cv2.VideoCapture(fn) for fn in filenames] |
162 if array([cap.isOpened() for cap in captures]).all(): | 165 if array([cap.isOpened() for cap in captures]).all(): |
163 key = -1 | 166 key = -1 |
164 ret = True | 167 ret = True |
165 frameNum = firstFrameNum | 168 nFramesShown = 0 |
166 for cap in captures: | 169 if firstFrameNums is not None: |
167 cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) | 170 for i in xrange(len(captures)): |
171 captures[i].set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNums[i]) | |
168 while ret and not quitKey(key): | 172 while ret and not quitKey(key): |
169 rets = [] | 173 rets = [] |
170 images = [] | 174 images = [] |
171 for cap in captures: | 175 for cap in captures: |
172 ret, img = cap.read() | 176 ret, img = cap.read() |
173 rets.append(ret) | 177 rets.append(ret) |
174 images.append(img) | 178 images.append(img) |
175 if array(rets).all(): | 179 if array(rets).all(): |
176 if printFrames: | 180 if printFrames: |
177 print('frame {0}'.format(frameNum)) | 181 print('frame shown {0}'.format(nFramesShown)) |
178 #if text is not None: | 182 for i in xrange(len(filenames)): |
179 # cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) | 183 if text is not None: |
180 for i in xrange(1, len(filenames)): | 184 cv2.putText(images[i], text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) |
181 cvImshow(windowNames[i], images[i], rescale) | 185 cvImshow(windowNames[i], images[i], rescale) # cv2.imshow('frame', img) |
182 cvImshow(windowNames[0], images[0], rescale) # cv2.imshow('frame', img) | |
183 key = cv2.waitKey(wait) | 186 key = cv2.waitKey(wait) |
184 if saveKey(key): | 187 if saveKey(key): |
185 cv2.imwrite('image-{}.png'.format(frameNum), img) | 188 cv2.imwrite('image-{}.png'.format(frameNum), img) |
186 frameNum += step | 189 nFramesShown += step |
187 if step > 1: | 190 if step > 1: |
188 for cap in captures: | 191 for i in xrange(len(captures)): |
189 cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) | 192 captures.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNums[i]+nFramesShown) |
190 cv2.destroyAllWindows() | 193 cv2.destroyAllWindows() |
191 else: | 194 else: |
192 print('Video captures for {} failed'.format(filenames)) | 195 print('Video captures for {} failed'.format(filenames)) |
193 | 196 |
194 def infoVideo(filename): | 197 def infoVideo(filename): |