comparison scripts/process.py @ 1021:16932cefabc1

work on paths in line with new configurations from tracker
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 05 Jun 2018 17:02:28 -0400
parents 0d29b75f74ea
children a13f47c8931d
comparison
equal deleted inserted replaced
1020:9fb82fe0156f 1021:16932cefabc1
38 38
39 ################################# 39 #################################
40 # Data preparation 40 # Data preparation
41 ################################# 41 #################################
42 session = connectDatabase(args.metadataFilename) 42 session = connectDatabase(args.metadataFilename)
43 parentDir = Path(args.metadataFilename).parent # files are relative to metadata location 43 parentPath = Path(args.metadataFilename).parent # files are relative to metadata location
44 videoSequences = [] 44 videoSequences = []
45 if args.videoIds is not None: 45 if args.videoIds is not None:
46 videoSequences = [session.query(VideoSequence).get(videoId) for videoId in args.videoIds] 46 videoSequences = [session.query(VideoSequence).get(videoId) for videoId in args.videoIds]
47 elif args.siteIds is not None: 47 elif args.siteIds is not None:
48 for siteId in args.siteIds: 48 for siteId in args.siteIds:
59 if args.delete == 'feature': 59 if args.delete == 'feature':
60 pass 60 pass
61 elif args.delete in ['object', 'interaction']: 61 elif args.delete in ['object', 'interaction']:
62 #parser.add_argument('-t', dest = 'dataType', help = 'type of the data to remove', required = True, choices = ['object','interaction', 'bb', 'pois', 'prototype']) 62 #parser.add_argument('-t', dest = 'dataType', help = 'type of the data to remove', required = True, choices = ['object','interaction', 'bb', 'pois', 'prototype'])
63 for vs in videoSequences: 63 for vs in videoSequences:
64 storage.deleteFromSqlite(str(parentDir/vs.getDatabaseFilename()), args.delete) 64 storage.deleteFromSqlite(str(parentPath/vs.getDatabaseFilename()), args.delete)
65 65
66 ################################# 66 #################################
67 # Process 67 # Process
68 ################################# 68 #################################
69 if args.process in ['feature', 'object']: # tracking 69 if args.process in ['feature', 'object']: # tracking
70 if args.nProcesses == 1: 70 if args.nProcesses == 1:
71 for vs in videoSequences: 71 for vs in videoSequences:
72 if not (parentDir/vs.getDatabaseFilename()).exists() or args.process == 'object': 72 if not (parentPath/vs.getDatabaseFilename()).exists() or args.process == 'object':
73 if args.configFilename is None: 73 if args.configFilename is None:
74 configFilename = str(parentDir/vs.cameraView.getTrackingConfigurationFilename()) 74 configFilename = str(parentPath/vs.cameraView.getTrackingConfigurationFilename())
75 else: 75 else:
76 configFilename = args.configFilename 76 configFilename = args.configFilename
77 if vs.cameraView.cameraType is None: 77 if vs.cameraView.cameraType is None:
78 cvutils.tracking(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, args.dryRun) 78 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)
79 else: 79 else:
80 cvutils.tracking(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, args.dryRun) 80 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)
81 else: 81 else:
82 print('SQLite already exists: {}'.format(parentDir/vs.getDatabaseFilename())) 82 print('SQLite already exists: {}'.format(parentPath/vs.getDatabaseFilename()))
83 else: 83 else:
84 pool = Pool(args.nProcesses) 84 pool = Pool(args.nProcesses)
85 for vs in videoSequences: 85 for vs in videoSequences:
86 if not (parentDir/vs.getDatabaseFilename()).exists() or args.process == 'object': 86 if not (parentPath/vs.getDatabaseFilename()).exists() or args.process == 'object':
87 if args.configFilename is None: 87 if args.configFilename is None:
88 configFilename = str(parentDir/vs.cameraView.getTrackingConfigurationFilename()) 88 configFilename = str(parentPath/vs.cameraView.getTrackingConfigurationFilename())
89 else: 89 else:
90 configFilename = args.configFilename 90 configFilename = args.configFilename
91 if vs.cameraView.cameraType is None: 91 if vs.cameraView.cameraType is None:
92 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, args.dryRun)) 92 pool.apply_async(cvutils.tracking, args = (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))
93 else: 93 else:
94 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, args.dryRun)) 94 pool.apply_async(cvutils.tracking, args = (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))
95 else: 95 else:
96 print('SQLite already exists: {}'.format(parentDir/vs.getDatabaseFilename())) 96 print('SQLite already exists: {}'.format(parentPath/vs.getDatabaseFilename()))
97 pool.close() 97 pool.close()
98 pool.join() 98 pool.join()
99 99
100 elif args.process == 'interaction': 100 elif args.process == 'interaction':
101 # safety analysis TODO make function in safety analysis script 101 # safety analysis TODO make function in safety analysis script
103 predictionParameters = prediction.CVDirectPredictionParameters() 103 predictionParameters = prediction.CVDirectPredictionParameters()
104 if args.predictionMethod == 'cve': 104 if args.predictionMethod == 'cve':
105 predictionParameters = prediction.CVExactPredictionParameters() 105 predictionParameters = prediction.CVExactPredictionParameters()
106 for vs in videoSequences: 106 for vs in videoSequences:
107 print('Processing '+vs.getDatabaseFilename()) 107 print('Processing '+vs.getDatabaseFilename())
108 objects = storage.loadTrajectoriesFromSqlite(str(parentDir/vs.getDatabaseFilename()), 'object')#, args.nObjects, withFeatures = (params.useFeaturesForPrediction or predictionMethod == 'ps' or predictionMethod == 'mp')) 108 objects = storage.loadTrajectoriesFromSqlite(str(parentPath/vs.getDatabaseFilename()), 'object')#, args.nObjects, withFeatures = (params.useFeaturesForPrediction or predictionMethod == 'ps' or predictionMethod == 'mp'))
109 interactions = events.createInteractions(objects) 109 interactions = events.createInteractions(objects)
110 #if args.nProcesses == 1: 110 #if args.nProcesses == 1:
111 #print(str(parentDir/vs.cameraView.getTrackingConfigurationFilename())) 111 #print(str(parentPath/vs.cameraView.getTrackingConfigurationFilename()))
112 params = storage.ProcessParameters(str(parentDir/vs.cameraView.getTrackingConfigurationFilename())) 112 params = storage.ProcessParameters(str(parentPath/vs.cameraView.getTrackingConfigurationFilename()))
113 #print(len(interactions), args.computePET, predictionParameters, params.collisionDistance, params.predictionTimeHorizon, params.crossingZones) 113 #print(len(interactions), args.computePET, predictionParameters, params.collisionDistance, params.predictionTimeHorizon, params.crossingZones)
114 processed = events.computeIndicators(interactions, True, args.computePET, predictionParameters, params.collisionDistance, params.predictionTimeHorizon, params.crossingZones, False, None) 114 processed = events.computeIndicators(interactions, True, args.computePET, predictionParameters, params.collisionDistance, params.predictionTimeHorizon, params.crossingZones, False, None)
115 storage.saveIndicatorsToSqlite(str(parentDir/vs.getDatabaseFilename()), processed) 115 storage.saveIndicatorsToSqlite(str(parentPath/vs.getDatabaseFilename()), processed)
116 # else: 116 # else:
117 # pool = Pool(processes = args.nProcesses) 117 # pool = Pool(processes = args.nProcesses)
118 # nInteractionPerProcess = int(np.ceil(len(interactions)/float(args.nProcesses))) 118 # nInteractionPerProcess = int(np.ceil(len(interactions)/float(args.nProcesses)))
119 # jobs = [pool.apply_async(events.computeIndicators, args = (interactions[i*nInteractionPerProcess:(i+1)*nInteractionPerProcess], not args.noMotionPrediction, args.computePET, predictionParameters, params.collisionDistance, params.predictionTimeHorizon, params.crossingZones, False, None)) for i in range(args.nProcesses)] 119 # jobs = [pool.apply_async(events.computeIndicators, args = (interactions[i*nInteractionPerProcess:(i+1)*nInteractionPerProcess], not args.noMotionPrediction, args.computePET, predictionParameters, params.collisionDistance, params.predictionTimeHorizon, params.crossingZones, False, None)) for i in range(args.nProcesses)]
120 # processed = [] 120 # processed = []
132 for vs in videoSequences: 132 for vs in videoSequences:
133 if not vs.cameraView.siteIdx in medianSpeeds: 133 if not vs.cameraView.siteIdx in medianSpeeds:
134 medianSpeeds[vs.cameraView.siteIdx] = [] 134 medianSpeeds[vs.cameraView.siteIdx] = []
135 speeds85[vs.cameraView.siteIdx] = [] 135 speeds85[vs.cameraView.siteIdx] = []
136 print('Extracting speed from '+vs.getDatabaseFilename()) 136 print('Extracting speed from '+vs.getDatabaseFilename())
137 objects = storage.loadTrajectoriesFromSqlite(str(parentDir/vs.getDatabaseFilename()), 'object') 137 objects = storage.loadTrajectoriesFromSqlite(str(parentPath/vs.getDatabaseFilename()), 'object')
138 for o in objects: 138 for o in objects:
139 if o.length() > minLength: 139 if o.length() > minLength:
140 speeds = 30*3.6*percentile(o.getSpeeds(), [50, 85]) 140 speeds = 30*3.6*percentile(o.getSpeeds(), [50, 85])
141 medianSpeeds[vs.cameraView.siteIdx].append(speeds[0]) 141 medianSpeeds[vs.cameraView.siteIdx].append(speeds[0])
142 speeds85[vs.cameraView.siteIdx].append(speeds[1]) 142 speeds85[vs.cameraView.siteIdx].append(speeds[1])
158 if not vs.cameraView.siteIdx in interactions: 158 if not vs.cameraView.siteIdx in interactions:
159 interactions[vs.cameraView.siteIdx] = [] 159 interactions[vs.cameraView.siteIdx] = []
160 indicators[vs.cameraView.siteIdx] = {} 160 indicators[vs.cameraView.siteIdx] = {}
161 for i in indicatorIds: 161 for i in indicatorIds:
162 indicators[vs.cameraView.siteIdx][i] = [] 162 indicators[vs.cameraView.siteIdx][i] = []
163 interactions[vs.cameraView.siteIdx] += storage.loadInteractionsFromSqlite(str(parentDir/vs.getDatabaseFilename())) 163 interactions[vs.cameraView.siteIdx] += storage.loadInteractionsFromSqlite(str(parentPath/vs.getDatabaseFilename()))
164 print(vs.getDatabaseFilename(), len(interactions[vs.cameraView.siteIdx])) 164 print(vs.getDatabaseFilename(), len(interactions[vs.cameraView.siteIdx]))
165 for inter in interactions[vs.cameraView.siteIdx]: 165 for inter in interactions[vs.cameraView.siteIdx]:
166 for i in indicatorIds: 166 for i in indicatorIds:
167 indic = inter.getIndicator(events.Interaction.indicatorNames[i]) 167 indic = inter.getIndicator(events.Interaction.indicatorNames[i])
168 if indic is not None: 168 if indic is not None: