#! /usr/bin/env python

import sys,getopt

import storage
import cvutils

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='
# 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]))
    sys.exit()

objectType = 'feature'
if '-t' in options.keys():
    objectType = options['-t']

objects = storage.loadTrajectoriesFromSqlite(options['-d'], objectType)

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)
