Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/process.py @ 1009:0d29b75f74ea
cleaning
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 04 Jun 2018 10:25:30 -0400 |
| parents | 192de96e5255 |
| children | 16932cefabc1 |
comparison
equal
deleted
inserted
replaced
| 1008:a5f2309bb1ff | 1009:0d29b75f74ea |
|---|---|
| 30 | 30 |
| 31 # need way of selecting sites as similar as possible to sql alchemy syntax | 31 # need way of selecting sites as similar as possible to sql alchemy syntax |
| 32 # override tracking.cfg from db | 32 # override tracking.cfg from db |
| 33 # manage cfg files, overwrite them (or a subset of parameters) | 33 # manage cfg files, overwrite them (or a subset of parameters) |
| 34 # delete sqlite files | 34 # delete sqlite files |
| 35 | |
| 36 # info of metadata | 35 # info of metadata |
| 37 | 36 |
| 37 args = parser.parse_args() | |
| 38 | 38 |
| 39 args = parser.parse_args() | 39 ################################# |
| 40 # files are relative to metadata location | 40 # Data preparation |
| 41 ################################# | |
| 42 session = connectDatabase(args.metadataFilename) | |
| 43 parentDir = Path(args.metadataFilename).parent # files are relative to metadata location | |
| 44 videoSequences = [] | |
| 45 if args.videoIds is not None: | |
| 46 videoSequences = [session.query(VideoSequence).get(videoId) for videoId in args.videoIds] | |
| 47 elif args.siteIds is not None: | |
| 48 for siteId in args.siteIds: | |
| 49 for site in getSite(session, siteId): | |
| 50 for cv in site.cameraViews: | |
| 51 videoSequences += cv.videoSequences | |
| 52 else: | |
| 53 print('No video/site to process') | |
| 41 | 54 |
| 42 session = connectDatabase(args.metadataFilename) | 55 ################################# |
| 43 parentDir = Path(args.metadataFilename).parent | 56 # Delete |
| 44 | 57 ################################# |
| 45 if args.delete is not None: | 58 if args.delete is not None: |
| 46 if args.delete in ['object', 'interaction']: | 59 if args.delete == 'feature': |
| 60 pass | |
| 61 elif args.delete in ['object', 'interaction']: | |
| 47 #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']) |
| 48 for videoId in args.videoIds: | 63 for vs in videoSequences: |
| 49 vs = session.query(VideoSequence).get(videoId) | |
| 50 storage.deleteFromSqlite(str(parentDir/vs.getDatabaseFilename()), args.delete) | 64 storage.deleteFromSqlite(str(parentDir/vs.getDatabaseFilename()), args.delete) |
| 51 | 65 |
| 52 import time | 66 ################################# |
| 53 def track(i): | 67 # Process |
| 54 time.sleep(1) | 68 ################################# |
| 55 print('process {}'.format(i)) | |
| 56 | |
| 57 | |
| 58 if args.process in ['feature', 'object']: # tracking | 69 if args.process in ['feature', 'object']: # tracking |
| 59 if args.videoIds is not None: | |
| 60 videoSequences = [session.query(VideoSequence).get(videoId) for videoId in args.videoIds] | |
| 61 elif args.siteIds is not None: | |
| 62 videoSequences = [] | |
| 63 for siteId in args.siteIds: | |
| 64 for site in getSite(session, siteId): | |
| 65 for cv in site.cameraViews: | |
| 66 videoSequences += cv.videoSequences | |
| 67 else: | |
| 68 print('No video/site to process') | |
| 69 videoSequences = [] | |
| 70 if args.nProcesses == 1: | 70 if args.nProcesses == 1: |
| 71 pass | 71 for vs in videoSequences: |
| 72 if not (parentDir/vs.getDatabaseFilename()).exists() or args.process == 'object': | |
| 73 if args.configFilename is None: | |
| 74 configFilename = str(parentDir/vs.cameraView.getTrackingConfigurationFilename()) | |
| 75 else: | |
| 76 configFilename = args.configFilename | |
| 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) | |
| 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) | |
| 81 else: | |
| 82 print('SQLite already exists: {}'.format(parentDir/vs.getDatabaseFilename())) | |
| 72 else: | 83 else: |
| 73 pool = Pool(args.nProcesses) | 84 pool = Pool(args.nProcesses) |
| 74 for vs in videoSequences: | 85 for vs in videoSequences: |
| 75 if not (parentDir/vs.getDatabaseFilename()).exists() or args.process == 'object': | 86 if not (parentDir/vs.getDatabaseFilename()).exists() or args.process == 'object': |
| 76 if args.configFilename is None: | 87 if args.configFilename is None: |
| 90 # safety analysis TODO make function in safety analysis script | 101 # safety analysis TODO make function in safety analysis script |
| 91 if args.predictionMethod == 'cvd': | 102 if args.predictionMethod == 'cvd': |
| 92 predictionParameters = prediction.CVDirectPredictionParameters() | 103 predictionParameters = prediction.CVDirectPredictionParameters() |
| 93 if args.predictionMethod == 'cve': | 104 if args.predictionMethod == 'cve': |
| 94 predictionParameters = prediction.CVExactPredictionParameters() | 105 predictionParameters = prediction.CVExactPredictionParameters() |
| 95 for videoId in args.videoIds: | 106 for vs in videoSequences: |
| 96 vs = session.query(VideoSequence).get(videoId) | |
| 97 print('Processing '+vs.getDatabaseFilename()) | 107 print('Processing '+vs.getDatabaseFilename()) |
| 98 objects = storage.loadTrajectoriesFromSqlite(str(parentDir/vs.getDatabaseFilename()), 'object')#, args.nObjects, withFeatures = (params.useFeaturesForPrediction or predictionMethod == 'ps' or predictionMethod == 'mp')) | 108 objects = storage.loadTrajectoriesFromSqlite(str(parentDir/vs.getDatabaseFilename()), 'object')#, args.nObjects, withFeatures = (params.useFeaturesForPrediction or predictionMethod == 'ps' or predictionMethod == 'mp')) |
| 99 interactions = events.createInteractions(objects) | 109 interactions = events.createInteractions(objects) |
| 100 #if args.nProcesses == 1: | 110 #if args.nProcesses == 1: |
| 101 #print(str(parentDir/vs.cameraView.getTrackingConfigurationFilename())) | 111 #print(str(parentDir/vs.cameraView.getTrackingConfigurationFilename())) |
| 110 # processed = [] | 120 # processed = [] |
| 111 # for job in jobs: | 121 # for job in jobs: |
| 112 # processed += job.get() | 122 # processed += job.get() |
| 113 # pool.close() | 123 # pool.close() |
| 114 | 124 |
| 125 ################################# | |
| 126 # Analyze | |
| 127 ################################# | |
| 115 if args.analyze == 'object': # user speed for now | 128 if args.analyze == 'object': # user speed for now |
| 116 medianSpeeds = {} | 129 medianSpeeds = {} |
| 117 speeds85 = {} | 130 speeds85 = {} |
| 118 minLength = 2*30 | 131 minLength = 2*30 |
| 119 for videoId in args.videoIds: | 132 for vs in videoSequences: |
| 120 vs = session.query(VideoSequence).get(videoId) | |
| 121 if not vs.cameraView.siteIdx in medianSpeeds: | 133 if not vs.cameraView.siteIdx in medianSpeeds: |
| 122 medianSpeeds[vs.cameraView.siteIdx] = [] | 134 medianSpeeds[vs.cameraView.siteIdx] = [] |
| 123 speeds85[vs.cameraView.siteIdx] = [] | 135 speeds85[vs.cameraView.siteIdx] = [] |
| 124 print('Extracting speed from '+vs.getDatabaseFilename()) | 136 print('Extracting speed from '+vs.getDatabaseFilename()) |
| 125 objects = storage.loadTrajectoriesFromSqlite(str(parentDir/vs.getDatabaseFilename()), 'object') | 137 objects = storage.loadTrajectoriesFromSqlite(str(parentDir/vs.getDatabaseFilename()), 'object') |
| 140 indicatorIds = [2,5,7,10] | 152 indicatorIds = [2,5,7,10] |
| 141 conversionFactors = {2: 1., 5: 30.*3.6, 7:1./30, 10:1./30} | 153 conversionFactors = {2: 1., 5: 30.*3.6, 7:1./30, 10:1./30} |
| 142 maxIndicatorValue = {2: float('inf'), 5: float('inf'), 7:10., 10:10.} | 154 maxIndicatorValue = {2: float('inf'), 5: float('inf'), 7:10., 10:10.} |
| 143 indicators = {} | 155 indicators = {} |
| 144 interactions = {} | 156 interactions = {} |
| 145 for videoId in args.videoIds: | 157 for vs in videoSequences: |
| 146 vs = session.query(VideoSequence).get(videoId) | |
| 147 if not vs.cameraView.siteIdx in interactions: | 158 if not vs.cameraView.siteIdx in interactions: |
| 148 interactions[vs.cameraView.siteIdx] = [] | 159 interactions[vs.cameraView.siteIdx] = [] |
| 149 indicators[vs.cameraView.siteIdx] = {} | 160 indicators[vs.cameraView.siteIdx] = {} |
| 150 for i in indicatorIds: | 161 for i in indicatorIds: |
| 151 indicators[vs.cameraView.siteIdx][i] = [] | 162 indicators[vs.cameraView.siteIdx][i] = [] |
