Mercurial > hg > nsaunier > traffic-intelligence
comparison trafficintelligence/processing.py @ 1066:862b55a87e63
work on extracting information
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 16 Jul 2018 01:14:37 -0400 |
| parents | 25db2383e7ae |
| children | 092bd9c7deaf |
comparison
equal
deleted
inserted
replaced
| 1065:d4d052a05337 | 1066:862b55a87e63 |
|---|---|
| 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, aggFunctions): | |
| 22 data = [] | |
| 23 d = startTime.date() | |
| 24 t1 = startTime.time() | |
| 25 minUserDuration = minUserDurationSeconds*frameRate | |
| 26 print('Extracting speed from '+dbFilename) | |
| 27 objects = storage.loadTrajectoriesFromSqlite(dbFilename, 'object', nObjects) | |
| 28 for o in objects: | |
| 29 if o.length() > minUserDuration: | |
| 30 row = [siteName, d, utils.framesToTime(o.getFirstInstant(), frameRate, t1), o.getUserType()] | |
| 31 tmp = o.getSpeeds() | |
| 32 for method,func in aggFunctions.items(): | |
| 33 aggSpeeds = frameRate*3.6*func(tmp) | |
| 34 if method == 'centile': | |
| 35 row += aggSpeeds.tolist() | |
| 36 else: | |
| 37 row.append(aggSpeeds) | |
| 38 data.append(row) | |
| 39 return data | |
| 20 | 40 |
| 21 def learnAssignMotionPatterns(learn, assign, objects, similarities, minSimilarity, similarityFunc, minClusterSize = 0, optimizeCentroid = False, randomInitialization = False, removePrototypesAfterAssignment = False, initialPrototypes = []): | 41 def learnAssignMotionPatterns(learn, assign, objects, similarities, minSimilarity, similarityFunc, minClusterSize = 0, optimizeCentroid = False, randomInitialization = False, removePrototypesAfterAssignment = False, initialPrototypes = []): |
| 22 '''Learns motion patterns | 42 '''Learns motion patterns |
| 23 | 43 |
| 24 During assignments, if using minClusterSize > 0, prototypes can change (be removed) | 44 During assignments, if using minClusterSize > 0, prototypes can change (be removed) |
