|
| 1 | +import json |
| 2 | +import os |
| 3 | +import time |
| 4 | +from klampt import vis |
| 5 | +from klampt.model.trajectory import Trajectory |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +from ..onboard.visualization.klampt_visualization import KlamptVisualization |
| 8 | +from ..onboard.visualization.mpl_visualization import MPLVisualization |
| 9 | + |
| 10 | +LOG_DIR = "logs" |
| 11 | +BEHAVIOR_FILE = "behavior.json" |
| 12 | + |
| 13 | +def select_log_folder(): |
| 14 | + log_folders = [f for f in os.listdir(LOG_DIR) if os.path.isdir(os.path.join(LOG_DIR, f))] |
| 15 | + if not log_folders: |
| 16 | + print("\033[91mNo log folders found.\033[0m") |
| 17 | + return None |
| 18 | + |
| 19 | + print("\n\033[94mAvailable log folders:\033[0m") |
| 20 | + for idx, folder in enumerate(log_folders): |
| 21 | + print(f"{idx + 1}. {folder}") |
| 22 | + |
| 23 | + while True: |
| 24 | + try: |
| 25 | + choice = int(input("\n\033[93mEnter the number of the log folder to use:\033[0m ")) - 1 |
| 26 | + if 0 <= choice < len(log_folders): |
| 27 | + return os.path.join(LOG_DIR, log_folders[choice]) |
| 28 | + print("\033[91mInvalid selection. Please enter a valid number.\033[0m") |
| 29 | + except ValueError: |
| 30 | + print("\033[91mPlease enter a number.\033[0m") |
| 31 | + |
| 32 | +def select_visualizer(): |
| 33 | + print("\n\033[94mChoose a visualization method:\033[0m") |
| 34 | + print("1. Klampt (3D visualization)") |
| 35 | + print("2. MPL (Matplotlib 2D visualization)") |
| 36 | + |
| 37 | + while True: |
| 38 | + try: |
| 39 | + choice = int(input("\n\033[93mEnter 1 or 2:\033[0m ")) |
| 40 | + if choice in [1, 2]: |
| 41 | + return choice |
| 42 | + print("\033[91mInvalid selection. Please enter 1 or 2.\033[0m") |
| 43 | + except ValueError: |
| 44 | + print("\033[91mPlease enter a valid number.\033[0m") |
| 45 | + |
| 46 | +# Load and extract pedestrian and vehicle data |
| 47 | +def extract_behavior_data(log_dir): |
| 48 | + behavior_path = os.path.join(log_dir, BEHAVIOR_FILE) |
| 49 | + if not os.path.exists(behavior_path): |
| 50 | + print(f"\033[91mError: {behavior_path} not found.\033[0m") |
| 51 | + return None, None, None, None, None, None, None, None |
| 52 | + |
| 53 | + with open(behavior_path, "r") as f: |
| 54 | + data = [json.loads(line) for line in f] |
| 55 | + |
| 56 | + pedestrian_paths = {} |
| 57 | + pedestrian_times = {} |
| 58 | + vehicle_path = [] |
| 59 | + vehicle_times = [] |
| 60 | + speeds = [] |
| 61 | + accelerators = [] |
| 62 | + brakes = [] |
| 63 | + steering_angles = [] |
| 64 | + |
| 65 | + for entry in data: |
| 66 | + time_stamp = entry.get("time", 0) |
| 67 | + |
| 68 | + # Extract pedestrian data |
| 69 | + if "agents" in entry: |
| 70 | + for agent_id, agent in entry["agents"].items(): |
| 71 | + agent_data = agent.get("data", {}) |
| 72 | + if agent_data.get("type") == 3: # Type 3 = pedestrian |
| 73 | + pose = agent_data.get("pose", {}) |
| 74 | + x, y = pose.get("x", 0), pose.get("y", 0) |
| 75 | + |
| 76 | + if agent_id not in pedestrian_paths: |
| 77 | + pedestrian_paths[agent_id] = [] |
| 78 | + pedestrian_times[agent_id] = [] |
| 79 | + |
| 80 | + pedestrian_paths[agent_id].append((x, y)) |
| 81 | + pedestrian_times[agent_id].append(time_stamp) |
| 82 | + |
| 83 | + # Extract vehicle data |
| 84 | + if "vehicle" in entry: |
| 85 | + vehicle_data = entry["vehicle"].get("data", {}) |
| 86 | + pose = vehicle_data.get("pose", {}) |
| 87 | + x, y = pose.get("x", 0), pose.get("y", 0) |
| 88 | + |
| 89 | + vehicle_path.append((x, y)) |
| 90 | + vehicle_times.append(time_stamp) |
| 91 | + speeds.append(vehicle_data.get("v", 0)) |
| 92 | + accelerators.append(vehicle_data.get("accelerator_pedal_position", 0)) |
| 93 | + brakes.append(vehicle_data.get("brake_pedal_position", 0)) |
| 94 | + steering_angles.append(vehicle_data.get("steering_wheel_angle", 0)) |
| 95 | + |
| 96 | + return pedestrian_paths, pedestrian_times, vehicle_path, vehicle_times, speeds, accelerators, brakes, steering_angles |
| 97 | + |
| 98 | +# Klampt 3D Visualization |
| 99 | +def visualize_with_klampt(pedestrian_paths, pedestrian_times, vehicle_path): |
| 100 | + """Uses Klampt to visualize pedestrian and vehicle paths.""" |
| 101 | + vis.init("PyQt6") |
| 102 | + vis.setWindowTitle("Pedestrian and Vehicle Path Visualization") |
| 103 | + |
| 104 | + klampt_vis = KlamptVisualization(vehicle_interface=None, rate=20.0) |
| 105 | + |
| 106 | + for agent_id, path in pedestrian_paths.items(): |
| 107 | + if len(path) < 2: |
| 108 | + continue |
| 109 | + |
| 110 | + times = pedestrian_times[agent_id] |
| 111 | + path_3d = [[float(x), float(y), 0.0] for x, y in path] |
| 112 | + |
| 113 | + trajectory = Trajectory(times, path_3d) |
| 114 | + vis.add(agent_id, trajectory, color=(0, 1, 0, 1), width=2) |
| 115 | + |
| 116 | + # if vehicle_path: |
| 117 | + # vehicle_x, vehicle_y = zip(*vehicle_path) |
| 118 | + # vehicle_tuples = [[float(x), float(y), 0.0] for x, y in zip(vehicle_x, vehicle_y)] |
| 119 | + # vis.add("Vehicle Path", vehicle_tuples, color=(1, 0, 0, 1), width=2) |
| 120 | + |
| 121 | + klampt_vis.initialize() |
| 122 | + vis.show() |
| 123 | + |
| 124 | + while vis.shown(): |
| 125 | + time.sleep(0.05) |
| 126 | + |
| 127 | + klampt_vis.cleanup() |
| 128 | + vis.kill() |
| 129 | + |
| 130 | +# MPL 2D Visualization |
| 131 | +def visualize_with_mpl(pedestrian_paths, pedestrian_times, vehicle_path, vehicle_data): |
| 132 | + vis = MPLVisualization(rate=10.0) |
| 133 | + vis.initialize() |
| 134 | + |
| 135 | + fig, axs = vis.fig, vis.axs |
| 136 | + axs[0].clear() |
| 137 | + axs[1].clear() |
| 138 | + |
| 139 | + # Left Plot: Pedestrian & Vehicle Trajectories |
| 140 | + axs[0].set_xlabel("X Position") |
| 141 | + axs[0].set_ylabel("Y Position") |
| 142 | + axs[0].set_title("Pedestrian & Vehicle Trajectories") |
| 143 | + |
| 144 | + for agent_id, path in pedestrian_paths.items(): |
| 145 | + path_x, path_y = zip(*path) |
| 146 | + axs[0].plot(path_x, path_y, linestyle='-', marker='o', label=f"Pedestrian {agent_id}") |
| 147 | + |
| 148 | + # if vehicle_path: |
| 149 | + # vehicle_x, vehicle_y = zip(*vehicle_path) |
| 150 | + # axs[0].plot(vehicle_x, vehicle_y, linestyle='-', marker='s', color='red', label="Vehicle Path") |
| 151 | + |
| 152 | + axs[0].legend() |
| 153 | + |
| 154 | + # Right Plot: Vehicle Controls Over Time |
| 155 | + times, speeds, accelerators, brakes, steering_angles = vehicle_data |
| 156 | + axs[1].set_xlabel("Time (s)") |
| 157 | + axs[1].set_title("Vehicle Controls Over Time") |
| 158 | + |
| 159 | + axs[1].plot(times, speeds, label="Speed (m/s)", color="blue") |
| 160 | + axs[1].plot(times, accelerators, label="Accelerator (%)", color="green") |
| 161 | + axs[1].plot(times, brakes, label="Brake (%)", color="red") |
| 162 | + axs[1].plot(times, steering_angles, label="Steering Angle", color="purple") |
| 163 | + axs[1].legend() |
| 164 | + |
| 165 | + fig.canvas.draw_idle() |
| 166 | + fig.canvas.flush_events() |
| 167 | + plt.show(block=True) |
| 168 | + |
| 169 | + vis.cleanup() |
| 170 | + |
| 171 | +# Main Execution |
| 172 | +if __name__ == "__main__": |
| 173 | + visualizer_choice = select_visualizer() |
| 174 | + selected_log_folder = select_log_folder() |
| 175 | + |
| 176 | + if selected_log_folder: |
| 177 | + print(f"\n\033[94mLoading behavior data from:\033[0m {selected_log_folder}") |
| 178 | + data = extract_behavior_data(selected_log_folder) |
| 179 | + |
| 180 | + pedestrian_paths, pedestrian_times, vehicle_path, vehicle_times, speeds, accelerators, brakes, steering_angles = data |
| 181 | + vehicle_data = (vehicle_times, speeds, accelerators, brakes, steering_angles) |
| 182 | + |
| 183 | + if pedestrian_paths or vehicle_path: |
| 184 | + if visualizer_choice == 1: |
| 185 | + print("\033[92mUsing Klampt for visualization...\033[0m") |
| 186 | + visualize_with_klampt(pedestrian_paths, pedestrian_times, vehicle_path) |
| 187 | + else: |
| 188 | + print("\033[92mUsing MPL (Matplotlib) for visualization...\033[0m") |
| 189 | + visualize_with_mpl(pedestrian_paths, pedestrian_times, vehicle_path, vehicle_data) |
| 190 | + else: |
| 191 | + print("\033[91mNo pedestrian or vehicle data found.\033[0m") |
0 commit comments