Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.py @ 680:da1352b89d02 dev
classification is working
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 05 Jun 2015 02:25:30 +0200 |
| parents | 5505f9dbb28e |
| children | 94b291a5f933 |
comparison
equal
deleted
inserted
replaced
| 678:97c305108460 | 680:da1352b89d02 |
|---|---|
| 15 except ImportError: | 15 except ImportError: |
| 16 print('Scikit-image library could not be loaded (HoG-based classification methods will not be available)') | 16 print('Scikit-image library could not be loaded (HoG-based classification methods will not be available)') |
| 17 skimageAvailable = False | 17 skimageAvailable = False |
| 18 | 18 |
| 19 from sys import stdout | 19 from sys import stdout |
| 20 from numpy import dot, array, append | 20 from numpy import dot, array, append, float32 |
| 21 | 21 |
| 22 #import aggdraw # agg on top of PIL (antialiased drawing) | 22 #import aggdraw # agg on top of PIL (antialiased drawing) |
| 23 | 23 |
| 24 | 24 |
| 25 cvRed = (0,0,255) | 25 cvRed = (0,0,255) |
| 198 '''Returns nFrames images from the video sequence''' | 198 '''Returns nFrames images from the video sequence''' |
| 199 from math import floor, log10 | 199 from math import floor, log10 |
| 200 images = [] | 200 images = [] |
| 201 capture = cv2.VideoCapture(videoFilename) | 201 capture = cv2.VideoCapture(videoFilename) |
| 202 if capture.isOpened(): | 202 if capture.isOpened(): |
| 203 nDigits = int(floor(log10(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))))+1 | 203 rawCount = capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) |
| 204 if rawCount < 0: | |
| 205 rawCount = firstFrameNum+nFrames+1 | |
| 206 nDigits = int(floor(log10(rawCount)))+1 | |
| 204 ret = False | 207 ret = False |
| 205 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) | 208 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) |
| 206 imgNum = 0 | 209 imgNum = 0 |
| 207 while imgNum<nFrames: | 210 while imgNum<nFrames: |
| 208 ret, img = capture.read() | 211 ret, img = capture.read() |
| 230 return fps | 233 return fps |
| 231 else: | 234 else: |
| 232 print('Video capture for {} failed'.format(videoFilename)) | 235 print('Video capture for {} failed'.format(videoFilename)) |
| 233 return None | 236 return None |
| 234 | 237 |
| 235 def imageBox(img, obj, frameNum, homography, width, height, px = 0.2, py = 0.2, pixelThreshold = 800): | 238 def imageBox(img, obj, frameNum, homography, width, height, px = 0.2, py = 0.2, minNPixels = 800): |
| 236 'Computes the bounding box of object at frameNum' | 239 'Computes the bounding box of object at frameNum' |
| 237 x = [] | 240 x = [] |
| 238 y = [] | 241 y = [] |
| 239 if obj.hasFeatures(): | 242 if obj.hasFeatures(): |
| 240 for f in obj.getFeatures(): | 243 for f in obj.getFeatures(): |
| 251 a = max(ymax - ymin + (2 * yMm), xmax - (xmin + 2 * xMm)) | 254 a = max(ymax - ymin + (2 * yMm), xmax - (xmin + 2 * xMm)) |
| 252 yCropMin = int(max(0, .5 * (ymin + ymax - a))) | 255 yCropMin = int(max(0, .5 * (ymin + ymax - a))) |
| 253 yCropMax = int(min(height - 1, .5 * (ymin + ymax + a))) | 256 yCropMax = int(min(height - 1, .5 * (ymin + ymax + a))) |
| 254 xCropMin = int(max(0, .5 * (xmin + xmax - a))) | 257 xCropMin = int(max(0, .5 * (xmin + xmax - a))) |
| 255 xCropMax = int(min(width - 1, .5 * (xmin + xmax + a))) | 258 xCropMax = int(min(width - 1, .5 * (xmin + xmax + a))) |
| 256 if yCropMax != yCropMin and xCropMax != xCropMin and (yCropMax - yCropMin) * (xCropMax - xCropMin) > pixelThreshold: | 259 if yCropMax != yCropMin and xCropMax != xCropMin and (yCropMax - yCropMin) * (xCropMax - xCropMin) > minNPixels: |
| 257 croppedImg = img[yCropMin : yCropMax, xCropMin : xCropMax] | 260 croppedImg = img[yCropMin : yCropMax, xCropMin : xCropMax] |
| 258 else: | 261 else: |
| 259 croppedImg = [] | 262 croppedImg = [] |
| 260 return croppedImg, yCropMin, yCropMax, xCropMin, xCropMax | 263 return croppedImg, yCropMin, yCropMax, xCropMin, xCropMax |
| 261 | 264 |
| 536 else: | 539 else: |
| 537 print(dp) | 540 print(dp) |
| 538 return None | 541 return None |
| 539 | 542 |
| 540 if skimageAvailable: | 543 if skimageAvailable: |
| 544 from skimage.feature import hog | |
| 545 from skimage import color, transform | |
| 546 | |
| 541 def HOG(image, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False): | 547 def HOG(image, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False): |
| 542 from skimage.feature import hog | |
| 543 from skimage import color, transform | |
| 544 | |
| 545 bwImg = color.rgb2gray(image) | 548 bwImg = color.rgb2gray(image) |
| 546 inputImg = transform.resize(bwImg, rescaleSize) | 549 inputImg = transform.resize(bwImg, rescaleSize) |
| 547 features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize) | 550 features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize) |
| 548 if visualize: | 551 if visualize: |
| 549 from matplotlib.pyplot import imshow, figure, subplot | 552 from matplotlib.pyplot import imshow, figure, subplot |
| 550 hogViz = features[1] | 553 hogViz = features[1] |
| 551 features = features[0] | 554 features = features[0] |
| 552 figure() | 555 figure() |
| 553 subplot(1,2,1) | 556 subplot(1,2,1) |
| 554 imshow(img) | 557 imshow(inputImg) |
| 555 subplot(1,2,2) | 558 subplot(1,2,2) |
| 556 imshow(hogViz) | 559 imshow(hogViz) |
| 557 return features | 560 return float32(features) |
| 558 | 561 |
| 559 def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False): | 562 def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False): |
| 560 from os import listdir | 563 from os import listdir |
| 561 from numpy import float32 | |
| 562 from matplotlib.pyplot import imread | 564 from matplotlib.pyplot import imread |
| 563 | 565 |
| 564 inputData = [] | 566 inputData = [] |
| 565 for filename in listdir(imageDirectory): | 567 for filename in listdir(imageDirectory): |
| 566 img = imread(imageDirectory+filename) | 568 img = imread(imageDirectory+filename) |
