diff 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
line wrap: on
line diff
--- a/python/cvutils.py	Wed Jun 03 16:00:46 2015 +0200
+++ b/python/cvutils.py	Fri Jun 05 02:25:30 2015 +0200
@@ -17,7 +17,7 @@
     skimageAvailable = False
     
 from sys import stdout
-from numpy import dot, array, append
+from numpy import dot, array, append, float32
 
 #import aggdraw # agg on top of PIL (antialiased drawing)
 
@@ -200,7 +200,10 @@
         images = []
         capture = cv2.VideoCapture(videoFilename)
         if capture.isOpened():
-            nDigits = int(floor(log10(capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))))+1
+            rawCount = capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
+            if rawCount < 0:
+                rawCount = firstFrameNum+nFrames+1
+            nDigits = int(floor(log10(rawCount)))+1
             ret = False
             capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum)
             imgNum = 0
@@ -232,7 +235,7 @@
             print('Video capture for {} failed'.format(videoFilename))
             return None
 
-    def imageBox(img, obj, frameNum, homography, width, height, px = 0.2, py = 0.2, pixelThreshold = 800):
+    def imageBox(img, obj, frameNum, homography, width, height, px = 0.2, py = 0.2, minNPixels = 800):
         'Computes the bounding box of object at frameNum'
         x = []
         y = []
@@ -253,7 +256,7 @@
         yCropMax = int(min(height - 1, .5 * (ymin + ymax + a)))
         xCropMin = int(max(0, .5 * (xmin + xmax - a)))
         xCropMax = int(min(width - 1, .5 * (xmin + xmax + a)))
-        if yCropMax != yCropMin and xCropMax != xCropMin and (yCropMax - yCropMin) * (xCropMax - xCropMin) > pixelThreshold:
+        if yCropMax != yCropMin and xCropMax != xCropMin and (yCropMax - yCropMin) * (xCropMax - xCropMin) > minNPixels:
             croppedImg = img[yCropMin : yCropMax, xCropMin : xCropMax]
         else:
             croppedImg = []
@@ -538,10 +541,10 @@
             return None
 
 if skimageAvailable:
+    from skimage.feature import hog
+    from skimage import color, transform
+    
     def HOG(image, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False):
-        from skimage.feature import hog
-        from skimage import color, transform
-
         bwImg = color.rgb2gray(image)
         inputImg = transform.resize(bwImg, rescaleSize)
         features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, visualize, normalize)
@@ -551,14 +554,13 @@
             features = features[0]
             figure()
             subplot(1,2,1)
-            imshow(img)
+            imshow(inputImg)
             subplot(1,2,2)
             imshow(hogViz)
-        return features
+        return float32(features)
 
     def createHOGTrainingSet(imageDirectory, classLabel, rescaleSize = (64, 64), orientations=9, pixelsPerCell=(8, 8), cellsPerBlock=(2, 2), visualize=False, normalize=False):
         from os import listdir
-        from numpy import float32
         from matplotlib.pyplot import imread
 
         inputData = []