更新液面diff代码

This commit is contained in:
琉璃月光
2025-12-28 00:14:08 +08:00
parent 14710eff25
commit 0adddd6306
87 changed files with 2764 additions and 213 deletions

5
zhuangtai_class_cls_1980x1080_61/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,5 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.10" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" />
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/zhuangtai_class_cls.iml" filepath="$PROJECT_DIR$/.idea/zhuangtai_class_cls.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.10" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
<option name="format" value="PLAIN" />
<option name="myDocStringFormat" value="Plain" />
</component>
</module>

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/1.png")
result = classify_frame_with_single_roi(model_path, frame, roi_file)
print(result)
```
##小堆料 / 大堆料加权判定说明
模型原始输出中小堆料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 越倾向于小堆料

View File

@ -0,0 +1 @@
445,540,931,319

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 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,163 @@
import os
import cv2
import numpy as np
from rknnlite.api import RKNNLite
# ---------------------------
# 类别映射
# ---------------------------
CLASS_NAMES = {
0: "未堆料",
1: "小堆料",
2: "大堆料",
3: "未浇筑满",
4: "浇筑满"
}
# ---------------------------
# RKNN 全局实例(只加载一次)
# ---------------------------
_global_rknn = None
def init_rknn_model(model_path):
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
# ---------------------------
# 预处理
# ---------------------------
def letterbox(image, new_size=640, color=(114,114,114)):
h, w = image.shape[:2]
scale = min(new_size/h, new_size/w)
nh, nw = int(h*scale), int(w*scale)
resized = cv2.resize(image, (nw, nh))
new_img = np.full((new_size, new_size,3), color, dtype=np.uint8)
top = (new_size-nh)//2
left = (new_size-nw)//2
new_img[top:top+nh, left:left+nw] = resized
return new_img
def resize_stretch(image, size=640):
return cv2.resize(image, (size, size))
def preprocess_image_for_rknn(img, size=640, resize_mode="stretch", to_rgb=False, normalize=False, layout="NHWC"):
if resize_mode=="letterbox":
img_box = letterbox(img, new_size=size)
else:
img_box = resize_stretch(img, size=size)
if to_rgb:
img_box = cv2.cvtColor(img_box, cv2.COLOR_BGR2RGB)
img_f = img_box.astype(np.float32)
if normalize:
img_f /= 255.0
if layout=="NHWC":
out = np.expand_dims(img_f, axis=0)
else:
out = np.expand_dims(np.transpose(img_f,(2,0,1)), axis=0)
return out.astype(np.float32)
# ---------------------------
# 单次 RKNN 推理
# ---------------------------
def rknn_classify_preprocessed(input_tensor, model_path):
rknn = init_rknn_model(model_path)
input_tensor = np.ascontiguousarray(input_tensor.astype(np.float32))
outs = rknn.inference([input_tensor])
pred = outs[0].reshape(-1).astype(float)
class_id = int(np.argmax(pred))
return class_id, pred
# ---------------------------
# ROI
# ---------------------------
def load_single_roi(txt_path):
if not os.path.exists(txt_path):
raise RuntimeError(f"ROI 文件不存在: {txt_path}")
with open(txt_path) as f:
for line in f:
s = line.strip()
if not s: continue
x,y,w,h = map(int, s.split(','))
return (x,y,w,h)
raise RuntimeError("ROI 文件为空")
def crop_and_return_roi(img, roi):
x,y,w,h = roi
h_img, w_img = img.shape[:2]
if x<0 or y<0 or x+w>w_img or y+h>h_img:
raise RuntimeError(f"ROI 超出图像范围: {roi}")
return img[y:y+h, x:x+w]
# ---------------------------
# class1/class2 加权
# ---------------------------
def weighted_small_large(pred, threshold=0.4, w1=0.3, w2=0.7):
p1,p2 = float(pred[1]), 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
# ---------------------------
# 单张图片推理
# ---------------------------
def classify_single_image(model_path, frame, roi_file,
threshold=0.4,
size=640, resize_mode="stretch",
to_rgb=True, normalize=False, layout="NHWC"):
"""
对单张图像进行分类推理(输入为 OpenCV 图像 ndarray
Args:
model_path (str): RKNN 模型路径
frame (np.ndarray): BGR 格式的 OpenCV 图像 (H, W, 3)
roi_file (str): ROI 坐标文件路径格式x,y,w,h
... 其他参数同上 ...
Returns:
dict: 分类结果
"""
if frame is None or frame.size == 0:
raise ValueError("❌ 输入图像为空或无效")
roi = load_single_roi(roi_file)
roi_img = crop_and_return_roi(frame, roi)
input_tensor = preprocess_image_for_rknn(roi_img, size=size, resize_mode=resize_mode,
to_rgb=to_rgb, normalize=normalize, layout=layout)
class_id, pred = rknn_classify_preprocessed(input_tensor, 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])
return {"class": final_class, "score": round(score, 4), "p1": round(p1, 4), "p2": round(p2, 4),
"raw": pred.tolist()}
# ---------------------------
# 示例调用
# ---------------------------
if __name__=="__main__":
model_path = "yiliao_cls61.rknn"
roi_file = "./roi_coordinates/61_rois.txt"
image_path = "./test_image/2.png"
result = classify_single_image(model_path, image_path, roi_file)
print("[RESULT]", result)