Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/nomad/site-parameters-optimization.py @ 1219:8a626226793e
update where optimization uses either nomad-parameter file depending on optimizing 1 or 2 steps
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 19 Jun 2023 17:09:56 -0400 |
| parents | 606817bc31e8 |
| children | 5a207c838323 |
comparison
equal
deleted
inserted
replaced
| 1218:1f0b1fc172f8 | 1219:8a626226793e |
|---|---|
| 5 from trafficintelligence import storage, moving | 5 from trafficintelligence import storage, moving |
| 6 import subprocess | 6 import subprocess |
| 7 import numpy as np | 7 import numpy as np |
| 8 | 8 |
| 9 | 9 |
| 10 def loadParametersStartProcess(filename): | 10 def loadParametersStartProcess(filename, intersections): |
| 11 # load initial parameters from x.txt | 11 # load initial parameters from x.txt |
| 12 f = open(filename, 'r+') | 12 f = open(filename, 'r+') |
| 13 l = f.readline() | 13 l = f.readline() |
| 14 x = [s for s in l.strip().split(" ")] | 14 x = [s for s in l.strip().split(" ")] |
| 15 f.close() | 15 f.close() |
| 16 | 16 |
| 17 # create para-value list | 17 # create para-value list |
| 18 para = paraValueList(x) | 18 para = paraValueList(x) |
| 19 | 19 |
| 20 # run process including trackingfeature, groupfeature, load groundtruth, compute mota | 20 # run process including trackingfeature, groupfeature, load groundtruth, compute mota |
| 21 print(process(para, intersections, optimizeGroupingOnly)) | 21 print(process(para, intersections)) |
| 22 | 22 |
| 23 def paraValueList(x): | 23 def paraValueList(x): |
| 24 #create para-value list | 24 #create para-value list |
| 25 #list of the 8 parameters and their values | 25 #list of the 8 parameters and their values |
| 26 pn = 8 | 26 p = 8*[None] |
| 27 p = pn*[None] | |
| 28 p[0] = '--feature-quality' #]0.-0.4] | 27 p[0] = '--feature-quality' #]0.-0.4] |
| 29 p[1] = '--min-feature-distanceklt' #]0.-6] | 28 p[1] = '--min-feature-distanceklt' #]0.-6] |
| 30 p[2] = '--window-size' #[1-10]integer | 29 p[2] = '--window-size' #[1-10]integer |
| 31 p[3] = '--min-tracking-error' #[0.01-0.3] | 30 p[3] = '--min-tracking-error' #[0.01-0.3] |
| 32 p[4] = '--min-feature-time' #[2-100]integer | 31 p[4] = '--min-feature-time' #[2-100]integer |
| 33 p[5] = '--mm-connection-distance' #[0.5-100] | 32 p[5] = '--mm-connection-distance' #[0.5-100] |
| 34 p[6] = '--mm-segmentation-distance' #[1-100] ~mm-connection-distance / 2.5 | 33 p[6] = '--mm-segmentation-distance' #[1-100] ~mm-connection-distance / 2.5 |
| 35 p[7] = '--min-nfeatures-group' #[2-4] | 34 p[7] = '--min-nfeatures-group' #[2-4] |
| 36 | 35 |
| 37 para = [] | 36 para = [] |
| 38 for n in range(pn): | 37 if len(x) == 4: |
| 39 para = para + [p[n],x[n]] | 38 for n in range(4): |
| 39 para = para + [p[4+n],x[n]] | |
| 40 else: | |
| 41 for n in range(len(x)): | |
| 42 para = para + [p[n],x[n]] | |
| 40 | 43 |
| 41 return para | 44 return para |
| 42 | 45 |
| 43 def process(para, intersections, optimizeGroupingOnly): | 46 def process(para, intersections): |
| 44 Mota = [] | 47 Mota = [] |
| 45 gtDatabaseaAbsPaths = [] | 48 gtDatabaseaAbsPaths = [] |
| 46 configFileAbsPaths = [] | 49 configFileAbsPaths = [] |
| 47 | 50 |
| 48 cwd = os.getcwd() | 51 cwd = os.getcwd() |
| 56 configFilename = os.path.abspath(intersectionAbsPath + '/' + glob.glob('*.cfg', recursive=True)[0]) | 59 configFilename = os.path.abspath(intersectionAbsPath + '/' + glob.glob('*.cfg', recursive=True)[0]) |
| 57 configFileAbsPaths.extend([configFilename]*len(newPaths)) | 60 configFileAbsPaths.extend([configFilename]*len(newPaths)) |
| 58 os.chdir(cwd) | 61 os.chdir(cwd) |
| 59 for gtDatabaseAbsPath, configFileAbsPath in zip(gtDatabaseaAbsPaths, configFileAbsPaths): | 62 for gtDatabaseAbsPath, configFileAbsPath in zip(gtDatabaseaAbsPaths, configFileAbsPaths): |
| 60 gtDatabaseBasename = gtDatabaseAbsPath[:-10] | 63 gtDatabaseBasename = gtDatabaseAbsPath[:-10] |
| 61 videoFilename = gtDatabaseBasename + ".MP4" | 64 videoFilename = gtDatabaseBasename + ".avi" # careful, it may be necessary to check video type / extension |
| 65 if not os.path.exists(videoFilename): | |
| 66 print('Video file {} does not exist'.format(videoFilename)) | |
| 62 databaseFilename = gtDatabaseBasename + ".sqlite" | 67 databaseFilename = gtDatabaseBasename + ".sqlite" |
| 63 gtDatabaseDirname = os.path.dirname(gtDatabaseAbsPath) | 68 gtDatabaseDirname = os.path.dirname(gtDatabaseAbsPath) |
| 64 homographyFilename = gtDatabaseDirname + "/homography.txt" | 69 homographyFilename = gtDatabaseDirname + "/homography.txt" |
| 65 maskFilename = gtDatabaseDirname + "/mask.png" | 70 maskFilename = gtDatabaseDirname + "/mask.png" |
| 66 # Skip feature tracking if the user specified to optimize only grouping parameters | 71 # Skip feature tracking if the user specified to optimize only grouping parameters |
| 67 if not optimizeGroupingOnly: | 72 if len(para) > 8: |
| 68 # Track features | 73 # Track features |
| 69 trackingFeature(para, configFileAbsPath, videoFilename, databaseFilename, homographyFilename, maskFilename) | 74 trackingFeature(para, configFileAbsPath, videoFilename, databaseFilename, homographyFilename, maskFilename) |
| 70 # Group features | 75 # Group features |
| 71 groupFeature(para, configFileAbsPath, videoFilename, databaseFilename, homographyFilename, maskFilename) | 76 groupFeature(para, configFileAbsPath, videoFilename, databaseFilename, homographyFilename, maskFilename) |
| 72 #load trajectory | 77 #load trajectory |
| 89 if os.path.exists(db): | 94 if os.path.exists(db): |
| 90 os.remove(db) | 95 os.remove(db) |
| 91 # trackingfeature command parameters | 96 # trackingfeature command parameters |
| 92 tf = ['feature-based-tracking', config, '--tf', '--video-filename', video, '--database-filename', db, '--homography-filename', homo, '--mask-filename', mask] | 97 tf = ['feature-based-tracking', config, '--tf', '--video-filename', video, '--database-filename', db, '--homography-filename', homo, '--mask-filename', mask] |
| 93 # run in command line and print directly | 98 # run in command line and print directly |
| 94 subprocess.check_output(tf + para[0:10]) | 99 subprocess.check_output(tf + para) |
| 95 | 100 |
| 96 def groupFeature(para, config, video, db, homo, mask): | 101 def groupFeature(para, config, video, db, homo, mask): |
| 97 #remove previous grouping | 102 #remove previous grouping |
| 98 storage.deleteFromSqlite(db, 'object') | 103 storage.deleteFromSqlite(db, 'object') |
| 99 #groupfeature command parameters | 104 #groupfeature command parameters |
| 100 gf = ['feature-based-tracking', config, '--gf', '--video-filename', video, '--database-filename', db, '--homography-filename', homo, '--mask-filename', mask] | 105 gf = ['feature-based-tracking', config, '--gf', '--video-filename', video, '--database-filename', db, '--homography-filename', homo, '--mask-filename', mask] |
| 101 #run in command line and print directly | 106 #run in command line and print directly |
| 102 subprocess.check_output(gf + para[8:16]) # ['--min-feature-time', 'x', '--mm-connection-distance', 'x', '--mm-segmentation-distance', 'x', '--min-nfeatures-group', 'x'] | 107 subprocess.check_output(gf + para) # ['--min-feature-time', 'x', '--mm-connection-distance', 'x', '--mm-segmentation-distance', 'x', '--min-nfeatures-group', 'x'] |
| 103 | |
| 104 def computeMota(annotations, objects, Mota): | |
| 105 matchingDistance = 5 | |
| 106 firstInstant = 0 | |
| 107 lastInstant = 50000 | |
| 108 return moving.computeClearMOT(annotations, objects, matchingDistance, firstInstant, lastInstant)[1] | |
| 109 | 108 |
| 110 | 109 |
| 111 if __name__ == "__main__": | 110 if __name__ == "__main__": |
| 112 # Load args that were given with select-arguments.py | 111 # Load args that were given with select-arguments.py |
| 113 with open('arguments.txt', 'r') as f: | 112 # with open('arguments.txt', 'r') as f: |
| 114 args = f.read().split('\n') | 113 # args = f.read().split('\n') |
| 115 intersections = args[0] | 114 # intersections = args[0] |
| 116 optimizeGroupingOnly = args[1] | 115 # optimizeGroupingOnly = eval(args[1]) |
| 117 # Convert string representation of list into list | 116 # intersections = eval(intersections) |
| 118 intersections = eval(intersections) | 117 |
| 118 # Just write the intersections to optimize here | |
| 119 intersections = ['../12-laurier'] | |
| 119 | 120 |
| 120 loadParametersStartProcess(sys.argv[1]) | 121 loadParametersStartProcess(sys.argv[1], intersections) |
| 121 sys.exit(0) | 122 sys.exit(0) |
| 122 |
