|
15 | 15 | import time |
16 | 16 | import os |
17 | 17 | import yaml |
18 | | - |
| 18 | +import json |
| 19 | +import matplotlib.pyplot as plt |
19 | 20 |
|
20 | 21 | class ConeDetector3D(Component): |
21 | 22 | """ |
@@ -94,6 +95,9 @@ def __init__( |
94 | 95 | self.undistort_map2 = None |
95 | 96 | self.camera_front = (camera_name=='front') |
96 | 97 |
|
| 98 | + # Logger |
| 99 | + self.detection_log = [] |
| 100 | + |
97 | 101 | def rate(self) -> float: |
98 | 102 | return 8 |
99 | 103 |
|
@@ -432,6 +436,45 @@ def update(self, vehicle: VehicleState) -> Dict[str, AgentState]: |
432 | 436 | end = time.time() |
433 | 437 | # print('-------processing time', end -start) |
434 | 438 | 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() |
435 | 478 |
|
436 | 479 | def save_sensor_data(self, vehicle: VehicleState, latest_image) -> None: |
437 | 480 | os.makedirs("data", exist_ok=True) |
|
0 commit comments