Mercurial > hg > nsaunier > traffic-intelligence
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 1003:75af46516b2b | 1004:75601be6019f |
|---|---|
| 1 #! /usr/bin/env python3 | 1 #! /usr/bin/env python3 |
| 2 | 2 |
| 3 import sys, argparse | 3 import sys, argparse |
| 4 from pathlib import Path | 4 from pathlib import Path |
| 5 from multiprocessing.pool import Pool | |
| 5 | 6 |
| 6 import matplotlib | 7 import matplotlib |
| 7 matplotlib.use('Agg') | 8 matplotlib.use('Agg') |
| 8 import matplotlib.pyplot as plt | 9 import matplotlib.pyplot as plt |
| 9 from numpy import percentile | 10 from numpy import percentile |
| 10 | 11 |
| 11 import storage, events, prediction | 12 import storage, events, prediction, cvutils |
| 12 from metadata import * | 13 from metadata import * |
| 13 | 14 |
| 14 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.') | 15 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.') |
| 15 parser.add_argument('--db', dest = 'metadataFilename', help = 'name of the metadata file', required = True) | 16 parser.add_argument('--db', dest = 'metadataFilename', help = 'name of the metadata file', required = True) |
| 16 parser.add_argument('--videos', dest = 'videoIds', help = 'indices of the video sequences', nargs = '*', type = int) | 17 parser.add_argument('--videos', dest = 'videoIds', help = 'indices of the video sequences', nargs = '*', type = int) |
| 18 parser.add_argument('--sites', dest = 'siteIds', help = 'indices of the video sequences', nargs = '*', type = int) | |
| 17 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file') | 19 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file') |
| 18 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects/interactions to process', type = int) | 20 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects/interactions to process', type = int) |
| 19 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']) | 21 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']) |
| 20 parser.add_argument('--pet', dest = 'computePET', help = 'computes PET', action = 'store_true') | 22 parser.add_argument('--pet', dest = 'computePET', help = 'computes PET', action = 'store_true') |
| 21 # override other tracking config, erase sqlite? | 23 # override other tracking config, erase sqlite? |
| 44 #parser.add_argument('-t', dest = 'dataType', help = 'type of the data to remove', required = True, choices = ['object','interaction', 'bb', 'pois', 'prototype']) | 46 #parser.add_argument('-t', dest = 'dataType', help = 'type of the data to remove', required = True, choices = ['object','interaction', 'bb', 'pois', 'prototype']) |
| 45 for videoId in args.videoIds: | 47 for videoId in args.videoIds: |
| 46 vs = session.query(VideoSequence).get(videoId) | 48 vs = session.query(VideoSequence).get(videoId) |
| 47 storage.deleteFromSqlite(str(parentDir/vs.getDatabaseFilename()), args.delete) | 49 storage.deleteFromSqlite(str(parentDir/vs.getDatabaseFilename()), args.delete) |
| 48 | 50 |
| 51 import time | |
| 52 def track(i): | |
| 53 time.sleep(1) | |
| 54 print('process {}'.format(i)) | |
| 55 | |
| 56 | |
| 49 if args.process in ['feature', 'object']: # tracking | 57 if args.process in ['feature', 'object']: # tracking |
| 50 for videoId in args.videoIds: | 58 if args.videoIds is not None: |
| 51 vs = session.query(VideoSequence).get(videoId) | 59 videoSequences = [session.query(VideoSequence).get(videoId) for videoId in args.videoIds] |
| 52 if args.configFilename is None: | 60 elif args.siteIds is not None: |
| 53 configFilename = vs.cameraView.getTrackingConfigurationFilename() | 61 videoSequences = [] |
| 54 else: | 62 for siteId in args.siteIds: |
| 55 configFilename = args.configFilename | 63 for site in getSite(session, siteId): |
| 56 #todo cvutils.tracking(configFilename, args.process == 'object', str(parentDir/vs.getVideoSequenceFilename(), str(parentDir/vs.getDatabaseFilename(), configFilename = vs.cameraView.getHomographyFilename()) | 64 for cv in site.cameraViews: |
| 57 | 65 videoSequences += cv.videoSequences |
| 66 else: | |
| 67 print('No video/site to process') | |
| 68 videoSequences = [] | |
| 69 if args.nProcesses == 1: | |
| 70 pass | |
| 71 else: | |
| 72 pool = Pool(args.nProcesses) | |
| 73 for vs in videoSequences: | |
| 74 if not (parentDir/vs.getDatabaseFilename()).exists(): | |
| 75 if args.configFilename is None: | |
| 76 configFilename = vs.cameraView.getTrackingConfigurationFilename() | |
| 77 else: | |
| 78 configFilename = args.configFilename | |
| 79 if vs.cameraView.cameraType is None: | |
| 80 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)) | |
| 81 else: | |
| 82 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)) | |
| 83 else: | |
| 84 print('SQLite already exists: {}'.format(parentDir/vs.getDatabaseFilename())) | |
| 85 pool.close() | |
| 86 pool.join() | |
| 87 | |
| 58 elif args.process == 'interaction': | 88 elif args.process == 'interaction': |
| 59 # safety analysis TODO make function in safety analysis script | 89 # safety analysis TODO make function in safety analysis script |
| 60 if args.predictionMethod == 'cvd': | 90 if args.predictionMethod == 'cvd': |
| 61 predictionParameters = prediction.CVDirectPredictionParameters() | 91 predictionParameters = prediction.CVDirectPredictionParameters() |
| 62 if args.predictionMethod == 'cve': | 92 if args.predictionMethod == 'cve': |
