Mercurial > hg > nsaunier > traffic-intelligence
comparison python/metadata.py @ 828:14e4ad7c7420
work on merging data for synchronized views
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 28 Jun 2016 17:18:45 -0400 |
| parents | 6e4357e9116d |
| children | 2a5856961933 |
comparison
equal
deleted
inserted
replaced
| 827:f6d5da619307 | 828:14e4ad7c7420 |
|---|---|
| 2 | 2 |
| 3 from datetime import datetime, timedelta | 3 from datetime import datetime, timedelta |
| 4 from os import path | 4 from os import path |
| 5 from math import floor | 5 from math import floor |
| 6 | 6 |
| 7 from numpy import zeros | |
| 8 | |
| 7 from sqlalchemy import orm, create_engine, Column, Integer, Float, DateTime, String, ForeignKey, Boolean, Interval | 9 from sqlalchemy import orm, create_engine, Column, Integer, Float, DateTime, String, ForeignKey, Boolean, Interval |
| 8 from sqlalchemy.orm import relationship, backref, sessionmaker | 10 from sqlalchemy.orm import relationship, backref, sessionmaker |
| 9 from sqlalchemy.ext.declarative import declarative_base | 11 from sqlalchemy.ext.declarative import declarative_base |
| 10 | 12 |
| 11 from utils import datetimeFormat, removeExtension | 13 from utils import datetimeFormat, removeExtension |
| 14 from moving import TimeInterval | |
| 12 | 15 |
| 13 Base = declarative_base() | 16 Base = declarative_base() |
| 14 | 17 |
| 15 class Site(Base): | 18 class Site(Base): |
| 16 __tablename__ = 'sites' | 19 __tablename__ = 'sites' |
| 33 self.worldDistanceUnit = worldDistanceUnit | 36 self.worldDistanceUnit = worldDistanceUnit |
| 34 | 37 |
| 35 def getFilename(self): | 38 def getFilename(self): |
| 36 return self.name | 39 return self.name |
| 37 | 40 |
| 41 @staticmethod | |
| 42 def getSite(session, siteId): | |
| 43 'Returns the site(s) matching the index or the name' | |
| 44 if str.isdigit(siteId): | |
| 45 return session.query(Site).filter(Site.idx == int(siteId)).all() | |
| 46 else: | |
| 47 return session.query(Site).filter(Site.description.like('%'+siteId+'%')).all() | |
| 48 | |
| 49 | |
| 38 class EnvironementalFactors(Base): | 50 class EnvironementalFactors(Base): |
| 39 '''Represents any environmental factors that may affect the results, in particular | 51 '''Represents any environmental factors that may affect the results, in particular |
| 40 * changing weather conditions | 52 * changing weather conditions |
| 41 * changing road configuration, geometry, signalization, etc. | 53 * changing road configuration, geometry, signalization, etc. |
| 42 ex: sunny, rainy, before counter-measure, after counter-measure''' | 54 ex: sunny, rainy, before counter-measure, after counter-measure''' |
| 118 self.distortionCoefficients3 = self.distortionCoefficients[3] | 130 self.distortionCoefficients3 = self.distortionCoefficients[3] |
| 119 self.distortionCoefficients4 = self.distortionCoefficients[4] | 131 self.distortionCoefficients4 = self.distortionCoefficients[4] |
| 120 | 132 |
| 121 @orm.reconstructor | 133 @orm.reconstructor |
| 122 def initOnLoad(self): | 134 def initOnLoad(self): |
| 123 from numpy import zeros | |
| 124 if self.intrinsicCameraMatrix00 is not None: | 135 if self.intrinsicCameraMatrix00 is not None: |
| 125 self.intrinsicCameraMatrix = zeros((3,3)) | 136 self.intrinsicCameraMatrix = zeros((3,3)) |
| 126 self.intrinsicCameraMatrix[0,0] = self.intrinsicCameraMatrix00 | 137 self.intrinsicCameraMatrix[0,0] = self.intrinsicCameraMatrix00 |
| 127 self.intrinsicCameraMatrix[0,1] = self.intrinsicCameraMatrix01 | 138 self.intrinsicCameraMatrix[0,1] = self.intrinsicCameraMatrix01 |
| 128 self.intrinsicCameraMatrix[0,2] = self.intrinsicCameraMatrix02 | 139 self.intrinsicCameraMatrix[0,2] = self.intrinsicCameraMatrix02 |
| 229 | 240 |
| 230 def __init__(self, name, startTime, duration, site, cameraView, databaseFilename = None): | 241 def __init__(self, name, startTime, duration, site, cameraView, databaseFilename = None): |
| 231 '''startTime is passed as string in utils.datetimeFormat, eg 2011-06-22 10:00:39 | 242 '''startTime is passed as string in utils.datetimeFormat, eg 2011-06-22 10:00:39 |
| 232 duration is a timedelta object''' | 243 duration is a timedelta object''' |
| 233 self.name = name | 244 self.name = name |
| 234 self.startTime = datetime.strptime(startTime, datetimeFormat) | 245 if isinstance(startTime, str): |
| 246 self.startTime = datetime.strptime(startTime, datetimeFormat) | |
| 247 else: | |
| 248 self.startTime = startTime | |
| 235 self.duration = duration | 249 self.duration = duration |
| 236 self.site = site | 250 self.site = site |
| 237 self.cameraView = cameraView | 251 self.cameraView = cameraView |
| 238 if databaseFilename is None and len(self.name) > 0: | 252 if databaseFilename is None and len(self.name) > 0: |
| 239 self.databaseFilename = removeExtension(self.name)+'.sqlite' | 253 self.databaseFilename = removeExtension(self.name)+'.sqlite' |
| 242 if relativeToSiteFilename: | 256 if relativeToSiteFilename: |
| 243 return self.site.getFilename()+path.sep+self.name | 257 return self.site.getFilename()+path.sep+self.name |
| 244 else: | 258 else: |
| 245 return self.name | 259 return self.name |
| 246 | 260 |
| 261 def getTimeInterval(self): | |
| 262 return TimeInterval(self.startTime, self.startTime+self.duration) | |
| 263 | |
| 247 def containsInstant(self, instant): | 264 def containsInstant(self, instant): |
| 248 'instant is a datetime' | 265 'instant is a datetime' |
| 249 return self.startTime <= instant and self.startTime+self.duration | 266 return self.startTime <= instant and self.startTime+self.duration |
| 267 | |
| 268 def intersection(self, startTime, endTime): | |
| 269 'returns the moving.TimeInterval intersection with [startTime, endTime]' | |
| 270 return TimeInterval.intersection(self.getTimeInterval(), TimeInterval(startTime, endTime)) | |
| 250 | 271 |
| 251 def getFrameNum(self, instant): | 272 def getFrameNum(self, instant): |
| 252 'Warning, there is no check of correct time units' | 273 'Warning, there is no check of correct time units' |
| 253 if self.containsInstant(instant): | 274 if self.containsInstant(instant): |
| 254 return int(floor((instant-self.startTime).seconds*self.cameraView.cameraType.frameRate)) | 275 return int(floor((instant-self.startTime).seconds*self.cameraView.cameraType.frameRate)) |
