changeset 464:dcc821b98efc

integrated and reorganized Sohail s work on exact ttc computation
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sun, 23 Feb 2014 23:18:08 -0500
parents cb9683f9efe7
children 16fe64136506
files TTC Sample/Video.png TTC Sample/bikeDes.png TTC Sample/bikeOri.png TTC Sample/carDes.png TTC Sample/carOri.png TTC Sample/computeTTC.py TTC Sample/timeToCollision.py TTC Sample/ttcCompare.py python/moving.py python/prediction.py samples/TTC/Video.png samples/TTC/bikeDes.png samples/TTC/bikeOri.png samples/TTC/carDes.png samples/TTC/carOri.png samples/TTC/computeTTC.py samples/TTC/ttcCompare.py scripts/TTCcomputation.py scripts/timeToCollision.py
diffstat 19 files changed, 156 insertions(+), 202 deletions(-) [+]
line wrap: on
line diff
Binary file TTC Sample/Video.png has changed
Binary file TTC Sample/bikeDes.png has changed
Binary file TTC Sample/bikeOri.png has changed
Binary file TTC Sample/carDes.png has changed
Binary file TTC Sample/carOri.png has changed
--- a/TTC Sample/computeTTC.py	Sun Feb 23 22:56:54 2014 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,61 +0,0 @@
-def computeTTC(databaseFilename,homography,framePerSecond,videoX,videoY,collisionDistanceThreshold,bikAreaOri,bikAreaDes,carAreaOri,carAreaDes):
-
-    import numpy as np
-    import sys
-    sys.path.append('/home/sohail/trafficintelligence/python/')
-    import moving, cvutils, storage
-    import timeToCollision
-
-    print 'Loading database...'
-    objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object')
-
-    bikCount=0
-    carCount=0
-    bik=[]
-    car=[]
-    bikSpeed=[]
-    carSpeed=[]
-
-    for obj in objects:
-	    inCarAreaOri = False
-	    inBikAreaOri = False
-	    for time in obj.getTimeInterval():
-		x=int(obj.getPositionAtInstant(time).project(homography).x)
-		y=int(obj.getPositionAtInstant(time).project(homography).y)
-		x=min(videoX-1,x)
-		y=min(videoY-1,y)
-		if bikAreaOri[y,x] == 1 and obj.userType == moving.userType2Num['bicycle']:
-		    inBikAreaOri = True
-		if bikAreaDes[y,x] == 1 and inBikAreaOri == True:
-		    bikCount += 1
-		    bik.append(obj)
-		    bikSpeed.append(framePerSecond*3.6*np.median(obj.getSpeeds()))
-		    break
-		if carAreaOri[y,x] == 1 and obj.userType == moving.userType2Num['car']:
-		    inCarAreaOri = True
-		if carAreaDes[y,x] == 1 and inCarAreaOri == True:
-		    carCount += 1
-		    car.append(obj)
-		    carSpeed.append(framePerSecond*3.6*np.median(obj.getSpeeds()))
-		    break
-
-    print 'Computing TTC...'
-    TTC=[]
-    potCollision=0
-    for obj1 in bik:
-	for obj2 in car:
-	    ti1=obj1.getTimeInterval()
-	    ti2=obj2.getTimeInterval()
-	    if ti1.first < ti2.last and ti2.first < ti1.last:
-		potCollision += 1
-		ttc=[]
-		for frameNum in range(max(ti1.first,ti2.first),min(ti1.last,ti2.last)):
-		    ttcp=timeToCollision.timeToCollision(obj1,obj2,collisionDistanceThreshold,frameNum,framePerSecond)
-		    if ttcp < 100:
-			ttc.append(ttcp)
-		if ttc != []:
-		    ttc.sort()
-	            TTC.append(ttc[int(.15*len(ttc))])
-
-    return bikCount,carCount,bikSpeed,carSpeed,TTC,potCollision
-
--- a/TTC Sample/timeToCollision.py	Sun Feb 23 22:56:54 2014 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-def timeToCollision(obj1,obj2,collisionDistanceThreshold,frameNum,framePerSecond):
-
-    import numpy as np
-
-    x1 = obj1.getPositionAtInstant(frameNum).x
-    y1 = obj1.getPositionAtInstant(frameNum).y
-    x2 = obj2.getPositionAtInstant(frameNum).x
-    y2 = obj2.getPositionAtInstant(frameNum).y
-    v1x = obj1.getVelocityAtInstant(frameNum).x * framePerSecond
-    v1y = obj1.getVelocityAtInstant(frameNum).y * framePerSecond
-    v2x = obj2.getVelocityAtInstant(frameNum).x * framePerSecond
-    v2y = obj2.getVelocityAtInstant(frameNum).y * framePerSecond
-    l = collisionDistanceThreshold
-
-    a = pow(v1x-v2x,2) + pow(v1y-v2y,2)
-    b = 2 * ((x1-x2) * (v1x-v2x) + (y1-y2) * (v1y-v2y))
-    c = pow(x1-x2,2) + pow(y1-y2,2) - pow(l,2)
-    
-    if pow(b,2) >= 4*a*c:
-        ttc1 = (-b + np.sqrt(pow(b,2) - 4*a*c)) / (2*a)
-        ttc2 = (-b - np.sqrt(pow(b,2) - 4*a*c)) / (2*a)
-        if ttc1 >= 0 and ttc2 >= 0:
-            ttc = min(ttc1,ttc2)
-        else:
-            if ttc1 < 0:
-                ttc = ttc2
-            if ttc2 < 0:
-                ttc = ttc1
-            if ttc1 < 0 and ttc2 < 0:
-                ttc = []
-    else:
-        ttc = []
-
-    return ttc
--- a/TTC Sample/ttcCompare.py	Sun Feb 23 22:56:54 2014 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-import matplotlib.image as mpimg
-from numpy.linalg.linalg import inv
-from numpy import loadtxt
-import computeTTC
-import numpy as np
-import glob
-
-#databaseName = ['11.sqlite']
-databaseName = glob.glob("*.sqlite")
-
-bikOri = mpimg.imread('bikeOri.png')
-bikAreaOri = bikOri[:,:,0]
-bikDes = mpimg.imread('bikeDes.png')
-bikAreaDes = bikDes[:,:,0]
-carOri = mpimg.imread('carOri.png')
-carAreaOri = carOri[:,:,0]
-carDes = mpimg.imread('carDes.png')
-carAreaDes = carDes[:,:,0]
-
-videoY = bikOri.shape[0]
-videoX = bikOri.shape[1]
-
-homography=inv(loadtxt('homography.txt'))
-framePerSecond=15
-collisionDistanceThreshold=10
-
-bikCount=0
-carCount=0
-bikSpeed=[]
-carSpeed=[]
-ttcDist=[]
-potentioalCollision=0
-
-for d in databaseName:
-    print d
-    bc,cc,bs,cs,ttcdist,potCollision = computeTTC.computeTTC(d,homography,framePerSecond,videoX,videoY,collisionDistanceThreshold,bikAreaOri,bikAreaDes,carAreaOri,carAreaDes)
-    bikCount += bc
-    carCount += cc
-    bikSpeed.extend(bs)
-    carSpeed.extend(cs)
-    ttcDist.extend(ttcdist)
-    potentioalCollision += potCollision
-
-print 'bikCount :', bikCount
-print 'carCount :', carCount
-print 'Number of Potential Collisions :', potentioalCollision
-np.savetxt('ttc.txt',ttcDist,fmt='%1.2f')
-np.savetxt('freq.txt',[bikCount,carCount,potentioalCollision],fmt='%i')
-np.savetxt('bikSpeed.txt',bikSpeed,fmt='%1.2f')
-np.savetxt('carSpeed.txt',carSpeed,fmt='%1.2f')
-
--- a/python/moving.py	Sun Feb 23 22:56:54 2014 -0500
+++ b/python/moving.py	Sun Feb 23 23:18:08 2014 -0500
@@ -266,6 +266,30 @@
         'Indicates whether the cosine of the vector and refDirection is smaller than cosineThreshold'
         return Point.cosine(self, refDirection) >= cosineThreshold
 
