comparison trafficintelligence/moving.py @ 1281:8915747a4fc8

adding a method to detect stationary objects (or sub time intervals)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 08 Jul 2024 16:42:30 -0400
parents 20a5e1292321
children 106365257da9
comparison
equal deleted inserted replaced
1280:2abeccdbb985 1281:8915747a4fc8
2 '''Libraries for moving objects, trajectories...''' 2 '''Libraries for moving objects, trajectories...'''
3 3
4 import copy 4 import copy
5 from math import sqrt, atan2, cos, sin, inf 5 from math import sqrt, atan2, cos, sin, inf
6 6
7 from numpy import median, mean, array, arange, zeros, ones, hypot, NaN, std, floor, ceil, float32, argwhere, minimum, issubdtype, integer as npinteger, percentile, full 7 from numpy import median, mean, array, arange, zeros, ones, hypot, NaN, std, floor, ceil, float32, argwhere, flatnonzero, minimum, issubdtype, integer as npinteger, percentile, quantile, full
8 from matplotlib.pyplot import plot, text, arrow 8 from matplotlib.pyplot import plot, text, arrow
9 from scipy.spatial.distance import cdist 9 from scipy.spatial.distance import cdist
10 from scipy.signal import savgol_filter 10 from scipy.signal import savgol_filter
11 11
12 try: 12 try:
1833 return self.positions.getXCoordinates() 1833 return self.positions.getXCoordinates()
1834 1834
1835 def getYCoordinates(self): 1835 def getYCoordinates(self):
1836 return self.positions.getYCoordinates() 1836 return self.positions.getYCoordinates()
1837 1837
1838 def isStationary(self, speedThreshold, distanceThreshold):
1839 '''Indicates if object is not moving
1840 if speed on average below threshold and final-initial position close enough
1841
1842 TODO: returns time interval stationary if starts moving or stops moving'''
1843 speeds = self.getSpeeds()
1844 if quantile(speeds, 0.5) <= speedThreshold and Point.distanceNorm2(self.getPositionAt(0),self.getPositionAt(-1)) <= distanceThreshold:
1845 return True, None
1846 else:
1847 indices = flatnonzero(speeds < speedThreshold).tolist()
1848 if len(indices) >= 2:
1849 i = 0
1850 j = len(indices)-1
1851 #incrementI = True
1852 while i<j and (quantile(speeds[indices[i]:indices[j]+1], 0.5) > speedThreshold or Point.distanceNorm2(self.getPositionAt(indices[i]),self.getPositionAt(indices[j])) > distanceThreshold):
1853 #if incrementI:
1854 i+=1
1855 #else:
1856 j-=1
1857 #incrementI = not incrementI
1858 if i<j: # we found a smaller subset
1859 return True, [indices[i], indices[j]]
1860 else:
1861 return False, None
1862 else:
1863 return False, None
1864
1838 def setStationary(self): 1865 def setStationary(self):
1839 '''Resets the position to the mean of existing positions and sets speeds to 0 1866 '''Resets the position to the mean of existing positions and sets speeds to 0
1840 And does the same to the features 1867 And does the same to the features
1841 TODO: other options (provide x, y, what to do with features?)''' 1868 TODO: other options (provide x, y, what to do with features?)'''
1842 meanPosition = self.positions.agg(mean) 1869 meanPosition = self.positions.agg(mean)