部署文件+resize_cls+resize_seg
@ -4,8 +4,8 @@ import numpy as np
|
||||
import os
|
||||
|
||||
# ================== 配置参数 ==================
|
||||
MODEL_PATH = r"/home/hx/yolo/ultralytics_yolo11-main/runs/train/exp_obb2/weights/best.pt"
|
||||
IMAGE_SOURCE_DIR = r"/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/f13" # 图像文件夹路径
|
||||
MODEL_PATH = r"/home/hx/yolo/ultralytics_yolo11-main/runs/train/exp_obb4/weights/best.pt"
|
||||
IMAGE_SOURCE_DIR = r"/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb2/test" # 图像文件夹路径
|
||||
OUTPUT_DIR = "./inference_results" # 输出结果保存路径
|
||||
IMG_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff', '.webp'}
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 93 KiB |
154
angle_base_obb/error_E.py
Normal file
@ -0,0 +1,154 @@
|
||||
import os
|
||||
import cv2
|
||||
import numpy as np
|
||||
from ultralytics import YOLO
|
||||
|
||||
# ================== 配置参数 ==================
|
||||
MODEL_PATH = r"/home/hx/yolo/ultralytics_yolo11-main/runs/train/exp_obb4/weights/best.pt"
|
||||
IMAGE_SOURCE_DIR = r"/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb2/test"
|
||||
LABEL_SOURCE_DIR = IMAGE_SOURCE_DIR # 假设标签和图像在同一目录
|
||||
OUTPUT_DIR = "./inference_results"
|
||||
|
||||
IMG_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff', '.webp'}
|
||||
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||
|
||||
# 加载模型
|
||||
print("🔄 加载 YOLO OBB 模型...")
|
||||
model = YOLO(MODEL_PATH)
|
||||
print("✅ 模型加载完成")
|
||||
|
||||
# 获取图像列表
|
||||
image_files = [
|
||||
f for f in os.listdir(IMAGE_SOURCE_DIR)
|
||||
if os.path.splitext(f.lower())[1] in IMG_EXTENSIONS
|
||||
]
|
||||
|
||||
if not image_files:
|
||||
print(f"❌ 错误:未找到图像文件")
|
||||
exit(1)
|
||||
print(f"📁 发现 {len(image_files)} 张图像待处理")
|
||||
all_angle_errors = [] # 存储每张图的夹角误差(度)
|
||||
|
||||
# ================== 工具函数 ==================
|
||||
|
||||
def parse_obb_label_file(label_path):
|
||||
"""解析 OBB 标签文件,返回 [{'cls': int, 'points': (4,2)}]"""
|
||||
boxes = []
|
||||
if not os.path.exists(label_path):
|
||||
return boxes
|
||||
with open(label_path, 'r') as f:
|
||||
for line in f:
|
||||
parts = line.strip().split()
|
||||
if len(parts) != 9:
|
||||
continue
|
||||
cls_id = int(parts[0])
|
||||
coords = list(map(float, parts[1:]))
|
||||
points = np.array(coords).reshape(4, 2)
|
||||
boxes.append({'cls': cls_id, 'points': points})
|
||||
return boxes
|
||||
|
||||
|
||||
def compute_main_direction(points):
|
||||
"""
|
||||
根据四个顶点计算旋转框的主方向(长边方向),
|
||||
返回 [0, π) 范围内的弧度值。
|
||||
"""
|
||||
edges = []
|
||||
for i in range(4):
|
||||
p1 = points[i]
|
||||
p2 = points[(i + 1) % 4]
|
||||
vec = p2 - p1
|
||||
length = np.linalg.norm(vec)
|
||||
if length > 1e-6:
|
||||
edges.append((length, vec))
|
||||
|
||||
if not edges:
|
||||
return 0.0
|
||||
|
||||
# 找最长边
|
||||
longest_edge = max(edges, key=lambda x: x[0])[1]
|
||||
angle_rad = np.arctan2(longest_edge[1], longest_edge[0])
|
||||
|
||||
# 归一化到 [0, π)
|
||||
angle_rad = angle_rad % np.pi
|
||||
return angle_rad
|
||||
|
||||
|
||||
def compute_min_angle_between_two_dirs(dir1_rad, dir2_rad):
|
||||
"""计算两个方向之间的最小夹角(0 ~ 90°),返回角度制"""
|
||||
diff = abs(dir1_rad - dir2_rad)
|
||||
min_diff_rad = min(diff, np.pi - diff)
|
||||
return np.degrees(min_diff_rad) # 返回 0~90°
|
||||
|
||||
|
||||
# ================== 主循环 ==================
|
||||
for img_filename in image_files:
|
||||
img_path = os.path.join(IMAGE_SOURCE_DIR, img_filename)
|
||||
label_path = os.path.join(LABEL_SOURCE_DIR, os.path.splitext(img_filename)[0] + ".txt")
|
||||
|
||||
print(f"\n🖼️ 处理: {img_filename}")
|
||||
|
||||
# 读图
|
||||
img = cv2.imread(img_path)
|
||||
if img is None:
|
||||
print("❌ 无法读取图像")
|
||||
continue
|
||||
|
||||
# 推理
|
||||
results = model(img, imgsz=640, conf=0.15, verbose=False)
|
||||
result = results[0]
|
||||
pred_boxes = result.obb
|
||||
|
||||
# === 提取预测框主方向 ===
|
||||
pred_dirs = []
|
||||
if pred_boxes is not None and len(pred_boxes) >= 2:
|
||||
for box in pred_boxes[:2]: # 只取前两个
|
||||
xywhr = box.xywhr.cpu().numpy()[0]
|
||||
cx, cy, w, h, r_rad = xywhr
|
||||
main_dir = r_rad if w >= h else r_rad + np.pi / 2
|
||||
pred_dirs.append(main_dir % np.pi)
|
||||
pred_angle = compute_min_angle_between_two_dirs(pred_dirs[0], pred_dirs[1])
|
||||
else:
|
||||
print("❌ 预测框不足两个")
|
||||
continue
|
||||
|
||||
# === 提取真实框主方向 ===
|
||||
true_boxes = parse_obb_label_file(label_path)
|
||||
if len(true_boxes) < 2:
|
||||
print("❌ 标签框不足两个")
|
||||
continue
|
||||
|
||||
true_dirs = []
|
||||
for tb in true_boxes[:2]: # 取前两个
|
||||
d = compute_main_direction(tb['points'])
|
||||
true_dirs.append(d)
|
||||
true_angle = compute_min_angle_between_two_dirs(true_dirs[0], true_dirs[1])
|
||||
|
||||
# === 计算夹角误差 ===
|
||||
error_deg = abs(pred_angle - true_angle)
|
||||
all_angle_errors.append(error_deg)
|
||||
|
||||
print(f" 🔹 预测夹角: {pred_angle:.2f}°")
|
||||
print(f" 🔹 真实夹角: {true_angle:.2f}°")
|
||||
print(f" 🔺 夹角误差: {error_deg:.2f}°")
|
||||
|
||||
# ================== 输出统计 ==================
|
||||
print("\n" + "=" * 60)
|
||||
print("📊 夹角误差统计(基于两框间最小夹角)")
|
||||
print("=" * 60)
|
||||
if all_angle_errors:
|
||||
mean_error = np.mean(all_angle_errors)
|
||||
std_error = np.std(all_angle_errors)
|
||||
max_error = np.max(all_angle_errors)
|
||||
min_error = np.min(all_angle_errors)
|
||||
|
||||
print(f"有效图像数: {len(all_angle_errors)}")
|
||||
print(f"平均夹角误差: {mean_error:.2f}°")
|
||||
print(f"标准差: {std_error:.2f}°")
|
||||
print(f"最大误差: {max_error:.2f}°")
|
||||
print(f"最小误差: {min_error:.2f}°")
|
||||
else:
|
||||
print("❌ 无有效数据用于统计")
|
||||
|
||||
print("=" * 60)
|
||||
print("🎉 所有图像处理完成!")
|
||||
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1009 KiB |
|
Before Width: | Height: | Size: 4.8 MiB |
|
Before Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 1004 KiB |
|
Before Width: | Height: | Size: 1021 KiB |
|
Before Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1.2 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1.2 MiB |
|
Before Width: | Height: | Size: 1.2 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1011 KiB |
|
Before Width: | Height: | Size: 996 KiB |
|
Before Width: | Height: | Size: 1023 KiB |
|
Before Width: | Height: | Size: 987 KiB |
|
Before Width: | Height: | Size: 981 KiB |
|
Before Width: | Height: | Size: 969 KiB |
|
Before Width: | Height: | Size: 949 KiB |
|
Before Width: | Height: | Size: 938 KiB |
|
Before Width: | Height: | Size: 942 KiB |
|
Before Width: | Height: | Size: 938 KiB |
|
Before Width: | Height: | Size: 954 KiB |
|
Before Width: | Height: | Size: 949 KiB |
|
Before Width: | Height: | Size: 865 KiB |
|
Before Width: | Height: | Size: 877 KiB |
|
Before Width: | Height: | Size: 918 KiB |
|
Before Width: | Height: | Size: 914 KiB |
|
Before Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1.2 MiB |
|
Before Width: | Height: | Size: 1.2 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 942 KiB |
|
Before Width: | Height: | Size: 970 KiB |
|
Before Width: | Height: | Size: 996 KiB |
|
Before Width: | Height: | Size: 980 KiB |
|
Before Width: | Height: | Size: 996 KiB |
|
Before Width: | Height: | Size: 1014 KiB |
|
Before Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1007 KiB |
|
Before Width: | Height: | Size: 1006 KiB |
|
Before Width: | Height: | Size: 1022 KiB |
|
Before Width: | Height: | Size: 1021 KiB |
|
Before Width: | Height: | Size: 1016 KiB |
|
Before Width: | Height: | Size: 1007 KiB |
|
Before Width: | Height: | Size: 974 KiB |
|
Before Width: | Height: | Size: 912 KiB |
|
Before Width: | Height: | Size: 931 KiB |
|
Before Width: | Height: | Size: 972 KiB |
|
Before Width: | Height: | Size: 981 KiB |
|
Before Width: | Height: | Size: 980 KiB |
|
Before Width: | Height: | Size: 971 KiB |
|
Before Width: | Height: | Size: 984 KiB |
|
Before Width: | Height: | Size: 952 KiB |
|
Before Width: | Height: | Size: 970 KiB |
|
Before Width: | Height: | Size: 971 KiB |
|
Before Width: | Height: | Size: 936 KiB |
|
Before Width: | Height: | Size: 997 KiB |
|
Before Width: | Height: | Size: 1020 KiB |
|
Before Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1.2 MiB |
|
Before Width: | Height: | Size: 1.2 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1.2 MiB |
|
Before Width: | Height: | Size: 992 KiB |
|
Before Width: | Height: | Size: 1000 KiB |
|
Before Width: | Height: | Size: 1013 KiB |
|
Before Width: | Height: | Size: 1024 KiB |
|
Before Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 910 KiB |
|
Before Width: | Height: | Size: 985 KiB |
|
Before Width: | Height: | Size: 1010 KiB |
|
Before Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 1024 KiB |
|
Before Width: | Height: | Size: 1018 KiB |
|
Before Width: | Height: | Size: 934 KiB |
|
Before Width: | Height: | Size: 904 KiB |
|
Before Width: | Height: | Size: 942 KiB |
|
Before Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 1.1 MiB |