Mercurial > hg > nsaunier > traffic-intelligence
comparison python/metadata.py @ 819:fc8b3ce629d1
important modifications to metadata
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 22 Jun 2016 14:51:00 -0400 |
| parents | 180b6b0231c0 |
| children | 26daf35180ad |
comparison
equal
deleted
inserted
replaced
| 818:181bcb6dad3a | 819:fc8b3ce629d1 |
|---|---|
| 1 # from moving import Point | 1 # from moving import Point |
| 2 | 2 |
| 3 from datetime import datetime | 3 from datetime import datetime |
| 4 from os import path | 4 from os import path |
| 5 | 5 |
| 6 from sqlalchemy import create_engine, Column, Integer, Float, DateTime, String, ForeignKey | 6 from sqlalchemy import orm, create_engine, Column, Integer, Float, DateTime, String, ForeignKey, Boolean |
| 7 from sqlalchemy.orm import relationship, backref, sessionmaker | 7 from sqlalchemy.orm import relationship, backref, sessionmaker |
| 8 from sqlalchemy.ext.declarative import declarative_base | 8 from sqlalchemy.ext.declarative import declarative_base |
| 9 | 9 |
| 10 from utils import datetimeFormat | 10 from utils import datetimeFormat, removeExtension |
| 11 | 11 |
| 12 Base = declarative_base() | 12 Base = declarative_base() |
| 13 | 13 |
| 14 class Site(Base): | 14 class Site(Base): |
| 15 __tablename__ = 'sites' | 15 __tablename__ = 'sites' |
| 18 description = Column(String) # longer names, eg intersection of road1 and road2 | 18 description = Column(String) # longer names, eg intersection of road1 and road2 |
| 19 xcoordinate = Column(Float) # ideally moving.Point, but needs to be | 19 xcoordinate = Column(Float) # ideally moving.Point, but needs to be |
| 20 ycoordinate = Column(Float) | 20 ycoordinate = Column(Float) |
| 21 mapImageFilename = Column(String) # path to filename, relative to site name, ie sitename/mapImageFilename | 21 mapImageFilename = Column(String) # path to filename, relative to site name, ie sitename/mapImageFilename |
| 22 nUnitsPerPixel = Column(Float) # number of units of distance per pixel in map image | 22 nUnitsPerPixel = Column(Float) # number of units of distance per pixel in map image |
| 23 | 23 worldDistanceUnit = Column(String, default = 'm') # make sure it is default in the database |
| 24 def __init__(self, name, description = "", xcoordinate = None, ycoordinate = None, mapImageFilename = None, nUnitsPerPixel = 1.): | 24 |
| 25 def __init__(self, name, description = "", xcoordinate = None, ycoordinate = None, mapImageFilename = None, nUnitsPerPixel = 1., worldDistanceUnit = 'm'): | |
| 25 self.name = name | 26 self.name = name |
| 26 self.description = description | 27 self.description = description |
| 27 self.xcoordinate = xcoordinate | 28 self.xcoordinate = xcoordinate |
| 28 self.ycoordinate = ycoordinate | 29 self.ycoordinate = ycoordinate |
| 29 self.mapImageFilename = mapImageFilename | 30 self.mapImageFilename = mapImageFilename |
| 30 self.nUnitsPerPixel = nUnitsPerPixel | 31 self.nUnitsPerPixel = nUnitsPerPixel |
| 32 self.worldDistanceUnit = worldDistanceUnit | |
| 31 | 33 |
| 32 def getFilename(self): | 34 def getFilename(self): |
| 33 return self.name | 35 return self.name |
| 34 | 36 |
| 35 class EnvironementalFactors(Base): | 37 class EnvironementalFactors(Base): |
| 51 self.startTime = datetime.strptime(startTime, datetimeFormat) | 53 self.startTime = datetime.strptime(startTime, datetimeFormat) |
| 52 self.endTime = datetime.strptime(endTime, datetimeFormat) | 54 self.endTime = datetime.strptime(endTime, datetimeFormat) |
| 53 self.description = description | 55 self.description = description |
| 54 self.site = site | 56 self.site = site |
| 55 | 57 |
| 58 class CameraType(Base): | |
| 59 ''' Represents parameters of the specific camera used. | |
| 60 | |
| 61 Taken and adapted from tvalib''' | |
| 62 __tablename__ = 'camera_types' | |
| 63 idx = Column(Integer, primary_key=True) | |
| 64 name = Column(String) | |
| 65 resX = Column(Integer) | |
| 66 resY = Column(Integer) | |
| 67 frameRate = Column(Float) | |
| 68 undistort = Column(Boolean) | |
| 69 intrinsicCameraMatrixStr = Column(String) | |
| 70 distortionCoefficientsStr = Column(String) | |
| 71 undistortedImageMultiplication = Column(Float) | |
| 72 | |
| 73 def __init__(self, name, resX, resY, frameRate, trackingConfigurationFilename = None, undistort = None, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = None): | |
| 74 self.name = name | |
| 75 self.resX = resX | |
| 76 self.resY = resY | |
| 77 self.frameRate = frameRate | |
| 78 self.undistort = False | |
| 79 | |
| 80 if trackingConfigurationFilename is not None: | |
| 81 from storage import ProcessParameters | |
| 82 params = ProcessParameters(trackingConfigurationFilename) | |
| 83 if params.undistort: | |
| 84 self.undistort = params.undistort | |
| 85 self.intrinsicCameraMatrix = params.intrinsicCameraMatrix | |
| 86 self.distortionCoefficients = params.distortionCoefficients | |
| 87 self.undistortedImageMultiplication = params.undistortedImageMultiplication | |
| 88 elif undistort is not None: | |
| 89 self.undistort = undistort | |
| 90 self.intrinsicCameraMatrix = intrinsicCameraMatrix | |
| 91 self.distortionCoefficients = distortionCoefficients | |
| 92 self.undistortedImageMultiplication = undistortedImageMultiplication | |
| 93 | |
| 94 # populate the db | |
| 95 if self.intrinsicCameraMatrix is not None and self.distortionCoefficients is not None: | |
| 96 self.intrinsicCameraMatrixStr = ' '.join('{}'.format(x) for x in self.intrinsicCameraMatrix.flatten('C')) | |
| 97 self.distortionCoefficientsStr = ' '.join('{}'.format(x)for x in self.distortionCoefficients) | |
| 98 | |
| 99 @orm.reconstructor | |
| 100 def initOnLoad(self): | |
| 101 from numpy import array | |
| 102 if len(self.intrinsicCameraMatrixStr) > 0: | |
| 103 self.intrinsicCameraMatrix = array([float(x) for x in self.intrinsicCameraMatrixStr.split(" ")]).reshape(3,3) | |
| 104 if len(self.distortionCoefficientsStr) > 0: | |
| 105 self.distortionCoefficients = [float(x) for x in self.distortionCoefficientsStr.split(" ")] | |
| 106 | |
| 56 class CameraView(Base): | 107 class CameraView(Base): |
| 57 __tablename__ = 'camera_views' | 108 __tablename__ = 'camera_views' |
| 58 idx = Column(Integer, primary_key=True) | 109 idx = Column(Integer, primary_key=True) |
| 59 frameRate = Column(Float) | |
| 60 homographyFilename = Column(String) # path to homograph filename, relative to the site name | 110 homographyFilename = Column(String) # path to homograph filename, relative to the site name |
| 61 cameraCalibrationFilename = Column(String) # path to full camera calibration, relative to the site name | |
| 62 siteIdx = Column(Integer, ForeignKey('sites.idx')) | 111 siteIdx = Column(Integer, ForeignKey('sites.idx')) |
| 112 cameraTypeIdx = Column(Integer, ForeignKey('camera_types.idx')) | |
| 63 homographyDistanceUnit = Column(String, default = 'm') # make sure it is default in the database | 113 homographyDistanceUnit = Column(String, default = 'm') # make sure it is default in the database |
| 64 configurationFilename = Column(String) # path to configuration .cfg file, relative to site name | 114 trackingConfigurationFilename = Column(String) # path to configuration .cfg file, relative to site name |
| 65 | 115 |
| 66 site = relationship("Site", backref=backref('camera_views', order_by = idx)) | 116 site = relationship("Site", backref=backref('sites', order_by = idx)) |
| 67 | 117 camera = relationship('CameraType', backref=backref('camera_views', order_by = idx)) |
| 68 def __init__(self, frameRate, homographyFilename, cameraCalibrationFilename, site, configurationFilename): | 118 |
| 69 self.frameRate = frameRate | 119 def __init__(self, homographyFilename, site, cameraType, trackingConfigurationFilename): |
| 70 self.homographyFilename = homographyFilename | 120 self.homographyFilename = homographyFilename |
| 71 self.site = site | 121 self.site = site |
| 72 self.configurationFilename = configurationFilename | 122 self.cameraType = cameraType |
| 123 self.trackingConfigurationFilename = trackingConfigurationFilename | |
| 73 | 124 |
| 74 def getHomographyFilename(self, relativeToSiteFilename = True): | 125 def getHomographyFilename(self, relativeToSiteFilename = True): |
| 75 if relativeToSiteFilename: | 126 if relativeToSiteFilename: |
| 76 return self.site.getFilename()+path.sep+self.homographyFilename | 127 return self.site.getFilename()+path.sep+self.homographyFilename |
| 77 else: | 128 else: |
| 78 return self.homographyFilename | 129 return self.homographyFilename |
| 130 | |
| 131 def getTrackingConfigurationFilename(self, relativeToSiteFilename = True): | |
| 132 if relativeToSiteFilename: | |
| 133 return self.site.getFilename()+path.sep+self.trackingConfigurationFilename | |
| 134 else: | |
| 135 return self.trackingConfigurationFilename | |
| 136 | |
| 137 def getTrackingParameters(self): | |
| 138 return ProcessParameters(getTrackingConfigurationFilename()) | |
| 79 | 139 |
| 80 class Alignment(Base): | 140 class Alignment(Base): |
| 81 __tablename__ = 'alignments' | 141 __tablename__ = 'alignments' |
| 82 idx = Column(Integer, primary_key=True) | 142 idx = Column(Integer, primary_key=True) |
| 83 cameraViewIdx = Column(Integer, ForeignKey('camera_views.idx')) | 143 cameraViewIdx = Column(Integer, ForeignKey('camera_views.idx')) |
| 107 idx = Column(Integer, primary_key=True) | 167 idx = Column(Integer, primary_key=True) |
| 108 name = Column(String) # path relative to the the site name | 168 name = Column(String) # path relative to the the site name |
| 109 startTime = Column(DateTime) | 169 startTime = Column(DateTime) |
| 110 duration = Column(Float) # video sequence duration | 170 duration = Column(Float) # video sequence duration |
| 111 durationUnit = Column(String, default = 's') | 171 durationUnit = Column(String, default = 's') |
| 172 databaseFilename = Column(String) # path relative to the the site name | |
| 112 siteIdx = Column(Integer, ForeignKey('sites.idx')) | 173 siteIdx = Column(Integer, ForeignKey('sites.idx')) |
| 113 cameraViewIdx = Column(Integer, ForeignKey('camera_views.idx')) | 174 cameraViewIdx = Column(Integer, ForeignKey('camera_views.idx')) |
| 114 configurationFilename = Column(String) | |
| 115 | 175 |
| 116 site = relationship("Site", backref=backref('video_sequences', order_by = idx)) | 176 site = relationship("Site", backref=backref('video_sequences', order_by = idx)) |
| 117 cameraView = relationship("CameraView", backref=backref('video_sequences', order_by = idx)) | 177 cameraView = relationship("CameraView", backref=backref('video_sequences', order_by = idx)) |
| 118 | 178 |
| 119 def __init__(self, name, startTime, duration, site, cameraView, configurationFilename = None): | 179 def __init__(self, name, startTime, duration, site, cameraView, databaseFilename = None): |
| 120 'startTime is passed as string in utils.datetimeFormat, eg 2011-06-22 10:00:39' | 180 'startTime is passed as string in utils.datetimeFormat, eg 2011-06-22 10:00:39' |
| 121 self.name = name | 181 self.name = name |
| 122 self.startTime = datetime.strptime(startTime, datetimeFormat) | 182 self.startTime = datetime.strptime(startTime, datetimeFormat) |
| 123 self.duration = duration | 183 self.duration = duration |
| 124 self.site = site | 184 self.site = site |
| 125 self.cameraView = cameraView | 185 self.cameraView = cameraView |
| 126 self.configurationFilename = configurationFilename | 186 if databaseFilename is None and len(self.name) > 0: |
| 187 self.databaseFilename = removeExtension(self.name)+'.sqlite' | |
| 127 | 188 |
| 128 def getVideoSequenceFilename(self, relativeToSiteFilename = True): | 189 def getVideoSequenceFilename(self, relativeToSiteFilename = True): |
| 129 if relativeToSiteFilename: | 190 if relativeToSiteFilename: |
| 130 return self.site.getFilename()+path.sep+self.name | 191 return self.site.getFilename()+path.sep+self.name |
| 131 else: | 192 else: |
| 132 return self.name | 193 return self.name |
| 133 | 194 |
| 134 #def getConfigurationFilename(self): | 195 |
| 135 #'returns the local configuration filename, or the one of the camera view otherwise' | |
| 136 | 196 |
| 137 # 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) | 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) |
| 138 | 198 |
| 139 # class SiteDescription(Base): # list of lines and polygons describing the site, eg for sidewalks, center lines | 199 # class SiteDescription(Base): # list of lines and polygons describing the site, eg for sidewalks, center lines |
| 140 | 200 |
