Mercurial Hosting > traffic-intelligence
comparison scripts/learn-motion-patterns.py @ 818:181bcb6dad3a
added option to learn motion patterns and show to display results
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 21 Jun 2016 17:08:07 -0400 |
parents | 0e875a7f5759 |
children | f3ae72d86762 |
comparison
equal
deleted
inserted
replaced
817:b9ec0cc2677d | 818:181bcb6dad3a |
---|---|
9 | 9 |
10 parser = argparse.ArgumentParser(description='The program learns prototypes for the motion patterns') #, epilog = '' | 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') | 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) | 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') | 13 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['objectfeatures', 'feature', 'object'], default = 'objectfeatures') |
14 parser.add_argument('--max-nobjectfeatures', dest = 'maxNObjectFeatures', help = 'maximum number of features per object to load', type = int, default = 3) | |
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('-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('-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('--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('-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('-c', dest = 'minClusterSize', help = 'minimum cluster size', type = int, default = None) |
32 | 33 |
33 if args.trajectoryType == 'objectfeatures': | 34 if args.trajectoryType == 'objectfeatures': |
34 features = [] | 35 features = [] |
35 for o in objects: | 36 for o in objects: |
36 tmp = utils.sortByLength(o.getFeatures(), reverse = True) | 37 tmp = utils.sortByLength(o.getFeatures(), reverse = True) |
37 features += tmp[:min(len(tmp), 3)] | 38 features += tmp[:min(len(tmp), args.maxNObjectFeatures)] |
38 objects = features | 39 objects = features |
39 | 40 |
40 trajectories = [o.getPositions().asArray().T for o in objects] | 41 trajectories = [o.getPositions().asArray().T for o in objects] |
41 | 42 |
42 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) | 43 lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) |
48 # similarities[i,j] = lcss.computeNormalized(trajectories[i], trajectories[j]) | 49 # similarities[i,j] = lcss.computeNormalized(trajectories[i], trajectories[j]) |
49 # similarities[j,i] = similarities[i,j] | 50 # similarities[j,i] = similarities[i,j] |
50 | 51 |
51 prototypeIndices, labels = ml.prototypeCluster(trajectories, similarities, args.minSimilarity, lambda x,y : lcss.computeNormalized(x, y), args.minClusterSize) # this line can be called again without reinitializing similarities | 52 prototypeIndices, labels = ml.prototypeCluster(trajectories, similarities, args.minSimilarity, lambda x,y : lcss.computeNormalized(x, y), args.minClusterSize) # this line can be called again without reinitializing similarities |
52 | 53 |
54 print(ml.computeClusterSizes(labels, prototypeIndices, -1)) | |
55 | |
53 if args.display: | 56 if args.display: |
54 from matplotlib.pyplot import figure | 57 from matplotlib.pyplot import figure, show |
55 figure() | 58 figure() |
56 for i,o in enumerate(objects): | 59 for i,o in enumerate(objects): |
57 if i not in prototypeIndices: | 60 if i not in prototypeIndices: |
58 if labels[i] < 0: | 61 if labels[i] < 0: |
59 o.plot('kx') | 62 o.plot('kx') |
60 else: | 63 else: |
61 o.plot(utils.colors[labels[i]]) | 64 o.plot(utils.colors[labels[i]]) |
62 for i in prototypeIndices: | 65 for i in prototypeIndices: |
63 objects[i].plot(utils.colors[i]+'o') | 66 objects[i].plot(utils.colors[i]+'o') |
67 show() | |
64 | 68 |
65 # TODO store the prototypes (if features, easy, if objects, info must be stored about the type) | 69 # TODO store the prototypes (if features, easy, if objects, info must be stored about the type) |