diff --git a/data/__init__.py b/data/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/qodana.yaml b/qodana.yaml new file mode 100644 index 0000000000..38e12241aa --- /dev/null +++ b/qodana.yaml @@ -0,0 +1,41 @@ +#-------------------------------------------------------------------------------# +# Qodana analysis is configured by qodana.yaml file # +# https://www.jetbrains.com/help/qodana/qodana-yaml.html # +#-------------------------------------------------------------------------------# +version: "1.0" + +#Specify inspection profile for code analysis +profile: + name: qodana.starter + +#Enable inspections +#include: +# - name: + +#Disable inspections +#exclude: +# - name: +# paths: +# - + +#Execute shell command before Qodana execution (Applied in CI/CD pipeline) +#bootstrap: sh ./prepare-qodana.sh + +#Install IDE plugins before Qodana execution (Applied in CI/CD pipeline) +#plugins: +# - id: #(plugin id can be found at https://plugins.jetbrains.com) + +# Quality gate. Will fail the CI/CD pipeline if any condition is not met +# severityThresholds - configures maximum thresholds for different problem severities +# testCoverageThresholds - configures minimum code coverage on a whole project and newly added code +# Code Coverage is available in Ultimate and Ultimate Plus plans +#failureConditions: +# severityThresholds: +# any: 15 +# critical: 5 +# testCoverageThresholds: +# fresh: 70 +# total: 50 + +#Specify Qodana linter for analysis (Applied in CI/CD pipeline) +linter: jetbrains/qodana-:2025.2 diff --git a/src/Car_Perception_Automatic_Sensing/.gitignore b/src/Car_Perception_Automatic_Sensing/.gitignore new file mode 100644 index 0000000000..f3285da82c --- /dev/null +++ b/src/Car_Perception_Automatic_Sensing/.gitignore @@ -0,0 +1,30 @@ +# Python 相关 +__pycache__/ +*.pyc +*.pyo +*.pyd +.venv/ +env/ +*.env + +# PyCharm 相关 +.idea/ +*.iml +*.iws +*.ipr +out/ + +# 项目无关目录/文件 +../../.idea/ + +# 数据与模型(后续会用到) +data/raw/ +data/kitti/ +*.npy +*.pth +train/checkpoints/ +infer/results/ + +# OS 相关 +.DS_Store +Thumbs.db \ No newline at end of file diff --git a/src/Car_Perception_Automatic_Sensing/README.md b/src/Car_Perception_Automatic_Sensing/README.md index a93adc2a1c..7602125d1c 100644 --- a/src/Car_Perception_Automatic_Sensing/README.md +++ b/src/Car_Perception_Automatic_Sensing/README.md @@ -1,2 +1,28 @@ -无人车 无人车动态障碍物避障算法 无人车动态障碍物避障的核心目标是:在实时感知环境中移动障碍物(行人和车辆)的基础上,快速规划出无碰撞、平滑且符合运动学约束的安全路径 -环境:python3.13 \ No newline at end of file +# Car_Perception_Automatic_Sensing +自动驾驶环境感知自动感知系统,支持车辆、行人、车道线等目标的检测与分割。 + +## 功能模块 +- 数据加载与预处理(支持 KITTI/Waymo 数据集) +- 深度学习模型搭建(CNN/Transformer 骨干网络) +- 模型训练与验证 +- 推理可视化与结果导出## +- 目录结构 +Car_Perception_Automatic_Sensing/ +- ├── data/ +- ├── model/ +- ├── loss/ +- ├── train/ # 训练脚本目录 +- ├── infer/ # 推理脚本目录 +- ├── utils/ # 工具类目录 +- ├── test/ # 测试用例目录 +- ├── requirements.txt # 依赖配置 +- └── README.md # 项目文档 + +## 环境要求 +Python >= 3.8,PyTorch >= 2.0.0 + +## 快速开始 +1. 克隆仓库 +2. 安装依赖 +3. 准备数据集 +4. 运行训练脚本 \ No newline at end of file diff --git a/src/Car_Perception_Automatic_Sensing/data/__init__.py b/src/Car_Perception_Automatic_Sensing/data/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/Car_Perception_Automatic_Sensing/data/data_loader.py b/src/Car_Perception_Automatic_Sensing/data/data_loader.py new file mode 100644 index 0000000000..f600a3e5e9 --- /dev/null +++ b/src/Car_Perception_Automatic_Sensing/data/data_loader.py @@ -0,0 +1,80 @@ +""" +数据加载器核心模块 +支持自动驾驶数据集的加载、样本管理与批次分发 +""" +import os +import numpy as np +import torch +from torch.utils.data import Dataset, DataLoader +from typing import Optional, List + + +class AutoDriveDataset(Dataset): + """自动驾驶感知数据集基类(所有自定义数据集需继承此类)""" + def __init__(self, data_root: str, split: str = "train", transform: Optional[object] = None): + self.data_root = os.path.abspath(data_root) + self.split = split + self.transform = transform + self.sample_paths: List[str] = [] # 存储所有样本路径 + self._load_sample_paths() # 加载样本列表 + + def _load_sample_paths(self): + """加载样本路径(抽象方法,子类实现)""" + raise NotImplementedError("请在子类中实现 _load_sample_paths 方法") + + def __len__(self) -> int: + """返回样本总数""" + return len(self.sample_paths) + + def __getitem__(self, idx: int): + """获取单个样本(抽象方法,子类实现)""" + raise NotImplementedError("请在子类中实现 __getitem__ 方法") + + +def build_dataloader( + dataset: Dataset, + batch_size: int, + shuffle: bool = True, + num_workers: int = 4, + drop_last: bool = False +) -> DataLoader: + """构建PyTorch DataLoader(跨系统兼容)""" + # Windows系统默认关闭多进程 + if os.name == "nt": + num_workers = 0 + + return DataLoader( + dataset=dataset, + batch_size=batch_size, + shuffle=shuffle, + num_workers=num_workers, + pin_memory=True, + drop_last=drop_last + ) + + +if __name__ == "__main__": + # 框架测试 + pass +# 新增导入 +import cv2 +from typing import Tuple + +# 在 AutoDriveDataset 类中新增方法 +def _load_image(self, img_rel_path: str) -> Tuple[np.ndarray, Tuple[int, int, int]]: + """ + 读取图像并转换为RGB格式 + :param img_rel_path: 图像相对路径(基于data_root) + :return: (RGB图像数组, 图像形状(h, w, c)) + """ + img_abs_path = os.path.join(self.data_root, img_rel_path) + # 路径校验 + if not os.path.exists(img_abs_path): + raise FileNotFoundError(f"图像不存在:{img_abs_path}") + # 读取图像(忽略透明通道) + img_bgr = cv2.imread(img_abs_path, cv2.IMREAD_COLOR) + if img_bgr is None: + raise ValueError(f"无法读取图像(损坏/格式不支持):{img_abs_path}") + # BGR转RGB + img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB) + return img_rgb, img_rgb.shape \ No newline at end of file diff --git a/src/Car_Perception_Automatic_Sensing/data/preprocess.py b/src/Car_Perception_Automatic_Sensing/data/preprocess.py new file mode 100644 index 0000000000..7e3b4ab936 --- /dev/null +++ b/src/Car_Perception_Automatic_Sensing/data/preprocess.py @@ -0,0 +1,40 @@ +"""数据预处理工具:归一化、标准化""" +import numpy as np + + +def normalize_image(img: np.ndarray, target_range: tuple = (0, 1)) -> np.ndarray: + """ + 图像像素值归一化到指定范围 + :param img: 输入图像(np.ndarray, uint8/float32) + :param target_range: 目标范围,默认(0,1) + :return: 归一化后的图像(float32) + """ + img_float = img.astype(np.float32) + min_val = img_float.min() + max_val = img_float.max() + # 避免除零 + if max_val - min_val < 1e-6: + return np.zeros_like(img_float) + target_range[0] + # 归一化计算 + normalized = (img_float - min_val) / (max_val - min_val) + normalized = normalized * (target_range[1] - target_range[0]) + target_range[0] + return normalized + + +def standardize_image(img: np.ndarray, mean: list = None, std: list = None) -> np.ndarray: + """ + 图像标准化(减均值、除标准差) + :param img: 输入RGB图像(np.ndarray, (h,w,3)) + :param mean: 通道均值,默认ImageNet均值 + :param std: 通道标准差,默认ImageNet标准差 + :return: 标准化后的图像 + """ + if mean is None: + mean = [0.485, 0.456, 0.406] + if std is None: + std = [0.229, 0.224, 0.225] + + img_float = img.astype(np.float32) / 255.0 # 先归一化到0-1 + for i in range(3): + img_float[..., i] = (img_float[..., i] - mean[i]) / std[i] + return img_float \ No newline at end of file diff --git a/src/Car_Perception_Automatic_Sensing/data_loader.py b/src/Car_Perception_Automatic_Sensing/data_loader.py deleted file mode 100644 index eba4500e65..0000000000 --- a/src/Car_Perception_Automatic_Sensing/data_loader.py +++ /dev/null @@ -1,73 +0,0 @@ -""" -数据加载器模块 -负责自动驾驶感知任务的数据集加载、数据预处理分发等功能 -支持图像类数据集(如KITTI、COCO自动驾驶子集)的加载 -""" -import numpy as np -import torch -from torch.utils.data import Dataset, DataLoader - - -class AutoDriveDataset(Dataset): - """ - 自动驾驶感知数据集基类 - 所有自定义数据集需继承此类并实现抽象方法 - """ - def __init__(self, data_root: str, split: str = "train", transform=None): - """ - 初始化数据集 - :param data_root: 数据集根目录路径 - :param split: 数据集划分(train/val/test) - :param transform: 数据增强/预处理变换 - """ - self.data_root = data_root - self.split = split - self.transform = transform - self.sample_list = [] # 存储样本路径/索引的列表 - - # 后续将实现:加载样本列表 - self._load_sample_list() - - def _load_sample_list(self): - """ - 加载数据集样本列表(抽象方法,需子类实现) - """ - raise NotImplementedError("子类必须实现 _load_sample_list 方法") - - def __len__(self): - """ - 返回数据集样本总数 - """ - return len(self.sample_list) - - def __getitem__(self, idx: int): - """ - 根据索引获取单个样本(抽象方法,需子类实现) - :param idx: 样本索引 - :return: 处理后的图像和标注数据 - """ - raise NotImplementedError("子类必须实现 __getitem__ 方法") - - -def build_dataloader(dataset: Dataset, batch_size: int, shuffle: bool = True, num_workers: int = 4): - """ - 构建数据加载器 - :param dataset: 实例化的 Dataset 对象 - :param batch_size: 批次大小 - :param shuffle: 是否打乱样本顺序 - :param num_workers: 数据加载进程数 - :return: DataLoader 对象 - """ - dataloader = DataLoader( - dataset=dataset, - batch_size=batch_size, - shuffle=shuffle, - num_workers=num_workers, - pin_memory=True # 加速GPU数据传输 - ) - return dataloader - - -if __name__ == "__main__": - # 测试代码框架(后续可完善) - pass \ No newline at end of file diff --git a/src/Car_Perception_Automatic_Sensing/main.py b/src/Car_Perception_Automatic_Sensing/main.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000000..e69de29bb2