This commit is contained in:
2025-11-17 00:05:40 +08:00
parent f860c5a216
commit e3ecd0550f
55 changed files with 3204 additions and 528 deletions

137
vision/.py Normal file
View File

@ -0,0 +1,137 @@
# 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_exec(self):
"""捕获当前帧并返回numpy数组设置5秒总超时"""
try:
if self.camera is None:
print("摄像头未初始化")
return None
# 设置总超时时间为5秒
total_timeout = 5.0 # 5秒总超时时间
start_time = time.time()
# 跳20帧获取最新图像
frames_skipped = 0
while frames_skipped < 20:
# 检查总超时
if time.time() - start_time > total_timeout:
print("捕获图像总超时")
return None
self.camera.grab()
time.sleep(0.05) # 稍微增加延迟,确保有新帧到达
frames_skipped += 1
# 尝试读取帧,使用同一超时计时器
read_attempts = 0
max_read_attempts = 3
if self.camera.grab():
while read_attempts < max_read_attempts:
# 使用同一个超时计时器检查
if time.time() - start_time > total_timeout:
print("捕获图像总超时")
return None
ret, frame = self.camera.retrieve()
if ret:
return frame
else:
print(f"尝试读取图像帧失败,重试 ({read_attempts+1}/{max_read_attempts})")
read_attempts += 1
# 短暂延迟后重试
time.sleep(0.05)
print("多次尝试后仍无法捕获有效图像帧")
return None
except Exception as e:
print(f"图像捕获失败: {e}")
return None
def capture_frame(self):
"""捕获当前帧并返回numpy数组"""
try:
if self.camera is None:
# self.set_config()
self.setup_capture()
frame = self.capture_frame_exec()
if frame is not None:
return frame
else:
print("无法捕获图像帧")
return None
except Exception as e:
print(f"图像捕获失败: {e}")
return None
def capture_frame_bak(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()
self.camera = None
def __del__(self):
"""析构函数,确保资源释放"""
self.release()

View File

View File

@ -0,0 +1,167 @@
import cv2
import numpy as np
import platform
from .labels import labels # 确保这个文件存在
# ------------------- 核心全局变量存储RKNN模型实例确保只加载一次 -------------------
# 初始化为None首次调用时加载模型后续直接复用
_global_rknn_instance = None
# device tree for RK356x/RK3576/RK3588
DEVICE_COMPATIBLE_NODE = '/proc/device-tree/compatible'
def get_host():
# get platform and device type
system = platform.system()
machine = platform.machine()
os_machine = system + '-' + machine
if os_machine == 'Linux-aarch64':
try:
with open(DEVICE_COMPATIBLE_NODE) as f:
device_compatible_str = f.read()
if 'rk3562' in device_compatible_str:
host = 'RK3562'
elif 'rk3576' in device_compatible_str:
host = 'RK3576'
elif 'rk3588' in device_compatible_str:
host = 'RK3588'
else:
host = 'RK3566_RK3568'
except IOError:
print('Read device node {} failed.'.format(DEVICE_COMPATIBLE_NODE))
exit(-1)
else:
host = os_machine
return host
def get_top1_class_str(result):
"""
从推理结果中提取出得分最高的类别,并返回字符串
参数:
result (list): 模型推理输出结果(格式需与原函数一致,如 [np.ndarray]
返回:
str:得分最高类别的格式化字符串
若推理失败,返回错误提示字符串
"""
if result is None:
print("Inference failed: result is None")
return
# 解析推理输出与原逻辑一致展平输出为1维数组
output = result[0].reshape(-1)
# 获取得分最高的类别索引np.argmax 直接返回最大值索引,比排序更高效)
top1_index = np.argmax(output)
# 处理标签(确保索引在 labels 列表范围内,避免越界)
if 0 <= top1_index < len(labels):
top1_class_name = labels[top1_index]
else:
top1_class_name = "Unknown Class" # 应对索引异常的边界情况
# 5. 格式化返回字符串包含索引、得分、类别名称得分保留6位小数
return top1_class_name
def preprocess(raw_image, target_size=(640, 640)):
"""
读取图像并执行预处理BGR转RGB、调整尺寸、添加Batch维度
参数:
image_path (str): 图像文件的完整路径(如 "C:/test.jpg""/home/user/test.jpg"
target_size (tuple): 预处理后图像的目标尺寸,格式为 (width, height),默认 (640, 640)
返回:
img (numpy.ndarray): 预处理后的图像
异常:
FileNotFoundError: 图像路径不存在或无法读取时抛出
ValueError: 图像读取成功但为空(如文件损坏)时抛出
"""
# img = cv2.cvtColor(raw_image, cv2.COLOR_BGR2RGB)
# 调整尺寸
img = cv2.resize(raw_image, target_size)
img = np.expand_dims(img, 0) # 添加batch维度
return img
# ------------------- 新增:模型初始化函数(控制只加载一次) -------------------
def init_rknn_model(model_path):
"""
初始化RKNN模型全局唯一实例
- 首次调用:加载模型+初始化运行时,返回模型实例
- 后续调用:直接返回已加载的全局实例,避免重复加载
"""
from rknnlite.api import RKNNLite
global _global_rknn_instance # 声明使用全局变量
# 若模型未加载过,执行加载逻辑
if _global_rknn_instance is None:
# 1. 创建RKNN实例关闭内置日志
rknn_lite = RKNNLite(verbose=False)
# 2. 加载RKNN模型
ret = rknn_lite.load_rknn(model_path)
if ret != 0:
print(f'[ERROR] Load CLS_RKNN model failed (code: {ret})')
exit(ret)
# 3. 初始化运行时绑定NPU核心0
ret = rknn_lite.init_runtime(core_mask=RKNNLite.NPU_CORE_0)
if ret != 0:
print(f'[ERROR] Init CLS_RKNN runtime failed (code: {ret})')
exit(ret)
# 4. 将加载好的实例赋值给全局变量
_global_rknn_instance = rknn_lite
print(f'[INFO] CLS_RKNN model loaded successfully (path: {model_path})')
return _global_rknn_instance
def yolov11_cls_inference(model_path, raw_image, target_size=(640, 640)):
"""
根据平台进行推理,并返回最终的分类结果
参数:
model_path (str): RKNN模型文件路径
image_path (str): 图像文件的完整路径(如 "C:/test.jpg""/home/user/test.jpg"
target_size (tuple): 预处理后图像的目标尺寸,格式为 (width, height),默认 (640, 640)
"""
rknn_model = model_path
img = preprocess(raw_image, target_size)
rknn = init_rknn_model(rknn_model)
if rknn is None:
return None, img
outputs = rknn.inference([img])
# Show the classification results
class_name = get_top1_class_str(outputs)
# rknn_lite.release()
return class_name
if __name__ == '__main__':
# 调用yolov11_cls_inference函数target_size使用默认值640x640也可显式传参如(112,112)
image_path = "/userdata/reenrr/inference_with_lite/cover_ready.jpg"
bgr_image = cv2.imread(image_path)
if bgr_image is None:
print(f"Failed to read image from {image_path}")
exit(-1)
rgb_frame = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
print(f"Read image from {image_path}, shape: {rgb_frame.shape}")
result = yolov11_cls_inference(
# model_path="/userdata/PyQt_main_test/app/view/yolo/yolov11_cls.rknn",
model_path="/userdata/chuyiwen/Feeding_control_system/vision/align_model/yolov11_cls_640v6.rknn",
raw_image=rgb_frame,
target_size=(640, 640)
)
# 打印最终结果
print(f"\n最终分类结果:{result}")

View File

@ -0,0 +1,6 @@
# the labels come from synset.txt, download link: https://s3.amazonaws.com/onnx-model-zoo/synset.txt
labels = \
{0: 'cover_noready',
1: 'cover_ready'
}

View File

@ -0,0 +1,93 @@
# yolo11_main.py
import cv2
import numpy as np
from collections import deque
import os
# 导入模块(不是函数)
from .aligment_inference import yolov11_cls_inference
# 模型路径
CLS_MODEL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "yolov11_cls_640v6.rknn")
class ClassificationStabilizer:
"""分类结果稳定性校验器,处理瞬时噪声帧"""
def __init__(self, window_size=5, switch_threshold=2):
self.window_size = window_size # 滑动窗口大小缓存最近N帧结果
self.switch_threshold = switch_threshold # 状态切换需要连续N帧一致
self.result_buffer = deque(maxlen=window_size) # 缓存最近结果
self.current_state = "盖板未对齐" # 初始状态
self.consecutive_count = 0 # 当前状态连续出现的次数
def stabilize(self, current_frame_result):
"""
输入当前帧的分类结果,返回经过稳定性校验的结果
Args:
current_frame_result: 当前帧的原始分类结果str
Returns:
str: 经过校验的稳定结果
"""
# 1. 将当前帧结果加入滑动窗口
self.result_buffer.append(current_frame_result)
# 2. 统计窗口内各结果的出现次数(多数投票基础)
result_counts = {}
for res in self.result_buffer:
result_counts[res] = result_counts.get(res, 0) + 1 # 使用 result_counts 字典记录每个元素出现的总次数。
# 3. 找到窗口中出现次数最多的结果(候选结果)
candidate = max(result_counts, key=result_counts.get)
# 4. 状态切换校验只有候选结果连续出现N次才允许切换
if candidate == self.current_state:
# 与当前状态一致,重置连续计数
self.consecutive_count = 0
else:
# 与当前状态不一致,累计连续次数
self.consecutive_count += 1
# 连续达到阈值,才更新状态
if self.consecutive_count >= self.switch_threshold:
self.current_state = candidate
self.consecutive_count = 0
return self.current_state
# 初始化稳定性校验器(全局唯一实例,确保状态连续)
cls_stabilizer = ClassificationStabilizer(
window_size=5, # 缓存最近5帧
switch_threshold=2 # 连续2帧一致才切换状态
)
# ====================== 分类接口(可选,保持原逻辑) ======================
def run_yolo_classification(rgb_frame):
"""
YOLO 图像分类接口函数
Args:
rgb_frame: numpy array (H, W, 3), RGB 格式
Returns:
str: 分类结果("盖板对齐" / "盖板未对齐" / "异常"
"""
if not isinstance(rgb_frame, np.ndarray):
print(f"[ERROR] 输入类型错误:需为 np.ndarray当前为 {type(rgb_frame)}")
return "异常"
try:
cover_cls = yolov11_cls_inference(CLS_MODEL_PATH, rgb_frame, target_size=(640, 640))
except Exception as e:
print(f"[WARN] 分类推理失败: {e}")
cover_cls = "异常"
raw_result = "盖板未对齐" # 默认值
# 结果映射
if cover_cls == "cover_ready":
raw_result = "盖板对齐"
elif cover_cls == "cover_noready":
raw_result = "盖板未对齐"
else:
raw_result = "异常"
# 通过稳定性校验器处理,返回最终结果
stable_result = cls_stabilizer.stabilize(raw_result)
print("raw_result, stable_result:",raw_result, stable_result)
return stable_result

