comparison scripts/nomad/site-parameters-optimization.py @ 1186:7117a31555c1

Etienne Beauchamp s work on optimization with Nomad software
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 21 Jun 2022 17:06:06 -0400
parents
children ccab20f85710
comparison
equal deleted inserted replaced
1177:aa88acf06876 1186:7117a31555c1
1 #! /usr/bin/env python3
2 import os
3 import sys
4 import glob
5 from trafficintelligence import storage, moving
6 import subprocess
7 import numpy as np
8
9
10 def loadParametersStartProcess(filename):
11 # load initial parameters from x.txt
12 f = open(filename, 'r+')
13 l = f.readline()
14 x = [s for s in l.strip().split(" ")]
15 f.close()
16
17 # create para-value list
18 para = paraValueList(x)
19
20 # run process including trackingfeature, groupfeature, load groundtruth, compute mota
21 print(process(para, intersections, optimizeGroupingOnly))
22
23 def paraValueList(x):
24 #create para-value list
25 #list of the 8 parameters and their values
26 pn = 8
27 p = pn*[None]
28 p[0] = '--feature-quality' #]0.-0.4]
29 p[1] = '--min-feature-distanceklt' #]0.-6]
30 p[2] = '--window-size' #[1-10]integer
31 p[3] = '--min-tracking-error' #[0.01-0.3]
32 p[4] = '--min-feature-time' #[2-100]integer
33 p[5] = '--mm-connection-distance' #[0.5-100]
34 p[6] = '--mm-segmentation-distance' #[1-100] ~mm-connection-distance / 2.5
35 p[7] = '--min-nfeatures-group' #[2-4]
36
37 para = []
38 for n in range(pn):
39 para = para + [p[n],x[n]]
40
41 return para
42
43 def process(para, intersections, optimizeGroupingOnly):
44 Mota = []
45 gtDatabaseaAbsPaths = []
46 configFileAbsPaths = []
47
48 cwd = os.getcwd()
49 # move to the location of the intersection
50 for intersectionPath in intersections:
51 intersectionAbsPath = os.path.abspath(intersectionPath)
52 os.chdir(intersectionAbsPath)
53 # iterate through all the subdirectories to find ground truth sqlite files
54 gtDatabaseaAbsPaths.extend([os.path.abspath(intersectionAbsPath + '/' + file) for file in glob.glob('**/*_gt.sqlite', recursive=True)])
55 configFileAbsPaths.append(os.path.abspath(intersectionAbsPath + '/' + glob.glob('*.cfg', recursive=True)[0]))
56 os.chdir(cwd)
57 for gtDatabaseAbsPath, configFileAbsPath in zip(gtDatabaseaAbsPaths, configFileAbsPaths):
58 gtDatabaseBasename = gtDatabaseAbsPath[:-10]
59 videoFilename = gtDatabaseBasename + ".MP4"
60 databaseFilename = gtDatabaseBasename + ".sqlite"
61 gtDatabaseDirname = os.path.dirname(gtDatabaseAbsPath)
62 homographyFilename = gtDatabaseDirname + "/homography.txt"
63 maskFilename = gtDatabaseDirname + "/mask.png"
64 # Skip feature tracking if the user specified to optimize only grouping parameters
65 if not optimizeGroupingOnly:
66 # Track features
67 trackingFeature(para, configFileAbsPath, videoFilename, databaseFilename, homographyFilename, maskFilename)
68 # Group features
69 groupFeature(para, configFileAbsPath, videoFilename, databaseFilename, homographyFilename, maskFilename)
70 #load trajectory
71 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object')
72 #load ground truth
73 annotations = storage.loadTrajectoriesFromSqlite(gtDatabaseAbsPath, 'object')
74 # Appending negative mota because nomad minimizes the output
75 Mota.append(-computeMota(annotations, objects, Mota))
76
77 # Change to the previous directory
78 os.chdir(cwd)
79
80 return np.mean(Mota)
81
82 def trackingFeature(para, config, video, db, homo, mask):
83 # remove previous tracking
84 if os.path.exists(db):
85 os.remove(db)
86 # trackingfeature command parameters
87 tf = ['feature-based-tracking', config, '--tf', '--video-filename', video, '--database-filename', db, '--homography-filename', homo, '--mask-filename', mask]
88 # run in command line and print directly
89 subprocess.check_output(tf + para[0:10])
90
91 def groupFeature(para, config, video, db, homo, mask):
92 #remove previous grouping
93 storage.deleteFromSqlite(db, 'object')
94 #groupfeature command parameters
95 gf = ['feature-based-tracking', config, '--gf', '--video-filename', video, '--database-filename', db, '--homography-filename', homo, '--mask-filename', mask]
96 #run in command line and print directly
97 subprocess.check_output(gf + para[8:16]) # ['--min-feature-time', 'x', '--mm-connection-distance', 'x', '--mm-segmentation-distance', 'x', '--min-nfeatures-group', 'x']
98
99 def computeMota(annotations, objects, Mota):
100 matchingDistance = 500
101 firstInstant = 0
102 lastInstant = 50000
103 return moving.computeClearMOT(annotations, objects, matchingDistance, firstInstant, lastInstant)[1]
104
105
106 if __name__ == "__main__":
107 # Load args that were given with select-arguments.py
108 with open('arguments.txt', 'r') as f:
109 args = f.read().split('\n')
110 intersections = args[0]
111 optimizeGroupingOnly = args[1]
112 # Convert string representation of list into list
113 intersections = eval(intersections)
114
115 loadParametersStartProcess(sys.argv[1])
116 sys.exit(0)
117