switched from argparse to getopt for compatibility with Python 2.6

This commit is contained in:
Alexander Mordvintsev 2011-06-11 21:11:02 +00:00
parent 8f4c7db3f6
commit b6c19d365f
3 changed files with 39 additions and 28 deletions

View File

@ -3,7 +3,7 @@ import video
import sys
try: fn = sys.argv[1]
except: fn = 'synth:bg=../cpp/lena.jpg:noise=0.1'
except: fn = video.presets['lena']
def nothing(*arg):
pass

View File

@ -91,31 +91,34 @@ class SVM(LetterStatModel):
if __name__ == '__main__':
import argparse
import getopt
import sys
models = [RTrees, KNearest, Boost, SVM] # MLP, NBayes
models = dict( [(cls.__name__.lower(), cls) for cls in models] )
parser = argparse.ArgumentParser()
parser.add_argument('-model', default='rtrees', choices=models.keys())
parser.add_argument('-data', nargs=1, default='../cpp/letter-recognition.data')
parser.add_argument('-load', nargs=1)
parser.add_argument('-save', nargs=1)
args = parser.parse_args()
print 'USAGE: letter_recog.py [--model <model>] [--data <data fn>] [--load <model fn>] [--save <model fn>]'
print 'Models: ', ', '.join(models)
print
print 'loading data %s ...' % args.data
samples, responses = load_base(args.data)
Model = models[args.model]
args, dummy = getopt.getopt(sys.argv[1:], '', ['model=', 'data=', 'load=', 'save='])
args = dict(args)
args.setdefault('--model', 'rtrees')
args.setdefault('--data', '../cpp/letter-recognition.data')
print 'loading data %s ...' % args['--data']
samples, responses = load_base(args['--data'])
Model = models[args['--model']]
model = Model()
train_n = int(len(samples)*model.train_ratio)
if args.load is None:
print 'training %s ...' % Model.__name__
model.train(samples[:train_n], responses[:train_n])
else:
fn = args.load[0]
if '--load' in args:
fn = args['--load']
print 'loading model from %s ...' % fn
model.load(fn)
else:
print 'training %s ...' % Model.__name__
model.train(samples[:train_n], responses[:train_n])
print 'testing...'
train_rate = np.mean(model.predict(samples[:train_n]) == responses[:train_n])
@ -123,7 +126,7 @@ if __name__ == '__main__':
print 'train rate: %f test rate: %f' % (train_rate*100, test_rate*100)
if args.save is not None:
fn = args.save[0]
if '--save' in args:
fn = args['--save']
print 'saving model to %s ...' % fn
model.save(fn)

View File

@ -47,19 +47,27 @@ def create_capture(source):
return cv2.VideoCapture(source)
presets = dict(
lena = 'synth:bg=../cpp/lena.jpg:noise=0.1'
)
if __name__ == '__main__':
import sys
import argparse
import getopt
parser = argparse.ArgumentParser()
parser.add_argument('sources', nargs='*', default=['synth:bg=../cpp/lena.jpg:noise=0.1'])
parser.add_argument('-shotdir', nargs=1, default='.')
args = parser.parse_args()
print args
print 'USAGE: video.py [--shotdir <dir>] [source0] [source1] ...'
print "source: '<int>' or '<filename>' or 'synth:<params>'"
print
args, sources = getopt.getopt(sys.argv[1:], '', 'shotdir=')
args = dict(args)
shotdir = args.get('--shotdir', '.')
if len(sources) == 0:
sources = [ presets['lena'] ]
print 'Press SPACE to save current frame'
caps = map(create_capture, args.sources)
caps = map(create_capture, sources)
shot_idx = 0
while True:
imgs = []
@ -72,7 +80,7 @@ if __name__ == '__main__':
break
if ch == ord(' '):
for i, img in enumerate(imgs):
fn = '%s/shot_%d_%03d.bmp' % (args.shotdir[0], i, shot_idx)
fn = '%s/shot_%d_%03d.bmp' % (shotdir, i, shot_idx)
cv2.imwrite(fn, img)
print fn, 'saved'
shot_idx += 1