comparison python/cvutils.py @ 820:e73e7b644428

generalized play-video for several files (already synchronized
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 22 Jun 2016 15:23:20 -0400
parents 52aa03260f03
children 26daf35180ad
comparison
equal deleted inserted replaced
819:fc8b3ce629d1 820:e73e7b644428
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(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1): 149 def playVideo(filenames, windowNames = None, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1):
150 '''Plays the video''' 150 '''Plays the video(s)'''
151 windowName = 'frame' 151 if windowNames is None:
152 windowNames = ['frame{}'.format(i) for i in xrange(len(filenames))]
152 wait = 5 153 wait = 5
153 if rescale == 1.: 154 if rescale == 1.:
154 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) 155 for windowName in windowNames:
156 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
155 if frameRate > 0: 157 if frameRate > 0:
156 wait = int(round(1000./frameRate)) 158 wait = int(round(1000./frameRate))
157 if interactive: 159 if interactive:
158 wait = 0 160 wait = 0
159 capture = cv2.VideoCapture(filename) 161 captures = [cv2.VideoCapture(fn) for fn in filenames]
160 if capture.isOpened(): 162 if array([cap.isOpened() for cap in captures]).all():
161 key = -1 163 key = -1
162 ret = True 164 ret = True
163 frameNum = firstFrameNum 165 frameNum = firstFrameNum
164 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) 166 for cap in captures:
167 cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum)
165 while ret and not quitKey(key): 168 while ret and not quitKey(key):
166 ret, img = capture.read() 169 rets = []
167 if ret: 170 images = []
171 for cap in captures:
172 ret, img = cap.read()
173 rets.append(ret)
174 images.append(img)
175 if array(rets).all():
168 if printFrames: 176 if printFrames:
169 print('frame {0}'.format(frameNum)) 177 print('frame {0}'.format(frameNum))
170 if text is not None: 178 #if text is not None:
171 cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) 179 # cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed)
172 cv2.imshow('frame', img)#cvImshow(windowName, img, rescale) 180 for i in xrange(1, len(filenames)):
181 cvImshow(windowNames[i], images[i], rescale)
182 cvImshow(windowNames[0], images[0], rescale) # cv2.imshow('frame', img)
173 key = cv2.waitKey(wait) 183 key = cv2.waitKey(wait)
174 if saveKey(key): 184 if saveKey(key):
175 cv2.imwrite('image-{}.png'.format(frameNum), img) 185 cv2.imwrite('image-{}.png'.format(frameNum), img)
176 frameNum += step 186 frameNum += step
177 if step > 1: 187 if step > 1:
178 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) 188 for cap in captures:
189 cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum)
179 cv2.destroyAllWindows() 190 cv2.destroyAllWindows()
180 else: 191 else:
181 print('Video capture for {} failed'.format(filename)) 192 print('Video captures for {} failed'.format(filenames))
182 193
183 def infoVideo(filename): 194 def infoVideo(filename):
184 '''Provides all available info on video ''' 195 '''Provides all available info on video '''
185 cvPropertyNames = {cv2.cv.CV_CAP_PROP_FORMAT: "format", 196 cvPropertyNames = {cv2.cv.CV_CAP_PROP_FORMAT: "format",
186 cv2.cv.CV_CAP_PROP_FOURCC: "codec (fourcc)", 197 cv2.cv.CV_CAP_PROP_FOURCC: "codec (fourcc)",