Mercurial Hosting > traffic-intelligence
comparison trafficintelligence/processing.py @ 1054:d13f9bfbf3ff
Retry
author | Wendlasida |
---|---|
date | Fri, 06 Jul 2018 18:42:58 -0400 |
parents | 25db2383e7ae |
children | 862b55a87e63 |
comparison
equal
deleted
inserted
replaced
1053:60cc87e824c4 | 1054:d13f9bfbf3ff |
---|---|
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 '''Algorithms to process trajectories and moving objects''' | 2 '''Algorithms to process trajectories and moving objects''' |
3 | 3 |
4 import numpy as np | 4 import numpy as np |
5 | 5 |
6 from trafficintelligence import moving | 6 from trafficintelligence import ml |
7 | 7 |
8 def extractSpeeds(objects, zone): | 8 def extractSpeeds(objects, zone): |
9 speeds = {} | 9 speeds = {} |
10 objectsNotInZone = [] | 10 objectsNotInZone = [] |
11 import matplotlib.nxutils as nx | 11 import matplotlib.nxutils as nx |
15 objspeeds = [o.getVelocityAt(i).norm2() for i in range(int(o.length()-1)) if inPolygon[i]] | 15 objspeeds = [o.getVelocityAt(i).norm2() for i in range(int(o.length()-1)) if inPolygon[i]] |
16 speeds[o.num] = np.mean(objspeeds) # km/h | 16 speeds[o.num] = np.mean(objspeeds) # km/h |
17 else: | 17 else: |
18 objectsNotInZone.append(o) | 18 objectsNotInZone.append(o) |
19 return speeds, objectsNotInZone | 19 return speeds, objectsNotInZone |
20 | |
21 def learnAssignMotionPatterns(learn, assign, objects, similarities, minSimilarity, similarityFunc, minClusterSize = 0, optimizeCentroid = False, randomInitialization = False, removePrototypesAfterAssignment = False, initialPrototypes = []): | |
22 '''Learns motion patterns | |
23 | |
24 During assignments, if using minClusterSize > 0, prototypes can change (be removed) | |
25 The argument removePrototypesAfterAssignment indicates whether the prototypes are removed or not''' | |
26 if len(initialPrototypes) > 0: | |
27 initialPrototypeIndices = list(range(len(initialPrototypes))) | |
28 trajectories = [p.getMovingObject().getPositions().asArray().T for p in initialPrototypes] | |
29 else: | |
30 initialPrototypeIndices = None | |
31 trajectories = [] | |
32 trajectories.extend([o.getPositions().asArray().T for o in objects]) | |
33 | |
34 if learn: | |
35 prototypeIndices = ml.prototypeCluster(trajectories, similarities, minSimilarity, similarityFunc, optimizeCentroid, randomInitialization, initialPrototypeIndices) | |
36 else: | |
37 prototypeIndices = initialPrototypeIndices | |
38 | |
39 if assign: | |
40 assignedPrototypeIndices, labels = ml.assignToPrototypeClusters(trajectories, prototypeIndices, similarities, minSimilarity, similarityFunc, minClusterSize) | |
41 if minClusterSize > 0 and removePrototypesAfterAssignment: # use prototypeIndices anyway | |
42 prototypeIndices = assignedPrototypeIndices | |
43 else: | |
44 labels = None | |
45 | |
46 return prototypeIndices, labels | |
47 |