changeset 462:af2222c0c9c0

TTC tested and updatet!
author Sohail Zangenehpour <sohail.zangenehpour@mail.mcgill.ca>
date Mon, 03 Feb 2014 15:41:57 -0500
parents cb41f9a4652b
children cb9683f9efe7
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
diffstat 8 files changed, 146 insertions(+), 0 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TTC Sample/computeTTC.py	Mon Feb 03 15:41:57 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/TTC Sample/timeToCollision.py	Mon Feb 03 15:41:57 2014 -0500
@@ -0,0 +1,34 @@
+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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TTC Sample/ttcCompare.py	Mon Feb 03 15:41:57 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')
+