diff --git a/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/CMakeLists.txt b/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/CMakeLists.txt new file mode 100644 index 0000000000..350c4d84a0 --- /dev/null +++ b/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/CMakeLists.txt @@ -0,0 +1,32 @@ +cmake_minimum_required(VERSION 2.8.3) +project(carla_yolo_ros) + +## Compile as C++11 (Standard for ROS Kinetic) +add_compile_options(-std=c++11) + +## Find catkin macros and libraries +find_package(catkin REQUIRED COMPONENTS + rospy + sensor_msgs + std_msgs +) + +## Catkin specific configuration +catkin_package( +# INCLUDE_DIRS include +# LIBRARIES carla_yolo_ros + CATKIN_DEPENDS rospy sensor_msgs std_msgs +# DEPENDS system_lib +) + +## Build configuration +include_directories( + ${catkin_INCLUDE_DIRS} +) + +## Install rules (Ensure Python scripts are executable) +catkin_install_python(PROGRAMS + src/object_detection_node.py + src/traffic_generator_node.py + DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +) \ No newline at end of file diff --git a/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/launch/start.launch b/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/launch/start.launch new file mode 100644 index 0000000000..0445fb6104 --- /dev/null +++ b/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/launch/start.launch @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/models/yolov8n.pt b/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/models/yolov8n.pt new file mode 100644 index 0000000000..d61ef50d3a Binary files /dev/null and b/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/models/yolov8n.pt differ diff --git a/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/package.xml b/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/package.xml new file mode 100644 index 0000000000..f1db681301 --- /dev/null +++ b/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/package.xml @@ -0,0 +1,68 @@ + + + carla_yolo_ros + 0.0.0 + The carla_yolo_ros package + + + + + ros-industrial + + + + + + TODO + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + catkin + rospy + sensor_msgs + std_msgs + rospy + sensor_msgs + std_msgs + rospy + sensor_msgs + std_msgs + + + + + + + + diff --git a/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/src/object_detection_node.py b/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/src/object_detection_node.py new file mode 100644 index 0000000000..a621ce5cca --- /dev/null +++ b/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/src/object_detection_node.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +import sys + +# 定义 ROS 的库路径 +ros_path = '/opt/ros/kinetic/lib/python2.7/dist-packages' + +# 【第一步】如果有 ROS 路径,先移除,确保 import cv2 加载的是虚拟环境里的新版本 +if ros_path in sys.path: + sys.path.remove(ros_path) + +import cv2 + +# 【第二步】再把 ROS 路径加回到末尾,确保后续能找到 sensor_msgs +sys.path.append(ros_path) + +import rospy +import numpy as np +import carla +from sensor_msgs.msg import Image # 现在这一行就能正常运行了 +from ultralytics import YOLO + +class CarlaYoloNode: + def __init__(self): + rospy.init_node('carla_yolo_node', anonymous=True) + + # 参数配置 + self.host = rospy.get_param('~host', '192.168.133.1') + self.port = rospy.get_param('~port', 2000) + self.model_path = rospy.get_param('~model_path', '') + self.conf_thres = rospy.get_param('~confidence', 0.5) + + self.pub_img = rospy.Publisher('/carla/camera/detection', Image, queue_size=1) + + # 连接 CARLA + try: + self.client = carla.Client(self.host, self.port, worker_threads=1) + self.client.set_timeout(30.0) + self.world = self.client.get_world() + except Exception as e: + rospy.logerr(f"Connect Fail: {e}") + sys.exit(1) + + # 加载 YOLO 模型 + rospy.loginfo(f"Loading model: {self.model_path}") + self.model = YOLO(self.model_path) + + self.setup_sensor() + + def setup_sensor(self): + bp_lib = self.world.get_blueprint_library() + spawn_points = self.world.get_map().get_spawn_points() + + # 1. 生成带自动驾驶的车辆 + vehicle_bp = bp_lib.filter("model3")[0] + self.vehicle = None + for point in spawn_points: + try: + self.vehicle = self.world.spawn_actor(vehicle_bp, point) + break + except RuntimeError: + continue + + if not self.vehicle: + rospy.logerr("Cannot spawn vehicle") + return + + self.vehicle.set_autopilot(True, 8005) + + # 2. 挂载摄像头 + camera_bp = bp_lib.find("sensor.camera.rgb") + camera_bp.set_attribute("image_size_x", "640") + camera_bp.set_attribute("image_size_y", "480") + self.camera = self.world.spawn_actor( + camera_bp, + carla.Transform(carla.Location(x=1.5, z=2.4)), + attach_to=self.vehicle + ) + self.camera.listen(self.callback) + + def callback(self, data): + if rospy.is_shutdown(): return + + # 手动将 CARLA 原始数据转为 Numpy (避免使用 cv_bridge) + array = np.frombuffer(data.raw_data, dtype=np.uint8) + array = array.reshape((data.height, data.width, 4)) + rgb = array[:, :, :3] + + # YOLO 推理 + results = self.model(rgb, verbose=False, conf=self.conf_thres) + + # 绘制检测框 (转为 BGR 供 OpenCV 绘图) + bgr = cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR) + for r in results: + for box in r.boxes: + x1, y1, x2, y2 = map(int, box.xyxy[0]) + if int(box.cls[0]) < len(self.model.names): + label = self.model.names[int(box.cls[0])] + cv2.rectangle(bgr, (x1, y1), (x2, y2), (0, 255, 0), 2) + cv2.putText(bgr, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) + + # 手动封装 ROS Image 消息 + msg = Image() + msg.header.stamp = rospy.Time.now() + msg.header.frame_id = "camera_link" + msg.height = bgr.shape[0] + msg.width = bgr.shape[1] + msg.encoding = "bgr8" + msg.step = bgr.shape[1] * 3 + msg.data = bgr.tobytes() + self.pub_img.publish(msg) + + def cleanup(self): + if self.camera: self.camera.destroy() + if self.vehicle: self.vehicle.destroy() + +if __name__ == '__main__': + node = CarlaYoloNode() + try: + rospy.spin() + except rospy.ROSInterruptException: + pass + finally: + node.cleanup() diff --git a/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/src/traffic_generator_node.py b/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/src/traffic_generator_node.py new file mode 100644 index 0000000000..ddd343f16a --- /dev/null +++ b/src/Autonomous_Vehicle_Object_Detection/catkin_ws_src/carla_yolo_ros/src/traffic_generator_node.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +import rospy +import carla +import logging +import random +import time + +logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) + +class TrafficGeneratorNode: + def __init__(self): + rospy.init_node('traffic_generator_node', anonymous=False) + self.host = rospy.get_param('~host', '192.168.133.1') + self.port = rospy.get_param('~port', 2000) + self.n_vehicles = rospy.get_param('~number_of_vehicles', 30) + self.n_walkers = rospy.get_param('~number_of_walkers', 10) + + self.vehicles_list = [] + self.walkers_list = [] + self.all_id = [] + self.client = None + + self.connect_carla() + self.setup_traffic() + + def connect_carla(self): + rospy.loginfo(f"Traffic: Connecting to {self.host}...") + try: + self.client = carla.Client(self.host, self.port, worker_threads=1) + self.client.set_timeout(30.0) + self.world = self.client.get_world() + except Exception as e: + rospy.logerr(f"Connection Error: {e}") + exit(1) + + def setup_traffic(self): + tm = self.client.get_trafficmanager(8000) + tm.set_global_distance_to_leading_vehicle(2.5) + tm.set_synchronous_mode(True) + + settings = self.world.get_settings() + settings.synchronous_mode = True + settings.fixed_delta_seconds = 0.05 + self.world.apply_settings(settings) + + # 1. 生成车辆 + # 直接使用 filter 函数,只获取车辆 + bps = self.world.get_blueprint_library().filter('vehicle.*') + spawn_points = self.world.get_map().get_spawn_points() + + SpawnActor = carla.command.SpawnActor + SetAutopilot = carla.command.SetAutopilot + FutureActor = carla.command.FutureActor + + batch = [] + for n, transform in enumerate(spawn_points): + if n >= self.n_vehicles: break + bp = random.choice(bps) + if bp.has_attribute('role_name'): bp.set_attribute('role_name', 'autopilot') + batch.append(SpawnActor(bp, transform).then(SetAutopilot(FutureActor, True, tm.get_port()))) + + for response in self.client.apply_batch_sync(batch, True): + if not response.error: self.vehicles_list.append(response.actor_id) + + rospy.loginfo(f"Spawned {len(self.vehicles_list)} vehicles") + + # 2. 生成行人 + bps_w = self.world.get_blueprint_library().filter("walker.pedestrian.*") + batch = [] + for i in range(self.n_walkers): + loc = self.world.get_random_location_from_navigation() + if loc: + sp = carla.Transform(loc) + batch.append(SpawnActor(random.choice(bps_w), sp)) + + results = self.client.apply_batch_sync(batch, True) + for r in results: + if not r.error: self.walkers_list.append({"id": r.actor_id}) + + # 3. 生成控制器 + batch = [SpawnActor(self.world.get_blueprint_library().find('controller.ai.walker'), carla.Transform(), w["id"]) for w in self.walkers_list] + results = self.client.apply_batch_sync(batch, True) + + for i, r in enumerate(results): + if not r.error: + self.walkers_list[i]["con"] = r.actor_id + # 【重要修改】必须把控制器(r.actor_id)放在偶数位,行人ID放在奇数位 + # 这样下面的 loop 才能正确调用 start() + self.all_id.extend([r.actor_id, self.walkers_list[i]["id"]]) + + self.world.tick() + + # 4. 启动逻辑 + all_actors = self.world.get_actors(self.all_id) + for i in range(0, len(self.all_id), 2): + # 这里调用 start(),所以 list 的第 0, 2, 4... 项必须是控制器(Controller) + all_actors[i].start() + all_actors[i].go_to_location(self.world.get_random_location_from_navigation()) + all_actors[i].set_max_speed(1.5) + + def run(self): + try: + while not rospy.is_shutdown(): + self.world.tick() + except Exception as e: + rospy.logerr(f"Error: {e}") + finally: + self.cleanup() + + def cleanup(self): + rospy.loginfo("Cleaning up...") + # 退出前必须关闭同步模式,否则服务器会死锁 + if self.world: + settings = self.world.get_settings() + settings.synchronous_mode = False + settings.fixed_delta_seconds = None + self.world.apply_settings(settings) + + if self.client: + self.client.apply_batch([carla.command.DestroyActor(x) for x in self.vehicles_list + self.all_id]) + +if __name__ == '__main__': + TrafficGeneratorNode().run() diff --git a/src/Autonomous_Vehicle_Object_Detection/generate_traffic.py b/src/Autonomous_Vehicle_Object_Detection/original_windows_scripts/generate_traffic.py similarity index 100% rename from src/Autonomous_Vehicle_Object_Detection/generate_traffic.py rename to src/Autonomous_Vehicle_Object_Detection/original_windows_scripts/generate_traffic.py diff --git a/src/Autonomous_Vehicle_Object_Detection/object_detection.py b/src/Autonomous_Vehicle_Object_Detection/original_windows_scripts/object_detection.py similarity index 100% rename from src/Autonomous_Vehicle_Object_Detection/object_detection.py rename to src/Autonomous_Vehicle_Object_Detection/original_windows_scripts/object_detection.py