diff trafficintelligence/storage.py @ 1272:785c86013d2c

added moving average smoothing for kitti loading
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 17 Jun 2024 22:49:22 -0400
parents 0f5bebd62a55
children 8e61ff3cd503
line wrap: on
line diff
--- a/trafficintelligence/storage.py	Fri Jun 14 15:56:01 2024 -0400
+++ b/trafficintelligence/storage.py	Mon Jun 17 22:49:22 2024 -0400
@@ -1312,13 +1312,14 @@
 # from https://github.com/utiasSTARS/pykitti/blob/master/pykitti/utils.py
 
 
-def loadTrajectoriesFromKITTI(filename, kittiCalibration = None, oxts = None, resultFile = False):
+def loadTrajectoriesFromKITTI(filename, kittiCalibration = None, oxts = None, resultFile = False, halfWidth = None):
     '''Reads data from KITTI ground truth or output from an object detection and tracking method
 
     kittiCalibration is obtained from loading training/testing calibration file for each sequence 00XX.txt
     oxts is obtained using utils.load_oxts_packets_and_poses(['./training/oxts/0001.txt']) from pykitti
+    Ref: https://github.com/pratikac/kitti/blob/master/readme.tracking.txt
 
-    Ref: https://github.com/pratikac/kitti/blob/master/readme.tracking.txt'''
+    non-null halfWidth indicates positions are smoothed using moving window avefraging'''
     from pykitti.utils import roty, rotz
     from trafficintelligence.cvutils import cartesian2Homogeneous
 
@@ -1431,6 +1432,9 @@
                 featureTrajectories[j].addPositionXY(xCoords[j], yCoords[j])
             # check https://docs.opencv.org/3.4/d9/d0c/group__calib3d.html#ga1019495a2c8d1743ed5cc23fa0daff8c
         if interval.length()>1:
+            if halfWidth is not None:
+                for i in range(4):
+                    featureTrajectories[i] = featureTrajectories[i].filterMovingWindow(halfWidth)
             objects.append(moving.MovingObject(num = objNum, timeInterval = interval, positions = t, velocities = t.differentiate(True), userType = userType, features = [moving.MovingObject(num = featureNum+i, timeInterval = copy(interval), positions = featureTrajectories[i], velocities = featureTrajectories[i].differentiate(True)) for i in range(4)]))
             featureNum += 4
     
@@ -1470,17 +1474,22 @@
             for i in range(4):
                 featureTrajectories[i].addPositionXY(xCoords[i], yCoords[i])                
                 # check https://docs.opencv.org/3.4/d9/d0c/group__calib3d.html#ga1019495a2c8d1743ed5cc23fa0daff8c        
+        if halfWidth is not None:
+            for i in range(4):
+                featureTrajectories[i] = featureTrajectories[i].filterMovingWindow(halfWidth)
         objects.append(moving.MovingObject(num = max([o.getNum() for o in objects])+1, timeInterval = interval, positions = t, velocities = t.differentiate(True), userType = 'Car', features = [moving.MovingObject(num = featureNum+i, timeInterval = copy(interval), positions = featureTrajectories[i], velocities = featureTrajectories[i].differentiate(True)) for i in range(4)]))
 
     return objects
 
-def loadTrajectoriesFromKITTI2D(filename, kittiCalibration = None, oxts = None):
+def loadTrajectoriesFromKITTI2D(filename, kittiCalibration = None, oxts = None, halfWidth = None):
     '''Loads x,y coordinate series
     e.g. obtained by projecting from image to ground world plane using a homography
     Format: frame_id,track_id,usertype,x,y,score 
 
     if oxts is not None, it is obtained using utils.load_oxts_packets_and_poses(['./training/oxts/0001.txt']) from pykitti
-    to generate the movement of the ego vehicle'''
+    to generate the movement of the ego vehicle
+
+    non-null halfWidth indicates positions are smoothed using moving window avefraging'''
 
     header = ['frame','trackingid','usertype','x','y','score']
     data = read_csv(filename, delimiter=' ', names = header)
@@ -1497,6 +1506,8 @@
             #print(tmp.info())
         if interval.length()>1:
             t = moving.Trajectory([tmp.x.to_list(), tmp.y.to_list()])
+            if halfWidth is not None:
+                t = t.filterMovingWindow(halfWidth)
             objects.append(moving.MovingObject(num = objNum, timeInterval = interval, positions = t, velocities = t.differentiate(True), userType = userType))
         
     # ego vehicle
@@ -1526,6 +1537,8 @@
             xCoords = worldCorners[:4,0]
             yCoords = worldCorners[:4,1]
             t.addPositionXY(xCoords.mean(), yCoords.mean())
+        if halfWidth is not None:
+            t = t.filterMovingWindow(halfWidth)
         objects.append(moving.MovingObject(num = max([o.getNum() for o in objects])+1, timeInterval = interval, positions = t, velocities = t.differentiate(True), userType = 'Car'))
     return objects