Mercurial Hosting > traffic-intelligence
comparison python/events.py @ 628:977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 16 Feb 2015 11:58:51 +0100 |
parents | 82e9f78a4714 |
children | 0a5e89d6fc62 |
comparison
equal
deleted
inserted
replaced
627:82e9f78a4714 | 628:977407c9f815 |
---|---|
6 from numpy import arccos | 6 from numpy import arccos |
7 | 7 |
8 import multiprocessing | 8 import multiprocessing |
9 import itertools | 9 import itertools |
10 | 10 |
11 import moving, prediction, indicators, utils | 11 import moving, prediction, indicators, utils, cvutils |
12 __metaclass__ = type | 12 __metaclass__ = type |
13 | 13 |
14 def findRoute(prototypes,objects,i,j,noiseEntryNums,noiseExitNums,minSimilarity= 0.3, spatialThreshold=1.0, delta=180): | 14 def findRoute(prototypes,objects,i,j,noiseEntryNums,noiseExitNums,minSimilarity= 0.3, spatialThreshold=1.0, delta=180): |
15 if i[0] not in noiseEntryNums: | 15 if i[0] not in noiseEntryNums: |
16 prototypesRoutes= [ x for x in sorted(prototypes.keys()) if i[0]==x[0]] | 16 prototypesRoutes= [ x for x in sorted(prototypes.keys()) if i[0]==x[0]] |
59 'Distance', | 59 'Distance', |
60 'Minimum Distance', | 60 'Minimum Distance', |
61 'Velocity Angle', | 61 'Velocity Angle', |
62 'Speed Differential', | 62 'Speed Differential', |
63 'Collision Probability', | 63 'Collision Probability', |
64 'Time to Collision', | 64 'Time to Collision', # 7 |
65 'Probability of Successful Evasive Action', | 65 'Probability of Successful Evasive Action', |
66 'predicted Post Encroachment Time'] | 66 'predicted Post Encroachment Time'] |
67 | 67 |
68 indicatorNameToIndices = utils.inverseEnumeration(indicatorNames) | 68 indicatorNameToIndices = utils.inverseEnumeration(indicatorNames) |
69 | 69 |
106 self.interactionInterval = None | 106 self.interactionInterval = None |
107 | 107 |
108 def getRoadUserNumbers(self): | 108 def getRoadUserNumbers(self): |
109 return self.roadUserNumbers | 109 return self.roadUserNumbers |
110 | 110 |
111 def setRoadUsers(self, objects): | |
112 nums = list(self.getRoadUserNumbers()) | |
113 if objects[nums[0]].getNum() == nums[0]: | |
114 self.roadUser1 = objects[nums[0]] | |
115 if objects[nums[1]].getNum() == nums[1]: | |
116 self.roadUser2 = objects[nums[1]] | |
117 | |
118 i = 0 | |
119 while i < len(objects) and self.roadUser2 == None: | |
120 if objects[i].getNum() in nums: | |
121 if self.roadUser1 == None: | |
122 self.roadUser1 = objects[i] | |
123 else: | |
124 self.roadUser2 = objects[i] | |
125 i += 1 | |
126 | |
111 def getIndicator(self, indicatorName): | 127 def getIndicator(self, indicatorName): |
112 return self.indicators.get(indicatorName, None) | 128 return self.indicators.get(indicatorName, None) |
113 | 129 |
114 def addIndicator(self, indicator): | 130 def addIndicator(self, indicator): |
115 if indicator: | 131 if indicator: |
116 self.indicators[indicator.name] = indicator | 132 self.indicators[indicator.name] = indicator |
133 | |
134 def plot(self, options = '', withOrigin = False, timeStep = 1, withFeatures = False, **kwargs): | |
135 self.roadUser1.plot(options, withOrigin, timeStep, withFeatures, **kwargs) | |
136 self.roadUser2.plot(options, withOrigin, timeStep, withFeatures, **kwargs) | |
137 | |
138 def plotOnWorldImage(self, nPixelsPerUnitDistance, options = '', withOrigin = False, timeStep = 1, **kwargs): | |
139 self.roadUser1.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, **kwargs) | |
140 self.roadUser2.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, **kwargs) | |
141 | |
142 def play(self, videoFilename, homography = None, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1.): | |
143 if self.roadUser1 != None and self.roadUser2 != None: | |
144 cvutils.displayTrajectories(videoFilename, [self.roadUser1, self.roadUser2], homography = homography, firstFrameNum = self.getFirstInstant(), lastFrameNumArg = self.getLastInstant(), undistort = undistort, intrinsicCameraMatrix = intrinsicCameraMatrix, distortionCoefficients = distortionCoefficients, undistortedImageMultiplication = undistortedImageMultiplication) | |
145 else: | |
146 print('Please set the interaction road user attributes roadUser1 and roadUser1 through the method setRoadUsers') | |
117 | 147 |
118 def computeIndicators(self): | 148 def computeIndicators(self): |
119 '''Computes the collision course cosine only if the cosine is positive''' | 149 '''Computes the collision course cosine only if the cosine is positive''' |
120 collisionCourseDotProducts = {}#[0]*int(self.timeInterval.length()) | 150 collisionCourseDotProducts = {}#[0]*int(self.timeInterval.length()) |
121 collisionCourseAngles = {} | 151 collisionCourseAngles = {} |
199 commonTimeInterval = objects[i].commonTimeInterval(others[j]) | 229 commonTimeInterval = objects[i].commonTimeInterval(others[j]) |
200 if not commonTimeInterval.empty(): | 230 if not commonTimeInterval.empty(): |
201 interactions.append(Interaction(num, commonTimeInterval, objects[i].num, others[j].num, objects[i], others[j])) | 231 interactions.append(Interaction(num, commonTimeInterval, objects[i].num, others[j].num, objects[i], others[j])) |
202 num += 1 | 232 num += 1 |
203 return interactions | 233 return interactions |
234 | |
235 def findInteraction(interactions, roadUserNum1, roadUserNum2): | |
236 'Returns the right interaction in the set' | |
237 i=0 | |
238 while i<len(interactions) and set([roadUserNum1, roadUserNum2]) != interactions[i].getRoadUserNumbers(): | |
239 i+=1 | |
240 if i<len(interactions): | |
241 return interactions[i] | |
242 else: | |
243 return None | |
204 | 244 |
205 def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity): | 245 def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity): |
206 '''Finds exemplar indicator time series for all interactions | 246 '''Finds exemplar indicator time series for all interactions |
207 Returns the prototype indices (in the interaction list) and the label of each indicator (interaction) | 247 Returns the prototype indices (in the interaction list) and the label of each indicator (interaction) |
208 | 248 |