Mercurial Hosting > traffic-intelligence
comparison python/cvutils.py @ 657:51269511229b
added script printing video info
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 13 May 2015 17:24:19 +0200 |
parents | 4e7925cb4f8f |
children | 6668f541b915 |
comparison
equal
deleted
inserted
replaced
656:2813d74b3635 | 657:51269511229b |
---|---|
34 return chr(key&255)== 'q' or chr(key&255) == 'Q' | 34 return chr(key&255)== 'q' or chr(key&255) == 'Q' |
35 | 35 |
36 def saveKey(key): | 36 def saveKey(key): |
37 return chr(key&255) == 's' | 37 return chr(key&255) == 's' |
38 | 38 |
39 def int2FOURCC(x): | |
40 fourcc = '' | |
41 for i in xrange(4): | |
42 fourcc += unichr((x >> 8*i)&255) | |
43 return fourcc | |
44 | |
39 def plotLines(filename, origins, destinations, w = 1, resultFilename='image.png'): | 45 def plotLines(filename, origins, destinations, w = 1, resultFilename='image.png'): |
40 '''Draws lines over the image ''' | 46 '''Draws lines over the image ''' |
41 import Image, ImageDraw # PIL | 47 import Image, ImageDraw # PIL |
42 | 48 |
43 img = Image.open(filename) | 49 img = Image.open(filename) |
117 newCameraMatrix = deepcopy(intrinsicCameraMatrix) | 123 newCameraMatrix = deepcopy(intrinsicCameraMatrix) |
118 newCameraMatrix[0,2] = newImgSize[0]/2. | 124 newCameraMatrix[0,2] = newImgSize[0]/2. |
119 newCameraMatrix[1,2] = newImgSize[1]/2. | 125 newCameraMatrix[1,2] = newImgSize[1]/2. |
120 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1) | 126 return cv2.initUndistortRectifyMap(intrinsicCameraMatrix, array(distortionCoefficients), identity(3), newCameraMatrix, newImgSize, cv2.CV_32FC1) |
121 | 127 |
122 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1.): | 128 def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1): |
123 '''Plays the video''' | 129 '''Plays the video''' |
124 windowName = 'frame' | 130 windowName = 'frame' |
125 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) | 131 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) |
126 wait = 5 | 132 wait = 5 |
127 if frameRate > 0: | 133 if frameRate > 0: |
133 key = -1 | 139 key = -1 |
134 ret = True | 140 ret = True |
135 frameNum = firstFrameNum | 141 frameNum = firstFrameNum |
136 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) | 142 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) |
137 while ret and not quitKey(key): | 143 while ret and not quitKey(key): |
138 ret, img = capture.read() | 144 #ret, img = capture.read() |
145 for i in xrange(step): | |
146 ret, img = capture.read() | |
139 if ret: | 147 if ret: |
140 if printFrames: | 148 if printFrames: |
141 print('frame {0}'.format(frameNum)) | 149 print('frame {0}'.format(frameNum)) |
142 frameNum+=1 | 150 frameNum+=step |
143 if text is not None: | 151 if text is not None: |
144 cv2.putText(img, text, (10,50), cv2.cv.CV_FONT_HERSHEY_PLAIN, 1, cvRed) | 152 cv2.putText(img, text, (10,50), cv2.cv.CV_FONT_HERSHEY_PLAIN, 1, cvRed) |
145 cvImshow(windowName, img, rescale) | 153 cvImshow(windowName, img, rescale) |
146 key = cv2.waitKey(wait) | 154 key = cv2.waitKey(wait) |
147 if saveKey(key): | 155 if saveKey(key): |
148 cv2.imwrite('image-{}.png'.format(frameNum), img) | 156 cv2.imwrite('image-{}.png'.format(frameNum), img) |
149 cv2.destroyAllWindows() | 157 cv2.destroyAllWindows() |
158 else: | |
159 print('Video capture for {} failed'.format(filename)) | |
160 | |
161 def infoVideo(filename): | |
162 '''Provides all available info on video ''' | |
163 cvPropertyNames = {cv2.cv.CV_CAP_PROP_FORMAT: "format", | |
164 cv2.cv.CV_CAP_PROP_FOURCC: "codec (fourcc)", | |
165 cv2.cv.CV_CAP_PROP_FPS: "fps", | |
166 cv2.cv.CV_CAP_PROP_FRAME_COUNT: "number of frames", | |
167 cv2.cv.CV_CAP_PROP_FRAME_HEIGHT: "heigh", | |
168 cv2.cv.CV_CAP_PROP_FRAME_WIDTH: "width", | |
169 cv2.cv.CV_CAP_PROP_RECTIFICATION: "rectification", | |
170 cv2.cv.CV_CAP_PROP_SATURATION: "saturation"} | |
171 capture = cv2.VideoCapture(filename) | |
172 if capture.isOpened(): | |
173 for cvprop in [#cv2.cv.CV_CAP_PROP_BRIGHTNESS | |
174 #cv2.cv.CV_CAP_PROP_CONTRAST | |
175 #cv2.cv.CV_CAP_PROP_CONVERT_RGB | |
176 #cv2.cv.CV_CAP_PROP_EXPOSURE | |
177 cv2.cv.CV_CAP_PROP_FORMAT, | |
178 cv2.cv.CV_CAP_PROP_FOURCC, | |
179 cv2.cv.CV_CAP_PROP_FPS, | |
180 cv2.cv.CV_CAP_PROP_FRAME_COUNT, | |
181 cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, | |
182 cv2.cv.CV_CAP_PROP_FRAME_WIDTH, | |
183 #cv2.cv.CV_CAP_PROP_GAIN, | |
184 #cv2.cv.CV_CAP_PROP_HUE | |
185 #cv2.cv.CV_CAP_PROP_MODE | |
186 #cv2.cv.CV_CAP_PROP_POS_AVI_RATIO | |
187 #cv2.cv.CV_CAP_PROP_POS_FRAMES | |
188 #cv2.cv.CV_CAP_PROP_POS_MSEC | |
189 #cv2.cv.CV_CAP_PROP_RECTIFICATION, | |
190 #cv2.cv.CV_CAP_PROP_SATURATION | |
191 ]: | |
192 prop = capture.get(cvprop) | |
193 if cvprop == cv2.cv.CV_CAP_PROP_FOURCC and prop > 0: | |
194 prop = int2FOURCC(int(prop)) | |
195 print('Video {}: {}'.format(cvPropertyNames[cvprop], prop)) | |
150 else: | 196 else: |
151 print('Video capture for {} failed'.format(filename)) | 197 print('Video capture for {} failed'.format(filename)) |
152 | 198 |
153 def getImagesFromVideo(videoFilename, firstFrameNum = 0, nFrames = 1, saveImage = False, outputPrefix = 'image'): | 199 def getImagesFromVideo(videoFilename, firstFrameNum = 0, nFrames = 1, saveImage = False, outputPrefix = 'image'): |
154 '''Returns nFrames images from the video sequence''' | 200 '''Returns nFrames images from the video sequence''' |