changeset 901:753a081989e2

factorized some argument handling code
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 22 Jun 2017 12:02:34 -0400
parents 85b81c46c526
children c69a8defe5c3
files python/storage.py scripts/classify-objects.py scripts/display-trajectories.py scripts/extract-appearance-images.py
diffstat 4 files changed, 62 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/python/storage.py	Wed Jun 21 17:49:58 2017 -0400
+++ b/python/storage.py	Thu Jun 22 12:02:34 2017 -0400
@@ -7,7 +7,7 @@
 
 from os import path
 import sqlite3, logging
-from numpy import log, min as npmin, max as npmax, round as npround, array, sum as npsum, loadtxt, floor as npfloor, ceil as npceil
+from numpy import log, min as npmin, max as npmax, round as npround, array, sum as npsum, loadtxt, floor as npfloor, ceil as npceil, linalg
 from pandas import read_csv, merge
 
 
@@ -1366,6 +1366,45 @@
         else:
             print('Configuration filename {} could not be loaded.'.format(filename))
 
+def processVideoArguments(args):
+    '''Loads information from configuration file
+    then checks what was passed on the command line
+    for override (eg video filename and database filename'''
+    if args.configFilename is not None: # consider there is a configuration file
+        params = ProcessParameters(args.configFilename)
+        videoFilename = params.videoFilename
+        databaseFilename = params.databaseFilename
+        if params.homography is not None:
+            invHomography = linalg.inv(params.homography)
+        else:
+            invHomography = None
+        intrinsicCameraMatrix = params.intrinsicCameraMatrix
+        distortionCoefficients = params.distortionCoefficients
+        undistortedImageMultiplication = params.undistortedImageMultiplication
+        undistort = params.undistort
+        firstFrameNum = params.firstFrameNum
+    else:
+        invHomography = None
+        undistort = False
+        intrinsicCameraMatrix = None
+        distortionCoefficients = []
+        undistortedImageMultiplication = None
+        undistort = False
+        firstFrameNum = 0
+
+    # override video and database filenames if present on command line
+    if args.videoFilename is not None:
+        videoFilename = args.videoFilename
+    else:
+        videoFilename = params.videoFilename
+    if args.databaseFilename is not None:
+        databaseFilename = args.databaseFilename
+    else:
+        databaseFilename = params.databaseFilename
+
+    return params, videoFilename, databaseFilename, invHomography, intrinsicCameraMatrix, distortionCoefficients, undistortedImageMultiplication, undistort, firstFrameNum
+    
+# deprecated
 class SceneParameters(object):
     def __init__(self, config, sectionName):
         from ConfigParser import NoOptionError
--- a/scripts/classify-objects.py	Wed Jun 21 17:49:58 2017 -0400
+++ b/scripts/classify-objects.py	Thu Jun 22 12:02:34 2017 -0400
@@ -19,24 +19,11 @@
 parser.add_argument('--max-speed-distribution-plot', dest = 'maxSpeedDistributionPlot', help = 'if plotting the user distributions, the maximum speed to display (km/h)', type = float, default = 50.)
 
 args = parser.parse_args()
-params = storage.ProcessParameters(args.configFilename)
+params, videoFilename, databaseFilename, invHomography, intrinsicCameraMatrix, distortionCoefficients, undistortedImageMultiplication, undistort, firstFrameNum = storage.processVideoArguments(args)
+
 classifierParams = storage.ClassifierParameters(params.classifierFilename)
 classifierParams.convertToFrames(params.videoFrameRate, 3.6) # conversion from km/h to m/frame
 
-if args.videoFilename is not None:
-    videoFilename = args.videoFilename
-else:
-    videoFilename = params.videoFilename
-if args.databaseFilename is not None:
-    databaseFilename = args.databaseFilename
-else:
-    databaseFilename = params.databaseFilename
-
-if params.homography is not None:
-    invHomography = np.linalg.inv(params.homography)
-else:
-    invHomography = None
-
 if classifierParams.speedAggregationMethod == 'median':
     speedAggregationFunc = np.median
 elif classifierParams.speedAggregationMethod == 'mean':
@@ -91,8 +78,8 @@
 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
 
 pastObjects = []
-if params.undistort: # setup undistortion
-    [map1, map2] = cvutils.computeUndistortMaps(width, height, params.undistortedImageMultiplication, params.intrinsicCameraMatrix, params.distortionCoefficients)
+if undistort: # setup undistortion
+    [map1, map2] = cvutils.computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients)
 if capture.isOpened():
     ret = True
     frameNum = timeInterval.first
@@ -104,7 +91,7 @@
         if ret:
             if frameNum%50 == 0:
                 print('frame number: {}'.format(frameNum))
-            if params.undistort:
+            if undistort:
                 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
             currentObjects = []
             for obj in objects:
--- a/scripts/display-trajectories.py	Wed Jun 21 17:49:58 2017 -0400
+++ b/scripts/display-trajectories.py	Thu Jun 22 12:02:34 2017 -0400
@@ -4,7 +4,7 @@
 
 import storage, cvutils, utils
 
-from numpy.linalg.linalg import inv
+from numpy.linalg import inv
 from numpy import loadtxt
 
 parser = argparse.ArgumentParser(description='The program displays feature or object trajectories overlaid over the video frames.', epilog = 'Either the configuration filename or the other parameters (at least video and database filenames) need to be provided.')
