Initial commit

This commit is contained in:
琉璃月光
2025-11-18 17:16:08 +08:00
commit caeb0457f4
26 changed files with 1141 additions and 0 deletions

12
LED_send/.idea/LED_send.iml generated Normal file
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,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
LED_send/.idea/misc.xml generated Normal file
View File

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

8
LED_send/.idea/modules.xml generated Normal file
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/LED_send.iml" filepath="$PROJECT_DIR$/.idea/LED_send.iml" />
</modules>
</component>
</project>

45
LED_send/.idea/workspace.xml generated Normal file
View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="722ee187-ac61-49c4-9cc8-e2dde3e3cecf" name="更改" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="ProjectColorInfo"><![CDATA[{
"associatedIndex": 6
}]]></component>
<component name="ProjectId" id="35dGBMBjEMzZi0YJZqDn3K3Mo9D" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"ModuleVcsDetector.initialDetectionPerformed": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"last_opened_file_path": "/home/hx/开发/LED_send"
}
}]]></component>
<component name="SharedIndexes">
<attachedChunks>
<set>
<option value="bundled-python-sdk-7e47963ff851-f0eec537fc84-com.jetbrains.pycharm.pro.sharedIndexes.bundled-PY-252.23892.515" />
</set>
</attachedChunks>
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="默认任务">
<changelist id="722ee187-ac61-49c4-9cc8-e2dde3e3cecf" name="更改" comment="" />
<created>1763432398540</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1763432398540</updated>
</task>
<servers />
</component>
</project>

75
LED_send/README.md Normal file
View File