Binary file not shown.

View File

@ -1,30 +1,35 @@
# vision/alignment_detector.py
def detect_vehicle_alignment(image_array, alignment_model):
from vision.align_model.yolo11_main import run_yolo_classification
def detect_vehicle_alignment(image_array):
"""
通过图像检测模具车是否对齐
"""
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()
# 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])
# # 类别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
# 使用yolov11_cls_inference函数进行推理
results = run_yolo_classification(image_array)
if results=="盖板对齐":
return True
else:
return False
# 只有当对齐且置信度>95%时才认为对齐
if class_id == 1 and confidence > 0.95:
return True
return False
except Exception as e:
print(f"对齐检测失败: {e}")
return False

View File

@ -1,88 +1,235 @@
import cv2
import os
import numpy as np
from ultralytics import YOLO
import math
from shapely.geometry import Polygon
from rknnlite.api import RKNNLite
import os
def predict_obb_best_angle(model=None, model_path=None, image=None, image_path=None, save_path=None):
# ------------------- 全局配置变量 -------------------
# 模型相关
CLASSES = ['clamp']
nmsThresh = 0.4
objectThresh = 0.35
# 可视化与保存控制(全局变量,可外部修改)
DRAW_RESULT = True # 是否在输出图像上绘制旋转框
SAVE_PATH = None # 保存路径,如 "./result.jpg";设为 None 则不保存
# RKNN 单例
_rknn_instance = None
# ------------------- RKNN 管理函数 -------------------
def init_rknn(model_path):
"""只加载一次 RKNN 模型"""
global _rknn_instance
if _rknn_instance is None:
_rknn_instance = RKNNLite(verbose=False)
ret = _rknn_instance.load_rknn(model_path)
if ret != 0:
print(f"[ERROR] Failed to load RKNN model: {ret}")
return None
ret = _rknn_instance.init_runtime(core_mask=RKNNLite.NPU_CORE_0)
if ret != 0:
print(f"[ERROR] Failed to init runtime: {ret}")
return None
return _rknn_instance
def release_rknn():
"""释放 RKNN 对象"""
global _rknn_instance
if _rknn_instance:
_rknn_instance.release()
_rknn_instance = None
# ------------------- 工具函数 -------------------
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_resized = 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_resized
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
cos_a, sin_a = math.cos(a), math.sin(a)
pts = [(x1, y1), (x1, y2), (x2, y2), (x2, y1)]
return [[int(cx + (xx - cx) * cos_a - (yy - cy) * sin_a),
int(cy + (xx - cx) * sin_a + (yy - cy) * cos_a)] for xx, yy in pts]
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):
x = np.clip(x, -709, 709) # 防止 exp 溢出
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:, :])
conf = conf.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[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 = (angle_feature[index + (h * model_w) + w] - 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_two_box_angle(model_path, rgb_frame):
"""
输入
model: 预加载的YOLO模型实例可选
model_path: YOLO 权重路径当model为None时使用
image: 图像数组numpy array
image_path: 图片路径当image为None时使用
save_path: 可选,保存带标注图像
输出:
angle_deg: 置信度最高两个框的主方向夹角(度),如果检测少于两个目标返回 None
annotated_img: 可视化图像
输入模型路径和 RGB 图像numpy array输出夹角和结果图像。
可视化和保存由全局变量 DRAW_RESULT 和 SAVE_PATH 控制。
"""
# 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参数")
global _rknn_instance, DRAW_RESULT, SAVE_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参数")
if not isinstance(rgb_frame, np.ndarray) or rgb_frame is None:
print(f"[ERROR] detect_two_box_angle 接收到错误类型: {type(rgb_frame)}")
return None, np.zeros((640, 640, 3), np.uint8)
# 3. 推理 OBB
results = loaded_model(img, save=False, imgsz=640, conf=0.5, mode='obb')
result = results[0]
# 注意:输入是 BGR因为 cv2.imread 返回 BGR但内部会转为 RGB 给模型
img = rgb_frame.copy()
img_resized, scale, offset_x, offset_y = letterbox_resize(img, (640, 640))
infer_img = np.expand_dims(cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB), 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}")
try:
rknn = init_rknn(model_path)
if rknn is None:
return None, img
results = rknn.inference([infer_img])
except Exception as e:
print(f"[ERROR] RKNN 推理失败: {e}")
return None, img
# 5. 提取旋转角度和置信度
boxes = result.obb
if boxes is None or len(boxes) < 2:
print("检测到少于两个目标,无法计算夹角。")
return None, annotated_img
outputs = []
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)
outputs += process(feature, x.shape[3], x.shape[2], stride, results[-1], index)
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))
predbox = NMS(outputs)
print(f"[DEBUG] 检测到 {len(predbox)} 个框")
# 6. 取置信度最高两个框
box_info = sorted(box_info, key=lambda x: x[0], reverse=True)
dir1, dir2 = box_info[0][1], box_info[1][1]
if len(predbox) < 2:
print("检测少于两个目标,无法计算夹角。")
return None, img
# 7. 计算夹角最小夹角0~90°
predbox = sorted(predbox, key=lambda x: x.score, reverse=True)
box1, box2 = predbox[:2]
output_img = img.copy() if DRAW_RESULT else img # 若不绘制,则直接用原图
if DRAW_RESULT:
for box in [box1, box2]:
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(output_img, [np.array(points, np.int32)], True, (0, 255, 0), 2)
def main_direction(box):
w, h = (box.xmax - box.xmin)/scale, (box.ymax - box.ymin)/scale
direction = box.angle if w >= h else box.angle + np.pi/2
return direction % np.pi
dir1 = main_direction(box1)
dir2 = main_direction(box2)
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 SAVE_PATH:
save_dir = os.path.dirname(SAVE_PATH)
if save_dir: # 非空目录才创建
os.makedirs(save_dir, exist_ok=True)
cv2.imwrite(SAVE_PATH, output_img)
return angle_deg, output_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()
# MODEL_PATH = "./obb.rknn"
# IMAGE_PATH = "./11.jpg"
# # === 全局控制开关 ===
# DRAW_RESULT = True # 是否绘制框
# SAVE_PATH = "./result11.jpg" # 保存路径,设为 None 则不保存
# frame = cv2.imread(IMAGE_PATH)
# if frame is None:
# print(f"[ERROR] 无法读取图像: {IMAGE_PATH}")
# else:
# angle_deg, output_image = detect_two_box_angle(MODEL_PATH, frame)
# if angle_deg is not None:
# print(f"检测到的角度差: {angle_deg:.2f}°")
# else:
# print("未能成功检测到目标或计算角度差")

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()

