diff python/cvutils.py @ 708:a37c565f4b68

merged dev
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 22 Jul 2015 14:17:44 -0400
parents 94b291a5f933
children 35bc5e30a53f
line wrap: on
line diff
--- a/python/cvutils.py	Wed Jul 22 14:17:19 2015 -0400
+++ b/python/cvutils.py	Wed Jul 22 14:17:44 2015 -0400
@@ -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)
 
@@ -126,7 +126,8 @@
     def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1):
         '''Plays the video'''
         windowName = 'frame'
-        cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
+        if rescale == 1.:
+            cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
         wait = 5
         if frameRate > 0:
             wait = int(round(1000./frameRate))
@@ -200,7 +201,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 +236,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,10 +257,10 @@
         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 = []
+            croppedImg = None
         return croppedImg, yCropMin, yCropMax, xCropMin, xCropMax
 
 
@@ -270,7 +274,8 @@
         height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
 
         windowName = 'frame'
-        #cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
+        if rescale == 1.:
+            cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
 
         if undistort: # setup undistortion
             [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients)
@@ -538,10 +543,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 +556,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 = []