+    @staticmethod
+    def timeToCollision(p1, p2, v1, v2):
+        from math import sqrt
+        a = pow(v1.x-v2.x,2) + pow(v1.y-v2.y,2)
+        b = 2 * ((p1.x-p2.x) * (v1.x-v2.x) + (p1.y-p2.y) * (v1.y-v2.y))
+        c = pow(p1.x-p2.x,2) + pow(p1.y-p2.y,2) - pow(l,2)
+
+        if pow(b,2) >= 4*a*c:
+            ttc1 = (-b + sqrt(pow(b,2) - 4*a*c)) / (2*a)
+            ttc2 = (-b - sqrt(pow(b,2) - 4*a*c)) / (2*a)
+            if ttc1 >= 0 and ttc2 >= 0:
+                ttc = min(ttc1,ttc2)
+            else:
+                if ttc1 < 0:
+                    ttc = ttc2
+                if ttc2 < 0:
+                    ttc = ttc1
+                if ttc1 < 0 and ttc2 < 0:
+                    ttc = None
+        else:
+            ttc = None
+        return ttc
+
+
 if shapelyAvailable:
     def pointsInPolygon(points, polygon):
         '''Optimized tests of a series of points within (Shapely) polygon '''
--- a/python/prediction.py	Sun Feb 23 22:56:54 2014 -0500
+++ b/python/prediction.py	Sun Feb 23 23:18:08 2014 -0500
@@ -356,8 +356,28 @@
 
         return collisionPoints, crossingZones
 
