This commit is contained in:
琉璃月光
2025-10-21 14:11:52 +08:00
parent 349449f2b7
commit df7c0730f5
363 changed files with 5386 additions and 578 deletions

View File

@ -1,105 +0,0 @@
from ultralytics import YOLO
import cv2
import os
import numpy as np
def get_best_obb_angle(image_path, weight_path, return_degree=False):
"""
输入:
image_path: 图像路径
weight_path: YOLO权重路径
return_degree: 是否返回角度单位为度,默认 False返回弧度
输出:
置信度最高目标的旋转角
如果未检测到目标返回 None
"""
# 读取图像
img = cv2.imread(image_path)
if img is None:
print(f"❌ 无法读取图像:{image_path}")
return None
# 加载模型并预测
model = YOLO(weight_path)
results = model(img, save=False, imgsz=640, conf=0.15, mode='obb')
result = results[0]
boxes = result.obb
if not boxes:
print("⚠️ 未检测到目标。")
return None
# 取置信度最高框的旋转角
best_box = max(boxes, key=lambda x: x.conf.cpu().numpy()[0])
r = best_box.xywhr.cpu().numpy()[0][4] # 弧度
if return_degree:
return np.degrees(r)
else:
return r
def save_obb_visual(image_path, weight_path, save_path):
"""
输入:
image_path: 图像路径
weight_path: YOLO权重路径
save_path: 保存带角度标注图像路径
功能:
检测 OBB 并标注置信度最高框旋转角度,保存图片
"""
img = cv2.imread(image_path)
if img is None:
print(f"❌ 无法读取图像:{image_path}")
return
model = YOLO(weight_path)
results = model(img, save=False, imgsz=640, conf=0.15, mode='obb')
result = results[0]
boxes = result.obb
if not boxes:
print("⚠️ 未检测到目标。")
return
best_box = max(boxes, key=lambda x: x.conf.cpu().numpy()[0])
cx, cy, w, h, r = best_box.xywhr.cpu().numpy()[0]
angle_deg = np.degrees(r)
# 绘制 OBB
annotated_img = img.copy()
rect = ((cx, cy), (w, h), angle_deg)
box_pts = cv2.boxPoints(rect).astype(int)
cv2.polylines(annotated_img, [box_pts], isClosed=True, color=(0, 255, 0), thickness=2)
# 标注角度
text = f"{angle_deg:.1f}°"
font_scale = max(0.5, min(w, h)/100)
thickness = 2
text_size, _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, font_scale, thickness)
text_x = int(cx - text_size[0]/2)
text_y = int(cy + text_size[1]/2)
cv2.putText(annotated_img, text, (text_x, text_y),
cv2.FONT_HERSHEY_SIMPLEX, font_scale, (0, 0, 255), thickness)
# 保存
os.makedirs(os.path.dirname(save_path), exist_ok=True)
cv2.imwrite(save_path, annotated_img)
print(f"✅ 检测结果已保存至: {save_path}")
# ===============================
# 示例调用
# ===============================
if __name__ == "__main__":
weight = r"/home/hx/yolo/ultralytics_yolo11-main/runs/train/exp_obb3/weights/best.pt"
image = r"/home/hx/yolo/output_masks/2.jpg"
save_path = "./inference_results/best_detected_2.jpg"
angle_rad = get_best_obb_angle(image, weight)
print(f"旋转角(弧度):{angle_rad:.4f}")
angle_deg = get_best_obb_angle(image, weight, return_degree=True)
print(f"旋转角(度):{angle_deg:.2f}°")
save_obb_visual(image, weight, save_path)

View File

