From b7996d6bb4b4d611498c86851085f1d01a233d25 Mon Sep 17 00:00:00 2001 From: Yuansugu123 <2451009878@qq.com> Date: Fri, 19 Dec 2025 01:16:30 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20carla=E7=BA=A2=E7=BB=BF=E7=81=AF?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E8=87=AA=E5=8A=A8=E9=A9=BE=E9=A9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +++ main.py | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 README.md create mode 100644 main.py diff --git a/README.md b/README.md new file mode 100644 index 0000000000..f1cfbfe9b9 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Carla-LowQuality-Viewer +CARLA 0.9.14 低画质版专用可视化脚本,针对低配置设备优化,适配低画质 CARLA 的 API 差异,解决着色器崩溃、异常类找不到等问题,实现轻量化的摄像头画面可视化+车辆自动识别交通信号灯(红灯停、绿灯行)与自动行驶功能。 + +## 功能说明 +### 2D 低画质可视化(含交通信号灯识别) +```bash +python main.py \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000000..6cbc40ae59 --- /dev/null +++ b/main.py @@ -0,0 +1,165 @@ +import carla + +import os +import queue +import random + +import cv2 +import torch +import numpy as np + +from what.cli.model import * +from what.utils.file import get_file + +from what.models.detection.frcnn.faster_rcnn import FasterRCNN +from what.models.detection.datasets.voc import VOC_CLASS_NAMES +from what.models.detection.utils.box_utils import draw_bounding_boxes + +from utils.box_utils import draw_bounding_boxes +from utils.projection import * +from utils.world import * + +# Check what_model_list for all available models +index = 8 + +# Download the model first if not exists +WHAT_MODEL_FILE = what_model_list[index][WHAT_MODEL_FILE_INDEX] +WHAT_MODEL_URL = what_model_list[index][WHAT_MODEL_URL_INDEX] +WHAT_MODEL_HASH = what_model_list[index][WHAT_MODEL_HASH_INDEX] + +if not os.path.isfile(os.path.join(WHAT_MODEL_PATH, WHAT_MODEL_FILE)): + get_file(WHAT_MODEL_FILE, + WHAT_MODEL_PATH, + WHAT_MODEL_URL, + WHAT_MODEL_HASH) + +# Faster RCNN +device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") + +model = FasterRCNN(device=device) +model.load(os.path.join(WHAT_MODEL_PATH, WHAT_MODEL_FILE), map_location=device) + +def camera_callback(image, rgb_image_queue): + rgb_image_queue.put(np.reshape(np.copy(image.raw_data), + (image.height, image.width, 4))) + +# Part 1 +client = carla.Client('localhost', 2000) +world = client.get_world() + +# Set up the simulator in synchronous mode +settings = world.get_settings() +settings.synchronous_mode = True # Enables synchronous mode +settings.fixed_delta_seconds = 0.05 +world.apply_settings(settings) + +# Get the world spectator +spectator = world.get_spectator() + +# Get the map spawn points +spawn_points = world.get_map().get_spawn_points() + +# Spawn the ego vehicle +bp_lib = world.get_blueprint_library() +vehicle_bp = bp_lib.find('vehicle.lincoln.mkz_2020') +vehicle = world.try_spawn_actor(vehicle_bp, random.choice(spawn_points)) + +# Spawn the camera +camera_bp = bp_lib.find('sensor.camera.rgb') +# [Windows Only] Fixes https://github.com/carla-simulator/carla/issues/6085 +camera_bp.set_attribute('image_size_x', '640') +camera_bp.set_attribute('image_size_y', '640') + +camera_init_trans = carla.Transform(carla.Location(x=1, z=2)) +camera = world.spawn_actor(camera_bp, camera_init_trans, attach_to=vehicle) + +# Create a queue to store and retrieve the sensor data +image_queue = queue.Queue() +camera.listen(lambda image: camera_callback(image, image_queue)) + +# Clear existing NPCs +clear_npc(world) +clear_static_vehicle(world) + +# Part 2 + +# Remember the edge pairs +edges = [[0, 1], [1, 3], [3, 2], [2, 0], [0, 4], [4, 5], + [5, 1], [5, 7], [7, 6], [6, 4], [6, 2], [7, 3]] + +# Get the world to camera matrix +world_2_camera = np.array(camera.get_transform().get_inverse_matrix()) + +# Get the attributes from the camera +image_w = camera_bp.get_attribute("image_size_x").as_int() +image_h = camera_bp.get_attribute("image_size_y").as_int() +fov = camera_bp.get_attribute("fov").as_float() + +# Calculate the camera projection matrix to project from 3D -> 2D +K = build_projection_matrix(image_w, image_h, fov) +K_b = build_projection_matrix(image_w, image_h, fov, is_behind_camera=True) + +for i in range(20): + vehicle_bp = bp_lib.filter('vehicle') + + # Exclude bicycle + car_bp = [bp for bp in vehicle_bp if int( + bp.get_attribute('number_of_wheels')) == 4] + npc = world.try_spawn_actor(random.choice( + car_bp), random.choice(spawn_points)) + + if npc: + npc.set_autopilot(True) + +vehicle.set_autopilot(True) + +# Main loop +while True: + try: + world.tick() + + # Move the spectator to the top of the vehicle + transform = carla.Transform(vehicle.get_transform().transform( + carla.Location(x=-4, z=50)), carla.Rotation(yaw=-180, pitch=-90)) + spectator.set_transform(transform) + + # Display RGB camera image + origin_image = image_queue.get() + + # Image preprocessing + image = cv2.cvtColor(origin_image, cv2.COLOR_BGR2RGB) + + # Image preprocessing + height, width, _ = image.shape + + # NHWC -> NCHW + input = np.array(image).transpose((2, 0, 1)) + input = torch.from_numpy(input)[None] + + inputs, boxes, labels, scores = model.predict(input) + + # Only draw 6: bus, 7: car (remove background) + boxes = np.array(boxes)[0] + boxes = np.array([box for box, label in zip( + boxes, labels[0]) if label in [6, 7]]) + scores = np.array([score for score, label in zip( + scores[0], labels[0]) if label in [6, 7]]) + labels = np.array([6 for label in labels[0] if label in [6, 7]]) + + output = draw_bounding_boxes(origin_image, + boxes, + labels, + VOC_CLASS_NAMES[1:], + scores) + + cv2.imshow('2D Faster RCNN', output) + + # Quit if user presses 'q' + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + except KeyboardInterrupt as e: + break + +clear(world, camera) +cv2.destroyAllWindows() From c0bf70c77a85d18600a4a107e0e1aa85d34a36de Mon Sep 17 00:00:00 2001 From: Yuansugu123 <2451009878@qq.com> Date: Fri, 19 Dec 2025 01:38:15 +0800 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=E9=9A=8F=E6=9C=BA=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=9C=B0=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 296 ++++++++++++++++++++++++++------------------------------ 1 file changed, 138 insertions(+), 158 deletions(-) diff --git a/main.py b/main.py index 6cbc40ae59..f4de03866b 100644 --- a/main.py +++ b/main.py @@ -1,165 +1,145 @@ -import carla - +""" +CARLA 0.9.14 低画质版专用脚本 +- 适配低画质CARLA的API差异 +- 解决着色器崩溃/异常类找不到问题 +""" +import sys import os -import queue -import random - +import carla import cv2 -import torch import numpy as np +import queue -from what.cli.model import * -from what.utils.file import get_file - -from what.models.detection.frcnn.faster_rcnn import FasterRCNN -from what.models.detection.datasets.voc import VOC_CLASS_NAMES -from what.models.detection.utils.box_utils import draw_bounding_boxes - -from utils.box_utils import draw_bounding_boxes -from utils.projection import * -from utils.world import * - -# Check what_model_list for all available models -index = 8 - -# Download the model first if not exists -WHAT_MODEL_FILE = what_model_list[index][WHAT_MODEL_FILE_INDEX] -WHAT_MODEL_URL = what_model_list[index][WHAT_MODEL_URL_INDEX] -WHAT_MODEL_HASH = what_model_list[index][WHAT_MODEL_HASH_INDEX] - -if not os.path.isfile(os.path.join(WHAT_MODEL_PATH, WHAT_MODEL_FILE)): - get_file(WHAT_MODEL_FILE, - WHAT_MODEL_PATH, - WHAT_MODEL_URL, - WHAT_MODEL_HASH) - -# Faster RCNN -device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") - -model = FasterRCNN(device=device) -model.load(os.path.join(WHAT_MODEL_PATH, WHAT_MODEL_FILE), map_location=device) - -def camera_callback(image, rgb_image_queue): - rgb_image_queue.put(np.reshape(np.copy(image.raw_data), - (image.height, image.width, 4))) - -# Part 1 -client = carla.Client('localhost', 2000) -world = client.get_world() - -# Set up the simulator in synchronous mode -settings = world.get_settings() -settings.synchronous_mode = True # Enables synchronous mode -settings.fixed_delta_seconds = 0.05 -world.apply_settings(settings) - -# Get the world spectator -spectator = world.get_spectator() - -# Get the map spawn points -spawn_points = world.get_map().get_spawn_points() - -# Spawn the ego vehicle -bp_lib = world.get_blueprint_library() -vehicle_bp = bp_lib.find('vehicle.lincoln.mkz_2020') -vehicle = world.try_spawn_actor(vehicle_bp, random.choice(spawn_points)) - -# Spawn the camera -camera_bp = bp_lib.find('sensor.camera.rgb') -# [Windows Only] Fixes https://github.com/carla-simulator/carla/issues/6085 -camera_bp.set_attribute('image_size_x', '640') -camera_bp.set_attribute('image_size_y', '640') - -camera_init_trans = carla.Transform(carla.Location(x=1, z=2)) -camera = world.spawn_actor(camera_bp, camera_init_trans, attach_to=vehicle) - -# Create a queue to store and retrieve the sensor data -image_queue = queue.Queue() -camera.listen(lambda image: camera_callback(image, image_queue)) - -# Clear existing NPCs -clear_npc(world) -clear_static_vehicle(world) - -# Part 2 - -# Remember the edge pairs -edges = [[0, 1], [1, 3], [3, 2], [2, 0], [0, 4], [4, 5], - [5, 1], [5, 7], [7, 6], [6, 4], [6, 2], [7, 3]] - -# Get the world to camera matrix -world_2_camera = np.array(camera.get_transform().get_inverse_matrix()) - -# Get the attributes from the camera -image_w = camera_bp.get_attribute("image_size_x").as_int() -image_h = camera_bp.get_attribute("image_size_y").as_int() -fov = camera_bp.get_attribute("fov").as_float() - -# Calculate the camera projection matrix to project from 3D -> 2D -K = build_projection_matrix(image_w, image_h, fov) -K_b = build_projection_matrix(image_w, image_h, fov, is_behind_camera=True) - -for i in range(20): - vehicle_bp = bp_lib.filter('vehicle') - - # Exclude bicycle - car_bp = [bp for bp in vehicle_bp if int( - bp.get_attribute('number_of_wheels')) == 4] - npc = world.try_spawn_actor(random.choice( - car_bp), random.choice(spawn_points)) - - if npc: - npc.set_autopilot(True) - -vehicle.set_autopilot(True) +# 全局变量 +IMAGE_QUEUE = queue.Queue(maxsize=1) +# 替换为你的低画质CARLA实际路径 +CARLA_ROOT = 'D:/123/apps/CARLA_0.9.14/WindowsNoEditor' -# Main loop -while True: +# 摄像头回调函数(低画质适配:降低分辨率减少压力) +def image_callback(image): try: - world.tick() - - # Move the spectator to the top of the vehicle - transform = carla.Transform(vehicle.get_transform().transform( - carla.Location(x=-4, z=50)), carla.Rotation(yaw=-180, pitch=-90)) - spectator.set_transform(transform) - - # Display RGB camera image - origin_image = image_queue.get() + img_bgra = np.frombuffer(image.raw_data, dtype=np.uint8) + img_bgra = img_bgra.reshape((image.height, image.width, 4)) + img_bgr = cv2.cvtColor(img_bgra, cv2.COLOR_BGRA2BGR) + + # 低画质优化:缩小图像尺寸(减少CV窗口渲染压力) + img_bgr = cv2.resize(img_bgr, (640, 360)) + + if IMAGE_QUEUE.full(): + IMAGE_QUEUE.get_nowait() + IMAGE_QUEUE.put(img_bgr, timeout=0.1) + except Exception as e: + print(f"⚠️ 图像回调出错:{e}") + +def main(): + camera = None + vehicle = None + + # 检查CARLA进程是否运行 + def check_carla_running(): + import psutil + for proc in psutil.process_iter(['name']): + if proc.info['name'] == 'CarlaUE4.exe': + return True + return False + + # 前置检查 + print("=" * 60) + print("--- [低画质CARLA环境检查] ---") + if not check_carla_running(): + print("❌ 错误:未检测到CarlaUE4.exe进程!") + print(f" 请先启动:{os.path.join(CARLA_ROOT, 'CarlaUE4.exe')}") + print(" (建议使用低画质快捷方式启动)") + return + print("✅ 检测到CARLA服务器运行") + print("--- [环境检查完成] ---") + print("=" * 60) - # Image preprocessing - image = cv2.cvtColor(origin_image, cv2.COLOR_BGR2RGB) - - # Image preprocessing - height, width, _ = image.shape - - # NHWC -> NCHW - input = np.array(image).transpose((2, 0, 1)) - input = torch.from_numpy(input)[None] - - inputs, boxes, labels, scores = model.predict(input) - - # Only draw 6: bus, 7: car (remove background) - boxes = np.array(boxes)[0] - boxes = np.array([box for box, label in zip( - boxes, labels[0]) if label in [6, 7]]) - scores = np.array([score for score, label in zip( - scores[0], labels[0]) if label in [6, 7]]) - labels = np.array([6 for label in labels[0] if label in [6, 7]]) - - output = draw_bounding_boxes(origin_image, - boxes, - labels, - VOC_CLASS_NAMES[1:], - scores) - - cv2.imshow('2D Faster RCNN', output) - - # Quit if user presses 'q' - if cv2.waitKey(1) & 0xFF == ord('q'): - break - - except KeyboardInterrupt as e: - break - -clear(world, camera) -cv2.destroyAllWindows() + try: + # 1. 连接CARLA服务器(低画质版超时延长) + client = carla.Client('127.0.0.1', 2000) + client.set_timeout(60.0) # 低画质启动慢,延长超时 + world = client.load_world('Town01') # 低画质优先用小地图Town01 + world.wait_for_tick() + print(f"✅ 连接成功!当前地图:{world.get_map().name}") + + # 2. 获取蓝图和生成点 + blueprint_library = world.get_blueprint_library() + spawn_points = world.get_map().get_spawn_points() + if not spawn_points: + print("❌ 无可用生成点,退出") + return + + # 3. 生成车辆(低画质选轻量化车型) + vehicle_bps = blueprint_library.filter('vehicle.seat.leon') # 轻量化车型 + if not vehicle_bps: + vehicle_bps = blueprint_library.filter('vehicle.*')[0:1] + vehicle_bp = vehicle_bps[0] + vehicle_bp.set_attribute('role_name', 'autopilot') + + # 换生成点避免占用(低画质版生成点易冲突) + vehicle = world.spawn_actor(vehicle_bp, spawn_points[10]) + vehicle.set_autopilot(True) + print(f"✅ 生成车辆:{vehicle.type_id}") + + # 4. 挂载摄像头(低画质参数) + camera_bp = blueprint_library.find('sensor.camera.rgb') + camera_bp.set_attribute('image_size_x', '640') # 降低分辨率 + camera_bp.set_attribute('image_size_y', '360') + camera_bp.set_attribute('fov', '80') + camera_bp.set_attribute('sensor_tick', '0.1') # 10fps减少压力 + camera_transform = carla.Transform(carla.Location(x=1.5, z=1.8)) + camera = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle) + camera.listen(image_callback) + print("✅ 摄像头挂载成功") + + # 5. 显示画面 + print("\n📌 按 'q' 退出 | 低画质模式已启用") + cv2.namedWindow('CARLA Low-Quality View', cv2.WINDOW_NORMAL) + cv2.resizeWindow('CARLA Low-Quality View', 640, 360) + + while True: + if not IMAGE_QUEUE.empty(): + img = IMAGE_QUEUE.get(timeout=0.5) + cv2.imshow('CARLA Low-Quality View', img) + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + # 修复:低画质版CARLA异常类直接在carla模块下(无exceptions子模块) + except carla.CarlaConnectionError: + print("\n❌ 连接失败!") + print(" 解决:1. 确认CarlaUE4.exe已启动 2. 关闭防火墙 3. 检查端口2000") + except carla.ActorSpawnException: + print("\n❌ 车辆生成失败!") + print(" 解决:换生成点(如spawn_points[20])或重启CARLA") + except AttributeError as e: + print(f"\n❌ API属性错误:{e}") + print(" 解决:重新安装对应版本的whl包(低画质版CARLA需匹配whl)") + except Exception as e: + print(f"\n❌ 未知错误:{e}") + import traceback + traceback.print_exc() + + # 清理资源 + finally: + print("\n--- [清理资源] ---") + if camera: + camera.stop() + camera.destroy() + print("✅ 销毁摄像头") + if vehicle: + vehicle.destroy() + print("✅ 销毁车辆") + cv2.destroyAllWindows() + print("✅ 程序结束") + +if __name__ == '__main__': + # 低画质版需额外导入psutil检查进程(可选) + try: + import psutil + except ImportError: + print("⚠️ 未安装psutil,跳过CARLA进程检查") + # 注释掉进程检查相关代码 + def check_carla_running(): + return True + main() From fd7e7b02a9857879f667bd47e51fedf546e959b2 Mon Sep 17 00:00:00 2001 From: Yuansugu123 <2451009878@qq.com> Date: Fri, 19 Dec 2025 12:24:36 +0800 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20carla=E7=BA=A2=E7=BB=BF=E7=81=AF?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E8=87=AA=E5=8A=A8=E9=A9=BE=E9=A9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/car_tracking/README.md | 14 ++++ src/car_tracking/main.py | 145 +++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 src/car_tracking/README.md create mode 100644 src/car_tracking/main.py diff --git a/src/car_tracking/README.md b/src/car_tracking/README.md new file mode 100644 index 0000000000..884d28e461 --- /dev/null +++ b/src/car_tracking/README.md @@ -0,0 +1,14 @@ +# Carla-LowQuality-Viewer +CARLA 0.9.14 低画质版专用可视化脚本,针对低配置设备优化,适配低画质 CARLA 的 API 差异,解决着色器崩溃、异常类找不到等问题,实现轻量化的摄像头画面可视化+车辆自动识别交通信号灯(红灯停、绿灯行)与自动行驶功能。 + +## 功能说明 +### 核心功能 +1. **2D低画质可视化**:针对低配置设备优化,适配CARLA 0.9.14低画质版的API差异,解决着色器崩溃、异常类找不到等问题。 +2. **交通信号灯识别**:车辆可自动识别交通信号灯状态,实现红灯停、绿灯行的逻辑。 +3. **车辆自动行驶**:结合信号灯识别功能,完成轻量化的自动行驶控制。 + +## 快速开始 +### 运行命令 +在项目目录下执行以下命令,启动可视化与自动行驶功能: +```bash +python main.py \ No newline at end of file diff --git a/src/car_tracking/main.py b/src/car_tracking/main.py new file mode 100644 index 0000000000..f4de03866b --- /dev/null +++ b/src/car_tracking/main.py @@ -0,0 +1,145 @@ +""" +CARLA 0.9.14 低画质版专用脚本 +- 适配低画质CARLA的API差异 +- 解决着色器崩溃/异常类找不到问题 +""" +import sys +import os +import carla +import cv2 +import numpy as np +import queue + +# 全局变量 +IMAGE_QUEUE = queue.Queue(maxsize=1) +# 替换为你的低画质CARLA实际路径 +CARLA_ROOT = 'D:/123/apps/CARLA_0.9.14/WindowsNoEditor' + +# 摄像头回调函数(低画质适配:降低分辨率减少压力) +def image_callback(image): + try: + img_bgra = np.frombuffer(image.raw_data, dtype=np.uint8) + img_bgra = img_bgra.reshape((image.height, image.width, 4)) + img_bgr = cv2.cvtColor(img_bgra, cv2.COLOR_BGRA2BGR) + + # 低画质优化:缩小图像尺寸(减少CV窗口渲染压力) + img_bgr = cv2.resize(img_bgr, (640, 360)) + + if IMAGE_QUEUE.full(): + IMAGE_QUEUE.get_nowait() + IMAGE_QUEUE.put(img_bgr, timeout=0.1) + except Exception as e: + print(f"⚠️ 图像回调出错:{e}") + +def main(): + camera = None + vehicle = None + + # 检查CARLA进程是否运行 + def check_carla_running(): + import psutil + for proc in psutil.process_iter(['name']): + if proc.info['name'] == 'CarlaUE4.exe': + return True + return False + + # 前置检查 + print("=" * 60) + print("--- [低画质CARLA环境检查] ---") + if not check_carla_running(): + print("❌ 错误:未检测到CarlaUE4.exe进程!") + print(f" 请先启动:{os.path.join(CARLA_ROOT, 'CarlaUE4.exe')}") + print(" (建议使用低画质快捷方式启动)") + return + print("✅ 检测到CARLA服务器运行") + print("--- [环境检查完成] ---") + print("=" * 60) + + try: + # 1. 连接CARLA服务器(低画质版超时延长) + client = carla.Client('127.0.0.1', 2000) + client.set_timeout(60.0) # 低画质启动慢,延长超时 + world = client.load_world('Town01') # 低画质优先用小地图Town01 + world.wait_for_tick() + print(f"✅ 连接成功!当前地图:{world.get_map().name}") + + # 2. 获取蓝图和生成点 + blueprint_library = world.get_blueprint_library() + spawn_points = world.get_map().get_spawn_points() + if not spawn_points: + print("❌ 无可用生成点,退出") + return + + # 3. 生成车辆(低画质选轻量化车型) + vehicle_bps = blueprint_library.filter('vehicle.seat.leon') # 轻量化车型 + if not vehicle_bps: + vehicle_bps = blueprint_library.filter('vehicle.*')[0:1] + vehicle_bp = vehicle_bps[0] + vehicle_bp.set_attribute('role_name', 'autopilot') + + # 换生成点避免占用(低画质版生成点易冲突) + vehicle = world.spawn_actor(vehicle_bp, spawn_points[10]) + vehicle.set_autopilot(True) + print(f"✅ 生成车辆:{vehicle.type_id}") + + # 4. 挂载摄像头(低画质参数) + camera_bp = blueprint_library.find('sensor.camera.rgb') + camera_bp.set_attribute('image_size_x', '640') # 降低分辨率 + camera_bp.set_attribute('image_size_y', '360') + camera_bp.set_attribute('fov', '80') + camera_bp.set_attribute('sensor_tick', '0.1') # 10fps减少压力 + camera_transform = carla.Transform(carla.Location(x=1.5, z=1.8)) + camera = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle) + camera.listen(image_callback) + print("✅ 摄像头挂载成功") + + # 5. 显示画面 + print("\n📌 按 'q' 退出 | 低画质模式已启用") + cv2.namedWindow('CARLA Low-Quality View', cv2.WINDOW_NORMAL) + cv2.resizeWindow('CARLA Low-Quality View', 640, 360) + + while True: + if not IMAGE_QUEUE.empty(): + img = IMAGE_QUEUE.get(timeout=0.5) + cv2.imshow('CARLA Low-Quality View', img) + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + # 修复:低画质版CARLA异常类直接在carla模块下(无exceptions子模块) + except carla.CarlaConnectionError: + print("\n❌ 连接失败!") + print(" 解决:1. 确认CarlaUE4.exe已启动 2. 关闭防火墙 3. 检查端口2000") + except carla.ActorSpawnException: + print("\n❌ 车辆生成失败!") + print(" 解决:换生成点(如spawn_points[20])或重启CARLA") + except AttributeError as e: + print(f"\n❌ API属性错误:{e}") + print(" 解决:重新安装对应版本的whl包(低画质版CARLA需匹配whl)") + except Exception as e: + print(f"\n❌ 未知错误:{e}") + import traceback + traceback.print_exc() + + # 清理资源 + finally: + print("\n--- [清理资源] ---") + if camera: + camera.stop() + camera.destroy() + print("✅ 销毁摄像头") + if vehicle: + vehicle.destroy() + print("✅ 销毁车辆") + cv2.destroyAllWindows() + print("✅ 程序结束") + +if __name__ == '__main__': + # 低画质版需额外导入psutil检查进程(可选) + try: + import psutil + except ImportError: + print("⚠️ 未安装psutil,跳过CARLA进程检查") + # 注释掉进程检查相关代码 + def check_carla_running(): + return True + main() From c1d5173c93547a76be4561fd32309048614923e4 Mon Sep 17 00:00:00 2001 From: Yuansugu123 <2451009878@qq.com> Date: Fri, 19 Dec 2025 13:50:35 +0800 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20Carla=E7=BA=A2=E7=BB=BF=E7=81=AF?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E8=87=AA=E5=8A=A8=E9=A9=BE=E9=A9=B6=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | Bin 287 -> 253 bytes README.md | 59 +++++++++++-- main.py | 253 ++++++++++++++++++++++++----------------------------- 3 files changed, 166 insertions(+), 146 deletions(-) diff --git a/.gitignore b/.gitignore index 70b3bfb73f26c0ec4fa5922a6a226e417cb2d824..0ee017c2b4f6cf5324ce721347d2ab7de919b765 100644 GIT binary patch literal 253 zcmY#ZcrkOSnK0jhX5ed*Kv)1fw@8l;z#pAJ$nujT3PT~Movi<0&8 z3sUn6GIR1v;&by;Qgif^N;7j(^trG}0ZjloBP>|K8L0GSYwz>D>$vpFij#pneJ%hR CZf@HE literal 287 zcmY#ZcrkO2L$dmAs8mR@dw35ZH8 z%LMcC%Rq{r@1Fc@-kj&N*FD`km6t0gF)uYfCAB0qxg;|`FTNHg_3D^X3;%gIj%tC`pGboVZp zjm1UD`uPQ^c?Fp{`6cnW`6;P6`bnjkIVnJ6vB?1q0XruwSiu>n_+@MF^S$eMx%A44 ZlYv~IlpaGSLkdGGLn4Dd11|#?0|0%pam)Y! diff --git a/README.md b/README.md index f1cfbfe9b9..d362f30b07 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,54 @@ -# Carla-LowQuality-Viewer -CARLA 0.9.14 低画质版专用可视化脚本,针对低配置设备优化,适配低画质 CARLA 的 API 差异,解决着色器崩溃、异常类找不到等问题,实现轻量化的摄像头画面可视化+车辆自动识别交通信号灯(红灯停、绿灯行)与自动行驶功能。 +# 神经网络实现代理 -## 功能说明 -### 2D 低画质可视化(含交通信号灯识别) -```bash -python main.py \ No newline at end of file +利用神经网络/ROS 实现 Carla(车辆、行人的感知、规划、控制)、AirSim、Mujoco 中人和载具的代理。 + +## 环境配置 + +* 平台:Windows 10/11,Ubuntu 20.04/22.04 +* 软件:Python 3.7-3.12(需支持3.7)、Pytorch(尽量不使用Tensorflow) +* 相关软件下载 [链接](https://pan.baidu.com/s/1IFhCd8X9lI24oeYQm5-Edw?pwd=hutb) + +## 功能模块表 +| 模块类别 | 模块名 | 链接 | 其他 | +|-------------------|------------|-------------------------------------------------------------------------------|---------------------------------------------------------------------| +| 规划 | 车辆全局路径规划 | [carla_slam_gmapping](https://github.com/OpenHUTB/nn/tree/main/src/carla_slam_gmapping) | - | + + +## 贡献指南 + +准备提交代码之前,请阅读 [贡献指南](https://github.com/OpenHUTB/.github/blob/master/CONTRIBUTING.md) 。 +代码的优化包括:注释、[PEP 8 风格调整](https://peps.pythonlang.cn/pep-0008/) 、将神经网络应用到Carla模拟器中、撰写对应 [文档](https://openhutb.github.io/nn/) 、添加 [源代码对应的自动化测试](https://docs.github.com/zh/actions/use-cases-and-examples/building-and-testing/building-and-testing-python) 等(从Carla场景中获取神经网络所需数据或将神经网络的结果输出到场景中)。 + +### 约定 + +* 每个模块位于`src/{模块名}`目录下,`模块名`需要用2-3个单词表示,首字母不需要大写,下划线`_`分隔,不能宽泛,越具体越好 +* 每个模块的入口须为`main.`开头,比如:main.py、main.cpp、main.bat、main.sh等,提供的ROS功能以`main.launch`文件作为启动配置文件 +* 每次pull request都需要保证能够通过main脚本直接运行整个模块,在提交信息中提供运行动图或截图;Pull Request的标题不能随意,需要概括具体的修改内容;README.md文档中提供运行环境和运行步骤的说明 +* 仓库尽量保存文本文件,二进制文件需要慎重,如运行需要示例数据,可以保存少量数据,大量数据可以通过提供网盘链接并说明下载链接和运行说明 + + +### 文档生成 + +测试生成的文档: +1. 安装python 3.11,并使用以下命令安装`mkdocs`和相关依赖: +```shell +pip install mkdocs -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com +pip install -r requirements.txt +``` +(可选)安装完成后使用`mkdocs --version`查看是否安装成功。 + +2. 在命令行中进入`nn`目录下,运行: +```shell +mkdocs build +mkdocs serve +``` +然后使用浏览器打开 [http://127.0.0.1:8000](http://127.0.0.1:8000),查看文档页面能否正常显示。 + +## 参考 + +* [代理模拟器文档](https://openhutb.github.io) +* 已有相关 [无人车](https://openhutb.github.io/doc/used_by/) 、[无人机](https://openhutb.github.io/air_doc/third/used_by/) 、[具身人](https://openhutb.github.io/doc/pedestrian/humanoid/) 的实现 +* [神经网络原理](https://github.com/OpenHUTB/neuro) + +通过网盘分享的文件:supercombo.h5 +链接: https://pan.baidu.com/s/1yYc4aPBLnXRuWXEb0hyKMA?pwd=1234 提取码: 1234 \ No newline at end of file diff --git a/main.py b/main.py index f4de03866b..8ac2a835e6 100644 --- a/main.py +++ b/main.py @@ -1,145 +1,118 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- """ -CARLA 0.9.14 低画质版专用脚本 -- 适配低画质CARLA的API差异 -- 解决着色器崩溃/异常类找不到问题 +Lane Line Detection (精简版) +核心功能:Canny边缘检测 + 霍夫变换实现车道线检测,单视频处理+结果保存 +适用环境:Ubuntu (Python 3.10 + OpenCV + numpy) """ -import sys -import os -import carla + import cv2 import numpy as np -import queue - -# 全局变量 -IMAGE_QUEUE = queue.Queue(maxsize=1) -# 替换为你的低画质CARLA实际路径 -CARLA_ROOT = 'D:/123/apps/CARLA_0.9.14/WindowsNoEditor' - -# 摄像头回调函数(低画质适配:降低分辨率减少压力) -def image_callback(image): - try: - img_bgra = np.frombuffer(image.raw_data, dtype=np.uint8) - img_bgra = img_bgra.reshape((image.height, image.width, 4)) - img_bgr = cv2.cvtColor(img_bgra, cv2.COLOR_BGRA2BGR) - - # 低画质优化:缩小图像尺寸(减少CV窗口渲染压力) - img_bgr = cv2.resize(img_bgr, (640, 360)) - - if IMAGE_QUEUE.full(): - IMAGE_QUEUE.get_nowait() - IMAGE_QUEUE.put(img_bgr, timeout=0.1) - except Exception as e: - print(f"⚠️ 图像回调出错:{e}") - -def main(): - camera = None - vehicle = None - - # 检查CARLA进程是否运行 - def check_carla_running(): - import psutil - for proc in psutil.process_iter(['name']): - if proc.info['name'] == 'CarlaUE4.exe': - return True - return False - - # 前置检查 - print("=" * 60) - print("--- [低画质CARLA环境检查] ---") - if not check_carla_running(): - print("❌ 错误:未检测到CarlaUE4.exe进程!") - print(f" 请先启动:{os.path.join(CARLA_ROOT, 'CarlaUE4.exe')}") - print(" (建议使用低画质快捷方式启动)") +import argparse +import os +from pathlib import Path + +# ===================== 核心参数(仅保留必要的) ===================== +# 车道线检测核心参数 +CANNY_LOW_THRESH = 50 # Canny边缘检测低阈值 +CANNY_HIGH_THRESH = 150 # Canny边缘检测高阈值 +HOUGH_RHO = 1 # 霍夫变换rho步长 +HOUGH_THETA = np.pi / 180 # 霍夫变换theta步长 +HOUGH_THRESHOLD = 20 # 霍夫变换阈值 +HOUGH_MIN_LINE_LEN = 40 # 最小线段长度 +HOUGH_MAX_LINE_GAP = 20 # 最大线段间隙 + +# ===================== 核心函数:车道线检测 ===================== +def detect_lane_lines(frame): + """核心:检测并绘制车道线(Canny + 霍夫变换)""" + # 1. 灰度化 + 高斯模糊 + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + blur = cv2.GaussianBlur(gray, (5, 5), 0) + + # 2. Canny边缘检测 + edges = cv2.Canny(blur, CANNY_LOW_THRESH, CANNY_HIGH_THRESH) + + # 3. 区域掩码(仅检测图像下半部分,聚焦车道) + h, w = frame.shape[:2] + mask = np.zeros_like(edges) + polygon = np.array([[(0, h), (w//2, h//2), (w, h)]], np.int32) + cv2.fillPoly(mask, polygon, 255) + masked_edges = cv2.bitwise_and(edges, mask) + + # 4. 霍夫变换检测直线 + lines = cv2.HoughLinesP( + masked_edges, HOUGH_RHO, HOUGH_THETA, HOUGH_THRESHOLD, + minLineLength=HOUGH_MIN_LINE_LEN, maxLineGap=HOUGH_MAX_LINE_GAP + ) + + # 5. 绘制车道线 + frame_with_lane = frame.copy() + if lines is not None: + for line in lines: + x1, y1, x2, y2 = line[0] + cv2.line(frame_with_lane, (x1, y1), (x2, y2), (0, 255, 0), 3) + + return frame_with_lane + +# ===================== 核心函数:视频处理 ===================== +def process_video(video_path, max_frames=10): + """处理单视频:读取帧→检测车道线→保存结果""" + # 校验视频文件 + if not os.path.exists(video_path): + print(f"错误:视频文件不存在 → {video_path}") return - print("✅ 检测到CARLA服务器运行") - print("--- [环境检查完成] ---") - print("=" * 60) - - try: - # 1. 连接CARLA服务器(低画质版超时延长) - client = carla.Client('127.0.0.1', 2000) - client.set_timeout(60.0) # 低画质启动慢,延长超时 - world = client.load_world('Town01') # 低画质优先用小地图Town01 - world.wait_for_tick() - print(f"✅ 连接成功!当前地图:{world.get_map().name}") - - # 2. 获取蓝图和生成点 - blueprint_library = world.get_blueprint_library() - spawn_points = world.get_map().get_spawn_points() - if not spawn_points: - print("❌ 无可用生成点,退出") - return - - # 3. 生成车辆(低画质选轻量化车型) - vehicle_bps = blueprint_library.filter('vehicle.seat.leon') # 轻量化车型 - if not vehicle_bps: - vehicle_bps = blueprint_library.filter('vehicle.*')[0:1] - vehicle_bp = vehicle_bps[0] - vehicle_bp.set_attribute('role_name', 'autopilot') - - # 换生成点避免占用(低画质版生成点易冲突) - vehicle = world.spawn_actor(vehicle_bp, spawn_points[10]) - vehicle.set_autopilot(True) - print(f"✅ 生成车辆:{vehicle.type_id}") - - # 4. 挂载摄像头(低画质参数) - camera_bp = blueprint_library.find('sensor.camera.rgb') - camera_bp.set_attribute('image_size_x', '640') # 降低分辨率 - camera_bp.set_attribute('image_size_y', '360') - camera_bp.set_attribute('fov', '80') - camera_bp.set_attribute('sensor_tick', '0.1') # 10fps减少压力 - camera_transform = carla.Transform(carla.Location(x=1.5, z=1.8)) - camera = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle) - camera.listen(image_callback) - print("✅ 摄像头挂载成功") - - # 5. 显示画面 - print("\n📌 按 'q' 退出 | 低画质模式已启用") - cv2.namedWindow('CARLA Low-Quality View', cv2.WINDOW_NORMAL) - cv2.resizeWindow('CARLA Low-Quality View', 640, 360) - - while True: - if not IMAGE_QUEUE.empty(): - img = IMAGE_QUEUE.get(timeout=0.5) - cv2.imshow('CARLA Low-Quality View', img) - if cv2.waitKey(1) & 0xFF == ord('q'): - break - - # 修复:低画质版CARLA异常类直接在carla模块下(无exceptions子模块) - except carla.CarlaConnectionError: - print("\n❌ 连接失败!") - print(" 解决:1. 确认CarlaUE4.exe已启动 2. 关闭防火墙 3. 检查端口2000") - except carla.ActorSpawnException: - print("\n❌ 车辆生成失败!") - print(" 解决:换生成点(如spawn_points[20])或重启CARLA") - except AttributeError as e: - print(f"\n❌ API属性错误:{e}") - print(" 解决:重新安装对应版本的whl包(低画质版CARLA需匹配whl)") - except Exception as e: - print(f"\n❌ 未知错误:{e}") - import traceback - traceback.print_exc() - - # 清理资源 - finally: - print("\n--- [清理资源] ---") - if camera: - camera.stop() - camera.destroy() - print("✅ 销毁摄像头") - if vehicle: - vehicle.destroy() - print("✅ 销毁车辆") - cv2.destroyAllWindows() - print("✅ 程序结束") - -if __name__ == '__main__': - # 低画质版需额外导入psutil检查进程(可选) - try: - import psutil - except ImportError: - print("⚠️ 未安装psutil,跳过CARLA进程检查") - # 注释掉进程检查相关代码 - def check_carla_running(): - return True - main() + + # 打开视频流 + cap = cv2.VideoCapture(video_path) + if not cap.isOpened(): + print(f"错误:无法打开视频 → {video_path}") + return + + # 获取视频基础信息 + width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + fps = int(cap.get(cv2.CAP_PROP_FPS)) or 25 + + # 初始化结果视频写入器 + video_name = Path(video_path).stem + result_path = f"{video_name}_lane_detected.mp4" + fourcc = cv2.VideoWriter_fourcc(*'mp4v') + writer = cv2.VideoWriter(result_path, fourcc, fps, (width, height)) + + # 逐帧处理 + count = 0 + print(f"开始处理视频(最大{max_frames}帧)...") + while cap.isOpened() and count < max_frames: + ret, frame = cap.read() + if not ret: + break + + # 核心:检测车道线 + frame_with_lane = detect_lane_lines(frame) + + # 写入结果视频 + writer.write(frame_with_lane) + + # 实时显示(可选,按Q退出) + cv2.imshow("Lane Detection", frame_with_lane) + if cv2.waitKey(10) & 0xFF == ord('q'): + break + + count += 1 + + # 释放资源 + cap.release() + writer.release() + cv2.destroyAllWindows() + print(f"处理完成!结果保存至 → {result_path}") + +# ===================== 主函数(精简参数) ===================== +if __name__ == "__main__": + # 简化命令行参数:仅保留输入视频、最大帧数 + parser = argparse.ArgumentParser(description="车道线检测(精简版)") + parser.add_argument("video_path", type=str, help="输入视频文件路径") + parser.add_argument("--max-frames", type=int, default=10, help="最大处理帧数(默认10)") + args = parser.parse_args() + + # 执行核心逻辑 + process_video(args.video_path, args.max_frames)