Mercurial Hosting > traffic-intelligence
comparison samples/TTC/computeTTC.py @ 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 | TTC Sample/computeTTC.py@af2222c0c9c0 |
children |
comparison
equal
deleted
inserted
replaced
463:cb9683f9efe7 | 464:dcc821b98efc |
---|---|
1 def computeTTC(databaseFilename,homography,framePerSecond,videoX,videoY,collisionDistanceThreshold,bikAreaOri,bikAreaDes,carAreaOri,carAreaDes): | |
2 | |
3 import numpy as np | |
4 import sys | |
5 sys.path.append('/home/sohail/trafficintelligence/python/') | |
6 import moving, cvutils, storage | |
7 import timeToCollision | |
8 | |
9 print 'Loading database...' | |
10 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object') | |
11 | |
12 bikCount=0 | |
13 carCount=0 | |
14 bik=[] | |
15 car=[] | |
16 bikSpeed=[] | |
17 carSpeed=[] | |
18 | |
19 for obj in objects: | |
20 inCarAreaOri = False | |
21 inBikAreaOri = False | |
22 for time in obj.getTimeInterval(): | |
23 x=int(obj.getPositionAtInstant(time).project(homography).x) | |
24 y=int(obj.getPositionAtInstant(time).project(homography).y) | |
25 x=min(videoX-1,x) | |
26 y=min(videoY-1,y) | |
27 if bikAreaOri[y,x] == 1 and obj.userType == moving.userType2Num['bicycle']: | |
28 inBikAreaOri = True | |
29 if bikAreaDes[y,x] == 1 and inBikAreaOri == True: | |
30 bikCount += 1 | |
31 bik.append(obj) | |
32 bikSpeed.append(framePerSecond*3.6*np.median(obj.getSpeeds())) | |
33 break | |
34 if carAreaOri[y,x] == 1 and obj.userType == moving.userType2Num['car']: | |
35 inCarAreaOri = True | |
36 if carAreaDes[y,x] == 1 and inCarAreaOri == True: | |
37 carCount += 1 | |
38 car.append(obj) | |
39 carSpeed.append(framePerSecond*3.6*np.median(obj.getSpeeds())) | |
40 break | |
41 | |
42 print 'Computing TTC...' | |
43 TTC=[] | |
44 potCollision=0 | |
45 for obj1 in bik: | |
46 for obj2 in car: | |
47 ti1=obj1.getTimeInterval() | |
48 ti2=obj2.getTimeInterval() | |
49 if ti1.first < ti2.last and ti2.first < ti1.last: | |
50 potCollision += 1 | |
51 ttc=[] | |
52 for frameNum in range(max(ti1.first,ti2.first),min(ti1.last,ti2.last)): | |
53 ttcp=timeToCollision.timeToCollision(obj1,obj2,collisionDistanceThreshold,frameNum,framePerSecond) | |
54 if ttcp < 100: | |
55 ttc.append(ttcp) | |
56 if ttc != []: | |
57 ttc.sort() | |
58 TTC.append(ttc[int(.15*len(ttc))]) | |
59 | |
60 return bikCount,carCount,bikSpeed,carSpeed,TTC,potCollision | |
61 |