comparison scripts/process.py @ 1048:27a822922cb0

work in progress
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 06 Jul 2018 11:13:10 -0400
parents 0b62e37991ab
children c9c03c97ed9f
comparison
equal deleted inserted replaced
1047:0b62e37991ab 1048:27a822922cb0
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, ones 10 import numpy as np
11 from pandas import DataFrame 11 from pandas import DataFrame
12 12
13 from trafficintelligence import storage, events, prediction, cvutils, utils, moving, processing, ml 13 from trafficintelligence import storage, events, prediction, cvutils, utils, moving, processing, ml
14 from trafficintelligence.metadata import * 14 from trafficintelligence.metadata import *
15 15
35 35
36 ### process options 36 ### process options
37 # motion pattern learning and assignment 37 # motion pattern learning and assignment
38 parser.add_argument('--prototype-filename', dest = 'outputPrototypeDatabaseFilename', help = 'name of the Sqlite database file to save prototypes', default = 'prototypes.sqlite') 38 parser.add_argument('--prototype-filename', dest = 'outputPrototypeDatabaseFilename', help = 'name of the Sqlite database file to save prototypes', default = 'prototypes.sqlite')
39 #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')
40 parser.add_argument('--nobjects-mp', dest = 'nMPObjects', help = 'number of objects/interactions to process', type = int)
40 parser.add_argument('--nfeatures-per-object', dest = 'nLongestFeaturesPerObject', help = 'maximum number of features per object to load', type = int) 41 parser.add_argument('--nfeatures-per-object', dest = 'nLongestFeaturesPerObject', help = 'maximum number of features per object to load', type = int)
41 parser.add_argument('--epsilon', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float) 42 parser.add_argument('--epsilon', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float)
42 parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance 43 parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance
43 parser.add_argument('--minsimil', dest = 'minSimilarity', help = 'minimum similarity to put a trajectory in a cluster', type = float) 44 parser.add_argument('--minsimil', dest = 'minSimilarity', help = 'minimum similarity to put a trajectory in a cluster', type = float)
44 parser.add_argument('--min-cluster-size', dest = 'minClusterSize', help = 'minimum cluster size', type = int, default = 0) 45 parser.add_argument('--min-cluster-size', dest = 'minClusterSize', help = 'minimum cluster size', type = int, default = 0)
147 148
148 elif args.process == 'prototype': # motion pattern learning 149 elif args.process == 'prototype': # motion pattern learning
149 # learn by site by default -> group videos by site (or by camera view? TODO add cameraviews) 150 # learn by site by default -> group videos by site (or by camera view? TODO add cameraviews)
150 # by default, load all objects, learn and then assign (BUT not save the assignments) 151 # by default, load all objects, learn and then assign (BUT not save the assignments)
151 for site in sites: 152 for site in sites:
152 print('Learning motion patterns for site {}'.format(site.name)) 153 print('Learning motion patterns for site {} ({})'.format(site.idx, site.name))
153 objects = {} 154 objects = {}
154 object2VideoSequences = {} 155 object2VideoSequences = {}
155 for cv in site.cameraViews: 156 for cv in site.cameraViews:
156 for vs in cv.videoSequences: 157 for vs in cv.videoSequences:
157 print('Loading '+vs.getDatabaseFilename()) 158 print('Loading '+vs.getDatabaseFilename())
164 prototypeType = args.trajectoryType 165 prototypeType = args.trajectoryType
165 for obj in objects[vs.idx]: 166 for obj in objects[vs.idx]:
166 object2VideoSequences[obj] = vs 167 object2VideoSequences[obj] = vs
167 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) 168 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon)
168 similarityFunc = lambda x,y : lcss.computeNormalized(x, y) 169 similarityFunc = lambda x,y : lcss.computeNormalized(x, y)
169 allobjects = [o for tmpobjects in objects.values() for o in tmpobjects] 170 trainingObjects = [o for tmpobjects in objects.values() for o in tmpobjects]
170 similarities = -ones((len(allobjects), len(allobjects))) 171 if args.nMPObjects is not None:
171 prototypeIndices, labels = processing.learnAssignMotionPatterns(True, True, allobjects, similarities, args.minSimilarity, similarityFunc, args.minClusterSize, args.optimizeCentroid, args.randomInitialization, True, []) 172 m = int(np.floor(float(len(trainingObjects))/args.nMPObjects))
173 trainingObjects = trainingObjects[::m]
174 similarities = -np.ones((len(trainingObjects), len(trainingObjects)))
175 prototypeIndices, labels = processing.learnAssignMotionPatterns(True, True, trainingObjects, similarities, args.minSimilarity, similarityFunc, args.minClusterSize, args.optimizeCentroid, args.randomInitialization, True, [])
172 if args.outputPrototypeDatabaseFilename is None: 176 if args.outputPrototypeDatabaseFilename is None:
173 outputPrototypeDatabaseFilename = args.databaseFilename 177 outputPrototypeDatabaseFilename = args.databaseFilename
174 else: 178 else:
175 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename 179 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename
176 # TODO maintain mapping from object prototype to db filename + compute nmatchings before 180 # TODO maintain mapping from object prototype to db filename + compute nmatchings before
177 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) 181 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1)
178 storage.savePrototypesToSqlite(str(parentPath/site.getPath()/outputPrototypeDatabaseFilename), [moving.Prototype(object2VideoSequences[allobjects[i]].getDatabaseFilename(False), allobjects[i].getNum(), prototypeType, clusterSizes[i]) for i in prototypeIndices]) 182 #print([clusterSizes[i] for i in prototypeIndices])
183 storage.savePrototypesToSqlite(str(parentPath/site.getPath()/outputPrototypeDatabaseFilename), [moving.Prototype(object2VideoSequences[trainingObjects[i]].getDatabaseFilename(False), trainingObjects[i].getNum(), prototypeType, clusterSizes[i]) for i in prototypeIndices])
179 184
180 185
181 elif args.process == 'interaction': 186 elif args.process == 'interaction':
182 # safety analysis TODO make function in safety analysis script 187 # safety analysis TODO make function in safety analysis script
183 if args.predictionMethod == 'cvd': 188 if args.predictionMethod == 'cvd':