重构目录结构:调整项目布局

This commit is contained in:
cdeyw
2025-09-26 13:32:34 +08:00
parent 486645a3aa
commit 3ebc4c3765
64 changed files with 2847 additions and 0 deletions

18
vision/__init__.py Normal file
View File

@ -0,0 +1,18 @@
# vision/__init__.py
"""
视觉处理模块
包含摄像头控制和视觉检测功能
"""
from .camera import CameraController
from .detector import VisionDetector
from .angle_detector import get_current_door_angle
from .overflow_detector import detect_overflow_from_image
from .alignment_detector import detect_vehicle_alignment
__all__ = [
'CameraController',
'VisionDetector',
'get_current_door_angle',
'detect_overflow_from_image',
'detect_vehicle_alignment'
]

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,30 @@
# vision/alignment_detector.py
def detect_vehicle_alignment(image_array, alignment_model):
"""
通过图像检测模具车是否对齐
"""
try:
# 检查模型是否已加载
if alignment_model is None:
print("对齐检测模型未加载")
return False
if image_array is None:
print("输入图像为空")
return False
# 直接使用模型进行推理
results = alignment_model(image_array)
pared_probs = results[0].probs.data.cpu().numpy().flatten()
# 类别0: 未对齐, 类别1: 对齐
class_id = int(pared_probs.argmax())
confidence = float(pared_probs[class_id])
# 只有当对齐且置信度>95%时才认为对齐
if class_id == 1 and confidence > 0.95:
return True
return False
except Exception as e:
print(f"对齐检测失败: {e}")
return False

88
vision/anger_caculate.py Normal file
View File

@ -0,0 +1,88 @@
import cv2
import os
import numpy as np
from ultralytics import YOLO
def predict_obb_best_angle(model=None, model_path=None, image=None, image_path=None, save_path=None):
"""
输入:
model: 预加载的YOLO模型实例可选
model_path: YOLO 权重路径当model为None时使用
image: 图像数组numpy array
image_path: 图片路径当image为None时使用
save_path: 可选,保存带标注图像
输出:
angle_deg: 置信度最高两个框的主方向夹角(度),如果检测少于两个目标返回 None
annotated_img: 可视化图像
"""
# 1. 使用预加载的模型或加载新模型
if model is not None:
loaded_model = model
elif model_path is not None:
loaded_model = YOLO(model_path)
else:
raise ValueError("必须提供model或model_path参数")
# 2. 读取图像(优先使用传入的图像数组)
if image is not None:
img = image
elif image_path is not None:
img = cv2.imread(image_path)
if img is None:
print(f"无法读取图像: {image_path}")
return None, None
else:
raise ValueError("必须提供image或image_path参数")
# 3. 推理 OBB
results = loaded_model(img, save=False, imgsz=640, conf=0.5, mode='obb')
result = results[0]
# 4. 可视化
annotated_img = result.plot()
if save_path:
os.makedirs(os.path.dirname(save_path), exist_ok=True)
cv2.imwrite(save_path, annotated_img)
print(f"推理结果已保存至: {save_path}")
# 5. 提取旋转角度和置信度
boxes = result.obb
if boxes is None or len(boxes) < 2:
print("检测到少于两个目标,无法计算夹角。")
return None, annotated_img
box_info = []
for box in boxes:
conf = box.conf.cpu().numpy()[0]
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
box_info.append((conf, direction))
# 6. 取置信度最高两个框
box_info = sorted(box_info, key=lambda x: x[0], reverse=True)
dir1, dir2 = box_info[0][1], box_info[1][1]
# 7. 计算夹角最小夹角0~90°
diff = abs(dir1 - dir2)
diff = min(diff, np.pi - diff)
angle_deg = np.degrees(diff)
print(f"置信度最高两个框主方向夹角: {angle_deg:.2f}°")
return angle_deg, annotated_img
# ------------------- 测试 -------------------
# if __name__ == "__main__":
# weight_path = r'angle.pt'
# image_path = r"./test_image/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,_ = predict_obb_best_angle(model_path=weight_path, image_path=image_path, save_path=save_path)
# annotated_img = None
# print(angle_deg)
# if annotated_img is not None:
# cv2.imshow("YOLO OBB Prediction", annotated_img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

