diff scripts/process.py @ 1004:75601be6019f

work on process
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sun, 03 Jun 2018 00:21:18 -0400
parents 75af46516b2b
children 666b38437d9a
line wrap: on
line diff
--- a/scripts/process.py	Fri Jun 01 17:19:31 2018 -0400
+++ b/scripts/process.py	Sun Jun 03 00:21:18 2018 -0400
@@ -2,18 +2,20 @@
 
 import sys, argparse
 from pathlib import Path
+from multiprocessing.pool import Pool
 
 import matplotlib
 matplotlib.use('Agg')
 import matplotlib.pyplot as plt
 from numpy import percentile
 
-import storage, events, prediction
+import storage, events, prediction, cvutils
 from metadata import *
 
 parser = argparse.ArgumentParser(description='This program manages the processing of several files based on a description of the sites and video data in an SQLite database following the metadata module.')
 parser.add_argument('--db', dest = 'metadataFilename', help = 'name of the metadata file', required = True)
 parser.add_argument('--videos', dest = 'videoIds', help = 'indices of the video sequences', nargs = '*', type = int)
+parser.add_argument('--sites', dest = 'siteIds', help = 'indices of the video sequences', nargs = '*', type = int)
 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file')
 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects/interactions to process', type = int)
 parser.add_argument('--prediction-method', dest = 'predictionMethod', help = 'prediction method (constant velocity (cvd: vector computation (approximate); cve: equation solving; cv: discrete time (approximate)), normal adaptation, point set prediction)', choices = ['cvd', 'cve', 'cv', 'na', 'ps', 'mp'])
@@ -46,15 +48,43 @@
             vs = session.query(VideoSequence).get(videoId)
             storage.deleteFromSqlite(str(parentDir/vs.getDatabaseFilename()), args.delete)
 
+import time
+def track(i):
+    time.sleep(1)
+    print('process {}'.format(i))
+
+            
 if args.process in ['feature', 'object']: # tracking
-    for videoId in args.videoIds:
-        vs = session.query(VideoSequence).get(videoId)
-        if args.configFilename is None:
-            configFilename = vs.cameraView.getTrackingConfigurationFilename()
-        else:
-            configFilename = args.configFilename
-        #todo cvutils.tracking(configFilename, args.process == 'object', str(parentDir/vs.getVideoSequenceFilename(), str(parentDir/vs.getDatabaseFilename(), configFilename = vs.cameraView.getHomographyFilename())
-    
+    if args.videoIds is not None:
+        videoSequences = [session.query(VideoSequence).get(videoId) for videoId in args.videoIds]
+    elif args.siteIds is not None:
+        videoSequences = []
+        for siteId in args.siteIds:
+            for site in getSite(session, siteId):
+                for cv in site.cameraViews:
+                    videoSequences += cv.videoSequences
+    else:
+        print('No video/site to process')
+        videoSequences = []
+    if args.nProcesses == 1:
+        pass
+    else:
+        pool = Pool(args.nProcesses)
+        for vs in videoSequences:
+            if not (parentDir/vs.getDatabaseFilename()).exists():
+                if args.configFilename is None:
+                    configFilename = vs.cameraView.getTrackingConfigurationFilename()
+                else:
+                    configFilename = args.configFilename
+                if vs.cameraView.cameraType is None:
+                    pool.apply_async(cvutils.tracking, args = (configFilename, args.process == 'object', str(parentDir.absolute()/vs.getVideoSequenceFilename()), str(parentDir.absolute()/vs.getDatabaseFilename()), str(parentDir.absolute()/vs.cameraView.getHomographyFilename()), str(parentDir.absolute()/vs.cameraView.getMaskFilename()), False, None, None, True))
+                else:
+                    pool.apply_async(cvutils.tracking, args = (configFilename, args.process == 'object', str(parentDir.absolute()/vs.getVideoSequenceFilename()), str(parentDir.absolute()/vs.getDatabaseFilename()), str(parentDir.absolute()/vs.cameraView.getHomographyFilename()), str(parentDir.absolute()/vs.cameraView.getMaskFilename()), True, vs.cameraView.cameraType.intrinsicCameraMatrix, vs.cameraView.cameraType.distortionCoefficients, True))
+            else:
+                print('SQLite already exists: {}'.format(parentDir/vs.getDatabaseFilename()))
+        pool.close()
+        pool.join()
+
 elif args.process == 'interaction':
     # safety analysis TODO make function in safety analysis script
     if args.predictionMethod == 'cvd':