完成夹具开合判断的代码框架,等图片来更新rknn

This commit is contained in:
琉璃月光
2025-11-12 15:05:27 +08:00
parent 468ac6829f
commit 91fd55b54a
53 changed files with 139 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 716 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 709 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 648 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 687 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 662 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 710 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 666 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 499 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 569 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 545 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 691 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 639 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 607 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 562 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 632 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 680 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 677 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 667 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 668 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 706 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 690 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 726 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 631 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 634 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 633 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 653 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 618 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 660 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 603 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 637 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 627 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 584 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 697 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 673 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 669 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 642 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 660 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 681 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 623 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 694 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 772 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 714 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 610 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 689 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 709 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 647 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 738 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 628 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 638 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 MiB

View File

@ -0,0 +1,91 @@
import cv2
import numpy as np
import platform
from rknnlite.api import RKNNLite
# ------------------- 全局变量 -------------------
_global_rknn_instance = None
labels = {0: '夹具未夹紧', 1: '夹具夹紧'}
DEVICE_COMPATIBLE_NODE = '/proc/device-tree/compatible'
def get_host():
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 preprocess(raw_image, target_size=(640, 640)):
img = cv2.resize(raw_image, target_size)
img = np.expand_dims(img, 0) # 添加 batch 维度
return img
# ------------------- RKNN 模型初始化 -------------------
def init_rknn_model(model_path):
global _global_rknn_instance
if _global_rknn_instance is None:
rknn_lite = RKNNLite(verbose=False)
ret = rknn_lite.load_rknn(model_path)
if ret != 0:
print(f'[ERROR] Load model failed (code: {ret})')
exit(ret)
ret = rknn_lite.init_runtime(core_mask=RKNNLite.NPU_CORE_0)
if ret != 0:
print(f'[ERROR] Init runtime failed (code: {ret})')
exit(ret)
_global_rknn_instance = rknn_lite
print(f'[INFO] Model loaded successfully: {model_path}')
return _global_rknn_instance
# ------------------- 推理 -------------------
def yolov11_cls_inference(model_path, raw_image, target_size=(640, 640)):
"""
返回:(class_id, boolean)
类别 0 -> False
类别 1 -> True
"""
img = preprocess(raw_image, target_size)
rknn = init_rknn_model(model_path)
outputs = rknn.inference([img])
# 获取类别 ID
output = outputs[0].reshape(-1)
class_id = int(np.argmax(output))
bool_value = True if class_id == 1 else False
return class_id, bool_value
# ------------------- 测试 -------------------
if __name__ == '__main__':
image_path = "12.png"
bgr_image = cv2.imread(image_path)
if bgr_image is None:
print(f"Failed to read image from {image_path}")
exit(-1)
rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
class_id, bool_value = yolov11_cls_inference(
model_path="yolov11_cls.rknn",
raw_image=rgb_image,
target_size=(640, 640)
)
print(f"类别ID: {class_id}, 布尔值: {bool_value}")

View File

@ -0,0 +1,48 @@
# yolov11_cls_inference README
## 概述
该模块用于对米厂输入图像执行二分类推理,用于判断机械臂夹爪是否夹紧。
类别定义:
0 → 夹具未夹紧 (False)
1 → 夹具夹紧 (True)
rknn模型只加载一次复用全局实例提高推理效率。
## 调用示例
您可以直接调用 yolov11_cls_inference 函数,以便集成到其他项目中:
示例 1: 测试仅获取单张图片的类别和布尔值
```bash
from yolov11_cls_inference import yolov11_cls_inference
import cv2
# 读取图像
bgr_image = cv2.imread("11.jpg")
rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
# 调用推理函数
class_id, bool_value = yolov11_cls_inference(
model_path="yolov11_cls.rknn",
raw_image=rgb_image,
target_size=(640, 640)
)
print(f"类别ID: {class_id}, 布尔值: {bool_value}")
```
示例 2: 直接在其他项目中集成使用
```bash
from yolov11_cls_inference import yolov11_cls_inference
# raw_image 已经读取或处理好的图像
class_id, bool_value = yolov11_cls_inference(model_path="yolov11_cls.rknn", raw_image=raw_image)
if bool_value:
print("夹具夹紧")
else:
print("夹具未夹紧")
```