27
vision/angle_detector.py Normal file
View File

@ -0,0 +1,27 @@
# vision/angle_detector.py
import sys
import os
from vision.anger_caculate import predict_obb_best_angle
# 添加项目根目录到Python路径
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
def get_current_door_angle(model=None, image=None, image_path=None):
"""
通过视觉系统获取当前出砼门角度
:param model: 模型实例
:param image: 图像数组numpy array
:param image_path: 图片路径
:return: 角度值(度)
"""
try:
# 调用实际的角度检测函数
angle_deg, _ = predict_obb_best_angle(
model=model,
image=image,
image_path=image_path
)
return angle_deg
except Exception as e:
print(f"角度检测失败: {e}")
return None

67
vision/camera.py Normal file
View File

@ -0,0 +1,67 @@
# vision/camera.py
import cv2
class CameraController:
def __init__(self):
self.camera = None
self.camera_type = "ip"
self.camera_ip = "192.168.1.51"
self.camera_port = 554
self.camera_username = "admin"
self.camera_password = "XJ123456"
self.camera_channel = 1
def set_config(self, camera_type="ip", ip=None, port=None, username=None, password=None, channel=1):
"""
设置摄像头配置
"""
self.camera_type = camera_type
if ip:
self.camera_ip = ip
if port:
self.camera_port = port
if username:
self.camera_username = username
if password:
self.camera_password = password
self.camera_channel = channel
def setup_capture(self, camera_index=0):
"""
设置摄像头捕获
"""
try:
rtsp_url = f"rtsp://{self.camera_username}:{self.camera_password}@{self.camera_ip}:{self.camera_port}/streaming/channels/{self.camera_channel}01"
self.camera = cv2.VideoCapture(rtsp_url)
if not self.camera.isOpened():
print(f"无法打开网络摄像头: {rtsp_url}")
return False
print(f"网络摄像头初始化成功,地址: {rtsp_url}")
return True
except Exception as e:
print(f"摄像头设置失败: {e}")
return False
def capture_frame(self):
"""捕获当前帧并返回numpy数组"""
try:
if self.camera is None:
print("摄像头未初始化")
return None
ret, frame = self.camera.read()
if ret:
return frame
else:
print("无法捕获图像帧")
return None
except Exception as e:
print(f"图像捕获失败: {e}")
return None
def release(self):
"""释放摄像头资源"""
if self.camera is not None:
self.camera.release()

86
vision/detector.py Normal file
View File

