Mercurial Hosting > traffic-intelligence
annotate trafficintelligence/events.py @ 1275:9f1711a85c56
added code to make sure TTC is replaced of recomputed and not having a value
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 19 Jun 2024 16:11:35 -0400 |
parents | 655a1646f0d5 |
children |
rev | line source |
---|---|
56
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
2 '''Libraries for events |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
3 Interactions, pedestrian crossing...''' |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
4 |
1028
cc5cb04b04b0
major update using the trafficintelligence package name and install through pip
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
5 from trafficintelligence import moving, prediction, indicators, utils, cvutils, ml |
cc5cb04b04b0
major update using the trafficintelligence package name and install through pip
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
6 from trafficintelligence.base import VideoFilenameAddable |
665
15e244d2a1b5
corrected bug with circular import for VideoFilenameAddable, moved to base module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
664
diff
changeset
|
7 |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
8 import numpy as np |
1256
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
9 from matplotlib.pyplot import subplot, figure, ylabel |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
10 |
292
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
11 import multiprocessing |
949
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
943
diff
changeset
|
12 import itertools, logging |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
13 |
56
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
14 |
613 | 15 def findRoute(prototypes,objects,i,j,noiseEntryNums,noiseExitNums,minSimilarity= 0.3, spatialThreshold=1.0, delta=180): |
619
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
16 if i[0] not in noiseEntryNums: |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
17 prototypesRoutes= [ x for x in sorted(prototypes.keys()) if i[0]==x[0]] |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
18 elif i[1] not in noiseExitNums: |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
19 prototypesRoutes=[ x for x in sorted(prototypes.keys()) if i[1]==x[1]] |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
20 else: |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
21 prototypesRoutes=[x for x in sorted(prototypes.keys())] |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
22 routeSim={} |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
23 lcss = utils.LCSS(similarityFunc=lambda x,y: (distanceForLCSS(x,y) <= spatialThreshold),delta=delta) |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
24 for y in prototypesRoutes: |
998
933670761a57
updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
954
diff
changeset
|
25 if y in prototypes: |
619
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
26 prototypesIDs=prototypes[y] |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
27 similarity=[] |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
28 for x in prototypesIDs: |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
29 s=lcss.computeNormalized(objects[j].positions, objects[x].positions) |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
30 similarity.append(s) |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
31 routeSim[y]=max(similarity) |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
32 route=max(routeSim, key=routeSim.get) |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
33 if routeSim[route]>=minSimilarity: |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
34 return route |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
35 else: |
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
36 return i |
613 | 37 |
610
0dc36203973d
remove dublicated code for collision/crossing computations
MohamedGomaa
parents:
607
diff
changeset
|
38 def getRoute(obj,prototypes,objects,noiseEntryNums,noiseExitNums,useDestination=True): |
0dc36203973d
remove dublicated code for collision/crossing computations
MohamedGomaa
parents:
607
diff
changeset
|
39 route=(obj.startRouteID,obj.endRouteID) |
0dc36203973d
remove dublicated code for collision/crossing computations
MohamedGomaa
parents:
607
diff
changeset
|
40 if useDestination: |
998
933670761a57
updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
954
diff
changeset
|
41 if route not in prototypes: |
613 | 42 route= findRoute(prototypes,objects,route,obj.getNum(),noiseEntryNums,noiseExitNums) |
610
0dc36203973d
remove dublicated code for collision/crossing computations
MohamedGomaa
parents:
607
diff
changeset
|
43 return route |
0dc36203973d
remove dublicated code for collision/crossing computations
MohamedGomaa
parents:
607
diff
changeset
|
44 |
664
455f9b93819c
added capability to set a videofilename to movingobject and interaction, renames interactiontype to collision in interaction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
662
diff
changeset
|
45 class Interaction(moving.STObject, VideoFilenameAddable): |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
46 '''Class for an interaction between two road users |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
47 or a road user and an obstacle |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
48 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
49 link to the moving objects |
294
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
50 contains the indicators in a dictionary with the names as keys |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
51 ''' |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
52 |
1144
6a8fe3ed3bc6
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1028
diff
changeset
|
53 categories = {'headon': 0, |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
54 'rearend': 1, |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
55 'side': 2, |
1266
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
56 'parallel': 3, |
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
57 'stationary': 4} |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
58 |
320
419f30491a4b
renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
317
diff
changeset
|
59 indicatorNames = ['Collision Course Dot Product', |
419f30491a4b
renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
317
diff
changeset
|
60 'Collision Course Angle', |
419f30491a4b
renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
317
diff
changeset
|
61 'Distance', |
419f30491a4b
renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
317
diff
changeset
|
62 'Minimum Distance', |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
63 'Velocity Angle', |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
64 'Speed Differential', |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
65 'Collision Probability', |
628
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
66 'Time to Collision', # 7 |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
67 'Probability of Successful Evasive Action', |
881
8ba82b371eea
work on storing PET
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
735
diff
changeset
|
68 'predicted Post Encroachment Time', |
8ba82b371eea
work on storing PET
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
735
diff
changeset
|
69 'Post Encroachment Time'] |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
70 |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
71 indicatorNameToIndices = utils.inverseEnumeration(indicatorNames) |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
72 |
388
6e0dedd34920
minor name change
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
387
diff
changeset
|
73 indicatorShortNames = ['CCDP', |
408
365d8dee44f3
last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
388
diff
changeset
|
74 'CCA', |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
75 'Dist', |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
76 'MinDist', |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
77 'VA', |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
78 'SD', |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
79 'PoC', |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
80 'TTC', |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
81 'P(SEA)', |
881
8ba82b371eea
work on storing PET
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
735
diff
changeset
|
82 'pPET', |
8ba82b371eea
work on storing PET
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
735
diff
changeset
|
83 'PET'] |
320
419f30491a4b
renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
317
diff
changeset
|
84 |
408
365d8dee44f3
last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
388
diff
changeset
|
85 indicatorUnits = ['', |
365d8dee44f3
last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
388
diff
changeset
|
86 'rad', |
365d8dee44f3
last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
388
diff
changeset
|
87 'm', |
365d8dee44f3
last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
388
diff
changeset
|
88 'm', |
365d8dee44f3
last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
388
diff
changeset
|
89 'rad', |
1161
b968c33f8c2f
work on producing figures and collision maps for large datasets with multiple sites
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1157
diff
changeset
|
90 'km/h', |
408
365d8dee44f3
last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
388
diff
changeset
|
91 '', |
365d8dee44f3
last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
388
diff
changeset
|
92 's', |
365d8dee44f3
last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
388
diff
changeset
|
93 '', |
881
8ba82b371eea
work on storing PET
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
735
diff
changeset
|
94 's', |
8ba82b371eea
work on storing PET
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
735
diff
changeset
|
95 's'] |
408
365d8dee44f3
last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
388
diff
changeset
|
96 |
1182
0e5d37b0b9ff
bug corrections
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1161
diff
changeset
|
97 mostSevereIsMinIndicators = ['Distance', 'Time to Collision', 'predicted Post Encroachment Time'] |
695
957126bfb456
corrected bug with indicator loading(also now correctly loading mostsevereismax)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
694
diff
changeset
|
98 |
1146
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
99 def __init__(self, num = None, timeInterval = None, roaduserNum1 = None, roaduserNum2 = None, roadUser1 = None, roadUser2 = None): |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
100 moving.STObject.__init__(self, num, timeInterval) |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
632
diff
changeset
|
101 if timeInterval is None and roadUser1 is not None and roadUser2 is not None: |
566
07b1bd0785cd
simplifications to interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
559
diff
changeset
|
102 self.timeInterval = roadUser1.commonTimeInterval(roadUser2) |
320
419f30491a4b
renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
317
diff
changeset
|
103 self.roadUser1 = roadUser1 |
419f30491a4b
renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
317
diff
changeset
|
104 self.roadUser2 = roadUser2 |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
632
diff
changeset
|
105 if roaduserNum1 is not None and roaduserNum2 is not None: |
566
07b1bd0785cd
simplifications to interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
559
diff
changeset
|
106 self.roadUserNumbers = set([roaduserNum1, roaduserNum2]) |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
632
diff
changeset
|
107 elif roadUser1 is not None and roadUser2 is not None: |
662
72174e66aba5
corrected bug that increased TTC by 1 frame and structure to store collision points and crossing zones
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
661
diff
changeset
|
108 self.roadUserNumbers = set([roadUser1.getNum(), roadUser2.getNum()]) |
566
07b1bd0785cd
simplifications to interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
559
diff
changeset
|
109 else: |
07b1bd0785cd
simplifications to interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
559
diff
changeset
|
110 self.roadUserNumbers = None |
294
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
111 self.indicators = {} |
662
72174e66aba5
corrected bug that increased TTC by 1 frame and structure to store collision points and crossing zones
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
661
diff
changeset
|
112 # list for collison points and crossing zones |
72174e66aba5
corrected bug that increased TTC by 1 frame and structure to store collision points and crossing zones
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
661
diff
changeset
|
113 self.collisionPoints = None |
72174e66aba5
corrected bug that increased TTC by 1 frame and structure to store collision points and crossing zones
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
661
diff
changeset
|
114 self.crossingZones = None |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
115 |
340
1046b7346886
work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
339
diff
changeset
|
116 def getRoadUserNumbers(self): |
1046b7346886
work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
339
diff
changeset
|
117 return self.roadUserNumbers |
1046b7346886
work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
339
diff
changeset
|
118 |
628
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
119 def setRoadUsers(self, objects): |
1273
655a1646f0d5
updated setRoadUsers to force the new objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1269
diff
changeset
|
120 tmpobjects = {o.getNum():o for o in objects} |
655a1646f0d5
updated setRoadUsers to force the new objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1269
diff
changeset
|
121 i, j = self.roadUserNumbers |
655a1646f0d5
updated setRoadUsers to force the new objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1269
diff
changeset
|
122 if i in tmpobjects: |
655a1646f0d5
updated setRoadUsers to force the new objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1269
diff
changeset
|
123 self.roadUser1 = tmpobjects[i] |
655a1646f0d5
updated setRoadUsers to force the new objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1269
diff
changeset
|
124 if j in tmpobjects: |
655a1646f0d5
updated setRoadUsers to force the new objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1269
diff
changeset
|
125 self.roadUser2 = tmpobjects[j] |
628
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
126 |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
127 def getIndicator(self, indicatorName): |
321
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
320
diff
changeset
|
128 return self.indicators.get(indicatorName, None) |
294
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
129 |
1275
9f1711a85c56
added code to make sure TTC is replaced of recomputed and not having a value
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1273
diff
changeset
|
130 def resetIndicator(self, indicatorName): |
9f1711a85c56
added code to make sure TTC is replaced of recomputed and not having a value
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1273
diff
changeset
|
131 if indicatorName in self.indicators: |
9f1711a85c56
added code to make sure TTC is replaced of recomputed and not having a value
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1273
diff
changeset
|
132 del self.indicators[indicatorName] |
9f1711a85c56
added code to make sure TTC is replaced of recomputed and not having a value
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1273
diff
changeset
|
133 |
294
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
134 def addIndicator(self, indicator): |
661
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
135 if indicator is not None: |
321
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
320
diff
changeset
|
136 self.indicators[indicator.name] = indicator |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
137 |
661
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
138 def getIndicatorValueAtInstant(self, indicatorName, instant): |
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
139 indicator = self.getIndicator(indicatorName) |
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
140 if indicator is not None: |
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
141 return indicator[instant] |
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
142 else: |
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
143 return None |
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
144 |
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
145 def getIndicatorValuesAtInstant(self, instant): |
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
146 '''Returns list of indicator values at instant |
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
147 as dict (with keys from indicators dict)''' |
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
148 values = {} |
998
933670761a57
updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
954
diff
changeset
|
149 for k, indicator in self.indicators.items(): |
661
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
150 values[k] = indicator[instant] |
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
151 return values |
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
152 |
1146
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
153 def plot(self, options = '', withOrigin = False, timeStep = 1, withFeatures = False, restricted = True, **kwargs): |
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
154 if restricted: |
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
155 self.roadUser1.getObjectInTimeInterval(self.timeInterval).plot(options, withOrigin, timeStep, withFeatures, **kwargs) |
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
156 self.roadUser2.getObjectInTimeInterval(self.timeInterval).plot(options, withOrigin, timeStep, withFeatures, **kwargs) |
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
157 else: |
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
158 self.roadUser1.plot(options, withOrigin, timeStep, withFeatures, **kwargs) |
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
159 self.roadUser2.plot(options, withOrigin, timeStep, withFeatures, **kwargs) |
628
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
160 |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
161 def plotOnWorldImage(self, nPixelsPerUnitDistance, options = '', withOrigin = False, timeStep = 1, **kwargs): |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
162 self.roadUser1.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, **kwargs) |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
163 self.roadUser2.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, **kwargs) |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
164 |
1256
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
165 def plotIndicators(self, _indicatorNames = indicatorNames): |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
166 nrows = int(np.ceil(len(_indicatorNames)/2)) |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
167 ncols = 2 |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
168 #subplot(nrows, 2) |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
169 for i, indicatorName in enumerate(_indicatorNames): |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
170 if i==0: |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
171 ax = subplot(nrows, ncols, i+1) |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
172 else: |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
173 subplot(nrows, ncols, i+1, sharex = ax) |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
174 ind = self.getIndicator(indicatorName) |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
175 if ind is not None: |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
176 ind.plot() |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
177 ylabel(indicatorName) |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
178 |
887
e2452abba0e7
added option to compute PET in safety analysis script, and save in database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
882
diff
changeset
|
179 def play(self, videoFilename, homography = None, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., allUserInstants = False): |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
632
diff
changeset
|
180 if self.roadUser1 is not None and self.roadUser2 is not None: |
887
e2452abba0e7
added option to compute PET in safety analysis script, and save in database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
882
diff
changeset
|
181 if allUserInstants: |
e2452abba0e7
added option to compute PET in safety analysis script, and save in database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
882
diff
changeset
|
182 firstFrameNum = min(self.roadUser1.getFirstInstant(), self.roadUser2.getFirstInstant()) |
e2452abba0e7
added option to compute PET in safety analysis script, and save in database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
882
diff
changeset
|
183 lastFrameNum = max(self.roadUser1.getLastInstant(), self.roadUser2.getLastInstant()) |
e2452abba0e7
added option to compute PET in safety analysis script, and save in database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
882
diff
changeset
|
184 else: |
e2452abba0e7
added option to compute PET in safety analysis script, and save in database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
882
diff
changeset
|
185 firstFrameNum = self.getFirstInstant() |
e2452abba0e7
added option to compute PET in safety analysis script, and save in database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
882
diff
changeset
|
186 lastFrameNum = self.getLastInstant() |
e2452abba0e7
added option to compute PET in safety analysis script, and save in database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
882
diff
changeset
|
187 cvutils.displayTrajectories(videoFilename, [self.roadUser1, self.roadUser2], homography = homography, firstFrameNum = firstFrameNum, lastFrameNumArg = lastFrameNum, undistort = undistort, intrinsicCameraMatrix = intrinsicCameraMatrix, distortionCoefficients = distortionCoefficients, undistortedImageMultiplication = undistortedImageMultiplication) |
628
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
188 else: |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
189 print('Please set the interaction road user attributes roadUser1 and roadUser1 through the method setRoadUsers') |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
190 |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
191 def computeIndicators(self): |
1267
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
192 '''Computes all cinematic indicators but the expensive safety indicators (TTC, PET)''' |
1146
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
193 collisionCourseDotProducts = {} |
320
419f30491a4b
renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
317
diff
changeset
|
194 collisionCourseAngles = {} |
325
6c9c7c956926
added velocity angle in indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
195 velocityAngles = {} |
1146
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
196 distances = {} |
294
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
197 speedDifferentials = {} |
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
198 for instant in self.timeInterval: |
320
419f30491a4b
renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
317
diff
changeset
|
199 deltap = self.roadUser1.getPositionAtInstant(instant)-self.roadUser2.getPositionAtInstant(instant) |
325
6c9c7c956926
added velocity angle in indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
200 v1 = self.roadUser1.getVelocityAtInstant(instant) |
6c9c7c956926
added velocity angle in indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
201 v2 = self.roadUser2.getVelocityAtInstant(instant) |
6c9c7c956926
added velocity angle in indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
202 deltav = v2-v1 |
1150
14140b55e580
corrected issue with motion pattern for motion prediction for safety analysis (to few matches)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1149
diff
changeset
|
203 v1Norm = v1.norm2() |
14140b55e580
corrected issue with motion pattern for motion prediction for safety analysis (to few matches)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1149
diff
changeset
|
204 v2Norm = v2.norm2() |
14140b55e580
corrected issue with motion pattern for motion prediction for safety analysis (to few matches)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1149
diff
changeset
|
205 if v1Norm != 0. and v2Norm != 0.: |
1256
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
206 velocityAngles[instant] = np.arccos(max(-1, min(1, moving.Point.dot(v1, v2)/(v1Norm*v2Norm)))) |
299
7e5fb4abd070
renaming event to events and correcting errors in indicator computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
294
diff
changeset
|
207 collisionCourseDotProducts[instant] = moving.Point.dot(deltap, deltav) |
317
d280b881e860
added indicator min distance
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
310
diff
changeset
|
208 distances[instant] = deltap.norm2() |
294
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
209 speedDifferentials[instant] = deltav.norm2() |
662
72174e66aba5
corrected bug that increased TTC by 1 frame and structure to store collision points and crossing zones
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
661
diff
changeset
|
210 if distances[instant] != 0 and speedDifferentials[instant] != 0: |
1256
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1252
diff
changeset
|
211 collisionCourseAngles[instant] = np.arccos(max(-1, min(1, collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant])))) # avoid values slightly higher than 1.0 |
306
93d851d0d21e
bug correction, minor work on indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
299
diff
changeset
|
212 |
387
91679eb2ff2c
cleaning up safety analysis and the new traditional constant velocity method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
213 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[0], collisionCourseDotProducts)) |
91679eb2ff2c
cleaning up safety analysis and the new traditional constant velocity method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
214 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[1], collisionCourseAngles)) |
691
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
215 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[2], distances, mostSevereIsMax = False)) |
387
91679eb2ff2c
cleaning up safety analysis and the new traditional constant velocity method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
216 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[4], velocityAngles)) |
91679eb2ff2c
cleaning up safety analysis and the new traditional constant velocity method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
217 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[5], speedDifferentials)) |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
218 |
317
d280b881e860
added indicator min distance
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
310
diff
changeset
|
219 # if we have features, compute other indicators |
661
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
220 if self.roadUser1.hasFeatures() and self.roadUser2.hasFeatures(): |
691
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
221 minDistances={} |
317
d280b881e860
added indicator min distance
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
310
diff
changeset
|
222 for instant in self.timeInterval: |
691
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
223 minDistances[instant] = moving.MovingObject.minDistance(self.roadUser1, self.roadUser2, instant) |
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
224 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[3], minDistances, mostSevereIsMax = False)) |
317
d280b881e860
added indicator min distance
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
310
diff
changeset
|
225 |
1266
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
226 def categorize(self, velocityAngleTolerance, parallelAngleTolerance, headonCollisionCourseAngleTolerance = None, speedThreshold = 0.): |
1146
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
227 '''Computes the interaction category by instant |
1157
173b7926734e
clarifying categorize method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1151
diff
changeset
|
228 all 3 angle arguments in radian |
1267
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
229 velocityAngleTolerance: indicates the angle threshold for rear and head on (180-velocityAngleTolerance), |
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
230 as well as the maximum collision course angle for head on (if headonCollisionCourseAngleTolerance is None) |
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
231 parallelAngleTolerance: indicates the tolerance on the expected 90 deg angle |
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
232 between velocity vector (average for parallel) and position vector for a parallel interaction |
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
233 speedThreshold defines stationary users: a stationary interaction is between one moving and |
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
234 one stationary user, and their distance decreases |
1157
173b7926734e
clarifying categorize method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1151
diff
changeset
|
235 |
173b7926734e
clarifying categorize method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1151
diff
changeset
|
236 an instant may not be categorized if it matches the side definition (angle) |
173b7926734e
clarifying categorize method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1151
diff
changeset
|
237 but the distance is growing (at least one user is probably past the point of trajectory crossing)''' |
1267
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
238 minParallelAngleCosine = np.cos(np.pi/2+parallelAngleTolerance) |
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
239 maxParallelAngleCosine = np.cos(np.pi/2-parallelAngleTolerance) |
1149
392db62ea1da
major bug in categorization, added option for method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1148
diff
changeset
|
240 if headonCollisionCourseAngleTolerance is None: |
392db62ea1da
major bug in categorization, added option for method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1148
diff
changeset
|
241 headonCollisionCourseAngleTolerance = velocityAngleTolerance |
1266
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
242 speedThreshold2 = speedThreshold**2 |
1149
392db62ea1da
major bug in categorization, added option for method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1148
diff
changeset
|
243 |
1146
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
244 self.categories = {} |
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
245 collisionCourseDotProducts = self.getIndicator(Interaction.indicatorNames[0]) |
1148
eb88d2984637
corrected interaction classification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1147
diff
changeset
|
246 collisionCourseAngles = self.getIndicator(Interaction.indicatorNames[1]) |
1149
392db62ea1da
major bug in categorization, added option for method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1148
diff
changeset
|
247 distances = self.getIndicator(Interaction.indicatorNames[2]) |
1146
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
248 velocityAngles = self.getIndicator(Interaction.indicatorNames[4]) |
b219d5a1bb55
added code to categorize interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1144
diff
changeset
|
249 for instant in self.timeInterval: |
1267
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
250 stationaryUser1 = self.roadUser1.getVelocityAtInstant(instant).norm2Squared() <= speedThreshold2 |
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
251 stationaryUser2 = self.roadUser2.getVelocityAtInstant(instant).norm2Squared() <= speedThreshold2 |
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
252 if stationaryUser1 != stationaryUser2 and collisionCourseDotProducts[instant] > 0: # only one is not moving and is getting closer |
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
253 self.categories[instant] = Interaction.categories["stationary"] |
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
254 # true stationary would be all the times (parked, difficult without semantic knowledge of the scene |
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
255 # alternatively, one could get the previous or next non zero velocity to identify user orientation |
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
256 elif velocityAngles.existsAtInstant(instant): |
1266
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
257 if velocityAngles[instant] < velocityAngleTolerance: # parallel or rear end |
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
258 midVelocity = self.roadUser1.getVelocityAtInstant(instant) + self.roadUser2.getVelocityAtInstant(instant) |
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
259 deltap = self.roadUser1.getPositionAtInstant(instant)-self.roadUser2.getPositionAtInstant(instant) |
1267
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
260 if minParallelAngleCosine < abs(moving.Point.dot(midVelocity, deltap)/(midVelocity.norm2()*distances[instant])) < maxParallelAngleCosine: |
1266
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
261 self.categories[instant] = Interaction.categories["parallel"] |
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
262 else: |
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
263 self.categories[instant] = Interaction.categories["rearend"] |
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
264 elif velocityAngles[instant] > np.pi - velocityAngleTolerance and collisionCourseAngles[instant] < headonCollisionCourseAngleTolerance: # head on |
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
265 self.categories[instant] = Interaction.categories["headon"] |
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
266 elif collisionCourseDotProducts[instant] > 0: |
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
267 self.categories[instant] = Interaction.categories["side"] |
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
268 # true stationary is when object does not move for the whole period of the interaction, otherwise get last (or next) velocity vector for user orientation |
1267
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
269 # leaving is not a good interaction category (issue in Etienne's 2022 paper): |
ad60e5adf084
cleaned interaction categorization and added stationary category
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1266
diff
changeset
|
270 # means we are past the situation in which users are approaching |
1266
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
271 # could try to predict what happened before, but it's not observed |
ebb18043616e
work in progress on categorization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1263
diff
changeset
|
272 |
1144
6a8fe3ed3bc6
bug correction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1028
diff
changeset
|
273 |
1269
ca70a79688ae
adding a speed threshold to avoid computing TTC at very low speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1267
diff
changeset
|
274 def computeCrossingsCollisions(self, predictionParameters, collisionDistanceThreshold, timeHorizon, computeCZ = False, debug = False, timeInterval = None, speedThreshold = 0.): |
ca70a79688ae
adding a speed threshold to avoid computing TTC at very low speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1267
diff
changeset
|
275 '''Computes all crossing and collision points at each common instant for two road users. |
ca70a79688ae
adding a speed threshold to avoid computing TTC at very low speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1267
diff
changeset
|
276 |
ca70a79688ae
adding a speed threshold to avoid computing TTC at very low speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1267
diff
changeset
|
277 speedThreshold defines when users are stationary: TTC is not computed when both users are stationary''' |
338
f3aceea2afbb
first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
325
diff
changeset
|
278 TTCs = {} |
937
b67a784beb69
work started on prototype prediction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
887
diff
changeset
|
279 collisionProbabilities = {} |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
632
diff
changeset
|
280 if timeInterval is not None: |
338
f3aceea2afbb
first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
325
diff
changeset
|
281 commonTimeInterval = timeInterval |
317
d280b881e860
added indicator min distance
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
310
diff
changeset
|
282 else: |
338
f3aceea2afbb
first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
325
diff
changeset
|
283 commonTimeInterval = self.timeInterval |
1269
ca70a79688ae
adding a speed threshold to avoid computing TTC at very low speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1267
diff
changeset
|
284 self.collisionPoints, crossingZones = predictionParameters.computeCrossingsCollisions(self.roadUser1, self.roadUser2, collisionDistanceThreshold, timeHorizon, computeCZ, debug, commonTimeInterval, speedThreshold) |
998
933670761a57
updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
954
diff
changeset
|
285 for i, cps in self.collisionPoints.items(): |
937
b67a784beb69
work started on prototype prediction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
887
diff
changeset
|
286 TTCs[i] = prediction.SafetyPoint.computeExpectedIndicator(cps) |
b67a784beb69
work started on prototype prediction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
887
diff
changeset
|
287 collisionProbabilities[i] = sum([p.probability for p in cps]) |
692
9a258687af4c
corrected some errors for ttc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
691
diff
changeset
|
288 if len(TTCs) > 0: |
9a258687af4c
corrected some errors for ttc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
691
diff
changeset
|
289 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[7], TTCs, mostSevereIsMax=False)) |
937
b67a784beb69
work started on prototype prediction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
887
diff
changeset
|
290 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[6], collisionProbabilities)) |
1275
9f1711a85c56
added code to make sure TTC is replaced of recomputed and not having a value
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1273
diff
changeset
|
291 else: |
9f1711a85c56
added code to make sure TTC is replaced of recomputed and not having a value
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1273
diff
changeset
|
292 for i in [6,7]: |
9f1711a85c56
added code to make sure TTC is replaced of recomputed and not having a value
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1273
diff
changeset
|
293 self.resetIndicator(Interaction.indicatorNames[i]) |
338
f3aceea2afbb
first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
325
diff
changeset
|
294 |
662
72174e66aba5
corrected bug that increased TTC by 1 frame and structure to store collision points and crossing zones
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
661
diff
changeset
|
295 # crossing zones and pPET |
338
f3aceea2afbb
first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
325
diff
changeset
|
296 if computeCZ: |
705
0ceee3b1a96d
cleanup crossing collisions and crossing zones in Interaction (not stored per type of prediction method)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
695
diff
changeset
|
297 self.crossingZones = crossingZones |
338
f3aceea2afbb
first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
325
diff
changeset
|
298 pPETs = {} |
998
933670761a57
updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
954
diff
changeset
|
299 for i, cz in self.crossingZones.items(): |
610
0dc36203973d
remove dublicated code for collision/crossing computations
MohamedGomaa
parents:
607
diff
changeset
|
300 pPETs[i] = prediction.SafetyPoint.computeExpectedIndicator(cz) |
631
2d1d33ae1c69
major work on pPET and pet, issues remain for pPET computed with predicted trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
630
diff
changeset
|
301 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[9], pPETs, mostSevereIsMax=False)) |
662
72174e66aba5
corrected bug that increased TTC by 1 frame and structure to store collision points and crossing zones
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
661
diff
changeset
|
302 # TODO add probability of collision, and probability of successful evasive action |
631
2d1d33ae1c69
major work on pPET and pet, issues remain for pPET computed with predicted trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
630
diff
changeset
|
303 |
1252
fe35473acee3
adding method to compute PET using polygon for the outline of a vehicle (bird eye view of the vehicle)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1182
diff
changeset
|
304 def computePET(self, collisionDistanceThreshold, computePetWithBoundingPoly): |
fe35473acee3
adding method to compute PET using polygon for the outline of a vehicle (bird eye view of the vehicle)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1182
diff
changeset
|
305 'Warning: when computing PET from interactions, there could be PETs between objects that do not coexist and therefore are not considered interactions' |
fe35473acee3
adding method to compute PET using polygon for the outline of a vehicle (bird eye view of the vehicle)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1182
diff
changeset
|
306 pet, t1, t2= moving.MovingObject.computePET(self.roadUser1, self.roadUser2, collisionDistanceThreshold, computePetWithBoundingPoly) |
887
e2452abba0e7
added option to compute PET in safety analysis script, and save in database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
882
diff
changeset
|
307 if pet is not None: |
1263
39740c4668ac
tweak to record t1 and t2 in indicators when computing PET
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1256
diff
changeset
|
308 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[10], {min(t1, t2): pet, max(t1, t2): pet}, mostSevereIsMax = False)) |
1275
9f1711a85c56
added code to make sure TTC is replaced of recomputed and not having a value
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1273
diff
changeset
|
309 else: |
9f1711a85c56
added code to make sure TTC is replaced of recomputed and not having a value
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1273
diff
changeset
|
310 self.resetIndicator(Interaction.indicatorNames[10]) |
619
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
613
diff
changeset
|
311 |
664
455f9b93819c
added capability to set a videofilename to movingobject and interaction, renames interactiontype to collision in interaction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
662
diff
changeset
|
312 def setCollision(self, collision): |
455f9b93819c
added capability to set a videofilename to movingobject and interaction, renames interactiontype to collision in interaction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
662
diff
changeset
|
313 '''indicates if it is a collision: argument should be boolean''' |
455f9b93819c
added capability to set a videofilename to movingobject and interaction, renames interactiontype to collision in interaction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
662
diff
changeset
|
314 self.collision = collision |
310
f7ca78a11ea6
add functions to add videofilename and interaction type in Interaction class
Mohamed Gomaa
parents:
306
diff
changeset
|
315 |
664
455f9b93819c
added capability to set a videofilename to movingobject and interaction, renames interactiontype to collision in interaction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
662
diff
changeset
|
316 def isCollision(self): |
455f9b93819c
added capability to set a videofilename to movingobject and interaction, renames interactiontype to collision in interaction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
662
diff
changeset
|
317 if hasattr(self, 'collision'): |
455f9b93819c
added capability to set a videofilename to movingobject and interaction, renames interactiontype to collision in interaction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
662
diff
changeset
|
318 return self.collision |
455f9b93819c
added capability to set a videofilename to movingobject and interaction, renames interactiontype to collision in interaction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
662
diff
changeset
|
319 else: |
455f9b93819c
added capability to set a videofilename to movingobject and interaction, renames interactiontype to collision in interaction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
662
diff
changeset
|
320 return None |
661
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
321 |
705
0ceee3b1a96d
cleanup crossing collisions and crossing zones in Interaction (not stored per type of prediction method)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
695
diff
changeset
|
322 def getCollisionPoints(self): |
0ceee3b1a96d
cleanup crossing collisions and crossing zones in Interaction (not stored per type of prediction method)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
695
diff
changeset
|
323 return self.collisionPoints |
661
dc70d9e711f5
some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
324 |
705
0ceee3b1a96d
cleanup crossing collisions and crossing zones in Interaction (not stored per type of prediction method)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
695
diff
changeset
|
325 def getCrossingZones(self): |
0ceee3b1a96d
cleanup crossing collisions and crossing zones in Interaction (not stored per type of prediction method)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
695
diff
changeset
|
326 return self.crossingZones |
451
cd342a774806
Point/CurvilinearTrajectory/Interaction utiles
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
436
diff
changeset
|
327 |
1151
658f87232536
extended creation of interactions to non simultaneous objects for PET calculations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1150
diff
changeset
|
328 def createInteractions(objects, _others = None, maxDurationApart = 0): |
487
e04b22ce2fcd
generalized createInteractions to 2 lists of objects (for cars and pedestrians for example)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
454
diff
changeset
|
329 '''Create all interactions of two co-existing road users''' |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
632
diff
changeset
|
330 if _others is not None: |
487
e04b22ce2fcd
generalized createInteractions to 2 lists of objects (for cars and pedestrians for example)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
454
diff
changeset
|
331 others = _others |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
332 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
333 interactions = [] |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
334 num = 0 |
998
933670761a57
updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
954
diff
changeset
|
335 for i in range(len(objects)): |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
632
diff
changeset
|
336 if _others is None: |
487
e04b22ce2fcd
generalized createInteractions to 2 lists of objects (for cars and pedestrians for example)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
454
diff
changeset
|
337 others = objects[:i] |
998
933670761a57
updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
954
diff
changeset
|
338 for j in range(len(others)): |
489
000bddf84ad0
corrected bugs in safety analysis
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
487
diff
changeset
|
339 commonTimeInterval = objects[i].commonTimeInterval(others[j]) |
1151
658f87232536
extended creation of interactions to non simultaneous objects for PET calculations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1150
diff
changeset
|
340 if not commonTimeInterval.empty() or (maxDurationApart > 0 and objects[i].getTimeInterval().distance(objects[j].getTimeInterval()) < maxDurationApart): |
489
000bddf84ad0
corrected bugs in safety analysis
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
487
diff
changeset
|
341 interactions.append(Interaction(num, commonTimeInterval, objects[i].num, others[j].num, objects[i], others[j])) |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
342 num += 1 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
343 return interactions |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
344 |
628
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
345 def findInteraction(interactions, roadUserNum1, roadUserNum2): |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
346 'Returns the right interaction in the set' |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
347 i=0 |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
348 while i<len(interactions) and set([roadUserNum1, roadUserNum2]) != interactions[i].getRoadUserNumbers(): |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
349 i+=1 |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
350 if i<len(interactions): |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
351 return interactions[i] |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
352 else: |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
353 return None |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
627
diff
changeset
|
354 |
1252
fe35473acee3
adding method to compute PET using polygon for the outline of a vehicle (bird eye view of the vehicle)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1182
diff
changeset
|
355 def computeIndicators(interactions, computeMotionPrediction, computePET, predictionParameters, collisionDistanceThreshold, computePetWithBoundingPoly, timeHorizon, computeCZ = False, debug = False, timeInterval = None): |
949
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
943
diff
changeset
|
356 for inter in interactions: |
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
943
diff
changeset
|
357 print('processing interaction {}'.format(inter.getNum())) # logging.debug('processing interaction {}'.format(inter.getNum())) |
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
943
diff
changeset
|
358 inter.computeIndicators() |
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
943
diff
changeset
|
359 if computeMotionPrediction: |
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
943
diff
changeset
|
360 inter.computeCrossingsCollisions(predictionParameters, collisionDistanceThreshold, timeHorizon, computeCZ, debug, timeInterval) |
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
943
diff
changeset
|
361 if computePET: |
1252
fe35473acee3
adding method to compute PET using polygon for the outline of a vehicle (bird eye view of the vehicle)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1182
diff
changeset
|
362 inter.computePET(collisionDistanceThreshold, computePetWithBoundingPoly) |
949
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
943
diff
changeset
|
363 return interactions |
d6c1c05d11f5
modified multithreading at the interaction level for safety computations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
943
diff
changeset
|
364 |
705
0ceee3b1a96d
cleanup crossing collisions and crossing zones in Interaction (not stored per type of prediction method)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
695
diff
changeset
|
365 def aggregateSafetyPoints(interactions, pointType = 'collision'): |
632
2f1a583bfd20
added utility for safety points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
631
diff
changeset
|
366 '''Put all collision points or crossing zones in a list for display''' |
2f1a583bfd20
added utility for safety points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
631
diff
changeset
|
367 allPoints = [] |
2f1a583bfd20
added utility for safety points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
631
diff
changeset
|
368 if pointType == 'collision': |
2f1a583bfd20
added utility for safety points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
631
diff
changeset
|
369 for i in interactions: |
705
0ceee3b1a96d
cleanup crossing collisions and crossing zones in Interaction (not stored per type of prediction method)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
695
diff
changeset
|
370 for points in i.collisionPoints.values(): |
632
2f1a583bfd20
added utility for safety points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
631
diff
changeset
|
371 allPoints += points |
2f1a583bfd20
added utility for safety points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
631
diff
changeset
|
372 elif pointType == 'crossing': |
2f1a583bfd20
added utility for safety points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
631
diff
changeset
|
373 for i in interactions: |
705
0ceee3b1a96d
cleanup crossing collisions and crossing zones in Interaction (not stored per type of prediction method)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
695
diff
changeset
|
374 for points in i.crossingZones.values(): |
632
2f1a583bfd20
added utility for safety points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
631
diff
changeset
|
375 allPoints += points |
2f1a583bfd20
added utility for safety points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
631
diff
changeset
|
376 else: |
705
0ceee3b1a96d
cleanup crossing collisions and crossing zones in Interaction (not stored per type of prediction method)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
695
diff
changeset
|
377 print('unknown type of point: '+pointType) |
632
2f1a583bfd20
added utility for safety points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
631
diff
changeset
|
378 return allPoints |
2f1a583bfd20
added utility for safety points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
631
diff
changeset
|
379 |
735
0e875a7f5759
modified prototypeCluster algorithm to enforce similarity when re-assigning and to compute only the necessary similarities
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
731
diff
changeset
|
380 def prototypeCluster(interactions, similarities, indicatorName, minSimilarity, similarityFunc = None, minClusterSize = None, randomInitialization = False): |
0e875a7f5759
modified prototypeCluster algorithm to enforce similarity when re-assigning and to compute only the necessary similarities
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
731
diff
changeset
|
381 return ml.prototypeCluster([inter.getIndicator(indicatorName) for inter in interactions], similarities, minSimilarity, similarityFunc, minClusterSize, randomInitialization) |
291
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
382 |
59
f955e83da499
developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
56
diff
changeset
|
383 class Crossing(moving.STObject): |
56
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
384 '''Class for the event of a street crossing |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
385 |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
386 TODO: detecter passage sur la chaussee |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
387 identifier origines et destination (ou uniquement chaussee dans FOV) |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
388 carac traversee |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
389 detecter proximite veh (retirer si trop similaire simultanement |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
390 carac interaction''' |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
391 |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
392 def __init__(self, roaduserNum = None, num = None, timeInterval = None): |
59
f955e83da499
developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
56
diff
changeset
|
393 moving.STObject.__init__(self, num, timeInterval) |
56
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
394 self.roaduserNum = roaduserNum |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
395 |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
396 |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
397 |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
398 if __name__ == "__main__": |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
399 import doctest |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
400 import unittest |
489
000bddf84ad0
corrected bugs in safety analysis
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
487
diff
changeset
|
401 suite = doctest.DocFileSuite('tests/events.txt') |
000bddf84ad0
corrected bugs in safety analysis
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
487
diff
changeset
|
402 #suite = doctest.DocTestSuite() |
56
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
403 unittest.TextTestRunner().run(suite) |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
404 |