comparison python/metadata.py @ 428:70accfa6692f

merged
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 05 Nov 2013 23:42:28 -0500
parents 334e1151828b
children 343cfd185ca6
comparison
equal deleted inserted replaced
427:1e3c7fe21a38 428:70accfa6692f
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 Column, Integer, Float, DateTime, String, ForeignKey 6 from sqlalchemy import create_engine, Column, Integer, Float, DateTime, String, ForeignKey
7 from sqlalchemy.orm import relationship, backref 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
11 11
12 Base = declarative_base() 12 Base = declarative_base()
57 id = Column(Integer, primary_key=True) 57 id = Column(Integer, primary_key=True)
58 frameRate = Column(Float) 58 frameRate = Column(Float)
59 homographyFilename = Column(String) # path to homograph filename, relative to the site name 59 homographyFilename = Column(String) # path to homograph filename, relative to the site name
60 cameraCalibrationFilename = Column(String) # path to full camera calibration, relative to the site name 60 cameraCalibrationFilename = Column(String) # path to full camera calibration, relative to the site name
61 siteId = Column(Integer, ForeignKey('sites.id')) 61 siteId = Column(Integer, ForeignKey('sites.id'))
62 homographyDistanceUnit = Column(String, default = 'm') 62 homographyDistanceUnit = Column(String, default = 'm') # make sure it is default in the database
63 # TODO default config filename 63 configurationFilename = Column(String) # path to configuration .cfg file, relative to site name
64 64
65 site = relationship("Site", backref=backref('camera_views', order_by = id)) 65 site = relationship("Site", backref=backref('camera_views', order_by = id))
66 66
67 def __init__(self, frameRate, homographyFilename, cameraCalibrationFilename, site): 67 def __init__(self, frameRate, homographyFilename, cameraCalibrationFilename, site, configurationFilename):
68 self.frameRate = frameRate 68 self.frameRate = frameRate
69 self.homographyFilename = homographyFilename 69 self.homographyFilename = homographyFilename
70 self.site = site 70 self.site = site
71 self.configurationFilename = configurationFilename
71 72
72 def getHomographyFilename(self, relativeToSiteFilename = True): 73 def getHomographyFilename(self, relativeToSiteFilename = True):
73 if relativeToSiteFilename: 74 if relativeToSiteFilename:
74 return self.site.getFilename()+path.sep+self.homographyFilename 75 return self.site.getFilename()+path.sep+self.homographyFilename
75 else: 76 else:
82 startTime = Column(DateTime) 83 startTime = Column(DateTime)
83 duration = Column(Float) # video sequence duration 84 duration = Column(Float) # video sequence duration
84 durationUnit = Column(String, default = 's') 85 durationUnit = Column(String, default = 's')
85 siteId = Column(Integer, ForeignKey('sites.id')) 86 siteId = Column(Integer, ForeignKey('sites.id'))
86 cameraViewId = Column(Integer, ForeignKey('camera_views.id')) 87 cameraViewId = Column(Integer, ForeignKey('camera_views.id'))
88 configurationFilename = Column(String)
87 89
88 site = relationship("Site", backref=backref('video_sequences', order_by = id)) 90 site = relationship("Site", backref=backref('video_sequences', order_by = id))
89 cameraView = relationship("CameraView", backref=backref('video_sequences', order_by = id)) 91 cameraView = relationship("CameraView", backref=backref('video_sequences', order_by = id))
90 92
91 def __init__(self, name, startTime, duration, site, cameraView): 93 def __init__(self, name, startTime, duration, site, cameraView, configurationFilename = None):
92 'startTime is passed as string in utils.datetimeFormat, eg 2011-06-22 10:00:39' 94 'startTime is passed as string in utils.datetimeFormat, eg 2011-06-22 10:00:39'
93 self.name = name 95 self.name = name
94 self.startTime = datetime.strptime(startTime, datetimeFormat) 96 self.startTime = datetime.strptime(startTime, datetimeFormat)
95 self.duration = duration 97 self.duration = duration
96 self.site = site 98 self.site = site
97 self.cameraView = cameraView 99 self.cameraView = cameraView
100 self.configurationFilename = configurationFilename
98 101
99 def getVideoSequenceFilename(self, relativeToSiteFilename = True): 102 def getVideoSequenceFilename(self, relativeToSiteFilename = True):
100 if relativeToSiteFilename: 103 if relativeToSiteFilename:
101 return self.site.getFilename()+path.sep+self.name 104 return self.site.getFilename()+path.sep+self.name
102 else: 105 else:
103 return self.name 106 return self.name
104 107
108 #def getConfigurationFilename(self):
109 #'returns the local configuration filename, or the one of the camera view otherwise'
110
105 # 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) 111 # 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)
106 112
107 # class SiteDescription(Base): # list of lines and polygons describing the site, eg for sidewalks, center lines 113 # class SiteDescription(Base): # list of lines and polygons describing the site, eg for sidewalks, center lines
108 114
109 # class Analysis(Base): # parameters necessary for processing the data: free form 115 # class Analysis(Base): # parameters necessary for processing the data: free form
110 # eg bounding box depends on camera view, tracking configuration depends on camera view 116 # eg bounding box depends on camera view, tracking configuration depends on camera view
117 # results: sqlite
111 118
112 def createDatabases(engine): 119 def createDatabase(filename):
120 'creates a session to query the filename'
121 engine = create_engine('sqlite:///'+filename)
113 Base.metadata.create_all(engine) 122 Base.metadata.create_all(engine)
123 Session = sessionmaker(bind=engine)
124 return Session()
125
126 def connectDatabase(filename):
127 'creates a session to query the filename'
128 engine = create_engine('sqlite:///'+filename)
129 Session = sessionmaker(bind=engine)
130 return Session()
131
132 def initializeSites(session, directoryName):
133 '''Initializes default site objects and Camera Views
134
135 eg somedirectory/montreal/ contains intersection1, intersection2, etc.
136 The site names would be somedirectory/montreal/intersection1, somedirectory/montreal/intersection2, etc.'''
137 from os import listdir, path
138 sites = []
139 cameraViews = []
140 names = listdir(directoryName)
141 for name in names:
142 if path.isdir(directoryName+'/'+name):
143 sites.append(Site(directoryName+'/'+name, None))
144 cameraViews.append(CameraView(-1, None, None, sites[-1], None))
145 session.add_all(sites)
146 session.add_all(cameraViews)
147 session.commit()
148 # TODO crawler for video files?