changeset 304:20f9cd972dde

added capability to parse cfg file for display-trajectories.py
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 29 Mar 2013 16:56:23 -0400
parents 514f6b98cd8c
children ca9131968bce
files python/display-trajectories.py python/utils.py
diffstat 2 files changed, 45 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/python/display-trajectories.py	Thu Mar 21 14:01:46 2013 -0400
+++ b/python/display-trajectories.py	Fri Mar 29 16:56:23 2013 -0400
@@ -4,30 +4,47 @@
 
 import storage
 import cvutils
+from utils import FakeSecHead
 
 from numpy.linalg.linalg import inv
 from numpy import loadtxt
+from ConfigParser import ConfigParser
 
 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='
-# todo parse the cfg file (problem, python ConfigParser needs section headers)
+
 options = dict(options)
 
-if '--help' in options.keys() or '-h' in options.keys() or len(sys.argv) == 1 or not '-i' in options.keys() or not '-d' in options.keys():
-    print('Usage: {0} --help|-h -i video-filename -d database-filename [-t object_type] [-o image2world_homography] [-f first_frame]'.format(sys.argv[0]))
+print options, args
+
+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()
 
 objectType = 'feature'
 if '-t' in options.keys():
     objectType = options['-t']
 
-objects = storage.loadTrajectoriesFromSqlite(options['-d'], objectType)
+if len(args)>0: # consider there is a configuration file
+    config = ConfigParser()
+    config.readfp(FakeSecHead(open(args[0])))
+    sectionHeader = config.sections()[0]
+    videoFilename = config.get(sectionHeader, 'video-filename')
+    databaseFilename = config.get(sectionHeader, 'database-filename')
+    homography = inv(loadtxt(config.get(sectionHeader, 'homography-filename')))
+    firstFrameNum = config.getint(sectionHeader, 'frame1')
+else:
+    videoFilename = options['-i']
+    databaseFilename = options['-d']
+    homography = None
+    if '-o' in options.keys():
+        homography = inv(loadtxt(options['-o']))            
+    firstFrameNum = 0
+    if '-f' in options.keys():
+        firstFrameNum = int(options['-f'])
 
-homography = None
-if '-o' in options.keys():
-    homography = inv(loadtxt(options['-o']))
-firstFrameNum = 0
-if '-f' in options.keys():
-    firstFrameNum = int(options['-f'])
-
-cvutils.displayTrajectories(options['-i'], objects, homography, firstFrameNum)
+objects = storage.loadTrajectoriesFromSqlite(databaseFilename, objectType)
+cvutils.displayTrajectories(videoFilename, objects, homography, firstFrameNum)
--- a/python/utils.py	Thu Mar 21 14:01:46 2013 -0400
+++ b/python/utils.py	Fri Mar 29 16:56:23 2013 -0400
@@ -330,6 +330,22 @@
         s = readline(f)
     return dataStrings
 
+class FakeSecHead(object):
+    '''Add fake section header [asection]
+
+    from http://stackoverflow.com/questions/2819696/parsing-properties-file-in-python/2819788#2819788
+    use read_file in Python 3.2+
+    '''
+    def __init__(self, fp):
+        self.fp = fp
+        self.sechead = '[main]\n'
+
+    def readline(self):
+        if self.sechead:
+            try: return self.sechead
+            finally: self.sechead = None
+        else: return self.fp.readline()
+
 def removeExtension(filename, delimiter = '.'):
     '''Returns the filename minus the extension (all characters after last .)'''
     i = filename.rfind(delimiter)