comparison python/metadata.py @ 821:26daf35180ad

finished modification and demo script to replay synchronized video (with same frame rate)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 22 Jun 2016 16:36:12 -0400
parents fc8b3ce629d1
children 6e4357e9116d
comparison
equal deleted inserted replaced
820:e73e7b644428 821:26daf35180ad
1 # from moving import Point 1 # from moving import Point
2 2
3 from datetime import datetime 3 from datetime import datetime, timedelta
4 from os import path 4 from os import path
5 5 from math import floor
6 from sqlalchemy import orm, create_engine, Column, Integer, Float, DateTime, String, ForeignKey, Boolean 6
7 from sqlalchemy import orm, create_engine, Column, Integer, Float, DateTime, String, ForeignKey, Boolean, Interval
7 from sqlalchemy.orm import relationship, backref, sessionmaker 8 from sqlalchemy.orm import relationship, backref, sessionmaker
8 from sqlalchemy.ext.declarative import declarative_base 9 from sqlalchemy.ext.declarative import declarative_base
9 10
10 from utils import datetimeFormat, removeExtension 11 from utils import datetimeFormat, removeExtension
11 12
63 idx = Column(Integer, primary_key=True) 64 idx = Column(Integer, primary_key=True)
64 name = Column(String) 65 name = Column(String)
65 resX = Column(Integer) 66 resX = Column(Integer)
66 resY = Column(Integer) 67 resY = Column(Integer)
67 frameRate = Column(Float) 68 frameRate = Column(Float)
69 frameRateTimeUnit = Column(String, default = 's')
68 undistort = Column(Boolean) 70 undistort = Column(Boolean)
69 intrinsicCameraMatrixStr = Column(String) 71 intrinsicCameraMatrixStr = Column(String)
70 distortionCoefficientsStr = Column(String) 72 distortionCoefficientsStr = Column(String)
71 undistortedImageMultiplication = Column(Float) 73 undistortedImageMultiplication = Column(Float)
72 74
73 def __init__(self, name, resX, resY, frameRate, trackingConfigurationFilename = None, undistort = None, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = None): 75 def __init__(self, name, resX, resY, frameRate, frameRateTimeUnit = 's', trackingConfigurationFilename = None, undistort = None, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = None):
74 self.name = name 76 self.name = name
75 self.resX = resX 77 self.resX = resX
76 self.resY = resY 78 self.resY = resY
77 self.frameRate = frameRate 79 self.frameRate = frameRate
80 self.frameRateTimeUnit = frameRateTimeUnit
78 self.undistort = False 81 self.undistort = False
79 82
80 if trackingConfigurationFilename is not None: 83 if trackingConfigurationFilename is not None:
81 from storage import ProcessParameters 84 from storage import ProcessParameters
82 params = ProcessParameters(trackingConfigurationFilename) 85 params = ProcessParameters(trackingConfigurationFilename)
90 self.intrinsicCameraMatrix = intrinsicCameraMatrix 93 self.intrinsicCameraMatrix = intrinsicCameraMatrix
91 self.distortionCoefficients = distortionCoefficients 94 self.distortionCoefficients = distortionCoefficients
92 self.undistortedImageMultiplication = undistortedImageMultiplication 95 self.undistortedImageMultiplication = undistortedImageMultiplication
93 96
94 # populate the db 97 # populate the db
95 if self.intrinsicCameraMatrix is not None and self.distortionCoefficients is not None: 98 if hasattr(self, 'intrinsicCameraMatrix') and self.intrinsicCameraMatrix is not None\
99 and hasattr(self, 'distortionCoefficients') and self.distortionCoefficients is not None:
96 self.intrinsicCameraMatrixStr = ' '.join('{}'.format(x) for x in self.intrinsicCameraMatrix.flatten('C')) 100 self.intrinsicCameraMatrixStr = ' '.join('{}'.format(x) for x in self.intrinsicCameraMatrix.flatten('C'))
97 self.distortionCoefficientsStr = ' '.join('{}'.format(x)for x in self.distortionCoefficients) 101 self.distortionCoefficientsStr = ' '.join('{}'.format(x)for x in self.distortionCoefficients)
98 102
99 @orm.reconstructor 103 @orm.reconstructor
100 def initOnLoad(self): 104 def initOnLoad(self):
105 self.distortionCoefficients = [float(x) for x in self.distortionCoefficientsStr.split(" ")] 109 self.distortionCoefficients = [float(x) for x in self.distortionCoefficientsStr.split(" ")]
106 110
107 class CameraView(Base): 111 class CameraView(Base):
108 __tablename__ = 'camera_views' 112 __tablename__ = 'camera_views'
109 idx = Column(Integer, primary_key=True) 113 idx = Column(Integer, primary_key=True)
114 description = Column(String)
110 homographyFilename = Column(String) # path to homograph filename, relative to the site name 115 homographyFilename = Column(String) # path to homograph filename, relative to the site name
111 siteIdx = Column(Integer, ForeignKey('sites.idx')) 116 siteIdx = Column(Integer, ForeignKey('sites.idx'))
112 cameraTypeIdx = Column(Integer, ForeignKey('camera_types.idx')) 117 cameraTypeIdx = Column(Integer, ForeignKey('camera_types.idx'))
113 homographyDistanceUnit = Column(String, default = 'm') # make sure it is default in the database 118 homographyDistanceUnit = Column(String, default = 'm') # make sure it is default in the database
114 trackingConfigurationFilename = Column(String) # path to configuration .cfg file, relative to site name 119 trackingConfigurationFilename = Column(String) # path to configuration .cfg file, relative to site name
115 120
116 site = relationship("Site", backref=backref('sites', order_by = idx)) 121 site = relationship("Site", backref=backref('sites', order_by = idx))
117 camera = relationship('CameraType', backref=backref('camera_views', order_by = idx)) 122 cameraType = relationship('CameraType', backref=backref('camera_views', order_by = idx))
118 123
119 def __init__(self, homographyFilename, site, cameraType, trackingConfigurationFilename): 124 def __init__(self, description, homographyFilename, site, cameraType, trackingConfigurationFilename):
125 self.description = description
120 self.homographyFilename = homographyFilename 126 self.homographyFilename = homographyFilename
121 self.site = site 127 self.site = site
122 self.cameraType = cameraType 128 self.cameraType = cameraType
123 self.trackingConfigurationFilename = trackingConfigurationFilename 129 self.trackingConfigurationFilename = trackingConfigurationFilename
124 130
165 class VideoSequence(Base): 171 class VideoSequence(Base):
166 __tablename__ = 'video_sequences' 172 __tablename__ = 'video_sequences'
167 idx = Column(Integer, primary_key=True) 173 idx = Column(Integer, primary_key=True)
168 name = Column(String) # path relative to the the site name 174 name = Column(String) # path relative to the the site name
169 startTime = Column(DateTime) 175 startTime = Column(DateTime)
170 duration = Column(Float) # video sequence duration 176 duration = Column(Interval) # video sequence duration
171 durationUnit = Column(String, default = 's')
172 databaseFilename = Column(String) # path relative to the the site name 177 databaseFilename = Column(String) # path relative to the the site name
173 siteIdx = Column(Integer, ForeignKey('sites.idx')) 178 siteIdx = Column(Integer, ForeignKey('sites.idx'))
174 cameraViewIdx = Column(Integer, ForeignKey('camera_views.idx')) 179 cameraViewIdx = Column(Integer, ForeignKey('camera_views.idx'))
175 180
176 site = relationship("Site", backref=backref('video_sequences', order_by = idx)) 181 site = relationship("Site", backref=backref('video_sequences', order_by = idx))
177 cameraView = relationship("CameraView", backref=backref('video_sequences', order_by = idx)) 182 cameraView = relationship("CameraView", backref=backref('video_sequences', order_by = idx))
178 183
179 def __init__(self, name, startTime, duration, site, cameraView, databaseFilename = None): 184 def __init__(self, name, startTime, duration, site, cameraView, databaseFilename = None):
180 'startTime is passed as string in utils.datetimeFormat, eg 2011-06-22 10:00:39' 185 '''startTime is passed as string in utils.datetimeFormat, eg 2011-06-22 10:00:39
186 duration is a timedelta object'''
181 self.name = name 187 self.name = name
182 self.startTime = datetime.strptime(startTime, datetimeFormat) 188 self.startTime = datetime.strptime(startTime, datetimeFormat)
183 self.duration = duration 189 self.duration = duration
184 self.site = site 190 self.site = site
185 self.cameraView = cameraView 191 self.cameraView = cameraView
190 if relativeToSiteFilename: 196 if relativeToSiteFilename:
191 return self.site.getFilename()+path.sep+self.name 197 return self.site.getFilename()+path.sep+self.name
192 else: 198 else:
193 return self.name 199 return self.name
194 200
195 201 def containsInstant(self, instant):
202 'instant is a datetime'
203 return self.startTime <= instant and self.startTime+self.duration
204
205 def getFrameNum(self, instant):
206 'Warning, there is no check of correct time units'
207 if self.containsInstant(instant):
208 return int(floor((instant-self.startTime).seconds*self.cameraView.cameraType.frameRate))
209 else:
210 return None
196 211
197 # add class for Analysis: foreign key VideoSequenceId, dataFilename, configFilename (get the one from camera view by default), mask? (no, can be referenced in the tracking cfg file) 212 # add class for Analysis: foreign key VideoSequenceId, dataFilename, configFilename (get the one from camera view by default), mask? (no, can be referenced in the tracking cfg file)
198 213
199 # class SiteDescription(Base): # list of lines and polygons describing the site, eg for sidewalks, center lines 214 # class SiteDescription(Base): # list of lines and polygons describing the site, eg for sidewalks, center lines
200 215