changeset 1066:862b55a87e63

work on extracting information
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 16 Jul 2018 01:14:37 -0400
parents d4d052a05337
children 092bd9c7deaf
files scripts/process.py trafficintelligence/processing.py
diffstat 2 files changed, 32 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/process.py	Mon Jul 16 00:05:17 2018 -0400
+++ b/scripts/process.py	Mon Jul 16 01:14:37 2018 -0400
@@ -116,7 +116,7 @@
     for site in sites:
         unprocessedVideoSequences = []
         for vs in getSiteVideoSequences(site):
-            if (parentPath/vs.getDatabaseFilename()).is_file():
+            if (parentPath/vs.getDatabaseFilename()).is_file(): # TODO check time of file?
                 tableNames = storage.tableNames(str(parentPath.absolute()/vs.getDatabaseFilename()))
                 data.append([site.name, vs.idx, 'positions' in tableNames, 'objects' in tableNames, 'interactions' in tableNames])
             else:
@@ -162,7 +162,7 @@
                     configFilename = args.configFilename
                 if vs.cameraView.cameraType is None:
                     cvutils.tracking(configFilename, args.process == 'object', str(parentPath.absolute()/vs.getVideoSequenceFilename()), str(parentPath.absolute()/vs.getDatabaseFilename()), str(parentPath.absolute()/vs.cameraView.getHomographyFilename()), str(parentPath.absolute()/vs.cameraView.getMaskFilename()), False, None, None, args.dryRun)
-                else:
+                else: #caution: cameratype can be not none, but without parameters for undistortion
                     cvutils.tracking(configFilename, args.process == 'object', str(parentPath.absolute()/vs.getVideoSequenceFilename()), str(parentPath.absolute()/vs.getDatabaseFilename()), str(parentPath.absolute()/vs.cameraView.getHomographyFilename()), str(parentPath.absolute()/vs.cameraView.getMaskFilename()), True, vs.cameraView.cameraType.intrinsicCameraMatrix, vs.cameraView.cameraType.distortionCoefficients, args.dryRun)
             else:
                 print('SQLite already exists: {}'.format(parentPath/vs.getDatabaseFilename()))
@@ -252,23 +252,15 @@
     headers = ['site', 'date', 'time', 'user_type']
     aggFunctions, tmpheaders = utils.aggregationMethods(args.aggMethods, args.aggCentiles)
     headers.extend(tmpheaders)
-    for vs in videoSequences:
-        d = vs.startTime.date()
-        t1 = vs.startTime.time()
-        minUserDuration = args.minUserDuration*vs.cameraView.cameraType.frameRate
-        print('Extracting speed from '+vs.getDatabaseFilename())
-        objects = storage.loadTrajectoriesFromSqlite(str(parentPath/vs.getDatabaseFilename()), 'object', args.nObjects)
-        for o in objects:
-            if o.length() > minUserDuration:
-                row = [vs.cameraView.site.name, d, utils.framesToTime(o.getFirstInstant(), vs.cameraView.cameraType.frameRate, t1), o.getUserType()]
-                tmp = o.getSpeeds()
-                for method,func in aggFunctions.items():
-                    aggSpeeds = vs.cameraView.cameraType.frameRate*3.6*func(tmp)
-                    if method == 'centile':
-                        row += aggSpeeds.tolist()
-                    else:
-                        row.append(aggSpeeds)
-            data.append(row)
+    if args.nProcesses == 1:
+        for vs in videoSequences:
+            data.extend(processing.extractVideoSequenceSpeeds(str(parentPath/vs.getDatabaseFilename()), vs.cameraView.site.name, args.nObjects, vs.startTime, vs.cameraView.cameraType.frameRate, args.minUserDuration, aggFunctions))
+    else:
+        jobs = [pool.apply_async(processing.extractVideoSequenceSpeeds, args = (str(parentPath/vs.getDatabaseFilename()), vs.cameraView.site.name, args.nObjects, vs.startTime, vs.cameraView.cameraType.frameRate, args.minUserDuration, aggFunctions)) for vs in videoSequences]
+        for job in jobs:
+            data.extend(job.get())
+        pool.close()
+            
     data = pd.DataFrame(data, columns = headers)
     if args.output == 'figure':
         for name in headers[4:]:
--- a/trafficintelligence/processing.py	Mon Jul 16 00:05:17 2018 -0400
+++ b/trafficintelligence/processing.py	Mon Jul 16 01:14:37 2018 -0400
@@ -3,7 +3,7 @@
 
 import numpy as np
 
-from trafficintelligence import ml
+from trafficintelligence import ml, storage, utils
 
 def extractSpeeds(objects, zone):
     speeds = {}
@@ -18,6 +18,26 @@
             objectsNotInZone.append(o)
     return speeds, objectsNotInZone
 
+def extractVideoSequenceSpeeds(dbFilename, siteName, nObjects, startTime, frameRate, minUserDurationSeconds, aggFunctions):
+    data = []
+    d = startTime.date()
+    t1 = startTime.time()
+    minUserDuration = minUserDurationSeconds*frameRate
+    print('Extracting speed from '+dbFilename)
+    objects = storage.loadTrajectoriesFromSqlite(dbFilename, 'object', nObjects)
+    for o in objects:
+        if o.length() > minUserDuration:
+            row = [siteName, d, utils.framesToTime(o.getFirstInstant(), frameRate, t1), o.getUserType()]
+            tmp = o.getSpeeds()
+            for method,func in aggFunctions.items():
+                aggSpeeds = frameRate*3.6*func(tmp)
+                if method == 'centile':
+                    row += aggSpeeds.tolist()
+                else:
+                    row.append(aggSpeeds)
+        data.append(row)
+    return data
+
 def learnAssignMotionPatterns(learn, assign, objects, similarities, minSimilarity, similarityFunc, minClusterSize = 0, optimizeCentroid = False, randomInitialization = False, removePrototypesAfterAssignment = False, initialPrototypes = []):
     '''Learns motion patterns