@ -1,171 +0,0 @@
import cv2
import numpy as np
import math
from shapely.geometry import Polygon
from rknnlite.api import RKNNLite
import os
CLASSES = ['clamp']
nmsThresh = 0.4
objectThresh = 0.5
# ------------------- 工具函数 -------------------
def letterbox_resize(image, size, bg_color=114):
target_width, target_height = size
image_height, image_width, _ = image.shape
scale = min(target_width / image_width, target_height / image_height)
new_width, new_height = int(image_width * scale), int(image_height * scale)
image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
canvas = np.ones((target_height, target_width, 3), dtype=np.uint8) * bg_color
offset_x, offset_y = (target_width - new_width) // 2, (target_height - new_height) // 2
canvas[offset_y:offset_y + new_height, offset_x:offset_x + new_width] = image
return canvas, scale, offset_x, offset_y
class DetectBox:
def __init__(self, classId, score, xmin, ymin, xmax, ymax, angle):
self.classId = classId
self.score = score
self.xmin = xmin
self.ymin = ymin
self.xmax = xmax
self.ymax = ymax
self.angle = angle
def rotate_rectangle(x1, y1, x2, y2, a):
cx, cy = (x1 + x2) / 2, (y1 + y2) / 2
x1_new = int((x1 - cx) * math.cos(a) - (y1 - cy) * math.sin(a) + cx)
y1_new = int((x1 - cx) * math.sin(a) + (y1 - cy) * math.cos(a) + cy)
x2_new = int((x2 - cx) * math.cos(a) - (y2 - cy) * math.sin(a) + cx)
y2_new = int((x2 - cx) * math.sin(a) + (y2 - cy) * math.cos(a) + cy)
x3_new = int((x1 - cx) * math.cos(a) - (y2 - cy) * math.sin(a) + cx)
y3_new = int((x1 - cx) * math.sin(a) + (y2 - cy) * math.cos(a) + cy)
x4_new = int((x2 - cx) * math.cos(a) - (y1 - cy) * math.sin(a) + cx)
y4_new = int((x2 - cx) * math.sin(a) + (y1 - cy) * math.cos(a) + cy)
return [(x1_new, y1_new), (x3_new, y3_new), (x2_new, y2_new), (x4_new, y4_new)]
def intersection(g, p):
g = Polygon(np.array(g).reshape(-1,2))
p = Polygon(np.array(p).reshape(-1,2))
if not g.is_valid or not p.is_valid:
return 0
inter = g.intersection(p).area
union = g.area + p.area - inter
return 0 if union == 0 else inter / union
def NMS(detectResult):
predBoxs = []
sort_detectboxs = sorted(detectResult, key=lambda x: x.score, reverse=True)
for i in range(len(sort_detectboxs)):
if sort_detectboxs[i].classId == -1:
continue
p1 = rotate_rectangle(sort_detectboxs[i].xmin, sort_detectboxs[i].ymin,
sort_detectboxs[i].xmax, sort_detectboxs[i].ymax,
sort_detectboxs[i].angle)
predBoxs.append(sort_detectboxs[i])
for j in range(i + 1, len(sort_detectboxs)):
if sort_detectboxs[j].classId == sort_detectboxs[i].classId:
p2 = rotate_rectangle(sort_detectboxs[j].xmin, sort_detectboxs[j].ymin,
sort_detectboxs[j].xmax, sort_detectboxs[j].ymax,
sort_detectboxs[j].angle)
if intersection(p1, p2) > nmsThresh:
sort_detectboxs[j].classId = -1
return predBoxs
def sigmoid(x):
return np.where(x >= 0, 1 / (1 + np.exp(-x)), np.exp(x) / (1 + np.exp(x)))
def softmax(x, axis=-1):
exp_x = np.exp(x - np.max(x, axis=axis, keepdims=True))
return exp_x / np.sum(exp_x, axis=axis, keepdims=True)
def process(out, model_w, model_h, stride, angle_feature, index, scale_w=1, scale_h=1):
class_num = len(CLASSES)
angle_feature = angle_feature.reshape(-1)
xywh = out[:, :64, :]
conf = sigmoid(out[:, 64:, :]).reshape(-1)
boxes = []
for ik in range(model_h * model_w * class_num):
if conf[ik] > objectThresh:
w = ik % model_w
h = (ik % (model_w * model_h)) // model_w
c = ik // (model_w * model_h)
# 解析xywh
xywh_ = xywh[0, :, (h * model_w) + w].reshape(1, 4, 16, 1)
data = np.arange(16).reshape(1, 1, 16, 1)
xywh_ = softmax(xywh_, 2)
xywh_ = np.sum(xywh_ * data, axis=2).reshape(-1)
xywh_add = xywh_[:2] + xywh_[2:]
xywh_sub = (xywh_[2:] - xywh_[:2]) / 2
# 安全取角度
angle_idx = min(index + (h * model_w) + w, len(angle_feature) - 1)
angle = (angle_feature[angle_idx] - 0.25) * math.pi
cos_a, sin_a = math.cos(angle), math.sin(angle)
xy = xywh_sub[0] * cos_a - xywh_sub[1] * sin_a, xywh_sub[0] * sin_a + xywh_sub[1] * cos_a
xywh1 = np.array([xy[0] + w + 0.5, xy[1] + h + 0.5, xywh_add[0], xywh_add[1]])
xywh1 *= stride
xmin = (xywh1[0] - xywh1[2]/2) * scale_w
ymin = (xywh1[1] - xywh1[3]/2) * scale_h
xmax = (xywh1[0] + xywh1[2]/2) * scale_w
ymax = (xywh1[1] + xywh1[3]/2) * scale_h
boxes.append(DetectBox(c, conf[ik], xmin, ymin, xmax, ymax, angle))
return boxes
# ------------------- 主函数 -------------------
def detect_boxes_angle_rknn(model_path, image_path, save_path=None):
img = cv2.imread(image_path)
if img is None:
print(f"❌ 无法读取图像: {image_path}")
return None, None
img_resized, scale, offset_x, offset_y = letterbox_resize(img, (640, 640))
infer_img = cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB)
infer_img = np.expand_dims(infer_img, 0)
rknn_lite = RKNNLite(verbose=False)
rknn_lite.load_rknn(model_path)
rknn_lite.init_runtime(core_mask=RKNNLite.NPU_CORE_0)
results = rknn_lite.inference([infer_img])
detect_boxes = []
for x in results[:-1]:
index, stride = 0, 0
if x.shape[2] == 20:
stride, index = 32, 20*4*20*4 + 20*2*20*2
elif x.shape[2] == 40:
stride, index = 16, 20*4*20*4
elif x.shape[2] == 80:
stride, index = 8, 0
feature = x.reshape(1, 65, -1)
detect_boxes += process(feature, x.shape[3], x.shape[2], stride, results[-1], index)
detect_boxes = NMS(detect_boxes)
# 输出每个检测框角度
for i, box in enumerate(detect_boxes):
print(f"{i+1}: angle = {box.angle:.4f} rad ({np.degrees(box.angle):.2f}°)")
if save_path:
xmin = int((box.xmin - offset_x)/scale)
ymin = int((box.ymin - offset_y)/scale)
xmax = int((box.xmax - offset_x)/scale)
ymax = int((box.ymax - offset_y)/scale)
points = rotate_rectangle(xmin, ymin, xmax, ymax, box.angle)
cv2.polylines(img, [np.array(points, np.int32)], True, (0, 255, 0), 1)
cv2.putText(img, f"{np.degrees(box.angle):.1f}°", (xmin, ymin-5),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 1)
if save_path:
os.makedirs(os.path.dirname(save_path), exist_ok=True)
cv2.imwrite(save_path, img)
print(f"✅ 带角度的检测结果已保存到 {save_path}")
rknn_lite.release()
return detect_boxes, img
# ------------------- 使用示例 -------------------
if __name__ == "__main__":
model_path = "obb.rknn"
image_path = "2.jpg"
save_path = "./inference_results/boxes_with_angle.jpg"
os.makedirs(os.path.dirname(save_path), exist_ok=True)
detect_boxes_angle_rknn(model_path, image_path, save_path)