View File

@ -1,12 +1,12 @@
# vision/angle_detector.py
import sys
import os
from vision.anger_caculate import predict_obb_best_angle
from vision.obb_angle_model.obb_angle import detect_two_box_angle
# 添加项目根目录到Python路径
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
# sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
def get_current_door_angle(model=None, image=None, image_path=None):
def get_current_door_angle(model,image=None, image_path=None):
"""
通过视觉系统获取当前出砼门角度
:param model: 模型实例
@ -16,10 +16,10 @@ def get_current_door_angle(model=None, image=None, image_path=None):
"""
try:
# 调用实际的角度检测函数
angle_deg, _ = predict_obb_best_angle(
model=model,
image=image,
image_path=image_path
angle_deg, _ = detect_two_box_angle(
model_path=model,
rgb_frame=image
# ,image_path=image_path
)
return angle_deg
except Exception as e:

View File

@ -1,67 +1,486 @@
# vision/camera.py
import cv2
import threading
import queue
import time
import numpy as np
from datetime import datetime
from typing import Optional, Tuple, Dict, Any
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}")
class DualCameraController:
"""双摄像头控制器 - 支持多线程捕获和同步帧获取"""
def __init__(self, camera_configs: Dict[str, Dict[str, Any]], max_queue_size: int = 10, sync_threshold_ms: float = 50.0):
# 摄像头配置
self.camera_configs = camera_configs
# 摄像头对象和队列
self.cameras: Dict[str, cv2.VideoCapture] = {}
self.frame_queues: Dict[str, queue.Queue] = {}
self.capture_threads: Dict[str, threading.Thread] = {}
# 线程控制
self.stop_event = threading.Event()
self.max_queue_size = max_queue_size
self.sync_threshold_ms = sync_threshold_ms
self.last_sync_pair: Tuple[Optional[np.ndarray], Optional[np.ndarray]] = (None, None)
# 摄像头状态
self.is_running = False
def set_camera_config(self, camera_id: str, ip: str, username: str = "admin",
password: str = "XJ123456", port: int = 554, channel: int = 1):
"""设置指定摄像头的配置"""
if camera_id in ['cam1', 'cam2']:
self.camera_configs[camera_id].update({
'ip': ip,
'username': username,
'password': password,
'port': port,
'channel': channel
})
print(f"摄像头 {camera_id} 配置已更新: IP={ip}")
else:
raise ValueError(f"效的摄像头ID: {camera_id}")
def _build_rtsp_url(self, camera_id: str) -> str:
"""构建RTSP URL"""
config = self.camera_configs[camera_id]
return f"rtsp://{config['username']}:{config['password']}@{config['ip']}:{config['port']}/Streaming/Channels/{config['channel']}01"
def _capture_thread(self, camera_id: str):
"""摄像头捕获线程"""
cap = self.cameras[camera_id]
q = self.frame_queues[camera_id]
rtsp_url = self._build_rtsp_url(camera_id)
print(f"启动 {camera_id} 捕获线程")
while not self.stop_event.is_set():
try:
# print('aaaaa')
ret, frame = cap.read()
if ret and frame is not None:
# 使用高精度时间戳
timestamp = time.time()
# 检查队列是否已满
if q.qsize() >= self.max_queue_size:
# 队列已满丢弃最旧帧FIFO
try:
q.get_nowait() # 移除最旧帧
q.put_nowait((timestamp, frame))
except queue.Empty:
# 理论上不会发生,但安全处理
pass
else:
# 队列未满,直接添加
q.put_nowait((timestamp, frame))
else:
print(f"{camera_id} 读取失败,重连中...")
time.sleep(1)
cap.open(rtsp_url)
except Exception as e:
print(f"{camera_id} 捕获异常: {e}")
time.sleep(1)
print(f"{camera_id} 捕获线程已停止")
def start_cameras(self) -> bool:
"""启动双摄像头"""
if self.is_running:
print("摄像头已在运行中")
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
# 初始化摄像头和队列
for camera_id in ['cam1', 'cam2']:
rtsp_url = self._build_rtsp_url(camera_id)
cap = cv2.VideoCapture(rtsp_url)
if not cap.isOpened():
print(f"无法打开摄像头 {camera_id}: {rtsp_url}")
# 清理已打开的摄像头
self.release()
return False
self.cameras[camera_id] = cap
self.frame_queues[camera_id] = queue.Queue(maxsize=self.max_queue_size)
print(f"摄像头 {camera_id} 初始化成功: {rtsp_url}")
# 启动捕获线程
self.stop_event.clear()
for camera_id in ['cam1', 'cam2']:
thread = threading.Thread(
target=self._capture_thread,
args=(camera_id,),
daemon=True
)
self.capture_threads[camera_id] = thread
thread.start()
self.is_running = True
print("双摄像头系统启动成功")
return True
except Exception as e:
print(f"图像捕获失败: {e}")
print(f"启动摄像头失败: {e}")
self.release()
return False
def get_latest_frames(self, sync_threshold_ms: Optional[float] = None) -> Optional[Tuple[np.ndarray, np.ndarray]]:
"""获取最新的同步帧对"""
if not self.is_running:
print("摄像头未运行")
return None
sync_threshold = sync_threshold_ms or self.sync_threshold_ms
sync_threshold_sec = sync_threshold / 1000.0
# 检查队列是否有数据
if (self.frame_queues['cam1'].empty() or
self.frame_queues['cam2'].empty()):
return None
try:
# 获取最新帧
ts1, f1 = self.frame_queues['cam1'].queue[-1]
ts2, f2 = self.frame_queues['cam2'].queue[-1]
dt = abs(ts1 - ts2)
if dt < sync_threshold_sec:
# 时间差在阈值内,认为是同步的
frame1, frame2 = f1.copy(), f2.copy()
self.last_sync_pair = (frame1, frame2)
return (frame1, frame2)
else:
# 搜索最近5帧找最小时间差
min_dt = float('inf')
best_pair = None
# 获取最近5帧
cam1_frames = list(self.frame_queues['cam1'].queue)[-5:]
cam2_frames = list(self.frame_queues['cam2'].queue)[-5:]
for t1_local, f1_local in cam1_frames:
for t2_local, f2_local in cam2_frames:
dt_local = abs(t1_local - t2_local)
if dt_local < min_dt and dt_local < sync_threshold_sec * 2: # 更宽松的阈值
min_dt = dt_local
best_pair = (f1_local.copy(), f2_local.copy())
if best_pair:
self.last_sync_pair = best_pair
return best_pair
else:
# 没找到同步帧,返回最新非同步帧
return (f1.copy(), f2.copy())
except Exception as e:
print(f"获取帧对失败: {e}")
return None
def get_single_frame(self, camera_id: str) -> Optional[np.ndarray]:
"""获取单个摄像头的最新帧"""
if not self.is_running:
print("摄像头未运行")
return None
if camera_id not in self.frame_queues:
print(f"无效的摄像头ID: {camera_id}")
return None
try:
if not self.frame_queues[camera_id].empty():
_, frame = self.frame_queues[camera_id].queue[-1]
return frame.copy()
return None
except Exception as e:
print(f"获取单帧失败: {e}")
return None
def get_single_latest_frame(self) -> Optional[np.ndarray]:
"""获取单个摄像头的最新帧"""
if not self.is_running:
print("摄像头未运行")
return None
try:
frame_latest = None
dt_t1 = None
# 获取cam1的最新帧
if not self.frame_queues['cam1'].empty():
dt_t1, frame_latest = self.frame_queues['cam1'].queue[-1]
# 获取cam2的最新帧选择时间戳更新的那个
if frame_latest is None:
if not self.frame_queues['cam2'].empty():
dt_t2, frame2 = self.frame_queues['cam2'].queue[-1]
if dt_t1 is None or dt_t2 > dt_t1:
frame_latest = frame2
# 返回最新帧的副本(如果找到)
return frame_latest.copy() if frame_latest is not None else None
except Exception as e:
print(f"获取单帧失败: {e}")
return None
def get_single_latest_frame2(self) -> Optional[np.ndarray]:
"""获取单个摄像头的最新帧"""
if not self.is_running:
print("摄像头未运行")
return None
try:
frame_latest = None
dt_t1 = None
# 获取cam1的最新帧
if not self.frame_queues['cam2'].empty():
dt_t1, frame_latest = self.frame_queues['cam2'].queue[-1]
# 获取cam2的最新帧选择时间戳更新的那个
if frame_latest is None:
if not self.frame_queues['cam1'].empty():
dt_t2, frame2 = self.frame_queues['cam1'].queue[-1]
if dt_t1 is None or dt_t2 > dt_t1:
frame_latest = frame2
# 返回最新帧的副本(如果找到)
return frame_latest.copy() if frame_latest is not None else None
except Exception as e:
print(f"获取单帧失败: {e}")
return None
def get_notification_frame(self, camera_id: str = None, use_sync: bool = True) -> Optional[np.ndarray]:
"""根据通知参数获取最近的帧
Args:
camera_id: 摄像头ID ('cam1', 'cam2')如果为None则根据use_sync决定
use_sync: 是否使用同步帧对如果为True则返回同步帧对否则返回指定摄像头的单帧
Returns:
单帧图像或同步帧对
"""
if not self.is_running:
print("摄像头未运行")
return None
if use_sync:
# 获取同步帧对,返回拼接后的图像
frames = self.get_latest_frames()
if frames:
frame1, frame2 = frames
# 调整大小并拼接
h, w = 480, 640
frame1_resized = cv2.resize(frame1, (w, h))
frame2_resized = cv2.resize(frame2, (w, h))
combined = np.hstack((frame1_resized, frame2_resized))
# 添加时间戳信息
ts1 = time.time()
cv2.putText(combined, f"Sync: {datetime.fromtimestamp(ts1).strftime('%H:%M:%S.%f')[:-3]}",
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
return combined
return None
else:
# 获取指定摄像头的单帧
if camera_id is None:
camera_id = 'cam1' # 默认返回cam1
return self.get_single_frame(camera_id)
def display_live_feed(self):
"""实时显示双摄像头画面(调试用)"""
if not self.is_running:
print("请先启动摄像头")
return
print("'q' 退出显示,按 's' 保存同步帧")
while True:
frame = self.get_notification_frame(use_sync=True)
if frame is not None:
cv2.imshow("Dual Camera Feed", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('s'):
# 保存同步帧
sync_frames = self.get_latest_frames()
if sync_frames:
frame1, frame2 = sync_frames
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")[:20]
cv2.imwrite(f"cam1_{timestamp}.jpg", frame1)
cv2.imwrite(f"cam2_{timestamp}.jpg", frame2)
print(f"✅ 保存同步帧: cam1_{timestamp}.jpg & cam2_{timestamp}.jpg")
cv2.destroyAllWindows()
def release(self):
"""释放摄像头资源"""
if self.camera is not None:
self.camera.release()
print("正在释放摄像头资源...")
# 停止捕获线程
if self.is_running:
self.stop_event.set()
# 等待线程结束
for camera_id, thread in self.capture_threads.items():
if thread.is_alive():
thread.join(timeout=2)
print(f"{camera_id} 捕获线程已停止")
self.capture_threads.clear()
self.is_running = False
# 释放摄像头
for camera_id, cap in self.cameras.items():
if cap is not None:
cap.release()
print(f"摄像头 {camera_id} 已释放")
self.cameras.clear()
self.frame_queues.clear()
print("摄像头资源释放完成")
def __del__(self):
"""析构函数,确保资源释放"""
self.release()
# 类方法:快速创建和启动
@classmethod
def create_and_start(cls, camera_configs: Dict[str, Dict[str, Any]]) -> Optional['DualCameraController']:
"""快速创建并启动双摄像头控制器"""
controller = cls(camera_configs)
if controller.start_cameras():
return controller
else:
return None
# 向后兼容的单摄像头控制器
class CameraController:
"""单摄像头控制器 - 向后兼容"""
def __init__(self):
self.dual_controller = DualCameraController()
self.default_camera = 'cam1'
def set_config(self, camera_type="ip", ip=None, port=None, username=None, password=None, channel=1):
"""设置摄像头配置 - 兼容旧接口"""
self.dual_controller.set_camera_config(
'cam1', ip or "192.168.1.51", username or "admin",
password or "XJ123456", port or 554, channel
)
def setup_capture(self, camera_index=0):
"""设置摄像头捕获 - 兼容旧接口"""
return self.dual_controller.start_cameras()
def capture_frame(self):
"""捕获当前帧 - 兼容旧接口"""
return self.dual_controller.capture_frame(self.default_camera)
def capture_frame_bak(self):
"""捕获当前帧(备用) - 兼容旧接口"""
return self.dual_controller.capture_frame_bak(self.default_camera)
def release(self):
"""释放摄像头资源"""
self.dual_controller.release()
def __del__(self):
"""析构函数"""
self.release()
# 使用示例和测试
if __name__ == "__main__":
# 创建双摄像头控制器
camera_configs = {
'cam1': {
'type': 'ip',
'ip': '192.168.250.60',
'port': 554,
'username': 'admin',
'password': 'XJ123456',
'channel': 1
},
'cam2': {
'type': 'ip',
'ip': '192.168.250.61',
'port': 554,
'username': 'admin',
'password': 'XJ123456',
'channel': 1
}
}
controller = DualCameraController.create_and_start(camera_configs)
if controller:
print("双摄像头系统启动成功!")
# 示例1获取同步帧对
print("\n=== 获取同步帧 ===")
while True:
single_frame = controller.get_single_latest_frame()
if single_frame is not None:
print(f"获取到帧形状: {single_frame.shape}")
cv2.imshow("Single Camera Frame", single_frame)
else:
print("未获取到帧")
key = cv2.waitKey(1) & 0xFF
if key == ord('s') and single_frame is not None:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")[:20]
cv2.imwrite(f"single_frame_{timestamp}.jpg", single_frame)
print(f"✅ 保存单帧: single_frame_{timestamp}.jpg")
if key == ord('q'):
break
time.sleep(1)
# controller.get_single_latest_frame('cam2')
# sync_frames = controller.get_latest_frames(sync_threshold_ms=50)
# if sync_frames:
# frame1, frame2 = sync_frames
# print(f"获取到同步帧对 - 帧1形状: {frame1.shape}, 帧2形状: {frame2.shape}")
# else:
# print("未获取到同步帧对")
# 示例2根据通知参数获取帧
# print("\n=== 根据通知参数获取帧 ===")
# # 获取同步拼接帧(用于显示)
# combined_frame = controller.get_notification_frame(use_sync=True)
# if combined_frame is not None:
# print(f"获取到同步拼接帧,形状: {combined_frame.shape}")
# cv2.imshow("Sync Frame", combined_frame)
# cv2.waitKey(1000) # 显示1秒
# cv2.destroyAllWindows()
# # 获取单个摄像头帧
# single_frame = controller.get_notification_frame(camera_id='cam1', use_sync=False)
# if single_frame is not None:
# print(f"获取到cam1单帧形状: {single_frame.shape}")
# # 示例3实时显示
# print("\n=== 实时显示模式 ===")
# print("按 'q' 退出显示,按 's' 保存同步帧")
# # controller.display_live_feed() # 取消注释以启用实时显示
# # 示例4兼容性测试
# print("\n=== 兼容性测试 ===")
# old_frame = controller.capture_frame('cam1')
# if old_frame is not None:
# print(f"旧接口兼容 - 帧形状: {old_frame.shape}")
# 清理
controller.release()
print("\n摄像头资源已释放")
else:
print("双摄像头系统启动失败!")

View File

@ -1,6 +1,6 @@
# vision/detector.py
import os
from ultralytics import YOLO
import cv2
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
@ -9,16 +9,14 @@ 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
#model路径在对应的模型里面
# self.alignment_model = os.path.join(current_dir, "align_model/yolov11_cls_640v6.rknn")
def load_models(self):
"""
加载所有视觉检测模型
"""
from ultralytics import YOLO
success = True
# 加载夹角检测模型
@ -59,28 +57,44 @@ class VisionDetector:
return success
def detect_angle(self, image=None, image_path=None):
def detect_angle(self, image=None):
"""
通过视觉系统获取当前出砼门角度
"""
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image=cv2.flip(image, 0)
return get_current_door_angle(
model=self.angle_model,
image=image,
image_path=image_path
model=self.settings.angle_model_path,
image=image
)
def detect_overflow(self, image_array):
"""
通过图像检测是否溢料
"""
# image_array=cv2.flip(image_array, 0)
# cv2.imwrite('test.jpg', image_array)
cv2.namedWindow("Alignment", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Alignment", 640, 480)
cv2.imshow("Alignment", image_array)
cv2.waitKey(1)
print('path:', self.settings.overflow_model_path)
print('roi:', self.settings.roi_file_path)
return detect_overflow_from_image(
image_array,
self.overflow_model,
self.settings.roi_file_path
self.settings.overflow_model_path,
self.settings.roi_file_path,
image_array
)
def detect_vehicle_alignment(self, image_array):
"""
通过图像检测模具车是否对齐
"""
return detect_vehicle_alignment(image_array, self.alignment_model)
image_array = cv2.cvtColor(image_array, cv2.COLOR_BGR2RGB)
image_array=cv2.flip(image_array, 0)
# cv2.namedWindow("Alignment", cv2.WINDOW_NORMAL)
# cv2.resizeWindow("Alignment", 640, 480)
# cv2.imshow("Alignment", image_array)
# cv2.waitKey(1)
return detect_vehicle_alignment(image_array)

Binary file not shown.

View File

@ -0,0 +1,236 @@
import cv2
import numpy as np
import math
from shapely.geometry import Polygon
import os
# ------------------- 全局配置变量 -------------------
# 模型相关
CLASSES = ['clamp']
nmsThresh = 0.4
objectThresh = 0.35
# 可视化与保存控制(全局变量,可外部修改)
DRAW_RESULT = True # 是否在输出图像上绘制旋转框
SAVE_PATH = None # 保存路径,如 "./result.jpg";设为 None 则不保存
# RKNN 单例
_rknn_instance = None
# ------------------- RKNN 管理函数 -------------------
def init_rknn(model_path):
from rknnlite.api import RKNNLite
"""只加载一次 RKNN 模型"""
global _rknn_instance
if _rknn_instance is None:
_rknn_instance = RKNNLite(verbose=False)
ret = _rknn_instance.load_rknn(model_path)
if ret != 0:
print(f"[ERROR] Failed to load RKNN model: {ret}")
return None
ret = _rknn_instance.init_runtime(core_mask=RKNNLite.NPU_CORE_0)
if ret != 0:
print(f"[ERROR] Failed to init runtime: {ret}")
return None
return _rknn_instance
def release_rknn():
"""释放 RKNN 对象"""
global _rknn_instance
if _rknn_instance:
_rknn_instance.release()
_rknn_instance = None
# ------------------- 工具函数 -------------------
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_resized = 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_resized
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
cos_a, sin_a = math.cos(a), math.sin(a)
pts = [(x1, y1), (x1, y2), (x2, y2), (x2, y1)]
return [[int(cx + (xx - cx) * cos_a - (yy - cy) * sin_a),
int(cy + (xx - cx) * sin_a + (yy - cy) * cos_a)] for xx, yy in pts]
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):
x = np.clip(x, -709, 709) # 防止 exp 溢出
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:, :])
conf = conf.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[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 = (angle_feature[index + (h * model_w) + w] - 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_two_box_angle(model_path, rgb_frame):
"""
输入模型路径和 RGB 图像numpy array输出夹角和结果图像。
可视化和保存由全局变量 DRAW_RESULT 和 SAVE_PATH 控制。
"""
global _rknn_instance, DRAW_RESULT, SAVE_PATH
if not isinstance(rgb_frame, np.ndarray) or rgb_frame is None:
print(f"[ERROR] detect_two_box_angle 接收到错误类型: {type(rgb_frame)}")
return None, np.zeros((640, 640, 3), np.uint8)
# 注意:输入是 BGR因为 cv2.imread 返回 BGR但内部会转为 RGB 给模型
img = rgb_frame.copy()
img_resized, scale, offset_x, offset_y = letterbox_resize(img, (640, 640))
infer_img = np.expand_dims(cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB), 0)
try:
rknn = init_rknn(model_path)
if rknn is None:
return None, img
results = rknn.inference([infer_img])
except Exception as e:
print(f"[ERROR] RKNN 推理失败: {e}")
return None, img
outputs = []
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)
outputs += process(feature, x.shape[3], x.shape[2], stride, results[-1], index)
predbox = NMS(outputs)
print(f"[DEBUG] 检测到 {len(predbox)} 个框")
if len(predbox) < 2:
print("检测少于两个目标,无法计算夹角。")
return None, img
predbox = sorted(predbox, key=lambda x: x.score, reverse=True)
box1, box2 = predbox[:2]
output_img = img.copy() if DRAW_RESULT else img # 若不绘制,则直接用原图
if DRAW_RESULT:
for box in [box1, box2]:
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(output_img, [np.array(points, np.int32)], True, (0, 255, 0), 2)
def main_direction(box):
w, h = (box.xmax - box.xmin)/scale, (box.ymax - box.ymin)/scale
direction = box.angle if w >= h else box.angle + np.pi/2
return direction % np.pi
dir1 = main_direction(box1)
dir2 = main_direction(box2)
diff = abs(dir1 - dir2)
diff = min(diff, np.pi - diff)
angle_deg = np.degrees(diff)
# 保存结果(如果需要)
if SAVE_PATH:
save_dir = os.path.dirname(SAVE_PATH)
if save_dir: # 非空目录才创建
os.makedirs(save_dir, exist_ok=True)
cv2.imwrite(SAVE_PATH, output_img)
return angle_deg, output_img
# ------------------- 示例调用 -------------------
if __name__ == "__main__":
MODEL_PATH = "./obb.rknn"
IMAGE_PATH = "./11.jpg"
# # === 全局控制开关 ===
# DRAW_RESULT = True # 是否绘制框
# SAVE_PATH = "./result11.jpg" # 保存路径,设为 None 则不保存
# frame = cv2.imread(IMAGE_PATH)
# if frame is None:
# print(f"[ERROR] 无法读取图像: {IMAGE_PATH}")
# else:
# angle_deg, output_image = detect_two_box_angle(MODEL_PATH, frame)
# if angle_deg is not None:
# print(f"检测到的角度差: {angle_deg:.2f}°")
# else:
# print("未能成功检测到目标或计算角度差")

