Mercurial Hosting > traffic-intelligence
comparison scripts/display-synced-trajectories.py @ 837:e01cabca4c55
minor modifications to merge-features
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 06 Jul 2016 10:58:18 -0400 |
parents | 7058a40a4bbc |
children | 2918de3d40fc |
comparison
equal
deleted
inserted
replaced
836:7058a40a4bbc | 837:e01cabca4c55 |
---|---|
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 | 2 |
3 import sys, argparse, os.path | 3 import sys, argparse, os.path |
4 from datetime import datetime, timedelta | 4 from datetime import datetime, timedelta |
5 from numpy import array | 5 import numpy as np |
6 import cv2 | 6 import cv2 |
7 import cvutils, utils, storage | 7 import cvutils, utils, storage |
8 from metadata import createDatabase, Site, VideoSequence | 8 from metadata import createDatabase, Site, CameraView, VideoSequence |
9 | 9 |
10 parser = argparse.ArgumentParser(description='The program displays several views of the same site synchronously.') | 10 parser = argparse.ArgumentParser(description='The program displays several views of the same site synchronously.') |
11 parser.add_argument('-i', dest = 'metadataFilename', help = 'name of the metadata file', required = True) | 11 parser.add_argument('-i', dest = 'metadataFilename', help = 'name of the metadata file', required = True) |
12 #parser.add_argument('-n', dest = 'siteId', help = 'site id or site name', required = True) | 12 #parser.add_argument('-n', dest = 'siteId', help = 'site id or site name', required = True) |
13 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) | 13 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) |
29 | 29 |
30 startTime = datetime.strptime(args.startTime, utils.datetimeFormat) | 30 startTime = datetime.strptime(args.startTime, utils.datetimeFormat) |
31 if startTime > mergedSequence.startTime: | 31 if startTime > mergedSequence.startTime: |
32 mergedFirstFrameNum = (startTime-mergedSequence.startTime).seconds*mergedSequence.cameraView.cameraType.frameRate | 32 mergedFirstFrameNum = (startTime-mergedSequence.startTime).seconds*mergedSequence.cameraView.cameraType.frameRate |
33 | 33 |
34 cameraViews = session.query(CameraView).filter(CameraView.site == mergedSequence.site).filter(CameraView.virtual == False) | 34 cameraViews = session.query(CameraView).filter(CameraView.site == mergedSequence.cameraView.site).filter(CameraView.virtual == False).all() |
35 videoSequences = session.query(VideoSequence).filter(VideoSequence.virtual == False).filter(VideoSequence.startTime <= startTime).all() | 35 videoSequences = session.query(VideoSequence).filter(VideoSequence.virtual == False).filter(VideoSequence.startTime <= startTime).all() |
36 #videoSequences.remove(mergedSequence) | 36 #videoSequences.remove(mergedSequence) |
37 videoSequences = [v for v in videoSequences if v.cameraView in cameraViews and v.containsInstant(startTime)] | 37 videoSequences = [v for v in videoSequences if v.cameraView in cameraViews and v.containsInstant(startTime)] |
38 filenames = [dirname+os.path.sep+v.getVideoSequenceFilename() for v in videoSequences] | 38 filenames = [dirname+os.path.sep+v.getVideoSequenceFilename() for v in videoSequences] |
39 firstFrameNums = [v.getFrameNum(startTime) for v in videoSequences] | 39 firstFrameNums = [v.getFrameNum(startTime) for v in videoSequences] |
40 windowNames = [v.cameraView.description for v in videoSequences] | 40 windowNames = [v.cameraView.description for v in videoSequences] |
41 homographies = [np.inv(np.loadtxt(dirname+os.path.sep+v.cameraView.getHomographyFilename())) for v in videoSequences] | |
41 | 42 |
42 objects = storage.loadTrajectoriesFromSqlite(dirname+os.path.sep+mergedSequence.getDatabaseFilename(), args.trajectoryType) | 43 objects = storage.loadTrajectoriesFromSqlite(dirname+os.path.sep+mergedSequence.getDatabaseFilename(), args.trajectoryType) |
43 | 44 |
44 #def playVideo(filenames, windowNames = None, firstFrameNums = None, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1): | 45 #def playVideo(filenames, windowNames = None, firstFrameNums = None, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1): |
45 if len(filenames) == 0: | 46 if len(filenames) == 0: |
57 #if interactive: | 58 #if interactive: |
58 wait = 0 | 59 wait = 0 |
59 step = 1 | 60 step = 1 |
60 rescale = 1. | 61 rescale = 1. |
61 captures = [cv2.VideoCapture(fn) for fn in filenames] | 62 captures = [cv2.VideoCapture(fn) for fn in filenames] |
62 if array([cap.isOpened() for cap in captures]).all(): | 63 if np.array([cap.isOpened() for cap in captures]).all(): |
63 key = -1 | 64 key = -1 |
64 ret = True | 65 ret = True |
65 nFramesShown = 0 | 66 nFramesShown = 0 |
66 if firstFrameNums is not None: | 67 if firstFrameNums is not None: |
67 for i in xrange(len(captures)): | 68 for i in xrange(len(captures)): |
71 images = [] | 72 images = [] |
72 for cap in captures: | 73 for cap in captures: |
73 ret, img = cap.read() | 74 ret, img = cap.read() |
74 rets.append(ret) | 75 rets.append(ret) |
75 images.append(img) | 76 images.append(img) |
76 if array(rets).all(): | 77 if np.array(rets).all(): |
77 #if printFrames: | 78 #if printFrames: |
78 print('frame shown {0}'.format(nFramesShown)) | 79 print('frame shown {0}'.format(nFramesShown)) |
79 for i in xrange(len(filenames)): | 80 for i in xrange(len(filenames)): |
80 for obj in objects: | 81 for obj in objects: |
82 | |
81 if obj.existsAtInstant(): | 83 if obj.existsAtInstant(): |
82 #if text is not None: | 84 |
83 # cv2.putText(images[i], text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) | 85 #if text is not None: |
86 # cv2.putText(images[i], text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) | |
84 cvutils.cvImshow(windowNames[i], images[i], rescale) # cv2.imshow('frame', img) | 87 cvutils.cvImshow(windowNames[i], images[i], rescale) # cv2.imshow('frame', img) |
85 key = cv2.waitKey(wait) | 88 key = cv2.waitKey(wait) |
86 #if cvutils.saveKey(key): | 89 #if cvutils.saveKey(key): |
87 # cv2.imwrite('image-{}.png'.format(frameNum), img) | 90 # cv2.imwrite('image-{}.png'.format(frameNum), img) |
88 nFramesShown += step | 91 nFramesShown += step |