View File

@ -25,6 +25,7 @@ def predict_obb_best_angle(model_path, image_path, save_path=None):
# 3. 推理 OBB # 3. 推理 OBB
results = model(img, save=False, imgsz=640, conf=0.5, mode='obb') results = model(img, save=False, imgsz=640, conf=0.5, mode='obb')
result = results[0] result = results[0]
print(result)
# 4. 可视化 # 4. 可视化
annotated_img = result.plot() annotated_img = result.plot()
@ -62,8 +63,8 @@ def predict_obb_best_angle(model_path, image_path, save_path=None):
# ------------------- 测试 ------------------- # ------------------- 测试 -------------------
if __name__ == "__main__": if __name__ == "__main__":
weight_path = r'best.pt' weight_path = r'obb.pt'
image_path = r"./test_image/3.jpg" image_path = r"./test_image/7.jpg"
save_path = "./inference_results/detected_3.jpg" save_path = "./inference_results/detected_3.jpg"
#angle_deg, annotated_img = predict_obb_best_angle(weight_path, image_path, save_path) #angle_deg, annotated_img = predict_obb_best_angle(weight_path, image_path, save_path)

View File

@ -91,8 +91,8 @@ def process_obb_images(model_path, image_dir, output_dir="./inference_results",
# ------------------- 测试调用 ------------------- # ------------------- 测试调用 -------------------
if __name__ == "__main__": if __name__ == "__main__":
MODEL_PATH = r'best.pt' MODEL_PATH = r'obb.pt'
IMAGE_SOURCE_DIR = r"./test_image" IMAGE_SOURCE_DIR = r"/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb4/val"
OUTPUT_DIR = "./inference_results" OUTPUT_DIR = "./inference_results"
results = process_obb_images(MODEL_PATH, IMAGE_SOURCE_DIR, OUTPUT_DIR) results = process_obb_images(MODEL_PATH, IMAGE_SOURCE_DIR, OUTPUT_DIR)

Binary file not shown.

View 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

Binary file not shown.

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 490 KiB

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
View 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']}")

View File

@ -2,15 +2,18 @@ import os
import cv2 import cv2
import numpy as np import numpy as np
from ultralytics import YOLO 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" MODEL_PATH = r"obb.pt"
IMAGE_SOURCE_DIR = r"/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb2/test" IMAGE_SOURCE_DIR = r"/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb4/train"
LABEL_SOURCE_DIR = IMAGE_SOURCE_DIR # 假设标签和图像在同一目录 LABEL_SOURCE_DIR = IMAGE_SOURCE_DIR # 假设标签和图像在同一目录
OUTPUT_DIR = "./inference_results" OUTPUT_DIR = "./inference_results"
VISUAL_DIR = os.path.join(OUTPUT_DIR, "visual_errors_gt5deg") # 保存误差 >5° 的可视化图
IMG_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff', '.webp'} IMG_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff', '.webp'}
os.makedirs(OUTPUT_DIR, exist_ok=True) os.makedirs(OUTPUT_DIR, exist_ok=True)
os.makedirs(VISUAL_DIR, exist_ok=True)
# 加载模型 # 加载模型
print("🔄 加载 YOLO OBB 模型...") print("🔄 加载 YOLO OBB 模型...")
@ -27,32 +30,38 @@ if not image_files:
print(f"❌ 错误:未找到图像文件") print(f"❌ 错误:未找到图像文件")
exit(1) exit(1)
print(f"📁 发现 {len(image_files)} 张图像待处理") print(f"📁 发现 {len(image_files)} 张图像待处理")
all_angle_errors = [] # 存储每张图的夹角误差(度) all_angle_errors = [] # 存储每张图的夹角误差(度)
# ================== 工具函数 ================== # ================== 工具函数 ==================
def parse_obb_label_file(label_path): def parse_obb_label_file(label_path, img_shape):
"""解析 OBB 标签文件,返回 [{'cls': int, 'points': (4,2)}]""" """
解析 OBB 标签文件并将归一化坐标转换为像素坐标
img_shape: (height, width) 用于去归一化
"""
boxes = [] boxes = []
h, w = img_shape[:2]
if not os.path.exists(label_path): if not os.path.exists(label_path):
print(f"⚠️ 标签文件不存在: {label_path}")
return boxes return boxes
with open(label_path, 'r') as f: with open(label_path, 'r') as f:
for line in f: for line in f:
parts = line.strip().split() parts = line.strip().split()
if len(parts) != 9: if len(parts) != 9:
print(f"⚠️ 标签行格式错误 (期望9列): {parts}")
continue continue
cls_id = int(parts[0]) cls_id = int(parts[0])
coords = list(map(float, parts[1:])) coords = list(map(float, parts[1:]))
points = np.array(coords).reshape(4, 2) points = np.array(coords).reshape(4, 2)
points[:, 0] *= w # x * width
points[:, 1] *= h # y * height
boxes.append({'cls': cls_id, 'points': points}) boxes.append({'cls': cls_id, 'points': points})
return boxes return boxes
def compute_main_direction(points): def compute_main_direction(points):
""" """根据四个顶点计算旋转框的主方向(长边方向),返回 [0, π) 范围内的弧度值"""
根据四个顶点计算旋转框的主方向长边方向
返回 [0, π) 范围内的弧度值
"""
edges = [] edges = []
for i in range(4): for i in range(4):
p1 = points[i] p1 = points[i]
@ -65,11 +74,8 @@ def compute_main_direction(points):
if not edges: if not edges:
return 0.0 return 0.0
# 找最长边
longest_edge = max(edges, key=lambda x: x[0])[1] longest_edge = max(edges, key=lambda x: x[0])[1]
angle_rad = np.arctan2(longest_edge[1], longest_edge[0]) angle_rad = np.arctan2(longest_edge[1], longest_edge[0])
# 归一化到 [0, π)
angle_rad = angle_rad % np.pi angle_rad = angle_rad % np.pi
return angle_rad return angle_rad
@ -78,9 +84,29 @@ def compute_min_angle_between_two_dirs(dir1_rad, dir2_rad):
"""计算两个方向之间的最小夹角0 ~ 90°返回角度制""" """计算两个方向之间的最小夹角0 ~ 90°返回角度制"""
diff = abs(dir1_rad - dir2_rad) diff = abs(dir1_rad - dir2_rad)
min_diff_rad = min(diff, np.pi - diff) 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: for img_filename in image_files:
img_path = os.path.join(IMAGE_SOURCE_DIR, img_filename) img_path = os.path.join(IMAGE_SOURCE_DIR, img_filename)
@ -102,7 +128,7 @@ for img_filename in image_files:
# === 提取预测框主方向 === # === 提取预测框主方向 ===
pred_dirs = [] pred_dirs = []
if pred_boxes is not None and len(pred_boxes) >= 2: 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] xywhr = box.xywhr.cpu().numpy()[0]
cx, cy, w, h, r_rad = xywhr cx, cy, w, h, r_rad = xywhr
main_dir = r_rad if w >= h else r_rad + np.pi / 2 main_dir = r_rad if w >= h else r_rad + np.pi / 2
@ -113,13 +139,13 @@ for img_filename in image_files:
continue continue
# === 提取真实框主方向 === # === 提取真实框主方向 ===
true_boxes = parse_obb_label_file(label_path) true_boxes = parse_obb_label_file(label_path, img.shape)
if len(true_boxes) < 2: if len(true_boxes) < 2:
print("❌ 标签框不足两个") print("❌ 标签框不足两个")
continue continue
true_dirs = [] true_dirs = []
for tb in true_boxes[:2]: # 取前两个 for tb in true_boxes[:2]:
d = compute_main_direction(tb['points']) d = compute_main_direction(tb['points'])
true_dirs.append(d) true_dirs.append(d)
true_angle = compute_min_angle_between_two_dirs(true_dirs[0], true_dirs[1]) 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" 🔹 真实夹角: {true_angle:.2f}°")
print(f" 🔺 夹角误差: {error_deg:.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("\n" + "=" * 60)
print("📊 夹角误差统计(基于两框间最小夹角)") print("📊 夹角误差统计(基于两框间最小夹角)")

137
camera/siyi_caiji.py Normal file
View File

@ -0,0 +1,137 @@
import cv2
import time
import os
import numpy as np
from PIL import Image
from skimage.metrics import structural_similarity as ssim
import shutil
# ================== 配置参数 ==================
rtsp_url = "rtsp://192.168.144.25:8554/main.264" # RTSP 流地址
capture_interval = 1.0 # 每隔多少秒采集一次(单位:秒)
SSIM_THRESHOLD = 0.9 # SSIM 相似度阈值,>0.9 认为太像
output_dir = os.path.join("userdata", "image") # 图片保存路径
# 灰色判断参数
GRAY_LOWER = 70
GRAY_UPPER = 230
GRAY_RATIO_THRESHOLD = 0.7
# 创建输出目录
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print(f"已创建目录: {output_dir}")
def is_large_gray(image, gray_lower=GRAY_LOWER, gray_upper=GRAY_UPPER, ratio_thresh=GRAY_RATIO_THRESHOLD):
"""
判断图片是否大面积为灰色R/G/B 都在 [gray_lower, gray_upper] 区间)
"""
img_array = np.array(image)
if len(img_array.shape) != 3 or img_array.shape[2] != 3:
return True # 非三通道图视为无效/灰色
h, w, _ = img_array.shape
total = h * w
gray_mask = (
(img_array[:, :, 0] >= gray_lower) & (img_array[:, :, 0] <= gray_upper) &
(img_array[:, :, 1] >= gray_lower) & (img_array[:, :, 1] <= gray_upper) &
(img_array[:, :, 2] >= gray_lower) & (img_array[:, :, 2] <= gray_upper)
)
gray_pixels = np.sum(gray_mask)
gray_ratio = gray_pixels / total
return gray_ratio > ratio_thresh
max_retry_seconds = 10 # 最大重试时间为10秒
retry_interval_seconds = 1 # 每隔1秒尝试重新连接一次
last_gray = None # 用于 SSIM 去重
while True: # 外层循环用于处理重新连接逻辑
cap = cv2.VideoCapture(rtsp_url)
start_time = time.time() # 记录开始尝试连接的时间
while not cap.isOpened():
if time.time() - start_time >= max_retry_seconds:
print(f"已尝试重新连接 {max_retry_seconds} 秒,但仍无法获取视频流。")
exit()
print("无法打开摄像头,正在尝试重新连接...")
time.sleep(retry_interval_seconds)
cap = cv2.VideoCapture(rtsp_url)
print("✅ 开始读取视频流...")
last_capture_time = time.time()
frame_count = 0
try:
while True:
ret, frame = cap.read()
if not ret:
print("读取帧失败,可能是流中断或摄像头断开")
cap.release()
break
current_time = time.time()
if current_time - last_capture_time < capture_interval:
continue
frame_count += 1
last_capture_time = current_time
print(f"处理帧 {frame_count}")
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(rgb_frame)
if is_large_gray(pil_image):
print(f"跳过:大面积灰色图像 (frame_{frame_count})")
continue
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if last_gray is not None:
similarity = ssim(gray, last_gray)
if similarity > SSIM_THRESHOLD:
print(f"跳过:与上一帧太相似 (SSIM={similarity:.3f})")
continue
last_gray = gray.copy()
timestamp = time.strftime("%Y%m%d_%H%M%S")
ms = int((time.time() % 1) * 1000)
filename = f"frame_{timestamp}_{ms:03d}.png"
filepath = os.path.join(output_dir, filename)
total, used, free = shutil.disk_usage(output_dir)
if free < 1024 * 1024 * 20: # 小于 20MB 就停止
print(f"❌ 磁盘空间严重不足(仅剩 {free / (1024**3):.2f} GB停止运行。")
raise SystemExit(1)
try:
pil_image.save(filepath, format='PNG')
print(f"已保存: {filepath}")
except (OSError, IOError) as e:
error_msg = str(e)
if "No space left on device" in error_msg or "disk full" in error_msg.lower() or "quota" in error_msg.lower():
print(f"磁盘空间不足,无法保存 {filepath}!错误: {e}")
print("停止程序以防止无限错误。")
raise SystemExit(1)
else:
print(f"保存失败 {filename}: {e}(非磁盘空间问题,继续运行)")
cv2.imshow('Camera Stream (Live)', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
raise KeyboardInterrupt
except KeyboardInterrupt:
print("\n用户中断")
break
finally:
cap.release()
cv2.destroyAllWindows()
print(f"视频流已关闭,共处理 {frame_count} 帧。")
print("程序结束")

32
camera/siyi_rtsp.py Normal file
View File

@ -0,0 +1,32 @@
import cv2
def read_rtsp(rtsp_url):
# 创建 VideoCapture 对象
cap = cv2.VideoCapture(rtsp_url)
# 判断是否成功连接到 RTSP 流
if not cap.isOpened():
print("无法连接到 RTSP 流")
return
try:
while True:
# 读取一帧
ret, frame = cap.read()
if not ret:
print("无法读取帧")
break
# 显示帧
cv2.imshow('frame', frame)
# 按下 'q' 键退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
# 释放资源
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
rtsp_url = "rtsp://192.168.144.25:8554/main.264" # 替换为实际的 RTSP 地址
read_rtsp(rtsp_url)

Binary file not shown.

After

Width:  |  Height:  |  Size: 781 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 780 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 778 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 784 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 770 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 769 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 764 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 761 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 774 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 766 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 757 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 758 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 774 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 764 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 824 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 797 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 790 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 811 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 790 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 791 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 785 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 739 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 790 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 788 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 804 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 798 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 786 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 692 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 801 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 758 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 735 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 736 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 723 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 780 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 857 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 882 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 750 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 885 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 841 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 858 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 661 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 819 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 746 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 613 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 820 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 789 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 806 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 781 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 783 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 762 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 712 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 777 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 760 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 756 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 751 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 754 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 752 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 780 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 775 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 765 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 757 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 773 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 820 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 833 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 835 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 829 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 780 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 788 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 781 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 780 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 707 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 708 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 666 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 647 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 742 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 730 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 733 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 694 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 716 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 672 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 681 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 705 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 706 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 711 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 685 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 KiB

Some files were not shown because too many files have changed in this diff Show More