Mercurial > hg > nsaunier > traffic-intelligence
comparison trafficintelligence/events.py @ 1256:56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 03 Apr 2024 14:41:20 -0400 |
| parents | fe35473acee3 |
| children | 39740c4668ac |
comparison
equal
deleted
inserted
replaced
| 1255:c0fe55a6b82f | 1256:56d0195d043e |
|---|---|
| 4 | 4 |
| 5 from trafficintelligence import moving, prediction, indicators, utils, cvutils, ml | 5 from trafficintelligence import moving, prediction, indicators, utils, cvutils, ml |
| 6 from trafficintelligence.base import VideoFilenameAddable | 6 from trafficintelligence.base import VideoFilenameAddable |
| 7 | 7 |
| 8 import numpy as np | 8 import numpy as np |
| 9 from matplotlib.pyplot import subplot, figure, ylabel | |
| 9 | 10 |
| 10 import multiprocessing | 11 import multiprocessing |
| 11 import itertools, logging | 12 import itertools, logging |
| 12 | 13 |
| 13 | 14 |
| 166 | 167 |
| 167 def plotOnWorldImage(self, nPixelsPerUnitDistance, options = '', withOrigin = False, timeStep = 1, **kwargs): | 168 def plotOnWorldImage(self, nPixelsPerUnitDistance, options = '', withOrigin = False, timeStep = 1, **kwargs): |
| 168 self.roadUser1.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, **kwargs) | 169 self.roadUser1.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, **kwargs) |
| 169 self.roadUser2.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, **kwargs) | 170 self.roadUser2.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, **kwargs) |
| 170 | 171 |
| 172 def plotIndicators(self, _indicatorNames = indicatorNames): | |
| 173 nrows = int(np.ceil(len(_indicatorNames)/2)) | |
| 174 ncols = 2 | |
| 175 #subplot(nrows, 2) | |
| 176 for i, indicatorName in enumerate(_indicatorNames): | |
| 177 if i==0: | |
| 178 ax = subplot(nrows, ncols, i+1) | |
| 179 else: | |
| 180 subplot(nrows, ncols, i+1, sharex = ax) | |
| 181 ind = self.getIndicator(indicatorName) | |
| 182 if ind is not None: | |
| 183 ind.plot() | |
| 184 ylabel(indicatorName) | |
| 185 | |
| 171 def play(self, videoFilename, homography = None, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., allUserInstants = False): | 186 def play(self, videoFilename, homography = None, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., allUserInstants = False): |
| 172 if self.roadUser1 is not None and self.roadUser2 is not None: | 187 if self.roadUser1 is not None and self.roadUser2 is not None: |
| 173 if allUserInstants: | 188 if allUserInstants: |
| 174 firstFrameNum = min(self.roadUser1.getFirstInstant(), self.roadUser2.getFirstInstant()) | 189 firstFrameNum = min(self.roadUser1.getFirstInstant(), self.roadUser2.getFirstInstant()) |
| 175 lastFrameNum = max(self.roadUser1.getLastInstant(), self.roadUser2.getLastInstant()) | 190 lastFrameNum = max(self.roadUser1.getLastInstant(), self.roadUser2.getLastInstant()) |
| 194 v2 = self.roadUser2.getVelocityAtInstant(instant) | 209 v2 = self.roadUser2.getVelocityAtInstant(instant) |
| 195 deltav = v2-v1 | 210 deltav = v2-v1 |
| 196 v1Norm = v1.norm2() | 211 v1Norm = v1.norm2() |
| 197 v2Norm = v2.norm2() | 212 v2Norm = v2.norm2() |
| 198 if v1Norm != 0. and v2Norm != 0.: | 213 if v1Norm != 0. and v2Norm != 0.: |
| 199 velocityAngles[instant] = np.arccos(moving.Point.dot(v1, v2)/(v1Norm*v2Norm)) | 214 velocityAngles[instant] = np.arccos(max(-1, min(1, moving.Point.dot(v1, v2)/(v1Norm*v2Norm)))) |
| 200 collisionCourseDotProducts[instant] = moving.Point.dot(deltap, deltav) | 215 collisionCourseDotProducts[instant] = moving.Point.dot(deltap, deltav) |
| 201 distances[instant] = deltap.norm2() | 216 distances[instant] = deltap.norm2() |
| 202 speedDifferentials[instant] = deltav.norm2() | 217 speedDifferentials[instant] = deltav.norm2() |
| 203 if collisionCourseDotProducts[instant] > 0: | 218 if collisionCourseDotProducts[instant] > 0: |
| 204 interactionInstants.append(instant) | 219 interactionInstants.append(instant) |
| 205 if distances[instant] != 0 and speedDifferentials[instant] != 0: | 220 if distances[instant] != 0 and speedDifferentials[instant] != 0: |
| 206 collisionCourseAngles[instant] = np.arccos(collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant])) | 221 collisionCourseAngles[instant] = np.arccos(max(-1, min(1, collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant])))) # avoid values slightly higher than 1.0 |
| 207 | 222 |
| 208 if len(interactionInstants) >= 2: | 223 if len(interactionInstants) >= 2: |
| 209 self.interactionInterval = moving.TimeInterval(interactionInstants[0], interactionInstants[-1]) | 224 self.interactionInterval = moving.TimeInterval(interactionInstants[0], interactionInstants[-1]) |
| 210 else: | 225 else: |
| 211 self.interactionInterval = moving.TimeInterval() | 226 self.interactionInterval = moving.TimeInterval() |
