changeset 628:977407c9f815

corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 16 Feb 2015 11:58:51 +0100
parents 82e9f78a4714
children 0a5e89d6fc62
files python/cvutils.py python/events.py python/storage.py
diffstat 3 files changed, 49 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/python/cvutils.py	Mon Feb 16 08:41:14 2015 +0100
+++ b/python/cvutils.py	Mon Feb 16 11:58:51 2015 +0100
@@ -240,7 +240,7 @@
             else:
                 lastFrameNum = lastFrameNumArg
             nZerosFilename = int(ceil(log10(lastFrameNum)))
-            while ret and not quitKey(key) and frameNum < lastFrameNum:
+            while ret and not quitKey(key) and frameNum <= lastFrameNum:
                 ret, img = capture.read()
                 if ret:
                     if undistort:
--- a/python/events.py	Mon Feb 16 08:41:14 2015 +0100
+++ b/python/events.py	Mon Feb 16 11:58:51 2015 +0100
@@ -8,7 +8,7 @@
 import multiprocessing
 import itertools
 
-import moving, prediction, indicators, utils
+import moving, prediction, indicators, utils, cvutils
 __metaclass__ = type
 
 def findRoute(prototypes,objects,i,j,noiseEntryNums,noiseExitNums,minSimilarity= 0.3, spatialThreshold=1.0, delta=180):
@@ -61,7 +61,7 @@
                       'Velocity Angle',
                       'Speed Differential',
                       'Collision Probability',
-                      'Time to Collision',
+                      'Time to Collision', # 7
                       'Probability of Successful Evasive Action',
                       'predicted Post Encroachment Time']
 
@@ -108,6 +108,22 @@
     def getRoadUserNumbers(self):
         return self.roadUserNumbers
 
+    def setRoadUsers(self, objects):
+        nums = list(self.getRoadUserNumbers())
+        if objects[nums[0]].getNum() == nums[0]:
+            self.roadUser1 = objects[nums[0]]
+        if objects[nums[1]].getNum() == nums[1]:
+            self.roadUser2 = objects[nums[1]]
+
+        i = 0
+        while i < len(objects) and self.roadUser2 == None:
+            if objects[i].getNum() in nums:
+                if self.roadUser1 == None:
+                    self.roadUser1 = objects[i]
+                else:
+                    self.roadUser2 = objects[i]
+            i += 1
+
     def getIndicator(self, indicatorName):
         return self.indicators.get(indicatorName, None)
 
@@ -115,6 +131,20 @@
         if indicator:
             self.indicators[indicator.name] = indicator
 
+    def plot(self, options = '', withOrigin = False, timeStep = 1, withFeatures = False, **kwargs):
+        self.roadUser1.plot(options, withOrigin, timeStep, withFeatures, **kwargs)
+        self.roadUser2.plot(options, withOrigin, timeStep, withFeatures, **kwargs)
+
+    def plotOnWorldImage(self, nPixelsPerUnitDistance, options = '', withOrigin = False, timeStep = 1, **kwargs):
+        self.roadUser1.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, **kwargs)
+        self.roadUser2.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, **kwargs)
+
+    def play(self, videoFilename, homography = None, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1.):
+        if self.roadUser1 != None and self.roadUser2 != None:
+            cvutils.displayTrajectories(videoFilename, [self.roadUser1, self.roadUser2], homography = homography, firstFrameNum = self.getFirstInstant(), lastFrameNumArg = self.getLastInstant(), undistort = undistort, intrinsicCameraMatrix = intrinsicCameraMatrix, distortionCoefficients = distortionCoefficients, undistortedImageMultiplication = undistortedImageMultiplication)
+        else:
+            print('Please set the interaction road user attributes roadUser1 and roadUser1 through the method setRoadUsers')
+
     def computeIndicators(self):
         '''Computes the collision course cosine only if the cosine is positive'''
         collisionCourseDotProducts = {}#[0]*int(self.timeInterval.length())
@@ -202,6 +232,16 @@
                 num += 1
     return interactions
 
+def findInteraction(interactions, roadUserNum1, roadUserNum2):
+    'Returns the right interaction in the set'
+    i=0
+    while i<len(interactions) and set([roadUserNum1, roadUserNum2]) != interactions[i].getRoadUserNumbers():
+        i+=1
+    if i<len(interactions):
+        return interactions[i]
+    else:
+        return None
+
 def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity):
     '''Finds exemplar indicator time series for all interactions
     Returns the prototype indices (in the interaction list) and the label of each indicator (interaction)
--- a/python/storage.py	Mon Feb 16 08:41:14 2015 +0100
+++ b/python/storage.py	Mon Feb 16 11:58:51 2015 +0100
@@ -518,27 +518,17 @@
         indicatorTypeNum = -1
         tmpIndicators = {}
         for row in cursor:
-            if row[0] != interactionNum: # save interaction and create new interaction
-                if interactionNum >= 0:
-                    interactions.append(events.Interaction(interactionNum, moving.TimeInterval(row[3],row[4]), roadUserNumbers[0], roadUserNumbers[1]))
-                    interactions[-1].indicators = tmpIndicators
-                    tmpIndicators = {}
+            if row[0] != interactionNum:
                 interactionNum = row[0]
-                roadUserNumbers = row[1:3]
+                interactions.append(events.Interaction(interactionNum, moving.TimeInterval(row[3],row[4]), row[1], row[2]))
+                interactions[-1].indicators = {}
             if indicatorTypeNum != row[5]:
-                if indicatorTypeNum >= 0:
-                    indicatorName = events.Interaction.indicatorNames[indicatorTypeNum]
-                    tmpIndicators[indicatorName] = indicators.SeverityIndicator(indicatorName, indicatorValues)
+                indicatorName = events.Interaction.indicatorNames[indicatorTypeNum]
+                indicatorValues = {row[6]:row[7]}
+                interactions[-1].indicators[indicatorName] = indicators.SeverityIndicator(indicatorName, indicatorValues)
                 indicatorTypeNum = row[5]
-                indicatorValues = {row[6]:row[7]}
             else:
                 indicatorValues[row[6]] = row[7]
-        if interactionNum >= 0:
-            if indicatorTypeNum >= 0:
-                indicatorName = events.Interaction.indicatorNames[indicatorTypeNum]
-                tmpIndicators[indicatorName] = indicators.SeverityIndicator(indicatorName, indicatorValues)
-            interactions.append(events.Interaction(interactionNum, moving.TimeInterval(row[3],row[4]), roadUserNumbers[0], roadUserNumbers[1]))
-            interactions[-1].indicators = tmpIndicators
     except sqlite3.OperationalError as error:
         printDBError(error)
         return []