View File

@ -1,47 +1,33 @@
# vision/overflow_detector.py
import sys
import os
from vision.resize_tuili_image_main import classify_image_weighted, load_global_rois, crop_and_resize
from typing import Optional
from vision.overflow_model.yiliao_main_rknn import classify_frame_with_rois
# 添加项目根目录到Python路径
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
# sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
def detect_overflow_from_image(image_array, overflow_model, roi_file_path):
def detect_overflow_from_image(overflow_model,roi_file_path,image_array)->Optional[str]:
"""
通过图像检测是否溢料
:param image_array: 图像数组
:param overflow_model: 溢料检测模型
:param roi_file_path: ROI文件路径
:return: 是否检测到溢料 (True/False)
:return: 检测到溢料类别 (未堆料、小堆料、大堆料、未浇筑满、浇筑满) 或 None
"""
try:
# 检查模型是否已加载
if overflow_model is None:
print("堆料检测模型未加载")
return False
outputs = classify_frame_with_rois(overflow_model, image_array, roi_file_path)
print("溢料检测结果:", outputs)
for res in outputs:
# 加载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
return res["class"]
# if "大堆料" in res["class"] or "小堆料" in res["class"]:
# print(f"检测到溢料: {res['class']}")
# return True
# return False
return None
except Exception as e:
print(f"溢料检测失败: {e}")
return False
return None

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

