Mercurial Hosting > traffic-intelligence
annotate scripts/merge-features.py @ 911:3dd5acfa1899
corrected potential issues with videos where one cannot reach a give frame from its number
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 28 Jun 2017 16:46:45 -0400 |
parents | 75530d8c0090 |
children | 9897a13772fb |
rev | line source |
---|---|
828
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
2 |
830
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
3 import sys, argparse, os.path, sqlite3 |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
4 import cvutils, utils, moving, storage |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
5 from metadata import createDatabase, Site, VideoSequence, CameraView |
828
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
6 from datetime import datetime, timedelta |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
7 |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
8 parser = argparse.ArgumentParser(description='The program merges feature trajectories recorded from the same site synchronously between start and end time.') |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
9 parser.add_argument('-i', dest = 'metadataFilename', help = 'name of the metadata file', required = True) |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
10 parser.add_argument('-n', dest = 'siteId', help = 'site id or site name', required = True) |
842
75530d8c0090
corrected help printing issue
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
837
diff
changeset
|
11 parser.add_argument('-f', dest = 'startTime', help = 'time to start merging features (format %%Y-%%m-%%d %%H:%%M:%%S, eg 2011-06-22 10:00:39)') # if not provided, take common time interval |
75530d8c0090
corrected help printing issue
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
837
diff
changeset
|
12 parser.add_argument('-l', dest = 'endTime', help = 'time to stop merging features (format %%Y-%%m-%%d %%H:%%M:%%S, eg 2011-06-22 10:00:39)') |
830
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
13 parser.add_argument('-o', dest = 'outputDBFilename', help = 'name of the output SQLite file', required = True) |
828
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
14 |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
15 args = parser.parse_args() |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
16 |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
17 session = createDatabase(args.metadataFilename) |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
18 |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
19 site = Site.getSite(session, args.siteId) |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
20 if site is None: |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
21 print('Site {} was not found in {}. Exiting'.format(args.siteId, args.metadataFilename)) |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
22 sys.exit() |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
23 else: |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
24 site = site[0] |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
25 |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
26 startTime = datetime.strptime(args.startTime, utils.datetimeFormat) |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
27 endTime = datetime.strptime(args.endTime, utils.datetimeFormat) |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
28 processInterval = moving.TimeInterval(startTime, endTime) |
836
7058a40a4bbc
updated metadata and code to merge features from different cameras
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
830
diff
changeset
|
29 cameraViews = session.query(CameraView).filter(CameraView.site == site).filter(CameraView.virtual == False) |
7058a40a4bbc
updated metadata and code to merge features from different cameras
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
830
diff
changeset
|
30 videoSequences = session.query(VideoSequence).filter(VideoSequence.virtual == False).order_by(VideoSequence.startTime.asc()).all() #.order_by(VideoSequence.cameraViewIdx) .filter(VideoSequence.startTime <= startTime) |
830
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
31 videoSequences = [vs for vs in videoSequences if vs.cameraView in cameraViews] |
828
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
32 #timeIntervals = [v.intersection(startTime, endTime) for v in videoSequences] |
830
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
33 #cameraViews = set([v.cameraView for v in videoSequences]) |
828
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
34 |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
35 videoSequences = {cv: [v for v in videoSequences if v.cameraView == cv] for cv in cameraViews} |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
36 timeIntervals = {} |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
37 for cv in videoSequences: |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
38 timeIntervals[cv] = moving.TimeInterval.unionIntervals([v.getTimeInterval() for v in videoSequences[cv]]) |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
39 |
830
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
40 # intersection of the time interval (union) for each camera view |
828
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
41 commonTimeInterval = timeIntervals.values()[0] |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
42 for inter in timeIntervals.values()[1:]: |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
43 commonTimeInterval = moving.TimeInterval.intersection(commonTimeInterval, inter) |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
44 commonTimeInterval = moving.TimeInterval.intersection(commonTimeInterval, processInterval) |
14e4ad7c7420
work on merging data for synchronized views
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
45 |
830
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
46 if commonTimeInterval.empty(): |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
47 print('Empty time interval. Exiting') |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
48 sys.exit() |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
49 |
829 | 50 if len(set([cv.cameraType.frameRate for cv in cameraViews])) > 1: |
51 print('Different framerates of the cameras ({}) are not handled yet. Exiting'.format([cv.cameraType.frameRate for cv in cameraViews])) | |
830
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
52 else: |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
53 frameRate = cv.cameraType.frameRate |
829 | 54 |
830
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
55 try: |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
56 outConnection = sqlite3.connect(args.outputDBFilename) |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
57 outCursor = outConnection.cursor() |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
58 storage.createTrajectoryTable(outCursor, 'positions') |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
59 storage.createTrajectoryTable(outCursor, 'velocities') |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
60 storage.createFeatureCorrespondenceTable(outCursor) |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
61 outConnection.commit() |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
62 except sqlite3.OperationalError as error: |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
63 storage.printDBError(error) |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
64 sys.exit() |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
65 |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
66 dirname = os.path.split(args.metadataFilename)[0] |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
67 if len(dirname) == 0: |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
68 dirname = '.' |
829 | 69 |
830
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
70 newTrajectoryId = -1 |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
71 # first frame num is commonTimeInterval |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
72 for cv, vs in videoSequences.iteritems(): |
836
7058a40a4bbc
updated metadata and code to merge features from different cameras
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
830
diff
changeset
|
73 print cv.idx, cv.description |
830
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
74 for videoSequence in vs: |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
75 try: |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
76 vsConnection = sqlite3.connect(dirname+os.path.sep+videoSequence.getDatabaseFilename()) |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
77 vsCursor = vsConnection.cursor() |
837
e01cabca4c55
minor modifications to merge-features
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
836
diff
changeset
|
78 firstFrameNum = utils.deltaFrames(videoSequence.startTime, commonTimeInterval.first, frameRate) |
830
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
79 lastFrameNum = (commonTimeInterval.last-videoSequence.startTime).seconds*frameRate |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
80 # positions table |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
81 vsCursor.execute('SELECT * FROM positions WHERE frame_number BETWEEN {} AND {} ORDER BY trajectory_id'.format(firstFrameNum, lastFrameNum)) |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
82 featureIdCorrespondences = {} |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
83 currentTrajectoryId = -1 |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
84 for row in vsCursor: |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
85 if row[0] != currentTrajectoryId: |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
86 currentTrajectoryId = row[0] |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
87 newTrajectoryId += 1 |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
88 featureIdCorrespondences[currentTrajectoryId] = newTrajectoryId |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
89 outCursor.execute(storage.insertTrajectoryQuery('positions'), (newTrajectoryId, row[1]-firstFrameNum, row[2], row[3])) |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
90 # velocities table |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
91 for row in vsCursor: |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
92 outCursor.execute(storage.insertTrajectoryQuery('velocities'), (featureIdCorrespondences[row[0]], row[1]-firstFrameNum, row[2], row[3])) |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
93 # saving the id correspondences |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
94 for oldId, newId in featureIdCorrespondences.iteritems(): |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
95 outCursor.execute("INSERT INTO feature_correspondences (trajectory_id, source_dbname, db_trajectory_id) VALUES ({},\"{}\",{})".format(newId, videoSequence.name, oldId)) |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
96 outConnection.commit() |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
97 except sqlite3.OperationalError as error: |
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
98 storage.printDBError(error) |
836
7058a40a4bbc
updated metadata and code to merge features from different cameras
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
830
diff
changeset
|
99 |
7058a40a4bbc
updated metadata and code to merge features from different cameras
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
830
diff
changeset
|
100 # save the information of the new virtual sequence and camera view in the metadata |
7058a40a4bbc
updated metadata and code to merge features from different cameras
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
830
diff
changeset
|
101 mergedCameraView = CameraView('merged', None, site, cv.cameraType, None, None, virtual = True) |
7058a40a4bbc
updated metadata and code to merge features from different cameras
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
830
diff
changeset
|
102 session.add(mergedCameraView) |
7058a40a4bbc
updated metadata and code to merge features from different cameras
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
830
diff
changeset
|
103 session.add(VideoSequence('merged', commonTimeInterval.first, commonTimeInterval.last-commonTimeInterval.first, mergedCameraView, virtual = True)) |
830
2a5856961933
first working version of feature merging (works with feature grouping)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
829
diff
changeset
|
104 session.commit() |