Mercurial Hosting > traffic-intelligence
changeset 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 |
files | scripts/process.py |
diffstat | 1 files changed, 11 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/process.py Thu Jul 05 23:17:12 2018 -0400 +++ b/scripts/process.py Fri Jul 06 11:13:10 2018 -0400 @@ -7,7 +7,7 @@ #import matplotlib #atplotlib.use('Agg') import matplotlib.pyplot as plt -from numpy import percentile, ones +import numpy as np from pandas import DataFrame from trafficintelligence import storage, events, prediction, cvutils, utils, moving, processing, ml @@ -37,6 +37,7 @@ # motion pattern learning and assignment parser.add_argument('--prototype-filename', dest = 'outputPrototypeDatabaseFilename', help = 'name of the Sqlite database file to save prototypes', default = 'prototypes.sqlite') #parser.add_argument('-i', dest = 'inputPrototypeDatabaseFilename', help = 'name of the Sqlite database file for prototypes to start the algorithm with') +parser.add_argument('--nobjects-mp', dest = 'nMPObjects', help = 'number of objects/interactions to process', type = int) parser.add_argument('--nfeatures-per-object', dest = 'nLongestFeaturesPerObject', help = 'maximum number of features per object to load', type = int) parser.add_argument('--epsilon', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float) parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance @@ -149,7 +150,7 @@ # learn by site by default -> group videos by site (or by camera view? TODO add cameraviews) # by default, load all objects, learn and then assign (BUT not save the assignments) for site in sites: - print('Learning motion patterns for site {}'.format(site.name)) + print('Learning motion patterns for site {} ({})'.format(site.idx, site.name)) objects = {} object2VideoSequences = {} for cv in site.cameraViews: @@ -166,16 +167,20 @@ object2VideoSequences[obj] = vs lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) similarityFunc = lambda x,y : lcss.computeNormalized(x, y) - allobjects = [o for tmpobjects in objects.values() for o in tmpobjects] - similarities = -ones((len(allobjects), len(allobjects))) - prototypeIndices, labels = processing.learnAssignMotionPatterns(True, True, allobjects, similarities, args.minSimilarity, similarityFunc, args.minClusterSize, args.optimizeCentroid, args.randomInitialization, True, []) + trainingObjects = [o for tmpobjects in objects.values() for o in tmpobjects] + if args.nMPObjects is not None: + m = int(np.floor(float(len(trainingObjects))/args.nMPObjects)) + trainingObjects = trainingObjects[::m] + similarities = -np.ones((len(trainingObjects), len(trainingObjects))) + prototypeIndices, labels = processing.learnAssignMotionPatterns(True, True, trainingObjects, similarities, args.minSimilarity, similarityFunc, args.minClusterSize, args.optimizeCentroid, args.randomInitialization, True, []) if args.outputPrototypeDatabaseFilename is None: outputPrototypeDatabaseFilename = args.databaseFilename else: outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename # TODO maintain mapping from object prototype to db filename + compute nmatchings before clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) - storage.savePrototypesToSqlite(str(parentPath/site.getPath()/outputPrototypeDatabaseFilename), [moving.Prototype(object2VideoSequences[allobjects[i]].getDatabaseFilename(False), allobjects[i].getNum(), prototypeType, clusterSizes[i]) for i in prototypeIndices]) + #print([clusterSizes[i] for i in prototypeIndices]) + storage.savePrototypesToSqlite(str(parentPath/site.getPath()/outputPrototypeDatabaseFilename), [moving.Prototype(object2VideoSequences[trainingObjects[i]].getDatabaseFilename(False), trainingObjects[i].getNum(), prototypeType, clusterSizes[i]) for i in prototypeIndices]) elif args.process == 'interaction':