changeset 347:7b865f4174aa

updated script to display trajectories with argparse module
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 26 Jun 2013 17:28:45 -0400
parents 5f75d6c23ed5
children c64a4f889b97
files scripts/display-trajectories.py
diffstat 1 files changed, 17 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/display-trajectories.py	Wed Jun 26 15:42:45 2013 -0400
+++ b/scripts/display-trajectories.py	Wed Jun 26 17:28:45 2013 -0400
@@ -1,49 +1,36 @@
 #! /usr/bin/env python
 
-import sys,getopt
+import sys, argparse
 
 import storage, cvutils, utils
 
 from numpy.linalg.linalg import inv
 from numpy import loadtxt
 
-options, args = getopt.getopt(sys.argv[1:], 'hi:d:t:o:f:',['help']) 
-# alternative long names are a pain to support ,'video-filename=','database-filename=', 'type='
-
-# replace with argparse 
-# type parser.add_argument('-t', choices=['feature', 'object'])
-
-options = dict(options)
-
-print options, args
+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.')
+parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file')
+parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file')
+parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file')
+parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['feature', 'object'], default = 'feature')
+parser.add_argument('-o', dest = 'homography', help = 'name of the image to world homography')
+parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', default = 0, type = int)
 
-if '--help' in options.keys() or '-h' in options.keys() or len(sys.argv) == 1:
-    print('Usage: '+sys.argv[0]+' --help|-h -i video-filename -d database-filename [-t object_type] [-o image2world_homography] [-f first_frame]\n'
-          +'Or   : '+sys.argv[0]+' [-t object_type] config_file.cfg\n\n'
-          'Order matters between positional and named arguments\n'
-          'object_type can be feature or object')
-    sys.exit()
+args = parser.parse_args()
 
-objectType = 'feature'
-if '-t' in options.keys():
-    objectType = options['-t']
-
-if len(args)>0: # consider there is a configuration file
+if args.configFilename: # consider there is a configuration file
     params = utils.TrackingParameters()
-    params.loadConfigFile(args[0])
+    params.loadConfigFile(args.configFilename)
     videoFilename = params.videoFilename
     databaseFilename = params.databaseFilename
     homography = inv(params.homography)
     firstFrameNum = params.firstFrameNum
 else:
-    videoFilename = options['-i']
-    databaseFilename = options['-d']
+    videoFilename = args.videoFilename
+    databaseFilename = args.databaseFilename
     homography = None
-    if '-o' in options.keys():
-        homography = inv(loadtxt(options['-o']))            
-    firstFrameNum = 0
-    if '-f' in options.keys():
-        firstFrameNum = int(options['-f'])
+    if args.homography:
+        homography = inv(loadtxt(args.homography))            
+    firstFrameNum = args.firstFrameNum
 
-objects = storage.loadTrajectoriesFromSqlite(databaseFilename, objectType)
+objects = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType)
 cvutils.displayTrajectories(videoFilename, objects, homography, firstFrameNum)