Skip to content

Commit 139354e

Browse files
dh-agarwalalo-20
authored andcommitted
cone logging logic?
1 parent 350d815 commit 139354e

1 file changed

Lines changed: 44 additions & 1 deletion

File tree

GEMstack/onboard/perception/cone_detection.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import time
1616
import os
1717
import yaml
18-
18+
import json
19+
import matplotlib.pyplot as plt
1920

2021
class ConeDetector3D(Component):
2122
"""
@@ -94,6 +95,9 @@ def __init__(
9495
self.undistort_map2 = None
9596
self.camera_front = (camera_name=='front')
9697

98+
# Logger
99+
self.detection_log = []
100+
97101
def rate(self) -> float:
98102
return 8
99103

@@ -432,6 +436,45 @@ def update(self, vehicle: VehicleState) -> Dict[str, AgentState]:
432436
end = time.time()
433437
# print('-------processing time', end -start)
434438
return self.tracked_agents
439+
440+
def log_cone_data_to_file(self, agents_dict, step_n, save_path="cone_log.json"):
441+
for agent_id, agent in agents_dict.items():
442+
self.detection_log.append({
443+
'n': step_n,
444+
'id': agent_id,
445+
'x': agent.pose.x,
446+
'y': agent.pose.y,
447+
'orientation': str(agent.activity)
448+
})
449+
with open(save_path, "w") as f:
450+
json.dump(self.detection_log, f, indent=2)
451+
452+
def visualize_cone_positions(self, log_path="cone_log.json"):
453+
with open(log_path, "r") as f:
454+
data = json.load(f)
455+
456+
xs, ys, colors = [], [], []
457+
for entry in data:
458+
xs.append(entry["x"])
459+
ys.append(entry["y"])
460+
orientation = entry["orientation"]
461+
if orientation == "AgentActivityEnum.RIGHT":
462+
colors.append("green")
463+
elif orientation == "AgentActivityEnum.LEFT":
464+
colors.append("red")
465+
elif orientation == "AgentActivityEnum.STANDING":
466+
colors.append("blue")
467+
else:
468+
colors.append("gray")
469+
470+
plt.figure(figsize=(8, 6))
471+
plt.scatter(xs, ys, c=colors, label="Cones")
472+
plt.xlabel("X Position (m)")
473+
plt.ylabel("Y Position (m)")
474+
plt.title("Cone Positions Over Time")
475+
plt.grid(True)
476+
plt.axis("equal")
477+
plt.show()
435478

436479
def save_sensor_data(self, vehicle: VehicleState, latest_image) -> None:
437480
os.makedirs("data", exist_ok=True)

0 commit comments

Comments
 (0)