comparison python/cvutils.py @ 411:31604ef1cad4

added hog functions and the display of road user types if known
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 21 Aug 2013 00:00:23 -0400
parents 91954c76d12c
children 8fdbc13dad8b
comparison
equal deleted inserted replaced
410:91954c76d12c 411:31604ef1cad4
171 return fps 171 return fps
172 else: 172 else:
173 print 'Cannot load file ' + videoFilename 173 print 'Cannot load file ' + videoFilename
174 return None 174 return None
175 175
176 def imageBox(img, obj, frameNum, homography, width, height, px=.2, py=.2, PixelThreshold=800):
177 x = []
178 y = []
179 for f in obj.features:
180 if f.existsAtInstant(frameNum):
181 x.append(f.getPositionAtInstant(frameNum).project(homography).x)
182 y.append(f.getPositionAtInstant(frameNum).project(homography).y)
183 Xmin = min(x)
184 Xmax = max(x)
185 Ymin = min(y)
186 Ymax = max(y)
187 XMm = px * (Xmax - Xmin)
188 YMm = py * (Ymax - Ymin)
189 a = max(Ymax - Ymin + (2 * YMm), Xmax - (Xmin + 2 * XMm))
190 Y_Crop_min = int(max(0, .5 * (Ymin + Ymax - a)))
191 Y_Crop_Max = int(min(height - 1, .5 * (Ymin + Ymax + a)))
192 X_Crop_min = int(max(0, .5 * (Xmin + Xmax - a)))
193 X_Crop_Max = int(min(width - 1, .5 * (Xmin + Xmax + a)))
194 if Y_Crop_Max != Y_Crop_min and X_Crop_Max != X_Crop_min and (Y_Crop_Max - Y_Crop_min) * (X_Crop_Max - X_Crop_min) > PixelThreshold:
195 img_crop = img[Y_Crop_min : Y_Crop_Max, X_Crop_min : X_Crop_Max]
196 else:
197 img_crop = []
198 return img_crop, Y_Crop_min, Y_Crop_Max, X_Crop_min, X_Crop_Max
199
200
176 def displayTrajectories(videoFilename, objects, boundingBoxes, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.): 201 def displayTrajectories(videoFilename, objects, boundingBoxes, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1.):
177 '''Displays the objects overlaid frame by frame over the video ''' 202 '''Displays the objects overlaid frame by frame over the video '''
203 from moving import userTypeNames
204
178 capture = cv2.VideoCapture(videoFilename) 205 capture = cv2.VideoCapture(videoFilename)
206 width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
207 height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
179 if capture.isOpened(): 208 if capture.isOpened():
180 key = -1 209 key = -1
181 ret = True 210 ret = True
182 frameNum = firstFrameNum 211 frameNum = firstFrameNum
183 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum) 212 capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum)
200 obj.projectedPositions = obj.positions 229 obj.projectedPositions = obj.positions
201 draw(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant()) 230 draw(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant())
202 if frameNum in boundingBoxes.keys(): 231 if frameNum in boundingBoxes.keys():
203 for rect in boundingBoxes[frameNum]: 232 for rect in boundingBoxes[frameNum]:
204 cv2.rectangle(img, rect[0].asint().astuple(), rect[1].asint().astuple(), cvRed) 233 cv2.rectangle(img, rect[0].asint().astuple(), rect[1].asint().astuple(), cvRed)
205 cv2.putText(img, '{0}'.format(obj.num), obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) 234 objDescription = '{} '.format(obj.num)
235 if userTypeNames[obj.userType] != 'unknown':
236 objDescription += userTypeNames[obj.userType][0].upper()
237 cv2.putText(img, objDescription, obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed)
238 if obj.features != None:
239 img_crop, Y_Crop_min, Y_Crop_Max, X_Crop_min, X_Crop_Max = imageBox(img, obj, frameNum, homography, width, height)
240 cv2.rectangle(img, (X_Crop_min, Y_Crop_min), (X_Crop_Max, Y_Crop_Max), cvBlue, 1)
206 cvImshow('frame', img, rescale) 241 cvImshow('frame', img, rescale)
207 key = cv2.waitKey() 242 key = cv2.waitKey()
208 if saveKey(key): 243 if saveKey(key):
209 cv2.imwrite('image.png', img) 244 cv2.imwrite('image.png', img)
210 frameNum += 1 245 frameNum += 1
297 else: 332 else:
298 print(dp) 333 print(dp)
299 return None 334 return None
300 335
301 if skimageAvailable: 336 if skimageAvailable:
302 def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(3, 3), visualize=False, normalize=False): 337 def HOG(image, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False):
303 from os import listdir
304 from matplotlib.pyplot import imread, imshow, figure, subplot
305 from skimage.feature import hog 338 from skimage.feature import hog
306 from skimage import color, transform 339 from skimage import color, transform
340
341 bwImg = color.rgb2gray(image)
342 inputImg = transform.resize(bwImg, rescaleSize)
343 features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize)
344 if visualize:
345 from matplotlib.pyplot import imshow, figure, subplot
346 hogViz = features[1]
347 features = features[0]
348 figure()
349 subplot(1,2,1)
350 imshow(img)
351 subplot(1,2,2)
352 imshow(hogViz)
353 return features
354
355 def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False):
356 from os import listdir
307 from numpy import array, float32 357 from numpy import array, float32
358 from matplotlib.pyplot import imread
308 359
309 inputData = [] 360 inputData = []
310 for filename in listdir(imageDirectory): 361 for filename in listdir(imageDirectory):
311 img = imread(imageDirectory+filename) 362 img = imread(imageDirectory+filename)
312 bwImg = color.rgb2gray(img) 363 features = HOG(img, rescaleSize, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize)
313 inputImg = transform.resize(bwImg, rescaleSize)
314 features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize)
315 if visualize:
316 hogViz = features[1]
317 features = features[0]
318 figure()
319 subplot(1,2,1)
320 imshow(img)
321 subplot(1,2,2)
322 imshow(hogViz)
323 inputData.append(features) 364 inputData.append(features)
324 365
325 nImages = len(inputData) 366 nImages = len(inputData)
326 return array(inputData, dtype = float32), array([classLabel]*nImages, dtype = float32) 367 return array(inputData, dtype = float32), array([classLabel]*nImages, dtype = float32)
327 368