Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/process.py @ 1046:f2ba9858e6c6
motion pattern learning seems to work
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 05 Jul 2018 23:12:11 -0400 |
| parents | 25db2383e7ae |
| children | 0b62e37991ab |
comparison
equal
deleted
inserted
replaced
| 1045:25db2383e7ae | 1046:f2ba9858e6c6 |
|---|---|
| 5 from multiprocessing.pool import Pool | 5 from multiprocessing.pool import Pool |
| 6 | 6 |
| 7 #import matplotlib | 7 #import matplotlib |
| 8 #atplotlib.use('Agg') | 8 #atplotlib.use('Agg') |
| 9 import matplotlib.pyplot as plt | 9 import matplotlib.pyplot as plt |
| 10 from numpy import percentile | 10 from numpy import percentile, ones |
| 11 from pandas import DataFrame | 11 from pandas import DataFrame |
| 12 | 12 |
| 13 from trafficintelligence import storage, events, prediction, cvutils, utils, moving | 13 from trafficintelligence import storage, events, prediction, cvutils, utils, moving, processing, ml |
| 14 from trafficintelligence.metadata import * | 14 from trafficintelligence.metadata import * |
| 15 | 15 |
| 16 parser = argparse.ArgumentParser(description='This program manages the processing of several files based on a description of the sites and video data in an SQLite database following the metadata module.') | 16 parser = argparse.ArgumentParser(description='This program manages the processing of several files based on a description of the sites and video data in an SQLite database following the metadata module.') |
| 17 # input | 17 # input |
| 18 parser.add_argument('--db', dest = 'metadataFilename', help = 'name of the metadata file', required = True) | 18 parser.add_argument('--db', dest = 'metadataFilename', help = 'name of the metadata file', required = True) |
| 19 parser.add_argument('--videos', dest = 'videoIds', help = 'indices of the video sequences', nargs = '*', type = int) | 19 parser.add_argument('--videos', dest = 'videoIds', help = 'indices of the video sequences', nargs = '*', type = int) |
| 20 parser.add_argument('--sites', dest = 'siteIds', help = 'indices of the video sequences', nargs = '*', type = int) | 20 parser.add_argument('--sites', dest = 'siteIds', help = 'indices of the video sequences', nargs = '*') |
| 21 | 21 |
| 22 # main function | 22 # main function |
| 23 parser.add_argument('--delete', dest = 'delete', help = 'data to delete', choices = ['feature', 'object', 'classification', 'interaction']) | 23 parser.add_argument('--delete', dest = 'delete', help = 'data to delete', choices = ['feature', 'object', 'classification', 'interaction']) |
| 24 parser.add_argument('--process', dest = 'process', help = 'data to process', choices = ['feature', 'object', 'classification', 'prototype', 'interaction']) | 24 parser.add_argument('--process', dest = 'process', help = 'data to process', choices = ['feature', 'object', 'classification', 'prototype', 'interaction']) |
| 25 parser.add_argument('--display', dest = 'display', help = 'data to display (replay over video)', choices = ['feature', 'object', 'classification', 'interaction']) | 25 parser.add_argument('--display', dest = 'display', help = 'data to display (replay over video)', choices = ['feature', 'object', 'classification', 'interaction']) |
| 30 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects/interactions to process', type = int) | 30 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects/interactions to process', type = int) |
| 31 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories', choices = ['feature', 'object'], default = 'feature') | 31 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories', choices = ['feature', 'object'], default = 'feature') |
| 32 parser.add_argument('--dry', dest = 'dryRun', help = 'dry run of processing', action = 'store_true') | 32 parser.add_argument('--dry', dest = 'dryRun', help = 'dry run of processing', action = 'store_true') |
| 33 parser.add_argument('--nthreads', dest = 'nProcesses', help = 'number of processes to run in parallel', type = int, default = 1) | 33 parser.add_argument('--nthreads', dest = 'nProcesses', help = 'number of processes to run in parallel', type = int, default = 1) |
| 34 parser.add_argument('--subsample', dest = 'positionSubsamplingRate', help = 'rate of position subsampling (1 every n positions)', type = int) | 34 parser.add_argument('--subsample', dest = 'positionSubsamplingRate', help = 'rate of position subsampling (1 every n positions)', type = int) |
| 35 parser.add_argument('--display', dest = 'display', help = 'display trajectories', action = 'store_true') | |
| 36 | 35 |
| 37 ### process options | 36 ### process options |
| 38 # motion pattern learning and assignment | 37 # motion pattern learning and assignment |
| 39 parser.add_argument('--prototype-filename', dest = 'outputPrototypeDatabaseFilename', help = 'name of the Sqlite database file to save prototypes') | 38 parser.add_argument('--prototype-filename', dest = 'outputPrototypeDatabaseFilename', help = 'name of the Sqlite database file to save prototypes', default = 'prototypes.sqlite') |
| 40 #parser.add_argument('-i', dest = 'inputPrototypeDatabaseFilename', help = 'name of the Sqlite database file for prototypes to start the algorithm with') | 39 #parser.add_argument('-i', dest = 'inputPrototypeDatabaseFilename', help = 'name of the Sqlite database file for prototypes to start the algorithm with') |
| 41 parser.add_argument('--nfeatures-per-object', dest = 'nLongestFeaturesPerObject', help = 'maximum number of features per object to load', type = int) | 40 parser.add_argument('--nfeatures-per-object', dest = 'nLongestFeaturesPerObject', help = 'maximum number of features per object to load', type = int) |
| 42 parser.add_argument('--epsilon', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float, required = True) | 41 parser.add_argument('--epsilon', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float) |
| 43 parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance | 42 parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance |
| 44 parser.add_argument('--minsimil', dest = 'minSimilarity', help = 'minimum similarity to put a trajectory in a cluster', type = float, required = True) | 43 parser.add_argument('--minsimil', dest = 'minSimilarity', help = 'minimum similarity to put a trajectory in a cluster', type = float) |
| 45 parser.add_argument('-min-cluster-size', dest = 'minClusterSize', help = 'minimum cluster size', type = int, default = 0) | 44 parser.add_argument('--min-cluster-size', dest = 'minClusterSize', help = 'minimum cluster size', type = int, default = 0) |
| 46 #parser.add_argument('--learn', dest = 'learn', help = 'learn', action = 'store_true') | 45 #parser.add_argument('--learn', dest = 'learn', help = 'learn', action = 'store_true') |
| 47 parser.add_argument('--optimize', dest = 'optimizeCentroid', help = 'recompute centroid at each assignment', action = 'store_true') | 46 parser.add_argument('--optimize', dest = 'optimizeCentroid', help = 'recompute centroid at each assignment', action = 'store_true') |
| 48 parser.add_argument('--random', dest = 'randomInitialization', help = 'random initialization of clustering algorithm', action = 'store_true') | 47 parser.add_argument('--random', dest = 'randomInitialization', help = 'random initialization of clustering algorithm', action = 'store_true') |
| 49 #parser.add_argument('--similarities-filename', dest = 'similaritiesFilename', help = 'filename of the similarities') | 48 #parser.add_argument('--similarities-filename', dest = 'similaritiesFilename', help = 'filename of the similarities') |
| 50 parser.add_argument('--save-similarities', dest = 'saveSimilarities', help = 'save computed similarities (in addition to prototypes)', action = 'store_true') | 49 parser.add_argument('--save-similarities', dest = 'saveSimilarities', help = 'save computed similarities (in addition to prototypes)', action = 'store_true') |
| 153 objects = {} | 152 objects = {} |
| 154 object2VideoSequences = {} | 153 object2VideoSequences = {} |
| 155 for cv in site.cameraViews: | 154 for cv in site.cameraViews: |
| 156 for vs in cv.videoSequences: | 155 for vs in cv.videoSequences: |
| 157 print('Loading '+vs.getDatabaseFilename()) | 156 print('Loading '+vs.getDatabaseFilename()) |
| 158 objects[vs.idx] = storage.loadTrajectoriesFromSqlite(str(parentPath/vs.getDatabaseFilename()), args.trajectoryType, args.nTrajectories, timeStep = args.positionSubsamplingRate, nLongestFeaturesPerObject = args.nLongestFeaturesPerObject) | 157 objects[vs.idx] = storage.loadTrajectoriesFromSqlite(str(parentPath/vs.getDatabaseFilename()), args.trajectoryType, args.nObjects, timeStep = args.positionSubsamplingRate, nLongestFeaturesPerObject = args.nLongestFeaturesPerObject) |
| 159 if args.trajectoryType == 'object' and args.nLongestFeaturesPerObject is not None: | 158 if args.trajectoryType == 'object' and args.nLongestFeaturesPerObject is not None: |
| 160 objectsWithFeatures = objects[vs.idx] | 159 objectsWithFeatures = objects[vs.idx] |
| 161 objects[vs.idx] = [f for o in objectsWithFeatures for f in o.getFeatures()] | 160 objects[vs.idx] = [f for o in objectsWithFeatures for f in o.getFeatures()] |
| 162 prototypeType = 'feature' | 161 prototypeType = 'feature' |
| 163 else: | 162 else: |
| 165 for obj in objects[vs.idx]: | 164 for obj in objects[vs.idx]: |
| 166 object2VideoSequences[obj] = vs | 165 object2VideoSequences[obj] = vs |
| 167 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) | 166 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) |
| 168 similarityFunc = lambda x,y : lcss.computeNormalized(x, y) | 167 similarityFunc = lambda x,y : lcss.computeNormalized(x, y) |
| 169 allobjects = [o for tmpobjects in objects.values() for o in tmpobjects] | 168 allobjects = [o for tmpobjects in objects.values() for o in tmpobjects] |
| 170 prototypeIndices, labels = processing.learnAssignMotionPatterns(True, True, allobjects, similarities, args.minsimil, similarityFunc, args.minClusterSize, args.optimizeCentroid, args.randomInitialization, True, []) | 169 similarities = -ones((len(allobjects), len(allobjects))) |
| 170 prototypeIndices, labels = processing.learnAssignMotionPatterns(True, True, allobjects, similarities, args.minSimilarity, similarityFunc, args.minClusterSize, args.optimizeCentroid, args.randomInitialization, True, []) | |
| 171 if args.outputPrototypeDatabaseFilename is None: | 171 if args.outputPrototypeDatabaseFilename is None: |
| 172 outputPrototypeDatabaseFilename = args.databaseFilename | 172 outputPrototypeDatabaseFilename = args.databaseFilename |
| 173 else: | 173 else: |
| 174 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename | 174 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename |
| 175 # TODO maintain mapping from object prototype to db filename + compute nmatchings before | 175 # TODO maintain mapping from object prototype to db filename + compute nmatchings before |
| 176 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) | 176 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) |
| 177 storage.savePrototypesToSqlite(outputPrototypeDatabaseFilename, [moving.Prototype(object2VideoSequences[allobjects[i]].getDatabaseFilename(False), allobjects[i].getNum(), prototypeType) for i in prototypeIndices]) | 177 storage.savePrototypesToSqlite(str(parentPath/site.getPath()/outputPrototypeDatabaseFilename), [moving.Prototype(object2VideoSequences[allobjects[i]].getDatabaseFilename(False), allobjects[i].getNum(), prototypeType, clusterSizes[i]) for i in prototypeIndices]) |
| 178 | 178 |
| 179 | 179 |
| 180 elif args.process == 'interaction': | 180 elif args.process == 'interaction': |
| 181 # safety analysis TODO make function in safety analysis script | 181 # safety analysis TODO make function in safety analysis script |
| 182 if args.predictionMethod == 'cvd': | 182 if args.predictionMethod == 'cvd': |
