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')

