|
2 weken geleden | |
---|---|---|
src | 2 weken geleden | |
tests | 2 maanden geleden | |
.gitignore | 2 weken geleden | |
README.md | 2 weken geleden | |
requirements.txt | 2 weken geleden |
基于 ONNX 的无人机检测系统,支持单张图片和批量处理。
本项目实现了多种UAV(无人机)检测模型的推理系统,支持三种不同的模型架构和相应的后处理方法。每种模型都有其特定的输入输出格式、后处理流程和可视化方式。
250411_Anti_UAV.onnx
scale_factor
,输出名称以 multiclass_nms3
开头UAV-250411.onnx
scale_factor
,输出为2个张量且第一个以 tmp_
开头uav_and_bird.onnx
scale_factor
,输出张量数量大于2pip install -r requirements.txt
# 处理单张图片
python -m src.core.inference --input path/to/image.jpg --output results
# 处理整个目录
python -m src.core.inference --input path/to/images_dir --output results
# 调整检测参数
python -m src.core.inference --input path/to/image.jpg --threshold 0.6 --max-bbox-ratio 0.1
# 保存未检测到目标的图片
python -m src.core.inference --input path/to/images_dir --save-empty
# 自动识别模型类型
python inference.py --input /path/to/images --threshold 0.5
# 指定模型类型
python inference.py --input /path/to/images --model-type uav_and_bird
# 批量处理
python inference.py --input /path/to/directory --output /path/to/results
python -m src.core.inference --gui
# 初始化检测器
detector = ONNXDetector(
input_dir="/path/to/images",
model_type="uav_and_bird",
prob_threshold=0.5
)
# 处理单张图像
detections, img_out, detection_list = detector.process_image("/path/to/image.jpg")
# 批量处理
total_detections = detector.process_directory()
--input
: 输入图像路径或目录(必需)--output
: 输出目录路径,默认为输入目录名+_results--threshold
: 检测置信度阈值,默认0.5--max-bbox-ratio
: 检测框最大面积比例阈值,默认0.05--save-empty
: 是否保存未检测到目标的图片--gui
: 启用图形界面选择输入目录--model-type
: 指定模型类型(可选,系统可自动识别)所有模型都采用相同的预处理流程:
# 图像预处理步骤
1. 颜色空间转换: BGR → RGB
2. 尺寸调整: 原图 → 640×640
3. 数据类型转换: uint8 → float32
4. 归一化: [0,255] → [0,1]
5. 标准化: 使用ImageNet均值和标准差
- mean = [0.485, 0.456, 0.406]
- std = [0.229, 0.224, 0.225]
6. 维度调整: HWC → CHW
7. 批次维度: CHW → NCHW
inputs = {
'image': img[None, :, :, :], # (1, 3, 640, 640)
'scale_factor': scale_factor[None, :] # (1, 2)
}
inputs = {
'images': img[None, :, :, :] # (1, 3, 640, 640)
}
特点: 模型内置NMS,输出已经过滤的检测结果
# 输出格式
bbox: (N, 4) # 边界框坐标 [x1, y1, x2, y2]
confidence: (N, 1) # 置信度分数
# 后处理流程
1. 置信度过滤: confidence > threshold
2. 坐标缩放: 模型输出 → 原图尺寸
3. 边界框验证: 面积比例检查
4. 结果保存和可视化
特点: 需要手动实现NMS处理
# 输出格式
output[0]: (N, 4) # 边界框坐标
output[1]: (N, 1) # 置信度分数
# 后处理流程
1. 置信度过滤: confidence > threshold
2. 坐标缩放: 模型输出 → 原图尺寸
3. NMS处理: IoU阈值 = 0.4
4. 边界框验证
5. 结果保存和可视化
特点: 多尺度输出,使用Distribution Focal Loss,需要复杂的解码过程
# 输出格式(3个尺度)
small_output: (1, 80, 80, 21) # 小目标检测
medium_output: (1, 40, 40, 21) # 中等目标检测
large_output: (1, 20, 20, 21) # 大目标检测
# 每个输出的通道分布
# 21 = 17(bbox) + 2(classes) + 2(quality)
# 后处理流程
1. 多尺度处理:
- 小尺度: stride=8, 网格80×80
- 中尺度: stride=16, 网格40×40
- 大尺度: stride=32, 网格20×20
2. Distribution Focal Loss解码:
- bbox_pred: 17维距离分布 → 4个距离值
- 使用softmax + 期望值计算实际距离
3. 网格坐标生成(带0.5偏移):
grid_x, grid_y = np.meshgrid(range(W), range(H))
grid_x = (grid_x.flatten() + 0.5)
grid_y = (grid_y.flatten() + 0.5)
4. 边界框计算:
x1 = (grid_x - d_left) * stride
y1 = (grid_y - d_top) * stride
x2 = (grid_x + d_right) * stride
y2 = (grid_y + d_bottom) * stride
5. 置信度计算:
final_scores = cls_pred * quality_pred
6. 多尺度结果合并和NMS处理
所有模型都使用相同的绘制方式:
# 边界框样式
cv2.rectangle(img_out, (x1, y1), (x2, y2), (255, 0, 0), 4) # 蓝色框,线宽4
# 标签样式
label = f'{class_name} {confidence:.2f}'
cv2.putText(img_out, label, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2) # 红色文字
# Anti_UAV 和 UAV-250411 模型
class_names = {0: 'UAV'}
# uav_and_bird 模型
class_names = {0: 'Bird', 1: 'UAV'}
# 保存检测到的目标区域
target_filename = f"{base_name}_{detection_count}.jpg"
cv2.imwrite(os.path.join(targets_dir, target_filename), roi)
# 保存带标注的完整图像
output_filename = f"detected_{base_name}"
cv2.imwrite(os.path.join(output_dir, output_filename), img_out)
# 面积比例检查
bbox_area = (x2 - x1) * (y2 - y1)
image_area = orig_w * orig_h
if bbox_area / image_area > max_bbox_ratio: # 默认0.05
continue
# 尺寸有效性检查
if x2 <= x1 or y2 <= y1:
continue
# 所有模型统一使用的NMS参数
conf_threshold = 0.5 # 置信度阈值
iou_threshold = 0.4 # IoU阈值
系统通过分析ONNX模型的输入输出结构自动识别模型类型:
def get_model_type(model_path: str) -> str:
model = onnx.load(model_path)
input_names = [i.name for i in model.graph.input]
output_names = [o.name for o in model.graph.output]
if 'scale_factor' in input_names:
if any(name.startswith('multiclass_nms3') for name in output_names):
return 'Anti_UAV'
else:
return 'UAV-250411'
else:
if len(output_names) > 2:
return 'uav_and_bird'
return 'unknown'
程序会在输出目录中生成以下内容:
detection_report.csv
: 检测报告,包含以下信息:
本项目实现了一个灵活的多模型UAV检测系统,具有以下特点:
每种模型都有其特定的应用场景和优势,用户可以根据实际需求选择合适的模型进行部署。