changeset 381:387cc0142211

script to replay event annotations
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 19 Jul 2013 11:58:35 -0400
parents adfd4f70ee1d
children ba813f148ade
files python/cvutils.py python/moving.py python/utils.py scripts/replay-event-annotation.py
diffstat 4 files changed, 43 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/python/cvutils.py	Fri Jul 19 09:11:57 2013 -0400
+++ b/python/cvutils.py	Fri Jul 19 11:58:35 2013 -0400
@@ -98,11 +98,13 @@
         for i in range(0, last-1):
             cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color)
 
-    def playVideo(filename, firstFrameNum = 0, frameRate = -1):
+    def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None):
         '''Plays the video'''
         wait = 5
         if frameRate > 0:
             wait = int(round(1000./frameRate))
+        if interactive:
+            wait = 0
         capture = cv2.VideoCapture(filename)
         if capture.isOpened():
             key = -1
@@ -112,8 +114,11 @@
             while ret and not quitKey(key):
                 ret, img = capture.read()
                 if ret:
-                    print('frame {0}'.format(frameNum))
+                    if printFrames:
+                        print('frame {0}'.format(frameNum))
                     frameNum+=1
+                    if text != None:
+                       cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) 
                     cv2.imshow('frame', img)
                     key = cv2.waitKey(wait)
             cv2.destroyAllWindows()
@@ -139,7 +144,7 @@
                         images.append(img)
         return images
 
-    def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0, lastFrameNumArg = None):
+    def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True):
         '''Displays the objects overlaid frame by frame over the video '''
         capture = cv2.VideoCapture(videoFilename)
         if capture.isOpened():
@@ -155,7 +160,8 @@
             while ret and not quitKey(key) and frameNum < lastFrameNum:
                 ret, img = capture.read()
                 if ret:
-                    print('frame {0}'.format(frameNum))
+                    if printFrames:
+                        print('frame {0}'.format(frameNum))
                     for obj in objects:
                         if obj.existsAtInstant(frameNum):
                             if not hasattr(obj, 'projectedPositions'):
--- a/python/moving.py	Fri Jul 19 09:11:57 2013 -0400
+++ b/python/moving.py	Fri Jul 19 11:58:35 2013 -0400
@@ -520,15 +520,15 @@
             displacement += Point.distanceNorm2(self.__getitem__(i),self.__getitem__(i+1))
         return displacement
 
-    def similarOrientation(self, refDirection, cosineThreshold):
-        '''Indicates whether the majority of the trajectory elements (vectors for velocity) 
+    def similarOrientation(self, refDirection, cosineThreshold, minProportion = 0.5):
+        '''Indicates whether the minProportion (<=1.) (eg half) of the trajectory elements (vectors for velocity) 
         have a cosine with refDirection is smaller than cosineThreshold'''
         count = 0
-        halfLength = float(self.length())/2
+        lengthTreshold = float(self.length())*minProportion
         for p in self:
             if p.similarOrientation(refDirection, cosineThreshold):
                 count += 1
-            if count > halfLength:
+            if count > lengthThreshold:
                 return True
         return False
 
--- a/python/utils.py	Fri Jul 19 09:11:57 2013 -0400
+++ b/python/utils.py	Fri Jul 19 11:58:35 2013 -0400
@@ -155,6 +155,9 @@
     seconds = seconds - m*60
     return time(h, m, seconds)
 
+def timeToFrames(t, frameRate):
+    return frameRate*(t.hour*3600+t.minute*60+t.second)
+
 def sortXY(X,Y):
     'returns the sorted (x, Y(x)) sorted on X'
     D = {}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/replay-event-annotation.py	Fri Jul 19 11:58:35 2013 -0400
@@ -0,0 +1,26 @@
+#! /usr/bin/env python
+
+import sys, argparse
+
+import storage, cvutils, utils
+
+import matplotlib.pylab as pylab
+import matplotlib.pyplot as plt
+import numpy as np
+
+
+annotations = pylab.csv2rec(sys.argv[1])
+
+frameRate = 30
+dirname = "/home/nicolas/Research/Data/montreal/infractions-pietons/"
+videoDirnames = {'amherst': '2011-06-22-sherbrooke-amherst/',
+                 'iberville': '2011-06-28-sherbrooke-iberville/'}
+
+# for amherst, subtract 40 seconds: add a delta
+
+for annotation in annotations:
+    video = annotation['video_name'].lower()
+    print('{} {}'.format(annotation['conflict_start_time'], annotation['conflict_end_time']))
+    print(annotation['road_user_1']+' '+annotation['road_user_2']+' '+annotation['conflict_quality'])
+    print(annotation['comments'])
+    cvutils.playVideo(dirname+videoDirnames[video]+video+'-{}.avi'.format(annotation['video_start_time']), utils.timeToFrames(annotation['conflict_start_time'], frameRate), frameRate, True, False, annotation['road_user_1']+' '+annotation['road_user_2']+' '+annotation['conflict_quality'])