Mercurial > hg > nsaunier > traffic-intelligence
comparison python/metadata.py @ 614:5e09583275a4
Merged Nicolas/trafficintelligence into default
| author | Mohamed Gomaa <eng.m.gom3a@gmail.com> |
|---|---|
| date | Fri, 05 Dec 2014 12:13:53 -0500 |
| parents | 1262faae12e7 |
| children | 180b6b0231c0 |
comparison
equal
deleted
inserted
replaced
| 598:11f96bd08552 | 614:5e09583275a4 |
|---|---|
| 1 # from moving import Point | |
| 2 | |
| 3 from datetime import datetime | |
| 4 from os import path | |
| 5 | |
| 6 from sqlalchemy import create_engine, Column, Integer, Float, DateTime, String, ForeignKey | |
| 7 from sqlalchemy.orm import relationship, backref, sessionmaker | |
| 8 from sqlalchemy.ext.declarative import declarative_base | |
| 9 | |
| 10 from utils import datetimeFormat | |
| 11 | |
| 12 Base = declarative_base() | |
| 13 | |
| 14 class Site(Base): | |
| 15 __tablename__ = 'sites' | |
| 16 idx = Column(Integer, primary_key=True) | |
| 17 name = Column(String) # same as path, relative to the database position | |
| 18 description = Column(String) # longer names, eg intersection of road1 and road2 | |
| 19 xcoordinate = Column(Float) # ideally moving.Point, but needs to be | |
| 20 ycoordinate = Column(Float) | |
| 21 | |
| 22 def __init__(self, name, description = "", xcoordinate = None, ycoordinate = None): | |
| 23 self.name = name | |
| 24 self.description = description | |
| 25 self.xcoordinate = xcoordinate | |
| 26 self.ycoordinate = ycoordinate | |
| 27 | |
| 28 def getFilename(self): | |
| 29 return self.name | |
| 30 | |
| 31 class EnvironementalFactors(Base): | |
| 32 '''Represents any environmental factors that may affect the results, in particular | |
| 33 * changing weather conditions | |
| 34 * changing road configuration, geometry, signalization, etc. | |
| 35 ex: sunny, rainy, before counter-measure, after counter-measure''' | |
| 36 __tablename__ = 'environmental_factors' | |
| 37 idx = Column(Integer, primary_key=True) | |
| 38 startTime = Column(DateTime) | |
| 39 endTime = Column(DateTime) | |
| 40 description = Column(String) # eg sunny, before, after | |
| 41 siteIdx = Column(Integer, ForeignKey('sites.idx')) | |
| 42 | |
| 43 site = relationship("Site", backref=backref('environmental_factors', order_by = idx)) | |
| 44 | |
| 45 def __init__(self, startTime, endTime, description, site): | |
| 46 'startTime is passed as string in utils.datetimeFormat, eg 2011-06-22 10:00:39' | |
| 47 self.startTime = datetime.strptime(startTime, datetimeFormat) | |
| 48 self.endTime = datetime.strptime(endTime, datetimeFormat) | |
| 49 self.description = description | |
| 50 self.site = site | |
| 51 | |
| 52 class CameraView(Base): | |
| 53 __tablename__ = 'camera_views' | |
| 54 idx = Column(Integer, primary_key=True) | |
| 55 frameRate = Column(Float) | |
| 56 homographyFilename = Column(String) # path to homograph filename, relative to the site name | |
| 57 cameraCalibrationFilename = Column(String) # path to full camera calibration, relative to the site name | |
| 58 siteIdx = Column(Integer, ForeignKey('sites.idx')) | |
| 59 homographyDistanceUnit = Column(String, default = 'm') # make sure it is default in the database | |
| 60 configurationFilename = Column(String) # path to configuration .cfg file, relative to site name | |
| 61 | |
| 62 site = relationship("Site", backref=backref('camera_views', order_by = idx)) | |
| 63 | |
| 64 def __init__(self, frameRate, homographyFilename, cameraCalibrationFilename, site, configurationFilename): | |
| 65 self.frameRate = frameRate | |
| 66 self.homographyFilename = homographyFilename | |
| 67 self.site = site | |
| 68 self.configurationFilename = configurationFilename | |
| 69 | |
| 70 def getHomographyFilename(self, relativeToSiteFilename = True): | |
| 71 if relativeToSiteFilename: | |
| 72 return self.site.getFilename()+path.sep+self.homographyFilename | |
| 73 else: | |
| 74 return self.homographyFilename | |
| 75 | |
| 76 class Alignment(Base): | |
| 77 __tablename__ = 'alignments' | |
| 78 idx = Column(Integer, primary_key=True) | |
| 79 cameraViewIdx = Column(Integer, ForeignKey('camera_views.idx')) | |
| 80 | |
| 81 cameraView = relationship("CameraView", backref=backref('alignments', order_by = idx)) | |
| 82 | |
| 83 def __init__(self, cameraView): | |
| 84 self.cameraView = cameraView | |
| 85 | |
| 86 class Point(Base): | |
| 87 __tablename__ = 'points' | |
| 88 alignmentIdx = Column(Integer, ForeignKey('alignments.idx'), primary_key=True) | |
| 89 index = Column(Integer, primary_key=True) # order of points in this alignment | |
| 90 x = Column(Float) | |
| 91 y = Column(Float) | |
| 92 | |
| 93 alignment = relationship("Alignment", backref=backref('points', order_by = index)) | |
| 94 | |
| 95 def __init__(self, alignmentIdx, index, x, y): | |
| 96 self.alignmentIdx = alignmentIdx | |
| 97 self.index = index | |
| 98 self.x = x | |
| 99 self.y = y | |
| 100 | |
| 101 class VideoSequence(Base): | |
| 102 __tablename__ = 'video_sequences' | |
| 103 idx = Column(Integer, primary_key=True) | |
| 104 name = Column(String) # path relative to the the site name | |
| 105 startTime = Column(DateTime) | |
| 106 duration = Column(Float) # video sequence duration | |
| 107 durationUnit = Column(String, default = 's') | |
| 108 siteIdx = Column(Integer, ForeignKey('sites.idx')) | |
| 109 cameraViewIdx = Column(Integer, ForeignKey('camera_views.idx')) | |
| 110 configurationFilename = Column(String) | |
| 111 | |
| 112 site = relationship("Site", backref=backref('video_sequences', order_by = idx)) | |
| 113 cameraView = relationship("CameraView", backref=backref('video_sequences', order_by = idx)) | |
| 114 | |
| 115 def __init__(self, name, startTime, duration, site, cameraView, configurationFilename = None): | |
| 116 'startTime is passed as string in utils.datetimeFormat, eg 2011-06-22 10:00:39' | |
| 117 self.name = name | |
| 118 self.startTime = datetime.strptime(startTime, datetimeFormat) | |
| 119 self.duration = duration | |
| 120 self.site = site | |
| 121 self.cameraView = cameraView | |
| 122 self.configurationFilename = configurationFilename | |
| 123 | |
| 124 def getVideoSequenceFilename(self, relativeToSiteFilename = True): | |
| 125 if relativeToSiteFilename: | |
| 126 return self.site.getFilename()+path.sep+self.name | |
| 127 else: | |
| 128 return self.name | |
| 129 | |
| 130 #def getConfigurationFilename(self): | |
| 131 #'returns the local configuration filename, or the one of the camera view otherwise' | |
| 132 | |
| 133 # 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) | |
| 134 | |
| 135 # class SiteDescription(Base): # list of lines and polygons describing the site, eg for sidewalks, center lines | |
| 136 | |
| 137 # class Analysis(Base): # parameters necessary for processing the data: free form | |
| 138 # eg bounding box depends on camera view, tracking configuration depends on camera view | |
| 139 # results: sqlite | |
| 140 | |
| 141 def createDatabase(filename): | |
| 142 'creates a session to query the filename' | |
| 143 engine = create_engine('sqlite:///'+filename) | |
| 144 Base.metadata.create_all(engine) | |
| 145 Session = sessionmaker(bind=engine) | |
| 146 return Session() | |
| 147 | |
| 148 def connectDatabase(filename): | |
| 149 'creates a session to query the filename' | |
| 150 engine = create_engine('sqlite:///'+filename) | |
| 151 Session = sessionmaker(bind=engine) | |
| 152 return Session() | |
| 153 | |
| 154 def initializeSites(session, directoryName): | |
| 155 '''Initializes default site objects and Camera Views | |
| 156 | |
| 157 eg somedirectory/montreal/ contains intersection1, intersection2, etc. | |
| 158 The site names would be somedirectory/montreal/intersection1, somedirectory/montreal/intersection2, etc.''' | |
| 159 from os import listdir, path | |
| 160 sites = [] | |
| 161 cameraViews = [] | |
| 162 names = listdir(directoryName) | |
| 163 for name in names: | |
| 164 if path.isdir(directoryName+'/'+name): | |
| 165 sites.append(Site(directoryName+'/'+name, None)) | |
| 166 cameraViews.append(CameraView(-1, None, None, sites[-1], None)) | |
| 167 session.add_all(sites) | |
| 168 session.add_all(cameraViews) | |
| 169 session.commit() | |
| 170 # TODO crawler for video files? |