@ -0,0 +1,86 @@
# vision/detector.py
import os
from ultralytics import YOLO
from vision.angle_detector import get_current_door_angle
from vision.overflow_detector import detect_overflow_from_image
from vision.alignment_detector import detect_vehicle_alignment
class VisionDetector:
def __init__(self, settings):
self.settings = settings
# 模型实例
self.angle_model = None
self.overflow_model = None
self.alignment_model = None
def load_models(self):
"""
加载所有视觉检测模型
"""
success = True
# 加载夹角检测模型
try:
if not os.path.exists(self.settings.angle_model_path):
print(f"夹角检测模型不存在: {self.settings.angle_model_path}")
success = False
else:
# 注意angle.pt模型通过predict_obb_best_angle函数使用不需要预加载
print(f"夹角检测模型路径: {self.settings.angle_model_path}")
except Exception as e:
print(f"检查夹角检测模型失败: {e}")
success = False
# 加载堆料检测模型
try:
if not os.path.exists(self.settings.overflow_model_path):
print(f"堆料检测模型不存在: {self.settings.overflow_model_path}")
success = False
else:
self.overflow_model = YOLO(self.settings.overflow_model_path)
print(f"成功加载堆料检测模型: {self.settings.overflow_model_path}")
except Exception as e:
print(f"加载堆料检测模型失败: {e}")
success = False
# 加载对齐检测模型
try:
if not os.path.exists(self.settings.alignment_model_path):
print(f"对齐检测模型不存在: {self.settings.alignment_model_path}")
success = False
else:
self.alignment_model = YOLO(self.settings.alignment_model_path)
print(f"成功加载对齐检测模型: {self.settings.alignment_model_path}")
except Exception as e:
print(f"加载对齐检测模型失败: {e}")
success = False
return success
def detect_angle(self, image=None, image_path=None):
"""
通过视觉系统获取当前出砼门角度
"""
return get_current_door_angle(
model=self.angle_model,
image=image,
image_path=image_path
)
def detect_overflow(self, image_array):
"""
通过图像检测是否溢料
"""
return detect_overflow_from_image(
image_array,
self.overflow_model,
self.settings.roi_file_path
)
def detect_vehicle_alignment(self, image_array):
"""
通过图像检测模具车是否对齐
"""
return detect_vehicle_alignment(image_array, self.alignment_model)

BIN
vision/models/alig.pt Normal file

Binary file not shown.

BIN
vision/models/angle.pt Normal file

Binary file not shown.

BIN
vision/models/overflow.pt Normal file

Binary file not shown.

View File

@ -0,0 +1,47 @@
# vision/overflow_detector.py
import sys
import os
from vision.resize_tuili_image_main import classify_image_weighted, load_global_rois, crop_and_resize
# 添加项目根目录到Python路径
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
def detect_overflow_from_image(image_array, overflow_model, roi_file_path):
"""
通过图像检测是否溢料
:param image_array: 图像数组
:param overflow_model: 溢料检测模型
:param roi_file_path: ROI文件路径
:return: 是否检测到溢料 (True/False)
"""
try:
# 检查模型是否已加载
if overflow_model is None:
print("堆料检测模型未加载")
return False
# 加载ROI区域
rois = load_global_rois(roi_file_path)
if not rois:
print(f"没有有效的ROI配置: {roi_file_path}")
return False
if image_array is None:
print("输入图像为空")
return False
# 裁剪和调整图像大小
crops = crop_and_resize(image_array, rois, 640)
# 对每个ROI区域进行分类检测
for roi_resized, _ in crops:
final_class, _, _, _ = classify_image_weighted(roi_resized, overflow_model, threshold=0.4)
if "大堆料" in final_class or "小堆料" in final_class:
print(f"检测到溢料: {final_class}")
return True
return False
except Exception as e:
print(f"溢料检测失败: {e}")
return False

106
vision/resize_main.py Normal file
View File

