Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/learn-motion-patterns.py @ 734:1d4dcb5c8708 dev
first example script to learn prototypes
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 11 Aug 2015 12:55:09 -0400 |
| parents | |
| children | 0e875a7f5759 |
comparison
equal
deleted
inserted
replaced
| 733:c35e4a4b199d | 734:1d4dcb5c8708 |
|---|---|
| 1 #! /usr/bin/env python | |
| 2 | |
| 3 import sys, argparse | |
| 4 | |
| 5 #import matplotlib.pyplot as plt | |
| 6 import numpy as np | |
| 7 | |
| 8 import ml, utils, storage | |
| 9 | |
| 10 parser = argparse.ArgumentParser(description='The program learns prototypes for the motion patterns') #, epilog = '' | |
| 11 #parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file') | |
| 12 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) | |
| 13 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['objectfeatures', 'feature', 'object'], default = 'objectfeatures') | |
| 14 parser.add_argument('-n', dest = 'nTrajectories', help = 'number of the object or feature trajectories to load', type = int, default = None) | |
| 15 parser.add_argument('-e', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float, required = True) | |
| 16 parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance | |
| 17 parser.add_argument('-s', dest = 'minSimilarity', help = 'minimum similarity to put a trajectory in a cluster', type = float, required = True) | |
| 18 parser.add_argument('-c', dest = 'minClusterSize', help = 'minimum cluster size', type = int, default = None) | |
| 19 parser.add_argument('--display', dest = 'display', help = 'display trajectories', action = 'store_true') # default is manhattan distance | |
| 20 | |
| 21 args = parser.parse_args() | |
| 22 | |
| 23 # TODO parameters (random init?) and what to learn from: objects, features, longest features from objects | |
| 24 # TODO add possibility to cluter with velocities | |
| 25 | |
| 26 trajectoryType = args.trajectoryType | |
| 27 if args.trajectoryType == 'objectfeatures': | |
| 28 trajectoryType = 'object' | |
| 29 | |
| 30 #features = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType) | |
| 31 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, withFeatures = (args.trajectoryType == 'objectfeatures'), objectNumbers = args.nTrajectories) | |
| 32 | |
| 33 if args.trajectoryType == 'objectfeatures': | |
| 34 features = [] | |
| 35 for o in objects: | |
| 36 tmp = utils.sortByLength(o.getFeatures(), reverse = True) | |
| 37 features += tmp[:min(len(tmp), 3)] | |
| 38 objects = features | |
| 39 | |
| 40 trajectories = [o.getPositions().asArray().T for o in objects] | |
| 41 | |
| 42 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) | |
| 43 nTrajectories = len(trajectories) | |
| 44 | |
| 45 similarities = np.zeros((nTrajectories, nTrajectories)) | |
| 46 for i in xrange(nTrajectories): | |
| 47 for j in xrange(i): | |
| 48 similarities[i,j] = lcss.computeNormalized(trajectories[i], trajectories[j]) | |
| 49 similarities[j,i] = similarities[i,j] | |
| 50 | |
| 51 prototypeIndices, labels = ml.prototypeCluster(trajectories, similarities, args.minSimilarity, args.minClusterSize) | |
| 52 | |
| 53 if args.display: | |
| 54 for i,o in enumerate(objects): | |
| 55 if i not in prototypeIndices: | |
| 56 o.plot(utils.colors[labels[i]]) | |
| 57 for i in prototypeIndices: | |
| 58 objects[i].plot(utils.colors[i]+'o') | |
| 59 | |
| 60 # TODO store the prototypes (if features, easy, if objects, info must be stored about the type) |
