bushu
This commit is contained in:
@ -25,6 +25,7 @@ def predict_obb_best_angle(model_path, image_path, save_path=None):
|
||||
# 3. 推理 OBB
|
||||
results = model(img, save=False, imgsz=640, conf=0.5, mode='obb')
|
||||
result = results[0]
|
||||
print(result)
|
||||
|
||||
# 4. 可视化
|
||||
annotated_img = result.plot()
|
||||
@ -62,8 +63,8 @@ def predict_obb_best_angle(model_path, image_path, save_path=None):
|
||||
|
||||
# ------------------- 测试 -------------------
|
||||
if __name__ == "__main__":
|
||||
weight_path = r'best.pt'
|
||||
image_path = r"./test_image/3.jpg"
|
||||
weight_path = r'obb.pt'
|
||||
image_path = r"./test_image/7.jpg"
|
||||
save_path = "./inference_results/detected_3.jpg"
|
||||
|
||||
#angle_deg, annotated_img = predict_obb_best_angle(weight_path, image_path, save_path)
|
||||
|
||||
@ -91,8 +91,8 @@ def process_obb_images(model_path, image_dir, output_dir="./inference_results",
|
||||
|
||||
# ------------------- 测试调用 -------------------
|
||||
if __name__ == "__main__":
|
||||
MODEL_PATH = r'best.pt'
|
||||
IMAGE_SOURCE_DIR = r"./test_image"
|
||||
MODEL_PATH = r'obb.pt'
|
||||
IMAGE_SOURCE_DIR = r"/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb4/val"
|
||||
OUTPUT_DIR = "./inference_results"
|
||||
|
||||
results = process_obb_images(MODEL_PATH, IMAGE_SOURCE_DIR, OUTPUT_DIR)
|
||||
|
||||
Binary file not shown.
120
angle_base_obb/label_view.py
Normal file
120
angle_base_obb/label_view.py
Normal file
@ -0,0 +1,120 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import os
|
||||
|
||||
# =========================
|
||||
# 强制使用非 GUI 后端(关键!)
|
||||
# =========================
|
||||
import matplotlib
|
||||
|
||||
matplotlib.use('Agg') # 必须在 import pyplot 之前设置
|
||||
|
||||
|
||||
def visualize_obb(image_path, label_path, output_dir="output_visualizations"):
|
||||
"""
|
||||
可视化图片及其 OBB 标签,并保存结果图像到指定目录。
|
||||
|
||||
:param image_path: 图片路径
|
||||
:param label_path: 标签路径
|
||||
:param output_dir: 输出目录(自动创建)
|
||||
"""
|
||||
# 读取图像
|
||||
image = cv2.imread(image_path)
|
||||
if image is None:
|
||||
print(f"❌ 无法读取图像: {image_path}")
|
||||
return
|
||||
|
||||
h, w = image.shape[:2]
|
||||
print(f"✅ 正在处理图像: {os.path.basename(image_path)} | 尺寸: {w} x {h}")
|
||||
|
||||
# 创建用于绘图的副本(BGR → 绘图用)
|
||||
img_draw = image.copy()
|
||||
|
||||
# 读取标签
|
||||
try:
|
||||
with open(label_path, 'r') as f:
|
||||
lines = f.readlines()
|
||||
except Exception as e:
|
||||
print(f"❌ 无法读取标签文件 {label_path}: {e}")
|
||||
return
|
||||
|
||||
for line in lines:
|
||||
parts = line.strip().split()
|
||||
if len(parts) < 9:
|
||||
print(f"⚠️ 跳过无效标签行: {line}")
|
||||
continue
|
||||
|
||||
# 解析:class_id x1 y1 x2 y2 x3 y3 x4 y4
|
||||
try:
|
||||
points = np.array([float(x) for x in parts[1:9]]).reshape(4, 2)
|
||||
except:
|
||||
print(f"⚠️ 坐标解析失败: {line}")
|
||||
continue
|
||||
|
||||
# 归一化坐标 → 像素坐标
|
||||
points[:, 0] *= w # x
|
||||
points[:, 1] *= h # y
|
||||
points = np.int32(points)
|
||||
|
||||
# 绘制四边形(绿色)
|
||||
cv2.polylines(img_draw, [points], isClosed=True, color=(0, 255, 0), thickness=3)
|
||||
|
||||
# 绘制顶点(红色圆圈)
|
||||
for (x, y) in points:
|
||||
cv2.circle(img_draw, (x, y), 6, (0, 0, 255), -1) # 红色实心圆
|
||||
|
||||
# 转为 RGB 用于 matplotlib 保存
|
||||
img_rgb = cv2.cvtColor(img_draw, cv2.COLOR_BGR2RGB)
|
||||
|
||||
# 创建输出目录
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
# 生成输出路径
|
||||
filename = os.path.splitext(os.path.basename(image_path))[0] + "_vis.png"
|
||||
output_path = os.path.join(output_dir, filename)
|
||||
|
||||
# 使用 matplotlib 保存图像(不显示)
|
||||
plt.figure(figsize=(16, 9), dpi=100)
|
||||
plt.imshow(img_rgb)
|
||||
plt.title(f"OBB Visualization - {os.path.basename(image_path)}", fontsize=14)
|
||||
plt.axis('off')
|
||||
plt.tight_layout()
|
||||
plt.savefig(output_path, bbox_inches='tight', pad_inches=0)
|
||||
plt.close() # 释放内存
|
||||
|
||||
print(f"✅ 可视化结果已保存: {output_path}")
|
||||
|
||||
|
||||
def process_directory(directory):
|
||||
"""
|
||||
遍历目录,处理所有图片和对应的 .txt 标签文件
|
||||
"""
|
||||
print(f"🔍 开始处理目录: {directory}")
|
||||
count = 0
|
||||
|
||||
for filename in os.listdir(directory):
|
||||
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.tiff')):
|
||||
image_path = os.path.join(directory, filename)
|
||||
label_path = os.path.splitext(image_path)[0] + ".txt"
|
||||
|
||||
if os.path.exists(label_path):
|
||||
visualize_obb(image_path, label_path)
|
||||
count += 1
|
||||
else:
|
||||
print(f"🟡 跳过 (无标签): {filename}")
|
||||
|
||||
print(f"🎉 处理完成!共处理 {count} 张图像。")
|
||||
|
||||
|
||||
# =========================
|
||||
# 主程序入口
|
||||
# =========================
|
||||
if __name__ == "__main__":
|
||||
# 设置你的数据目录
|
||||
directory = '/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb4/labels'
|
||||
|
||||
if not os.path.exists(directory):
|
||||
raise FileNotFoundError(f"目录不存在: {directory}")
|
||||
|
||||
process_directory(directory)
|
||||
BIN
angle_base_obb/obb.pt
Normal file
BIN
angle_base_obb/obb.pt
Normal file
Binary file not shown.
BIN
angle_base_obb/test_image/4.jpg
Executable file
BIN
angle_base_obb/test_image/4.jpg
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 503 KiB |
BIN
angle_base_obb/test_image/5.jpg
Executable file
BIN
angle_base_obb/test_image/5.jpg
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 490 KiB |
BIN
angle_base_obb/test_image/6.jpg
Executable file
BIN
angle_base_obb/test_image/6.jpg
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 527 KiB |
101
angle_base_obb/tongji.py
Normal file
101
angle_base_obb/tongji.py
Normal file
@ -0,0 +1,101 @@
|
||||
import cv2
|
||||
import os
|
||||
import numpy as np
|
||||
from ultralytics import YOLO
|
||||
|
||||
IMG_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff', '.webp'}
|
||||
|
||||
|
||||
def process_obb_images_for_angle_distribution(model_path, image_dir, conf_thresh=0.15, imgsz=640):
|
||||
"""
|
||||
批量处理图像的 OBB 推理,计算每张图像检测目标的主方向和夹角,并统计夹角分布情况。
|
||||
|
||||
输入:
|
||||
model_path: YOLO 权重路径
|
||||
image_dir: 图像文件夹路径
|
||||
conf_thresh: 置信度阈值
|
||||
imgsz: 输入图像大小
|
||||
输出:
|
||||
angle_distribution: {'<6': count, '6-20': count, '>20': count}
|
||||
"""
|
||||
results_dict = {}
|
||||
angle_distribution = {'<6': 0, '6-20': 0, '>20': 0}
|
||||
|
||||
print("加载 YOLO 模型...")
|
||||
model = YOLO(model_path)
|
||||
print("✅ 模型加载完成")
|
||||
|
||||
# 获取图像文件
|
||||
image_files = [f for f in os.listdir(image_dir) if os.path.splitext(f.lower())[1] in IMG_EXTENSIONS]
|
||||
if not image_files:
|
||||
print(f"❌ 未找到图像文件:{image_dir}")
|
||||
return angle_distribution
|
||||
|
||||
print(f"发现 {len(image_files)} 张图像待处理")
|
||||
|
||||
for img_filename in image_files:
|
||||
img_path = os.path.join(image_dir, img_filename)
|
||||
print(f"\n正在处理:{img_filename}")
|
||||
|
||||
img = cv2.imread(img_path)
|
||||
if img is None:
|
||||
print(f"❌ 跳过:无法读取图像 {img_path}")
|
||||
continue
|
||||
|
||||
# 推理 OBB
|
||||
results = model(img, save=False, imgsz=imgsz, conf=conf_thresh, mode='obb')
|
||||
result = results[0]
|
||||
|
||||
# 提取旋转角
|
||||
boxes = result.obb
|
||||
angles_deg = []
|
||||
if boxes is None or len(boxes) == 0:
|
||||
print("❌ 该图像中未检测到任何目标")
|
||||
else:
|
||||
for i, box in enumerate(boxes):
|
||||
cx, cy, w, h, r_rad = box.xywhr.cpu().numpy()[0]
|
||||
direction = r_rad if w >= h else r_rad + np.pi / 2
|
||||
direction = direction % np.pi
|
||||
angle_deg = np.degrees(direction)
|
||||
angles_deg.append(angle_deg)
|
||||
|
||||
# 两两夹角
|
||||
pairwise_angles_deg = []
|
||||
if len(angles_deg) >= 2:
|
||||
for i in range(len(angles_deg)):
|
||||
for j in range(i + 1, len(angles_deg)):
|
||||
diff_rad = abs(np.radians(angles_deg[i]) - np.radians(angles_deg[j]))
|
||||
min_diff_rad = min(diff_rad, np.pi - diff_rad)
|
||||
angle_deg_diff = np.degrees(min_diff_rad)
|
||||
pairwise_angles_deg.append(angle_deg_diff)
|
||||
|
||||
# 更新角度分布统计
|
||||
if angle_deg_diff < 6:
|
||||
angle_distribution['<6'] += 1
|
||||
elif 6 <= angle_deg_diff <= 20:
|
||||
angle_distribution['6-20'] += 1
|
||||
else:
|
||||
angle_distribution['>20'] += 1
|
||||
|
||||
print(f" Box {i + 1} 与 Box {j + 1} 夹角: {angle_deg_diff:.2f}°")
|
||||
|
||||
# 保存每张图像结果
|
||||
results_dict[img_filename] = {
|
||||
"angles_deg": angles_deg,
|
||||
"pairwise_angles_deg": pairwise_angles_deg
|
||||
}
|
||||
|
||||
print("\n所有图像处理完成!")
|
||||
return angle_distribution
|
||||
|
||||
|
||||
# ------------------- 测试调用 -------------------
|
||||
if __name__ == "__main__":
|
||||
MODEL_PATH = r'best1.pt'
|
||||
IMAGE_SOURCE_DIR = r"/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb3/train"
|
||||
|
||||
distribution = process_obb_images_for_angle_distribution(MODEL_PATH, IMAGE_SOURCE_DIR)
|
||||
print("\n夹角分布统计:")
|
||||
print(f"小于6度的夹角数量: {distribution['<6']}")
|
||||
print(f"在6至20度之间的夹角数量: {distribution['6-20']}")
|
||||
print(f"大于20度的夹角数量: {distribution['>20']}")
|
||||
@ -2,15 +2,18 @@ import os
|
||||
import cv2
|
||||
import numpy as np
|
||||
from ultralytics import YOLO
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# ================== 配置参数 ==================
|
||||
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"
|
||||
MODEL_PATH = r"obb.pt"
|
||||
IMAGE_SOURCE_DIR = r"/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb4/train"
|
||||
LABEL_SOURCE_DIR = IMAGE_SOURCE_DIR # 假设标签和图像在同一目录
|
||||
OUTPUT_DIR = "./inference_results"
|
||||
|
||||
VISUAL_DIR = os.path.join(OUTPUT_DIR, "visual_errors_gt5deg") # 保存误差 >5° 的可视化图
|
||||
IMG_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff', '.webp'}
|
||||
|
||||
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||
os.makedirs(VISUAL_DIR, exist_ok=True)
|
||||
|
||||
# 加载模型
|
||||
print("🔄 加载 YOLO OBB 模型...")
|
||||
@ -27,32 +30,38 @@ 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)}]"""
|
||||
def parse_obb_label_file(label_path, img_shape):
|
||||
"""
|
||||
解析 OBB 标签文件,并将归一化坐标转换为像素坐标
|
||||
img_shape: (height, width) 用于去归一化
|
||||
"""
|
||||
boxes = []
|
||||
h, w = img_shape[:2]
|
||||
if not os.path.exists(label_path):
|
||||
print(f"⚠️ 标签文件不存在: {label_path}")
|
||||
return boxes
|
||||
with open(label_path, 'r') as f:
|
||||
for line in f:
|
||||
parts = line.strip().split()
|
||||
if len(parts) != 9:
|
||||
print(f"⚠️ 标签行格式错误 (期望9列): {parts}")
|
||||
continue
|
||||
cls_id = int(parts[0])
|
||||
coords = list(map(float, parts[1:]))
|
||||
points = np.array(coords).reshape(4, 2)
|
||||
points[:, 0] *= w # x * width
|
||||
points[:, 1] *= h # y * height
|
||||
boxes.append({'cls': cls_id, 'points': points})
|
||||
return boxes
|
||||
|
||||
|
||||
def compute_main_direction(points):
|
||||
"""
|
||||
根据四个顶点计算旋转框的主方向(长边方向),
|
||||
返回 [0, π) 范围内的弧度值。
|
||||
"""
|
||||
"""根据四个顶点计算旋转框的主方向(长边方向),返回 [0, π) 范围内的弧度值"""
|
||||
edges = []
|
||||
for i in range(4):
|
||||
p1 = points[i]
|
||||
@ -65,11 +74,8 @@ def compute_main_direction(points):
|
||||
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
|
||||
|
||||
@ -78,9 +84,29 @@ 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°
|
||||
return np.degrees(min_diff_rad)
|
||||
|
||||
|
||||
def draw_boxes_on_image(image, pred_boxes=None, true_boxes=None):
|
||||
"""在图像上绘制预测框(绿色)和真实框(红色)"""
|
||||
img_vis = image.copy()
|
||||
|
||||
# 绘制真实框(红色)
|
||||
if true_boxes is not None:
|
||||
for box in true_boxes:
|
||||
pts = np.int32(box['points']).reshape((-1, 1, 2))
|
||||
cv2.polylines(img_vis, [pts], isClosed=True, color=(0, 0, 255), thickness=2)
|
||||
|
||||
# 绘制预测框(绿色)
|
||||
if pred_boxes is not None:
|
||||
for box in pred_boxes:
|
||||
xyxyxyxy = box.xyxyxyxy.cpu().numpy()[0]
|
||||
pts = xyxyxyxy.reshape(4, 2).astype(int)
|
||||
pts = pts.reshape((-1, 1, 2))
|
||||
cv2.polylines(img_vis, [pts], isClosed=True, color=(0, 255, 0), thickness=2)
|
||||
|
||||
return img_vis
|
||||
|
||||
# ================== 主循环 ==================
|
||||
for img_filename in image_files:
|
||||
img_path = os.path.join(IMAGE_SOURCE_DIR, img_filename)
|
||||
@ -102,7 +128,7 @@ for img_filename in image_files:
|
||||
# === 提取预测框主方向 ===
|
||||
pred_dirs = []
|
||||
if pred_boxes is not None and len(pred_boxes) >= 2:
|
||||
for box in 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
|
||||
@ -113,13 +139,13 @@ for img_filename in image_files:
|
||||
continue
|
||||
|
||||
# === 提取真实框主方向 ===
|
||||
true_boxes = parse_obb_label_file(label_path)
|
||||
true_boxes = parse_obb_label_file(label_path, img.shape)
|
||||
if len(true_boxes) < 2:
|
||||
print("❌ 标签框不足两个")
|
||||
continue
|
||||
|
||||
true_dirs = []
|
||||
for tb in true_boxes[:2]: # 取前两个
|
||||
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])
|
||||
@ -132,6 +158,17 @@ for img_filename in image_files:
|
||||
print(f" 🔹 真实夹角: {true_angle:.2f}°")
|
||||
print(f" 🔺 夹角误差: {error_deg:.2f}°")
|
||||
|
||||
# === 可视化误差 >5° 的情况 ===
|
||||
if error_deg > 3:
|
||||
print(f" 🎯 误差 >5°,生成可视化图像...")
|
||||
img_with_boxes = draw_boxes_on_image(img, pred_boxes=pred_boxes, true_boxes=true_boxes)
|
||||
# 添加文字
|
||||
cv2.putText(img_with_boxes, f"Error: {error_deg:.2f}°", (20, 50),
|
||||
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
|
||||
vis_output_path = os.path.join(VISUAL_DIR, f"error_{error_deg:.2f}deg_{img_filename}")
|
||||
cv2.imwrite(vis_output_path, img_with_boxes)
|
||||
print(f" ✅ 已保存可视化图像: {vis_output_path}")
|
||||
|
||||
# ================== 输出统计 ==================
|
||||
print("\n" + "=" * 60)
|
||||
print("📊 夹角误差统计(基于两框间最小夹角)")
|
||||
Reference in New Issue
Block a user