@ -0,0 +1,106 @@
import os
import shutil
from pathlib import Path
from ultralytics import YOLO
import cv2
# ---------------------------
# ROI 裁剪函数
# ---------------------------
def load_global_rois(txt_path):
"""加载全局 ROI 坐标"""
rois = []
if not os.path.exists(txt_path):
print(f"❌ ROI 文件不存在: {txt_path}")
return rois
with open(txt_path, 'r') as f:
for line in f:
line = line.strip()
if line:
try:
x, y, w, h = map(int, line.split(','))
rois.append((x, y, w, h))
print(f"📌 加载 ROI: (x={x}, y={y}, w={w}, h={h})")
except Exception as e:
print(f"⚠️ 无法解析 ROI 行: {line}, 错误: {e}")
return rois
def crop_and_resize(img, rois, target_size=640):
"""根据 ROI 裁剪并 resize"""
crops = []
for i, (x, y, w, h) in enumerate(rois):
h_img, w_img = img.shape[:2]
if x < 0 or y < 0 or x + w > w_img or y + h > h_img:
print(f"⚠️ ROI 越界,跳过: {x},{y},{w},{h}")
continue
roi_img = img[y:y+h, x:x+w]
roi_resized = cv2.resize(roi_img, (target_size, target_size), interpolation=cv2.INTER_AREA)
crops.append((roi_resized, i))
return crops
# ---------------------------
# 分类函数
# ---------------------------
def classify_and_save_images(model_path, input_folder, output_root, roi_file, target_size=640):
# 加载模型
model = YOLO(model_path)
# 确保输出根目录存在
output_root = Path(output_root)
output_root.mkdir(parents=True, exist_ok=True)
# 创建类别子文件夹 (class0 到 class4)
class_dirs = []
for i in range(5): # 假设有5个类别 (0-4)
class_dir = output_root / f"class{i}"
class_dir.mkdir(exist_ok=True)
class_dirs.append(class_dir)
# 加载 ROI
rois = load_global_rois(roi_file)
if len(rois) == 0:
print("❌ 没有有效 ROI退出")
return
# 遍历输入文件夹
for img_path in Path(input_folder).glob("*.*"):
if img_path.suffix.lower() not in ['.jpg', '.jpeg', '.png', '.bmp', '.tif']:
continue
try:
# 读取原图
img = cv2.imread(str(img_path))
if img is None:
print(f"❌ 无法读取图像: {img_path}")
continue
# 根据 ROI 裁剪
crops = crop_and_resize(img, rois, target_size)
for roi_img, roi_idx in crops:
# YOLO 推理
results = model(roi_img)
pred = results[0].probs.data # 获取概率分布
class_id = int(pred.argmax())
# 保存到对应类别文件夹
suffix = f"_roi{roi_idx}" if len(crops) > 1 else ""
dst_path = class_dirs[class_id] / f"{img_path.stem}{suffix}{img_path.suffix}"
cv2.imwrite(dst_path, roi_img) # 保存裁剪后的 ROI 图像
print(f"Processed {img_path.name}{suffix} -> Class {class_id}")
except Exception as e:
print(f"Error processing {img_path.name}: {str(e)}")
# ---------------------------
# 主程序
# ---------------------------
if __name__ == "__main__":
model_path = r"models/overflow.pt"
input_folder = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/f6"
output_root = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/class111"
roi_file = "./roi_coordinates/1_rois.txt" # 训练时使用的 ROI 文件
target_size = 640
classify_and_save_images(model_path, input_folder, output_root, roi_file, target_size)

View File