+class CVExactPredictionParameters(PredictionParameters):
+    '''Prediction parameters of prediction at constant velocity
+    using direct computation of the intersecting point (solving for the equation'''
+    
+    def __init__(self):
+        PredictionParameters.__init__(self, 'constant velocity (direct exact computation)', None)
 
+    def computeCrossingsCollisionsAtInstant(self, currentInstant, obj1, obj2, collisionDistanceThreshold, timeHorizon, computeCZ = False, debug = False):
+        'TODO add collision point coordinates, compute pPET'
+        #collisionPoints = []
+        #crossingZones = []
 
+        p1 = obj1.getPositionAtInstant(currentInstant)
+        p2 = obj2.getPositionAtInstant(currentInstant)
+        v1 = obj1.getVelocityAtInstant(currentInstant)
+        v2 = obj2.getVelocityAtInstant(currentInstant)
+        
+        ttc = moving.Point.timeToCollision(p1, p2, v1, v2, collisionDistanceThreshold)
+        if ttc:
+            return [SafetyPoint(moving.Point(), 1., ttc)], []
+        else:
+            return [],[]
 
 ####
 # Other Methods
Binary file samples/TTC/Video.png has changed
Binary file samples/TTC/bikeDes.png has changed
Binary file samples/TTC/bikeOri.png has changed
Binary file samples/TTC/carDes.png has changed
Binary file samples/TTC/carOri.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/samples/TTC/computeTTC.py	Sun Feb 23 23:18:08 2014 -0500
@@ -0,0 +1,61 @@
+def computeTTC(databaseFilename,homography,framePerSecond,videoX,videoY,collisionDistanceThreshold,bikAreaOri,bikAreaDes,carAreaOri,carAreaDes):
+
+    import numpy as np
+    import sys
+    sys.path.append('/home/sohail/trafficintelligence/python/')
+    import moving, cvutils, storage
+    import timeToCollision
+
+    print 'Loading database...'
+    objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object')
+
+    bikCount=0
+    carCount=0
+    bik=[]
+    car=[]
+    bikSpeed=[]
+    carSpeed=[]
+
+    for obj in objects:
+	    inCarAreaOri = False
+	    inBikAreaOri = False
+	    for time in obj.getTimeInterval():
+		x=int(obj.getPositionAtInstant(time).project(homography).x)
+		y=int(obj.getPositionAtInstant(time).project(homography).y)
+		x=min(videoX-1,x)
+		y=min(videoY-1,y)
+		if bikAreaOri[y,x] == 1 and obj.userType == moving.userType2Num['bicycle']:
+		    inBikAreaOri = True
+		if bikAreaDes[y,x] == 1 and inBikAreaOri == True:
+		    bikCount += 1
+		    bik.append(obj)
+		    bikSpeed.append(framePerSecond*3.6*np.median(obj.getSpeeds()))
+		    break
+		if carAreaOri[y,x] == 1 and obj.userType == moving.userType2Num['car']:
+		    inCarAreaOri = True
+		if carAreaDes[y,x] == 1 and inCarAreaOri == True:
+		    carCount += 1
+		    car.append(obj)
+		    carSpeed.append(framePerSecond*3.6*np.median(obj.getSpeeds()))
+		    break
+
+    print 'Computing TTC...'
+    TTC=[]
+    potCollision=0
+    for obj1 in bik:
+	for obj2 in car:
+	    ti1=obj1.getTimeInterval()
+	    ti2=obj2.getTimeInterval()
+	    if ti1.first < ti2.last and ti2.first < ti1.last:
+		potCollision += 1
+		ttc=[]
+		for frameNum in range(max(ti1.first,ti2.first),min(ti1.last,ti2.last)):
+		    ttcp=timeToCollision.timeToCollision(obj1,obj2,collisionDistanceThreshold,frameNum,framePerSecond)
+		    if ttcp < 100:
+			ttc.append(ttcp)
+		if ttc != []:
+		    ttc.sort()
+	            TTC.append(ttc[int(.15*len(ttc))])
+
+    return bikCount,carCount,bikSpeed,carSpeed,TTC,potCollision
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/samples/TTC/ttcCompare.py	Sun Feb 23 23:18:08 2014 -0500
@@ -0,0 +1,51 @@
+import matplotlib.image as mpimg
+from numpy.linalg.linalg import inv
+from numpy import loadtxt
+import computeTTC
+import numpy as np
+import glob
+
+#databaseName = ['11.sqlite']
+databaseName = glob.glob("*.sqlite")
+
+bikOri = mpimg.imread('bikeOri.png')
+bikAreaOri = bikOri[:,:,0]
+bikDes = mpimg.imread('bikeDes.png')
+bikAreaDes = bikDes[:,:,0]
+carOri = mpimg.imread('carOri.png')
+carAreaOri = carOri[:,:,0]
+carDes = mpimg.imread('carDes.png')
+carAreaDes = carDes[:,:,0]
+
+videoY = bikOri.shape[0]
+videoX = bikOri.shape[1]
+
+homography=inv(loadtxt('homography.txt'))
+framePerSecond=15
+collisionDistanceThreshold=10
+
+bikCount=0
+carCount=0
+bikSpeed=[]
+carSpeed=[]
+ttcDist=[]
+potentioalCollision=0
+
+for d in databaseName:
+    print d
+    bc,cc,bs,cs,ttcdist,potCollision = computeTTC.computeTTC(d,homography,framePerSecond,videoX,videoY,collisionDistanceThreshold,bikAreaOri,bikAreaDes,carAreaOri,carAreaDes)
+    bikCount += bc
+    carCount += cc
+    bikSpeed.extend(bs)
+    carSpeed.extend(cs)
+    ttcDist.extend(ttcdist)
+    potentioalCollision += potCollision
+
+print 'bikCount :', bikCount
+print 'carCount :', carCount
+print 'Number of Potential Collisions :', potentioalCollision
+np.savetxt('ttc.txt',ttcDist,fmt='%1.2f')
+np.savetxt('freq.txt',[bikCount,carCount,potentioalCollision],fmt='%i')
+np.savetxt('bikSpeed.txt',bikSpeed,fmt='%1.2f')
+np.savetxt('carSpeed.txt',carSpeed,fmt='%1.2f')
+
--- a/scripts/TTCcomputation.py	Sun Feb 23 22:56:54 2014 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,22 +0,0 @@
-def TTC(x1,y1,x2,y2,v1x,v1y,v2x,v2y,l):
-
-    a = pow(v1x-v2x,2) + pow(v1y-v2y,2)
-    b = 2 * ((x1-x2) * (v1x-v2x) + (y1-y2) * (v1y-v2y))
-    c = pow(x1-x2,2) + pow(y1-y2,2) - pow(l,2)
-    
-    if pow(b,2) >= 4*a*c:
-        ttc1 = (-b + sqrt(pow(b,2) - 4*a*c)) / (2*a)
-        ttc2 = (-b - sqrt(pow(b,2) - 4*a*c)) / (2*a)
-        if ttc1 >= 0 and ttc2 >= 0:
-            ttc = min(ttc1,ttc2)
-        else:
-            if ttc1 < 0:
-                ttc = ttc
-            if ttc2 < 0:
-                ttc = ttc1
-            if ttc1 < 0 and ttc2 < 0:
-                ttc = []
-    else:
-        ttc = []
-
-    return ttc
--- a/scripts/timeToCollision.py	Sun Feb 23 22:56:54 2014 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-def timeToCollision(obj1,obj2,collisionDistanceThreshold,frameNum,framePerSecond):
-
-    import numpy as np
-
-    x1 = obj1.getPositionAtInstant(frameNum).x
-    y1 = obj1.getPositionAtInstant(frameNum).y
-    x2 = obj2.getPositionAtInstant(frameNum).x
-    y2 = obj2.getPositionAtInstant(frameNum).y
-    v1x = obj1.getVelocityAtInstant(frameNum).x * framePerSecond
-    v1y = obj1.getVelocityAtInstant(frameNum).y * framePerSecond
-    v2x = obj2.getVelocityAtInstant(frameNum).x * framePerSecond
-    v2y = obj2.getVelocityAtInstant(frameNum).y * framePerSecond
-    l = collisionDistanceThreshold
-
-    a = pow(v1x-v2x,2) + pow(v1y-v2y,2)
-    b = 2 * ((x1-x2) * (v1x-v2x) + (y1-y2) * (v1y-v2y))
-    c = pow(x1-x2,2) + pow(y1-y2,2) - pow(l,2)
-    
-    if pow(b,2) >= 4*a*c:
-        ttc1 = (-b + np.sqrt(pow(b,2) - 4*a*c)) / (2*a)
-        ttc2 = (-b - np.sqrt(pow(b,2) - 4*a*c)) / (2*a)
-        if ttc1 >= 0 and ttc2 >= 0:
-            ttc = min(ttc1,ttc2)
-        else:
-            if ttc1 < 0:
-                ttc = ttc2
-            if ttc2 < 0:
-                ttc = ttc1
-            if ttc1 < 0 and ttc2 < 0:
-                ttc = []
-    else:
-        ttc = []
-
-    return ttc