comparison scripts/display-trajectories.py @ 901:753a081989e2

factorized some argument handling code
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 22 Jun 2017 12:02:34 -0400
parents 95e7622b11be
children c69a8defe5c3
comparison
equal deleted inserted replaced
900:85b81c46c526 901:753a081989e2
2 2
3 import sys, argparse 3 import sys, argparse
4 4
5 import storage, cvutils, utils 5 import storage, cvutils, utils
6 6
7 from numpy.linalg.linalg import inv 7 from numpy.linalg import inv
8 from numpy import loadtxt 8 from numpy import loadtxt
9 9
10 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.') 10 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.')
11 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file') 11 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file')
12 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file') 12 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file')
16 parser.add_argument('--intrinsic', dest = 'intrinsicCameraMatrixFilename', help = 'name of the intrinsic camera file') 16 parser.add_argument('--intrinsic', dest = 'intrinsicCameraMatrixFilename', help = 'name of the intrinsic camera file')
17 parser.add_argument('--distortion-coefficients', dest = 'distortionCoefficients', help = 'distortion coefficients', nargs = '*', type = float) 17 parser.add_argument('--distortion-coefficients', dest = 'distortionCoefficients', help = 'distortion coefficients', nargs = '*', type = float)
18 parser.add_argument('--undistorted-multiplication', dest = 'undistortedImageMultiplication', help = 'undistorted image multiplication', type = float) 18 parser.add_argument('--undistorted-multiplication', dest = 'undistortedImageMultiplication', help = 'undistorted image multiplication', type = float)
19 parser.add_argument('-u', dest = 'undistort', help = 'undistort the video (because features have been extracted that way)', action = 'store_true') 19 parser.add_argument('-u', dest = 'undistort', help = 'undistort the video (because features have been extracted that way)', action = 'store_true')
20 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int) 20 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int)
21 parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to save (for image saving, no display is made)', type = int)
21 parser.add_argument('-r', dest = 'rescale', help = 'rescaling factor for the displayed image', default = 1., type = float) 22 parser.add_argument('-r', dest = 'rescale', help = 'rescaling factor for the displayed image', default = 1., type = float)
22 parser.add_argument('-s', dest = 'nFramesStep', help = 'number of frames between each display', default = 1, type = int) 23 parser.add_argument('-s', dest = 'nFramesStep', help = 'number of frames between each display', default = 1, type = int)
23 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to display', type = int) 24 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to display', type = int)
24 parser.add_argument('--save-images', dest = 'saveAllImages', help = 'save all images', action = 'store_true') 25 parser.add_argument('--save-images', dest = 'saveAllImages', help = 'save all images', action = 'store_true')
25 parser.add_argument('--last-frame', dest = 'lastFrameNum', help = 'number of last frame number to save (for image saving, no display is made)', type = int)
26 26
27 args = parser.parse_args() 27 args = parser.parse_args()
28 28
29 if args.configFilename is not None: # consider there is a configuration file 29 params, videoFilename, databaseFilename, invHomography, intrinsicCameraMatrix, distortionCoefficients, undistortedImageMultiplication, undistort, firstFrameNum = storage.processVideoArguments(args)
30 params = storage.ProcessParameters(args.configFilename)
31 videoFilename = params.videoFilename
32 databaseFilename = params.databaseFilename
33 if params.homography is not None:
34 homography = inv(params.homography)
35 else:
36 homography = None
37 intrinsicCameraMatrix = params.intrinsicCameraMatrix
38 distortionCoefficients = params.distortionCoefficients
39 undistortedImageMultiplication = params.undistortedImageMultiplication
40 undistort = params.undistort
41 firstFrameNum = params.firstFrameNum
42 else:
43 homography = None
44 undistort = False
45 intrinsicCameraMatrix = None
46 distortionCoefficients = []
47 undistortedImageMultiplication = None
48 firstFrameNum = 0
49 30
50 if args.configFilename is None and args.videoFilename is not None: 31 if args.homographyFilename is not None:
51 videoFilename = args.videoFilename 32 invHomography = inv(loadtxt(args.homographyFilename))
52 if args.configFilename is None and args.databaseFilename is not None: 33 if args.intrinsicCameraMatrixFilename is not None:
53 databaseFilename = args.databaseFilename
54 if args.configFilename is None and args.homographyFilename is not None:
55 homography = inv(loadtxt(args.homographyFilename))
56 if args.configFilename is None and args.intrinsicCameraMatrixFilename is not None:
57 intrinsicCameraMatrix = loadtxt(args.intrinsicCameraMatrixFilename) 34 intrinsicCameraMatrix = loadtxt(args.intrinsicCameraMatrixFilename)
58 if args.configFilename is None and args.distortionCoefficients is not None: 35 if args.distortionCoefficients is not None:
59 distortionCoefficients = args.distortionCoefficients 36 distortionCoefficients = args.distortionCoefficients
60 if args.configFilename is None and args.undistortedImageMultiplication is not None: 37 if args.undistortedImageMultiplication is not None:
61 undistortedImageMultiplication = args.undistortedImageMultiplication 38 undistortedImageMultiplication = args.undistortedImageMultiplication
62
63 if args.firstFrameNum is not None: 39 if args.firstFrameNum is not None:
64 firstFrameNum = args.firstFrameNum 40 firstFrameNum = args.firstFrameNum
65 if args.nObjects is not None: 41 if args.nObjects is not None:
66 nObjects = args.nObjects 42 nObjects = args.nObjects
67 else: 43 else:
68 nObjects = None 44 nObjects = None
69 45
70 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType, nObjects) 46 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType, nObjects)
71 boundingBoxes = storage.loadBoundingBoxTableForDisplay(databaseFilename) 47 boundingBoxes = storage.loadBoundingBoxTableForDisplay(databaseFilename)
72 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) 48 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)