playback_matplotlib.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import pandas as pd
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import matplotlib.patches as patches
  5. from matplotlib.animation import FuncAnimation
  6. import os
  7. # 位置偏移常量
  8. X_OFFSET = 258109.4239876
  9. Y_OFFSET = 4149969.964821
  10. # 读取数据
  11. csv_data_path = "/home/kevin/kevin/zhaoyuan/zhaoyuan/data/processed/data_zhaoyuan1"
  12. lane_map_df = pd.read_csv(os.path.join(csv_data_path, 'LaneMap.csv'))
  13. car_df = pd.read_csv(os.path.join(csv_data_path, 'ObjState.csv'))
  14. # 创建颜色映射
  15. # 你可以通过 playerId 生成颜色,也可以使用 matplotlib 的 colormap
  16. unique_vehicle_ids = car_df['playerId'].unique()
  17. colors = ['red', 'blue', 'green', 'orange', 'purple', 'cyan', 'magenta', 'yellow', 'black', 'brown']
  18. # colors = plt.cm.viridis(np.linspace(0, 1, len(unique_vehicle_ids))) # 使用 Viridis colormap
  19. # 创建颜色字典
  20. vehicle_colors = {vehicle_id: colors[i] for i, vehicle_id in enumerate(unique_vehicle_ids)}
  21. # 依据时间分组车辆数据,按 simTime 进行分组
  22. grouped_car_data = car_df.groupby("simTime")
  23. # 设置画布
  24. fig, ax = plt.subplots()
  25. ax.set_aspect("equal")
  26. ax.set_xlim([lane_map_df["centerLine_x"].min() - 20, lane_map_df["centerLine_x"].max() + 20])
  27. ax.set_ylim([lane_map_df["centerLine_y"].min() - 20, lane_map_df["centerLine_y"].max() + 20])
  28. # 绘制车道线
  29. ax.plot(lane_map_df["centerLine_x"], lane_map_df["centerLine_y"], color="red", linewidth=2, linestyle="--", label="Vehicle Center Line")
  30. ax.plot(lane_map_df["lane_left_x"], lane_map_df["lane_left_y"], color="blue", linewidth=2, label="Left Lane Line")
  31. ax.plot(lane_map_df["lane_right_x"], lane_map_df["lane_right_y"], color="blue", linewidth=2, label="Right Lane Line")
  32. # 动态绘制车辆
  33. def update(frame):
  34. # 清除之前的矩形
  35. for patch in ax.patches:
  36. patch.remove()
  37. # 获取当前时间的车辆数据
  38. current_time = list(grouped_car_data.groups.keys())[frame] # 获取当前帧的时间
  39. current_data = grouped_car_data.get_group(current_time)
  40. # 遍历当前时间点上的所有车辆
  41. for index, row in current_data.iterrows():
  42. posX = row['posX'] + X_OFFSET
  43. posY = row['posY'] + Y_OFFSET
  44. posH = row['posH'] # 假设已在弧度或者直接为弧度
  45. dimX = row['dimX']
  46. dimY = row['dimY']
  47. # 计算矩形的四个角点
  48. corners = np.array([
  49. [-dimX / 2, -dimY / 2],
  50. [dimX / 2, -dimY / 2],
  51. [dimX / 2, dimY / 2],
  52. [-dimX / 2, dimY / 2],
  53. ])
  54. # 旋转矩形
  55. rotation_matrix = np.array(
  56. [[np.cos(posH), np.sin(posH)], [-np.sin(posH), np.cos(posH)]]
  57. )
  58. rotated_corners = corners.dot(rotation_matrix) + np.array([posX, posY])
  59. # 获取车辆的颜色
  60. vehicle_color = vehicle_colors[row['playerId']]
  61. # 创建矩形对象
  62. vehicle = patches.Polygon(
  63. rotated_corners, closed=True, fill=True, color=vehicle_color, alpha=0.5
  64. )
  65. ax.add_patch(vehicle)
  66. plt.title(f'Time: {current_time:.2f}s')
  67. # 设置动画,frames为时间段的数量
  68. ani = FuncAnimation(fig, update, frames=len(grouped_car_data.groups), repeat=True, interval=50)
  69. plt.show()