View File

@ -0,0 +1,78 @@
# RKNN 堆料分类推理系统 README
本项目用于在 RK3588 平台上运行 RKNN 分类模型,对多个 ROI 区域进行堆料状态分类,包括:
未堆料 0
小堆料 1
大堆料 2
未浇筑满 3
浇筑满 4
项目中支持 多 ROI 裁剪、模型推理、加权判断(小/大堆料) 和分类结果输出。
## 目录结构
project/
│── yiliao_cls.rknn # RKNN 模型
│── best.pt # pt 模型
│── roi_coordinates/ # ROI 坐标文件目录
│ └── 1_rois.txt
│── test_image/ # 测试图片目录
│ └── 1.jpg
└── 2.jpg
└── 3.jpg
│── yiliao_main_rknn.py # RKNN主推理脚本
│── yiliao_main_pc.py # PC推理脚本
│── README.md
## 配置(略)
## 安装依赖(略)
## 调用示例
单张图片推理调用示例
```bash
from yiliao_main_rknn import classify_frame_with_rois
if __name__ == "__main__":
model_path = "yiliao_cls.rknn"
roi_file = "./roi_coordinates/1_rois.txt"
frame = cv2.imread("./test_image/2.jpg")
outputs = classify_frame_with_rois(model_path, frame, roi_file)
for res in outputs:
print(res)
```
##小堆料 / 大堆料加权判定说明
模型原始输出中小堆料class 1与大堆料class 2相比时容易出现概率接近的情况。
通过加权机制:
✔ 可以避免因整体概率偏低导致分类不稳定
✔ 优先放大“大堆料 的可能性”(因为 w2 > w1
✔ score 更能反映堆料大小的趋势,而不是绝对概率
为提高判断稳定性采用了加权评分方式这些参数都可以根据实际情况在文件中对weighted_small_large中参数进行修改
score = (0.3 * p1 + 0.7 * p2) / (p1 + p2)
score ≥ 0.4 → 大堆料
score < 0.4 小堆料
p1小堆料概率
p2大堆料概率
score 越接近 1 越倾向于大堆料
score 越接近 0 越倾向于小堆料

Binary file not shown.

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 587 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 513 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 MiB

Binary file not shown.

View File

@ -0,0 +1,168 @@
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 batch_classify_images(model_path, input_folder, output_root, roi_file, target_size=640, threshold=0.5):
# 加载模型
model = YOLO(model_path)
# 确保输出根目录存在
output_root = Path(output_root)
output_root.mkdir(parents=True, exist_ok=True)
# 为所有类别创建目录
class_dirs = {}
for name in CLASS_NAMES.values():
d = output_root / name
d.mkdir(exist_ok=True)
class_dirs[name] = d
rois = load_global_rois(roi_file)
if not rois:
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:
continue
crops = crop_and_resize(img, rois, target_size)
for roi_resized, roi_idx in crops:
final_class, score, p1, p2 = classify_image_weighted(roi_resized, model, threshold=threshold)
# 文件名中保存 ROI、类别、加权分数、class1/class2 置信度
suffix = f"_roi{roi_idx}_{final_class}_score{score:.2f}_p1{p1:.2f}_p2{p2:.2f}"
dst_path = class_dirs[final_class] / f"{img_path.stem}{suffix}{img_path.suffix}"
cv2.imwrite(dst_path, roi_resized)
print(f"{img_path.name}{suffix} -> {final_class} (score={score:.2f}, p1={p1:.2f}, p2={p2:.2f})")
except Exception as e:
print(f"处理失败 {img_path.name}: {e}")
# ---------------------------
# 单张图片使用示例(保留 ROI不保存文件
# ---------------------------
if __name__ == "__main__":
model_path = r"best.pt"
image_path = r"./test_image/2.jpg" # 单张图片路径
roi_file = r"./roi_coordinates/1_rois.txt"
target_size = 640
threshold = 0.4 #加权得分阈值可以根据大小堆料分类结果进行调整
# 加载模型
model = YOLO(model_path)
# 读取 ROI
rois = load_global_rois(roi_file)
if not rois:
print("❌ 没有有效 ROI退出")
exit(1)
# 读取图片
img = cv2.imread(image_path)
if img is None:
print(f"❌ 无法读取图片: {image_path}")
exit(1)
# 注意:必须裁剪 ROI 并推理因为训练的时候输入的图像是经过resize的
crops = crop_and_resize(img, rois, target_size)
for roi_resized, roi_idx in crops:
#final_class, score, p1, p2 = classify_image_weighted(roi_resized, model, threshold=threshold)
final_class,_,_,_ = classify_image_weighted(roi_resized, model, threshold=threshold)
# 只输出信息,不保存文件
#print(f"ROI {roi_idx} -> 类别: {final_class}, 加权分数: {score:.2f}, "
#f"class1 置信度: {p1:.2f}, class2 置信度: {p2:.2f}")
print(f"类别: {final_class}")

View File

@ -0,0 +1,185 @@
import os
from pathlib import Path
import cv2
import numpy as np
import platform
# ---------------------------
# 类别映射
# ---------------------------
CLASS_NAMES = {
0: "未堆料",
1: "小堆料",
2: "大堆料",
3: "未浇筑满",
4: "浇筑满"
}
# ---------------------------
# RKNN 全局实例(只加载一次)
# ---------------------------
_global_rknn = None
DEVICE_COMPATIBLE_NODE = '/proc/device-tree/compatible'
# =====================================================
# RKNN MODEL
# =====================================================
def init_rknn_model(model_path):
from rknnlite.api import RKNNLite
global _global_rknn
if _global_rknn is not None:
return _global_rknn
rknn = RKNNLite(verbose=False)
ret = rknn.load_rknn(model_path)
if ret != 0:
raise RuntimeError(f"Load RKNN failed: {ret}")
ret = rknn.init_runtime(core_mask=RKNNLite.NPU_CORE_0)
if ret != 0:
raise RuntimeError(f"Init runtime failed: {ret}")
_global_rknn = rknn
print(f"[INFO] RKNN 模型加载成功: {model_path}")
return rknn
# ---------------------------
# 图像预处理(统一 640×640
# ---------------------------
def preprocess(img, size=(640, 640)):
img = cv2.resize(img, size)
img = np.expand_dims(img, 0)
return img
# ---------------------------
# 单次 RKNN 分类
# ---------------------------
def rknn_classify(img_resized, model_path):
rknn = init_rknn_model(model_path)
input_tensor = preprocess(img_resized)
outs = rknn.inference([input_tensor])
pred = outs[0].reshape(-1)
class_id = int(np.argmax(pred))
return class_id, pred.astype(float)
# =====================================================
# ROI 逻辑
# =====================================================
def load_rois(txt_path):
rois = []
if not os.path.exists(txt_path):
print(f"❌ ROI 文件不存在: {txt_path}")
return rois
with open(txt_path) 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:
print("ROI 格式错误:", s)
return rois
def crop_and_resize(img, rois, target_size=640):
crops = []
h_img, w_img = img.shape[:2]
for idx, (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, idx))
return crops
# =====================================================
# class1/class2 加权分类增强
# =====================================================
def weighted_small_large(pred, threshold=0.4, w1=0.3, w2=0.7):
p1 = float(pred[1])
p2 = float(pred[2])
total = p1 + p2
score = (w1 * p1 + w2 * p2) / total if total > 0 else 0.0
final_class = "大堆料" if score >= threshold else "小堆料"
return final_class, score, p1, p2
# =====================================================
# ⭐ 高复用:一行完成 ROI + 推理 ⭐
# =====================================================
def classify_frame_with_rois(model_path, frame, roi_file, threshold=0.4):
"""
输入:
- frame: BGR 图像 (numpy array)
- model_path: RKNN 模型路径
- roi_file: ROI 的 txt 文件
- threshold: class1/class2 小/大堆料判断阈值
输出:
[
{ "roi": idx, "class": 类别, "score": 0.93, "p1": 0.22, "p2": 0.71 },
...
]
"""
if frame is None or not isinstance(frame, np.ndarray):
raise RuntimeError("❌ classify_frame_with_rois 传入的 frame 无效")
rois = load_rois(roi_file)
if not rois:
raise RuntimeError("ROI 文件为空")
crops = crop_and_resize(frame, rois)
results = []
for roi_img, idx in crops:
class_id, pred = rknn_classify(roi_img, model_path)
class_name = CLASS_NAMES.get(class_id, f"未知类别({class_id})")
if class_id in [1, 2]:
final_class, score, p1, p2 = weighted_small_large(pred, threshold)
else:
final_class = class_name
score = float(pred[class_id])
p1, p2 = float(pred[1]), float(pred[2])
results.append({
"roi": idx,
"class": final_class,
"score": round(score, 4),
"p1": round(p1, 4),
"p2": round(p2, 4)
})
return results
# =====================================================
# 示例调用
# =====================================================
if __name__ == "__main__":
model_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "yiliao_cls.rknn")
roi_file = "./roi_coordinates/1_rois.txt"
frame = cv2.imread("./test_image/2.jpg")
outputs = classify_frame_with_rois(model_path, frame, roi_file)
for res in outputs:
print(res)