Mercurial > hg > nsaunier > traffic-intelligence
comparison trafficintelligence/processing.py @ 1077:3939ae415be0
Merging
| author | Wendlasida |
|---|---|
| date | Fri, 20 Jul 2018 14:03:34 -0400 |
| parents | 092bd9c7deaf |
| children | c67f8c36ebc7 |
comparison
equal
deleted
inserted
replaced
| 1076:108c5dc4e34a | 1077:3939ae415be0 |
|---|---|
| 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 ml | 6 from trafficintelligence import ml, storage, utils |
| 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 extractVideoSequenceSpeeds(dbFilename, siteName, nObjects, startTime, frameRate, minUserDurationSeconds, aggMethods, aggCentiles): | |
| 22 data = [] | |
| 23 d = startTime.date() | |
| 24 t1 = startTime.time() | |
| 25 minUserDuration = minUserDurationSeconds*frameRate | |
| 26 print('Extracting speed from '+dbFilename) | |
| 27 aggFunctions, tmpheaders = utils.aggregationMethods(aggMethods, aggCentiles) | |
| 28 objects = storage.loadTrajectoriesFromSqlite(dbFilename, 'object', nObjects) | |
| 29 for o in objects: | |
| 30 if o.length() > minUserDuration: | |
| 31 row = [siteName, d, utils.framesToTime(o.getFirstInstant(), frameRate, t1), o.getUserType()] | |
| 32 tmp = o.getSpeeds() | |
| 33 for method,func in aggFunctions.items(): | |
| 34 aggSpeeds = frameRate*3.6*func(tmp) | |
| 35 if method == 'centile': | |
| 36 row.extend(aggSpeeds.tolist()) | |
| 37 else: | |
| 38 row.append(aggSpeeds) | |
| 39 data.append(row) | |
| 40 return data | |
| 20 | 41 |
| 21 def learnAssignMotionPatterns(learn, assign, objects, similarities, minSimilarity, similarityFunc, minClusterSize = 0, optimizeCentroid = False, randomInitialization = False, removePrototypesAfterAssignment = False, initialPrototypes = []): | 42 def learnAssignMotionPatterns(learn, assign, objects, similarities, minSimilarity, similarityFunc, minClusterSize = 0, optimizeCentroid = False, randomInitialization = False, removePrototypesAfterAssignment = False, initialPrototypes = []): |
| 22 '''Learns motion patterns | 43 '''Learns motion patterns |
| 23 | 44 |
| 24 During assignments, if using minClusterSize > 0, prototypes can change (be removed) | 45 During assignments, if using minClusterSize > 0, prototypes can change (be removed) |
