diff 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
line wrap: on
line diff
--- a/python/metadata.py	Wed Jun 22 15:23:20 2016 -0400
+++ b/python/metadata.py	Wed Jun 22 16:36:12 2016 -0400
@@ -1,9 +1,10 @@
 # from moving import Point
 
-from datetime import datetime
+from datetime import datetime, timedelta
 from os import path
+from math import floor
 
-from sqlalchemy import orm, create_engine, Column, Integer, Float, DateTime, String, ForeignKey, Boolean
+from sqlalchemy import orm, create_engine, Column, Integer, Float, DateTime, String, ForeignKey, Boolean, Interval
 from sqlalchemy.orm import relationship, backref, sessionmaker
 from sqlalchemy.ext.declarative import declarative_base
 
@@ -65,16 +66,18 @@
     resX = Column(Integer)
     resY = Column(Integer)
     frameRate = Column(Float)
+    frameRateTimeUnit = Column(String, default = 's')
     undistort = Column(Boolean)
     intrinsicCameraMatrixStr = Column(String)
     distortionCoefficientsStr = Column(String)
     undistortedImageMultiplication = Column(Float)
     
-    def __init__(self, name, resX, resY, frameRate, trackingConfigurationFilename = None, undistort = None, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = None):
+    def __init__(self, name, resX, resY, frameRate, frameRateTimeUnit = 's', trackingConfigurationFilename = None, undistort = None, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = None):
         self.name = name
         self.resX = resX
         self.resY = resY
         self.frameRate = frameRate
+        self.frameRateTimeUnit = frameRateTimeUnit
         self.undistort = False
 
         if trackingConfigurationFilename is not None:
@@ -92,7 +95,8 @@
             self.undistortedImageMultiplication = undistortedImageMultiplication
 
         # populate the db
-        if self.intrinsicCameraMatrix is not None and self.distortionCoefficients is not None:
+        if hasattr(self, 'intrinsicCameraMatrix') and self.intrinsicCameraMatrix is not None\
+        and hasattr(self, 'distortionCoefficients') and self.distortionCoefficients is not None:
             self.intrinsicCameraMatrixStr = ' '.join('{}'.format(x) for x in self.intrinsicCameraMatrix.flatten('C'))
             self.distortionCoefficientsStr = ' '.join('{}'.format(x)for x in self.distortionCoefficients)
 
@@ -107,6 +111,7 @@
 class CameraView(Base):
     __tablename__ = 'camera_views'
     idx = Column(Integer, primary_key=True)
+    description = Column(String)
     homographyFilename = Column(String) # path to homograph filename, relative to the site name
     siteIdx = Column(Integer, ForeignKey('sites.idx'))
     cameraTypeIdx = Column(Integer, ForeignKey('camera_types.idx'))
@@ -114,9 +119,10 @@
     trackingConfigurationFilename = Column(String) # path to configuration .cfg file, relative to site name
 
     site = relationship("Site", backref=backref('sites', order_by = idx))
-    camera = relationship('CameraType', backref=backref('camera_views', order_by = idx))
+    cameraType = relationship('CameraType', backref=backref('camera_views', order_by = idx))
 
-    def __init__(self, homographyFilename, site, cameraType, trackingConfigurationFilename):
+    def __init__(self, description, homographyFilename, site, cameraType, trackingConfigurationFilename):
+        self.description = description
         self.homographyFilename = homographyFilename
         self.site = site
         self.cameraType = cameraType
@@ -167,8 +173,7 @@
     idx = Column(Integer, primary_key=True)
     name = Column(String) # path relative to the the site name
     startTime = Column(DateTime)
-    duration = Column(Float) # video sequence duration
-    durationUnit = Column(String, default = 's')
+    duration = Column(Interval) # video sequence duration
     databaseFilename = Column(String) # path relative to the the site name
     siteIdx = Column(Integer, ForeignKey('sites.idx'))
     cameraViewIdx = Column(Integer, ForeignKey('camera_views.idx'))
@@ -177,7 +182,8 @@
     cameraView = relationship("CameraView", backref=backref('video_sequences', order_by = idx))
 
     def __init__(self, name, startTime, duration, site, cameraView, databaseFilename = None):
-        'startTime is passed as string in utils.datetimeFormat, eg 2011-06-22 10:00:39'
+        '''startTime is passed as string in utils.datetimeFormat, eg 2011-06-22 10:00:39
+        duration is a timedelta object'''
         self.name = name
         self.startTime = datetime.strptime(startTime, datetimeFormat)
         self.duration = duration
@@ -192,7 +198,16 @@
         else:
             return self.name
 
-
+    def containsInstant(self, instant):
+        'instant is a datetime'
+        return self.startTime <= instant and self.startTime+self.duration
+        
+    def getFrameNum(self, instant):
+        'Warning, there is no check of correct time units'
+        if self.containsInstant(instant):
+            return int(floor((instant-self.startTime).seconds*self.cameraView.cameraType.frameRate))
+        else:
+            return None
 
 # 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)