@@ -18,48 +18,24 @@
 parser.add_argument('--undistorted-multiplication', dest = 'undistortedImageMultiplication', help = 'undistorted image multiplication', type = float)
 parser.add_argument('-u', dest = 'undistort', help = 'undistort the video (because features have been extracted that way)', action = 'store_true')
 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int)
+parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to save (for image saving, no display is made)', type = int)
 parser.add_argument('-r', dest = 'rescale', help = 'rescaling factor for the displayed image', default = 1., type = float)
 parser.add_argument('-s', dest = 'nFramesStep', help = 'number of frames between each display', default = 1, type = int)
 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to display', type = int)
 parser.add_argument('--save-images', dest = 'saveAllImages', help = 'save all images', action = 'store_true')
-parser.add_argument('--last-frame', dest = 'lastFrameNum', help = 'number of last frame number to save (for image saving, no display is made)', type = int)
 
 args = parser.parse_args()
 
-if args.configFilename is not None: # consider there is a configuration file
-    params = storage.ProcessParameters(args.configFilename)
-    videoFilename = params.videoFilename
-    databaseFilename = params.databaseFilename
-    if params.homography is not None:
-        homography = inv(params.homography)
-    else:
-        homography = None
-    intrinsicCameraMatrix = params.intrinsicCameraMatrix
-    distortionCoefficients = params.distortionCoefficients
-    undistortedImageMultiplication = params.undistortedImageMultiplication
-    undistort = params.undistort
-    firstFrameNum = params.firstFrameNum
-else:
-    homography = None
-    undistort = False
-    intrinsicCameraMatrix = None
-    distortionCoefficients = []
-    undistortedImageMultiplication = None
-    firstFrameNum = 0
+params, videoFilename, databaseFilename, invHomography, intrinsicCameraMatrix, distortionCoefficients, undistortedImageMultiplication, undistort, firstFrameNum = storage.processVideoArguments(args)
 
-if args.configFilename is None and args.videoFilename is not None:
-    videoFilename = args.videoFilename
-if args.configFilename is None and args.databaseFilename is not None:
-    databaseFilename = args.databaseFilename
-if args.configFilename is None and args.homographyFilename is not None:
-    homography = inv(loadtxt(args.homographyFilename))            
-if args.configFilename is None and args.intrinsicCameraMatrixFilename is not None:
+if args.homographyFilename is not None:
+    invHomography = inv(loadtxt(args.homographyFilename))            
+if args.intrinsicCameraMatrixFilename is not None:
     intrinsicCameraMatrix = loadtxt(args.intrinsicCameraMatrixFilename)
-if args.configFilename is None and args.distortionCoefficients is not None:
+if args.distortionCoefficients is not None:
     distortionCoefficients = args.distortionCoefficients
-if args.configFilename is None and args.undistortedImageMultiplication is not None:
+if args.undistortedImageMultiplication is not None:
     undistortedImageMultiplication = args.undistortedImageMultiplication
-
 if args.firstFrameNum is not None:
     firstFrameNum = args.firstFrameNum
 if args.nObjects is not None:
@@ -69,4 +45,4 @@
 
 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType, nObjects)
 boundingBoxes = storage.loadBoundingBoxTableForDisplay(databaseFilename)
-cvutils.displayTrajectories(videoFilename, objects, boundingBoxes, homography, firstFrameNum, args.lastFrameNum, rescale = args.rescale, nFramesStep = args.nFramesStep, saveAllImages = args.saveAllImages, undistort = (undistort or args.undistort), intrinsicCameraMatrix = intrinsicCameraMatrix, distortionCoefficients = distortionCoefficients, undistortedImageMultiplication = undistortedImageMultiplication)
+cvutils.displayTrajectories(videoFilename, objects, boundingBoxes, invHomography, firstFrameNum, args.lastFrameNum, rescale = args.rescale, nFramesStep = args.nFramesStep, saveAllImages = args.saveAllImages, undistort = (undistort or args.undistort), intrinsicCameraMatrix = intrinsicCameraMatrix, distortionCoefficients = distortionCoefficients, undistortedImageMultiplication = undistortedImageMultiplication)
--- a/scripts/extract-appearance-images.py	Wed Jun 21 17:49:58 2017 -0400
+++ b/scripts/extract-appearance-images.py	Thu Jun 22 12:02:34 2017 -0400
@@ -8,10 +8,17 @@
 import cvutils, moving, ml, storage
 
 parser = argparse.ArgumentParser(description='The program extracts labeled image patches to train the HoG-SVM classifier, and optionnally speed information')
-parser.add_argument('-d', dest = 'directoryName', help = 'parent directory name for the directories containing the samples for the different road users', required = True)
+parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file', required = True)
+
+#parser.add_argument('-d', dest = 'directoryName', help = 'parent directory name for the directories containing the samples for the different road users', required = True)
+
+args = parser.parse_args()
+params = storage.ProcessParameters(args.configFilename)
+classifierParams = storage.ClassifierParameters(params.classifierFilename)
 
 # need all info as for classification (image info)
 
 # todo speed info: distributions AND min speed equiprobable
 
 # provide csv delimiter for the classification file as arg
+