@ -0,0 +1,75 @@
# LED 信息屏动态显示 Python 调用C/C++ SDK
本项目提供一个完整的 Python 示例,用于生成 LED 信息屏显示内容并通过 SDK 发送动态区域帧到 LED 屏。
它支持:
自动加载依赖的 .so 库(包括 libiconv和主 SDK 库 libbx_sdkDual.so
生成参数化 LED 表格图片
使用 SDK 的动态区接口发送帧到 LED 屏
## 目录结构
LED_send/
├── led_send.py # 主程序脚本
├── libbx_sdkDual.so # SDK 动态库
├── libiconv.so.2 # SDK 依赖库
├── simsun.ttc # 字体文件,用于生成 LED 表格
└── README.md # 说明文档
## 配置
### 配置库路径
脚本为方便调用默认加载当前目录下的 libbx_sdkDual.so如需修改
```bash
MAIN_SO_NAME = "libbx_sdkDual.so"
CURRENT_DIR = "/your/path/to/so"
```
### 数据对齐
#### data数据
```bash
data = {
"PlateVolume": "2.00",
"MouldCode": "SHR2B1-3",
"ProduceStartTime": "15:06",
"ArtifactID": "QR2B13099115D",
"Temper": "18.6℃",
"PlateIDSerial": "85",
"CheckResult": "合格",
"TotMete": "353.2",
"LowBucketWeighingValue": "75",
"HighBucketWeighingValue": "115",
"WorkshopTemperature": "12.4℃",
"VibrationFrequency": "10min/220HZ",
"FormulaProportion": "水泥:砂:石:粉煤灰:矿粉:外加剂:水\r\n0.70:1.56:2.78:0.15:0.15:0.006:0.33",
"DayStrengthValue": "白班:2024/11/27 22:00抗压 龄期:15h 强度25.9",
"NihtStrengthValue": "晚班:2024/11/26 07:55抗压 龄期:12h 强度25.2"
}
```
#### LED 屏表格数据对应关系
| LED 区域 | 示例图片位置 | 对应数据字段 | 示例值 |
|----------- |-----------------|-----------------------------------------|-------------------------------|
| 表头 | 屏幕最上方 | 固定文字 | 浇筑工序信息屏测试 |
| 本盘方量 | 第一行第1列 | PlateVolume | 2.00 |
| 当前模具 | 第一行第2列 | MouldCode | SHR2B1-3 |
| 高斗称值 | 第一行第3列 | HighBucketWeighingValue | 115 |
| 低斗称值 | 第一行第4列 | LowBucketWeighingValue | 75 |
| 投料时间 | 第二行第1列 | ProduceStartTime | 15:06 |
| 当前管片 | 第二行第2列 | ArtifactID | QR2B13099115D |
| 砼出料温度 | 第二行第3列 | Temper | 18.6℃ |
| 振捣频率 | 第二行第4列 | VibrationFrequency | 10min/220HZ |
| 累计盘次 | 第三行第1列 | PlateIDSerial | 85 |
| 隐蔽验收 | 第三行第2列 | CheckResult | 合格 |
| 车间环温 | 第三行第3列 | WorkshopTemperature | 12.4℃ |
| 任务方量 | 第三行第4列 | TotMete / 固定值 | 353.2 / 214.1 |
| 配方比例 | 第四行跨3列 | FormulaProportion | 水泥:砂:石:粉煤灰:矿粉:外加剂:水... |
| 拆模强度 | 第五行跨3列 | DayStrengthValue / NihtStrengthValue | 白班:2024/11/27 22:00抗压 龄期:15h 强度25.9 / 晚班:2024/11/26 07:55抗压 龄期:12h 强度25.2 |
## 函数调用
```bash
from led_send import send_led_data
send_led_data(data)
```

292
LED_send/led_send.py Normal file
View File

@ -0,0 +1,292 @@
#!/usr/bin/env python3
# coding: utf-8
import os
import cv2
from PIL import Image, ImageDraw, ImageFont
import ctypes
from ctypes import *
import glob
import sys
# ============================================================
# SDK Load
# ============================================================
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
MAIN_SO_NAME = "libbx_sdkDual.so"
MAIN_SO = os.path.join(CURRENT_DIR, MAIN_SO_NAME)
def preload_shared_objects(so_dir):
print(f"自动加载 so 路径:{so_dir}")
if not os.path.isdir(so_dir):
print(f"错误:目录不存在: {so_dir}")
return None
so_list = glob.glob(os.path.join(so_dir, "*.so*"))
iconv_files = [s for s in so_list if "libiconv" in os.path.basename(s)]
loaded = set()
for f in iconv_files:
try:
ctypes.CDLL(f, mode=ctypes.RTLD_GLOBAL)
print(f"已加载 libiconv: {f}")
loaded.add(f)
except Exception as e:
print(f"加载失败 {f}: {e}")
for f in so_list:
if os.path.basename(f) == MAIN_SO_NAME or f in loaded:
continue
try:
ctypes.CDLL(f, mode=ctypes.RTLD_GLOBAL)
print(f"已加载依赖库: {f}")
except Exception as e:
print(f"跳过无法加载的库 {f}: {e}")
if os.path.exists(MAIN_SO):
try:
lib = ctypes.CDLL(MAIN_SO, mode=ctypes.RTLD_GLOBAL)
print(f"成功加载主库: {MAIN_SO}")
return lib
except Exception as e:
print(f"主库加载失败: {MAIN_SO} -> {e}")
return None
else:
print(f"主库不存在: {MAIN_SO}")
return None
os.environ["LD_LIBRARY_PATH"] = CURRENT_DIR + ":" + os.environ.get("LD_LIBRARY_PATH", "")
os.environ["PATH"] = CURRENT_DIR + ":" + os.environ.get("PATH", "")
lib = preload_shared_objects(CURRENT_DIR)
if lib is None:
print("无法加载主库,程序退出")
sys.exit(1)
# ====================== 生成 LED 表格 ======================
def generate_led_table(data, output_path="led_send.png", font_path="simsun.ttc"):
"""
根据接口返回的 Data 生成 LED 显示表格,适配 640x448 LED 屏
"""
try:
font_title = ImageFont.truetype(font_path, 24)
font_data = ImageFont.truetype(font_path, 20)
font_data_big = ImageFont.truetype(font_path, 22)
font_small = ImageFont.truetype(font_path, 16)
header_font = ImageFont.truetype(font_path, 30)
except IOError:
print("字体未找到,使用默认字体")
font_title = font_data = font_data_big = font_small = ImageFont.load_default()
header_font = ImageFont.load_default()
total_width, total_height = 640, 448
img = Image.new("RGB", (total_width, total_height), (0, 0, 0))
draw = ImageDraw.Draw(img)
col_count = 4
row_count = 8
row_heights = [int(total_height * 0.095)] * 6 + [int(total_height * 0.15), int(total_height * 0.15)]
y_positions = [0]
for h in row_heights[:-1]:
y_positions.append(y_positions[-1] + h)
col_width = total_width // col_count
header_text = "浇筑工序信息屏测试"
bbox = draw.textbbox((0, 0), header_text, font=header_font)
tw, th = bbox[2] - bbox[0], bbox[3] - bbox[1]
draw.text(((total_width - tw) // 2, 7), header_text, fill="Yellow", font=header_font)
# safe float parse
try:
task_quantity = float(data.get("TotMete", 0))
except Exception:
task_quantity = 0.0
fixed_value = 214.1
task_quantity_str = f"{task_quantity}"
fixed_value_str = f"/{fixed_value}"
table_data = [
["本盘方量", "当前模具", "高斗称值", "低斗称值"],
[str(data.get("PlateVolume", "")), str(data.get("MouldCode", "")), str(data.get("HighBucketWeighingValue", "")), str(data.get("LowBucketWeighingValue", ""))],
["投料时间", "当前管片", "砼出料温度", "振捣频率"],
[str(data.get("ProduceStartTime", "")), str(data.get("ArtifactID", "")), str(data.get("Temper", "")), str(data.get("VibrationFrequency", ""))],
["累计盘次", "隐蔽验收", "车间环温", "任务方量"],
[str(data.get("PlateIDSerial", "任务方量")), str(data.get("CheckResult", "")), str(data.get("WorkshopTemperature", "")), ""],
["配方比例", "", "", ""],
["拆模强度", "", "", ""]
]
# 画表格框
for r in range(row_count):
y1 = y_positions[r] + 40
h = row_heights[r]
for c in range(col_count):
x1 = c * col_width
if r >= 6 and c == 1:
draw.rectangle([x1, y1, total_width - 1, y1 + h - 1], outline="white", width=1)
break
elif r >= 6 and c > 1:
continue
else:
draw.rectangle([x1, y1, x1 + col_width - 1, y1 + h - 1], outline="white", width=1)
# 绘制文本
for r in range(row_count):
y1 = y_positions[r] + 40
h = row_heights[r]
for c in range(col_count):
x1 = c * col_width
content = table_data[r][c]
if not content.strip():
if r == 5 and c == 3:
bbox_task = draw.textbbox((0, 0), task_quantity_str, font=font_data)
tw_task = bbox_task[2] - bbox_task[0]
th_task = bbox_task[3] - bbox_task[1]
draw.text((x1 + (col_width - 1.8 * tw_task) // 2, y1 + (h - th_task) // 2), task_quantity_str, fill="red", font=font_data)
bbox_fixed = draw.textbbox((0, 0), fixed_value_str, font=font_data)
tw_fixed = bbox_fixed[2] - bbox_fixed[0]
draw.text((x1 + (col_width - tw_fixed) // 2 + 0.78 * tw_task, y1 + (h - th_task) // 2), fixed_value_str, fill="green", font=font_data)
continue
is_header = r in (0, 2, 4, 6, 7)
color = "green" if is_header else "red"
if color == "red" and r < 3:
font = font_data_big
elif color == "red" and r >= 6:
font = font_small
else:
font = font_title if is_header else font_data
bbox = draw.textbbox((0, 0), content, font=font)
tw = bbox[2] - bbox[0]
th = bbox[3] - bbox[1]
draw.text((x1 + (col_width - tw) // 2, y1 + (h - th) // 2), content, fill=color, font=font)
# 多行文本居中函数
def draw_multiline_text_center(draw_obj, x, y, width, height, text, font_obj, fill="red"):
lines = text.split('\n')
bboxs = [draw_obj.textbbox((0, 0), line, font=font_obj) for line in lines]
total_h = sum(b[3] - b[1] for b in bboxs)
y_start = y + (height - total_h) // 2
curr_y = y_start
for line, b in zip(lines, bboxs):
w = b[2] - b[0]
h = b[3] - b[1]
draw_obj.text((x + (width - w) // 2, curr_y), line, fill=fill, font=font_obj)
curr_y += h
draw_multiline_text_center(draw, col_width * 1, y_positions[6] + 40, col_width * 3, row_heights[6],
str(data.get("FormulaProportion", "")).replace("\r", ""), font_small)
draw_multiline_text_center(draw, col_width * 1, y_positions[7] + 40, col_width * 3, row_heights[7],
f"{data.get('DayStrengthValue', '')}\n{data.get('NihtStrengthValue', '')}", font_small)
img.save(output_path)
print(f"已生成参数化表格:{output_path}")
# ====================== 动态区结构体 ======================
class EQpageHeader_G6(Structure):
_fields_ = [
("PageStyle", c_uint8), ("DisplayMode", c_uint8), ("ClearMode", c_uint8),
("Speed", c_uint8), ("StayTime", c_uint16), ("RepeatTime", c_uint8),
("ValidLen", c_uint8), ("CartoonFrameRate", c_uint8), ("BackNotValidFlag", c_uint8),
("arrMode", c_uint8), ("fontSize", c_uint8), ("color", c_uint8),
("fontBold", c_uint8), ("fontItalic", c_uint8), ("tdirection", c_uint8),
("txtSpace", c_uint8), ("Valign", c_uint8), ("Halign", c_uint8)
]
lib.bxDual_dynamicArea_DelArea_6G.argtypes = [c_char_p, c_uint32, c_uint8]
lib.bxDual_dynamicArea_AddAreaPic_6G.argtypes = [
c_char_p, c_uint32, c_uint8, c_uint8, c_uint16, c_uint16,
c_uint16, c_uint16, POINTER(EQpageHeader_G6), c_char_p
]
lib.bxDual_dynamicArea_DelArea_6G.restype = c_int
lib.bxDual_dynamicArea_AddAreaPic_6G.restype = c_int
# ====================== 发送动态区帧 ======================
def send_dynamic_frame(ip="10.6.242.2", port=5005, frame=None, filename="led_send.png"):
if frame is None:
print("frame 为空!") #因为相机SDK接口需要的是待发送图片的地址所以加上确认需要发送图片是否存在。
return
target_w, target_h = 640, 448
resized = cv2.resize(frame, (target_w, target_h))
current_dir = os.path.dirname(os.path.abspath(__file__))
save_path = os.path.join(current_dir, filename)
# 使用 cv2.imwrite 保存确保文件编码一致
cv2.imwrite(save_path, resized)
# 这些参数都可以设置,我备注一下参数名称和调节的信息
page = EQpageHeader_G6()
page.PageStyle = 0 #数据页类型默认为0
page.DisplayMode = 2 #显示方式: 0x00 :随机显示 0x01 :静止显示 0x02 :快速打出 0x03 :向左移动 0x04 :向左连移 0x05 :向上移动 0x06 :向上连移 0x07 :闪烁 ......
page.ClearMode = 1 #退出方式/清屏方式
page.Speed = 10 #速度等级/背景速度等级
page.StayTime = 1000 #停留时间, 单位为 10ms
page.RepeatTime = 1 #重复次数/背景拼接步长(左右拼接下为宽度, 上下拼接为高度)
page.ValidLen = 64 #用法比较复杂请参考协议,默认不动
page.CartoonFrameRate = 0 #特技为动画方式时,该值代表其帧率
page.BackNotValidFlag = 0 #背景无效标志
#字体信息
page.arrMode = 1 #排列方式--单行多行
page.fontSize = 16 #字体大小
page.color = 1 #字体颜色 E_Color_G56此通过此枚举值可以直接配置七彩色如果大于枚举范围使用RGB888模式
page.fontBold = 0 #是否为粗体
page.fontItalic = 0 #是否为斜体
page.tdirection = 0 #文字方向
page.txtSpace = 0 #文字间隔
page.Valign = 2 #纵向对齐方式0系统自适应、1上对齐、2居中、3下对齐
page.Halign = 1 #横向对齐方式0系统自适应、1左对齐、2居中、3右对齐
print("删除旧动态区 ...")
try:
ret_del = lib.bxDual_dynamicArea_DelArea_6G(ip.encode(), port, 0xFF)
print("删除返回码:", ret_del)
except Exception as e:
print("调用 DelArea 失败:", e)
try:
ret = lib.bxDual_dynamicArea_AddAreaPic_6G(
ip.encode("ascii"), port, 2, 0, 0, 0, target_w, target_h,
byref(page), save_path.encode("gb2312")
)
if ret == 0:
print("Frame 发送成功!")
else:
print("Frame 发送失败,返回码:", ret)
except Exception as e:
print("调用 AddAreaPic 失败:", e)
def send_led_data(data: dict):
img_path = os.path.join(CURRENT_DIR, "led_send.png")
generate_led_table(data, output_path=img_path)
#这里读取图片是为了保证生成图片函数已经在改文件夹下生成了图片因为相机SDK接口需要的是待发送图片的地址所以加上确认。
frame = cv2.imread(img_path)
send_dynamic_frame(frame=frame, filename="led_send.png")
# ============================================================
# 主程序示例
# ============================================================
if __name__ == "__main__":
data = {
"PlateVolume": "2.00",
"MouldCode": "SHR2B1-3",
"ProduceStartTime": "15:06",
"ArtifactID": "QR2B13099115D",
"Temper": "18.6℃",
"PlateIDSerial": "85",
"CheckResult": "合格",
"TotMete": "353.2",
"LowBucketWeighingValue": "75",
"HighBucketWeighingValue": "115",
"WorkshopTemperature": "12.4℃",
"VibrationFrequency": "10min/220HZ",
"FormulaProportion": "水泥:砂:石:粉煤灰:矿粉:外加剂:水\r\n0.70:1.56:2.78:0.15:0.15:0.006:0.33",
"DayStrengthValue": "白班:2024/11/27 22:00抗压 龄期:15h 强度25.9",
"NihtStrengthValue": "晚班:2024/11/26 07:55抗压 龄期:12h 强度25.2"
}
send_led_data(data)

BIN
LED_send/libbx_sdkDual.so Normal file

Binary file not shown.

BIN
LED_send/libiconv.so.2 Normal file

Binary file not shown.

BIN
LED_send/simsun.ttc Normal file

Binary file not shown.

BIN
obb_angle/obb.rknn Normal file

Binary file not shown.

235
obb_angle/obb_angle.py Normal file
View File

@ -0,0 +1,235 @@
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.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):
"""
输入模型路径和 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("未能成功检测到目标或计算角度差")

5
zhuangtai_class_cls_1980x1080/.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 越倾向于小堆料

Binary file not shown.

View File

@ -0,0 +1,2 @@
644, 608, 522, 246

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,178 @@
import os
from pathlib import Path
import cv2
import numpy as np
import platform
from rknnlite.api import RKNNLite
# ---------------------------
# 类别映射
# ---------------------------
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):
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_single_roi(txt_path):
"""
只加载第一个 ROI
格式: x,y,w,h
"""
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
try:
x, y, w, h = map(int, s.split(','))
return (x, y, w, h)
except:
raise RuntimeError(f"❌ ROI 格式错误: {s}")
raise RuntimeError("❌ ROI 文件为空")
def crop_and_resize_single(img, roi, target_size=640):
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}")
roi_img = img[y:y + h, x:x + w]
roi_resized = cv2.resize(roi_img, (target_size, target_size), interpolation=cv2.INTER_AREA)
return roi_resized
# =====================================================
# 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_single_roi(model_path, frame, roi_file, threshold=0.4):
"""
输入:
- frame: BGR 图像
- model_path: RKNN 模型
- roi_file: 只包含一个 ROI 的 txt 文件
- threshold: class1/class2 判断阈值
输出:
{ "class": 类别, "score": x, "p1": x, "p2": x }
"""
if frame is None or not isinstance(frame, np.ndarray):
raise RuntimeError("❌ classify_frame_with_single_roi 传入的 frame 无效")
# ------- 只加载第一个 ROI -------
roi = load_single_roi(roi_file)
# ------- 裁剪并 resize -------
roi_img = crop_and_resize_single(frame, roi)
# ------- RKNN 推理 -------
class_id, pred = rknn_classify(roi_img, model_path)
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, 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)
}
# =====================================================
# 示例调用
# =====================================================
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)