前两期搭建了 Carla + ROS 环境,添加了 NPC 交通流。这期是最关键的一步——用 YOLOv8 做实时目标检测。
但遇到一个麻烦:Carla ROS Bridge 跑在 Python 2.7 上,而 YOLOv8 需要 Python 3.10。
为了解决"数据在 Python 2.7,模型在 Python 3.10"这个问题,我试了两种方案。
思路是通过 ros bridge 把 ROS 话题通过 WebSocket 转发出去,Python 3.10 这边用 roslibpy 接收。
# 启动 ros bridgeroslaunch ros bridge_server ros bridge_websocket.launch queue_size:=1 port:=9099# Python 3.10 环境接收python ros bridge_roslib.py
启动简单,但跑起来发现两个问题:
放弃 ros bridge,直接用 TCP 把图像数组从 Python 2.7 发给 Python 3.10。
服务端(Python 2.7):
# 订阅 ROS 图像话题 → 转为 numpy → TCP 发送def image_callback(msg):img = np.frombuffer(msg.data, dtype=np.uint8)img = img.reshape(msg.height, msg.width, 4)[:, :, :3]header = str(len(img.tobytes())).encode().ljust(16)conn.sendall(header + img.tobytes())
客户端(Python 3.10):
# TCP 接收 → numpy → YOLOv8 检测 → 可视化def recv_all(sock, length):buf = b""while len(buf) < length:chunk = sock.recv(length - len(buf))if not chunk: raise ConnectionError()buf += chunkreturn buf# 主循环while True:header = recv_all(sock, 16)img_bin = recv_all(sock, int(header.strip()))frame = pickle.loads(img_bin)results = model(frame)# 绘制检测框并显示
整个跑起来就很流畅了,基本不影响使用。
终端1: Carla UE4终端2: ROS Bridge终端3: NPC 生成(可选)终端4: TCP Bridge Server(Python 2.7环境)终端5: YOLOv8 检测(Python 3.10环境)
按顺序启动:
# 终端1-3:跟第二期一样,先启动Carla、ROS Bridge、NPC# 终端4:启动 TCP 服务端(Python 2.7)cd /opt/carla_wspython tcp_bridge/tcp_bridge_server.py# 终端5:启动 YOLOv8 检测(Python 3.10)cd /opt/carla_ws/sim_workspace/image_detectpython image_detect_nms.py
启动后,终端5会弹出检测窗口,实时显示 YOLOv8 的检测结果——车辆、行人、骑行者都被框出来了。