changeset 1140:78dddfe7aa0f

added alignments for sites
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 15 Apr 2020 00:18:35 -0400
parents e9c12982ed28
children 3e0f43edb4d6
files trafficintelligence/metadata.py
diffstat 1 files changed, 37 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/trafficintelligence/metadata.py	Sun Mar 22 18:57:39 2020 +0000
+++ b/trafficintelligence/metadata.py	Wed Apr 15 00:18:35 2020 -0400
@@ -11,7 +11,7 @@
 
 from trafficintelligence.utils import datetimeFormat, removeExtension, getExtension, TimeConverter
 from trafficintelligence.cvutils import computeUndistortMaps, videoFilenameExtensions, infoVideo
-from trafficintelligence.moving import TimeInterval
+from trafficintelligence.moving import TimeInterval, Trajectory
 
 """
 Metadata to describe how video data and configuration files for video analysis are stored
@@ -212,30 +212,36 @@
     def getHomographyDistanceUnit(self):
         return self.site.worldDistanceUnit
     
-# class Alignment(Base):
-#     __tablename__ = 'alignments'
-#     idx = Column(Integer, primary_key=True)
-#     siteIdx = Column(Integer, ForeignKey('sites.idx'))
+class Alignment(Base):
+    __tablename__ = 'alignments'
+    idx = Column(Integer, primary_key=True)
+    siteIdx = Column(Integer, ForeignKey('sites.idx'))
     
-#     cameraView = relationship("Site", backref = backref('alignments'))
+    site = relationship("Site", backref = backref('alignments'))
 
-#     def __init__(self, cameraView):
-#         self.cameraView = cameraView
+    def __init__(self, site):
+        self.site = site
 
-# class Point(Base):
-#     __tablename__ = 'points'
-#     alignmentIdx = Column(Integer, ForeignKey('alignments.idx'), primary_key=True)
-#     index = Column(Integer, primary_key=True) # order of points in this alignment
-#     x = Column(Float)
-#     y = Column(Float)
+    def getTrajectory(self):
+        t = Trajectory()
+        for p in self.point:
+            t.addPositionXY(p.x_coordinate, p.y_coordinate)
+        return t
 
-#     alignment = relationship("Alignment", backref = backref('points', order_by = index))
+class Point(Base):
+    __tablename__ = 'positions'
+    trajectory_id = Column(Integer, ForeignKey('alignments.idx'), primary_key=True)
+    frame_number = Column(Integer, primary_key=True) # order of points in this alignment, as index
+    x_coordinate = Column(Float)
+    y_coordinate = Column(Float)
+
+    alignment = relationship("Alignment", backref = backref('points', order_by = trajectory_id))
     
-#     def __init__(self, alignmentIdx, index, x, y):
-#         self.alignmentIdx = alignmentIdx
-#         self.index = index
-#         self.x = x
-#         self.y = y
+    def __init__(self, alignment, index, x, y):
+        self.alignment = alignment
+        self.frame_number = index
+        self.x_coordinate = x
+        self.y_coordinate = y
 
 class VideoSequence(Base):
     __tablename__ = 'video_sequences'
@@ -425,6 +431,17 @@
 
 def generateTimeIntervals(videoSequences, maxTimeGap):
     ''
+
+def addAlignment(session, site, t):
+    'Adds trajectory (moving.Trajectory) t to metadata of site'
+    al = Alignment(site)
+    session.add(al)
+    session.commit()
+    points = []
+    for i,p in enumerate(t):
+        points.append(Point(al, i, p.x, p.y))
+    session.add_all(points)
+    session.commit()
     
 # management
 # TODO need to be able to copy everything from a site from one sqlite to another, and delete everything attached to a site