@ -0,0 +1,184 @@
import os
from pathlib import Path
import cv2
import numpy as np
from ultralytics import YOLO
# ---------------------------
# 类别映射
# ---------------------------
CLASS_NAMES = {
0: "未堆料",
1: "小堆料",
2: "大堆料",
3: "未浇筑满",
4: "浇筑满"
}
# ---------------------------
# 加载 ROI 列表
# ---------------------------
def load_global_rois(txt_path):
rois = []
if not os.path.exists(txt_path):
print(f"ROI 文件不存在: {txt_path}")
return rois
with open(txt_path, 'r') as f:
for line in f:
s = line.strip()
if s:
try:
x, y, w, h = map(int, s.split(','))
rois.append((x, y, w, h))
except Exception as e:
print(f"无法解析 ROI 行 '{s}': {e}")
return rois
# ---------------------------
# 裁剪并 resize ROI
# ---------------------------
def crop_and_resize(img, rois, target_size=640):
crops = []
h_img, w_img = img.shape[:2]
for i, (x, y, w, h) in enumerate(rois):
if x < 0 or y < 0 or x + w > w_img or y + h > h_img:
continue
roi = img[y:y + h, x:x + w]
roi_resized = cv2.resize(roi, (target_size, target_size), interpolation=cv2.INTER_AREA)
crops.append((roi_resized, i))
return crops
# ---------------------------
# class1/class2 加权判断
# ---------------------------
def weighted_small_large(pred_probs, threshold=0.4, w1=0.3, w2=0.7):
p1 = float(pred_probs[1])
p2 = float(pred_probs[2])
total = p1 + p2
if total > 0:
score = (w1 * p1 + w2 * p2) / total
else:
score = 0.0
final_class = "大堆料" if score >= threshold else "小堆料"
return final_class, score, p1, p2
# ---------------------------
# 单张图片推理函数
# ---------------------------
def classify_image_weighted(image, model, threshold=0.4):
results = model(image)
pred_probs = results[0].probs.data.cpu().numpy().flatten()
class_id = int(pred_probs.argmax())
confidence = float(pred_probs[class_id])
class_name = CLASS_NAMES.get(class_id, f"未知类别({class_id})")
# class1/class2 使用加权得分
if class_id in [1, 2]:
final_class, score, p1, p2 = weighted_small_large(pred_probs, threshold=threshold)
else:
final_class = class_name
score = confidence
p1 = float(pred_probs[1])
p2 = float(pred_probs[2])
return final_class, score, p1, p2
# ---------------------------
# 实时视频流推理函数
# ---------------------------
def real_time_inference(rtsp_url, model_path, roi_file, target_size=640, threshold=0.4):
"""
从RTSP流实时推理
:param rtsp_url: RTSP流URL
:param model_path: 模型路径
:param roi_file: ROI文件路径
:param target_size: 目标尺寸
:param threshold: 分类阈值
"""
# 加载模型
model = YOLO(model_path)
# 加载ROI
rois = load_global_rois(roi_file)
if not rois:
print("❌ 没有有效 ROI退出")
return
# 打开RTSP流
cap = cv2.VideoCapture(rtsp_url)
if not cap.isOpened():
print(f"❌ 无法打开视频流: {rtsp_url}")
return
print(f"✅ 成功连接到视频流: {rtsp_url}")
print("'q' 键退出,按 's' 键保存当前帧")
frame_count = 0
while True:
ret, frame = cap.read()
if not ret:
print("❌ 无法读取帧,可能连接已断开")
break
frame_count += 1
print(f"\n处理第 {frame_count}")
try:
# 裁剪并调整ROI
crops = crop_and_resize(frame, rois, target_size)
for roi_resized, roi_idx in crops:
final_class, score, p1, p2 = classify_image_weighted(roi_resized, model, threshold=threshold)
print(f"ROI {roi_idx} -> 类别: {final_class}, 加权分数: {score:.2f}, "
f"class1 置信度: {p1:.2f}, class2 置信度: {p2:.2f}")
# 判断是否溢料
if "大堆料" in final_class or "浇筑满" in final_class:
print(f"🚨 检测到溢料: ROI {roi_idx} - {final_class}")
# 可视化(可选)
cv2.imshow(f'ROI {roi_idx}', roi_resized)
# 显示原始帧
cv2.imshow('Original Frame', frame)
except Exception as e:
print(f"处理帧时出错: {e}")
continue
# 键盘控制
key = cv2.waitKey(1) & 0xFF
if key == ord('q'): # 按q退出
break
elif key == ord('s'): # 按s保存当前帧
cv2.imwrite(f"frame_{frame_count}.jpg", frame)
print(f"保存帧到 frame_{frame_count}.jpg")
# 清理资源
cap.release()
cv2.destroyAllWindows()
print("✅ 视频流处理结束")
# ---------------------------
# 主函数 - 实时推理示例
# ---------------------------
# if __name__ == "__main__":
# # RTSP流URL
# rtsp_url = "rtsp://admin:XJ123456@192.168.1.51:554/streaming/channels/101"
#
# # 配置参数
# model_path = r"models/overflow.pt"
# roi_file = r"./roi_coordinates/1_rois.txt"
# target_size = 640
# threshold = 0.4
#
# print("开始实时视频流推理...")
# real_time_inference(rtsp_url, model_path, roi_file, target_size, threshold)

View File

@ -0,0 +1 @@
859,810,696,328