完成夹具开合判断的代码框架,等图片来更新rknn
BIN
ailai_pc/results/result_192.168.0.234_01_20251014144144937.jpg
Normal file
|
After Width: | Height: | Size: 716 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014144222222.jpg
Normal file
|
After Width: | Height: | Size: 709 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014144303840.jpg
Normal file
|
After Width: | Height: | Size: 648 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014144341882.jpg
Normal file
|
After Width: | Height: | Size: 687 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014144421259.jpg
Normal file
|
After Width: | Height: | Size: 662 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014144500393.jpg
Normal file
|
After Width: | Height: | Size: 710 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014144538968.jpg
Normal file
|
After Width: | Height: | Size: 666 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014144619387.jpg
Normal file
|
After Width: | Height: | Size: 499 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014144814974.jpg
Normal file
|
After Width: | Height: | Size: 569 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014144929890.jpg
Normal file
|
After Width: | Height: | Size: 545 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014145006232.jpg
Normal file
|
After Width: | Height: | Size: 691 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014145045330.jpg
Normal file
|
After Width: | Height: | Size: 639 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014145128164.jpg
Normal file
|
After Width: | Height: | Size: 607 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014145244471.jpg
Normal file
|
After Width: | Height: | Size: 562 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014145321687.jpg
Normal file
|
After Width: | Height: | Size: 632 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014150210497.jpg
Normal file
|
After Width: | Height: | Size: 680 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014150246105.jpg
Normal file
|
After Width: | Height: | Size: 677 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014150325231.jpg
Normal file
|
After Width: | Height: | Size: 667 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014150406560.jpg
Normal file
|
After Width: | Height: | Size: 668 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014150444617.jpg
Normal file
|
After Width: | Height: | Size: 706 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_2025101415052359.jpg
Normal file
|
After Width: | Height: | Size: 690 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014150601333.jpg
Normal file
|
After Width: | Height: | Size: 726 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014150642365.jpg
Normal file
|
After Width: | Height: | Size: 631 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014150720686.jpg
Normal file
|
After Width: | Height: | Size: 634 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014150918102.jpg
Normal file
|
After Width: | Height: | Size: 633 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_2025101415103290.jpg
Normal file
|
After Width: | Height: | Size: 653 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_2025101415111060.jpg
Normal file
|
After Width: | Height: | Size: 618 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014151150768.jpg
Normal file
|
After Width: | Height: | Size: 660 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014151232368.jpg
Normal file
|
After Width: | Height: | Size: 603 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014151311222.jpg
Normal file
|
After Width: | Height: | Size: 637 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014151353357.jpg
Normal file
|
After Width: | Height: | Size: 627 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_202510141514352.jpg
Normal file
|
After Width: | Height: | Size: 584 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_2025101415152183.jpg
Normal file
|
After Width: | Height: | Size: 697 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014151922555.jpg
Normal file
|
After Width: | Height: | Size: 673 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014153132629.jpg
Normal file
|
After Width: | Height: | Size: 669 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014153215762.jpg
Normal file
|
After Width: | Height: | Size: 642 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014153257985.jpg
Normal file
|
After Width: | Height: | Size: 660 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014153338300.jpg
Normal file
|
After Width: | Height: | Size: 681 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014153420611.jpg
Normal file
|
After Width: | Height: | Size: 623 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014153506692.jpg
Normal file
|
After Width: | Height: | Size: 694 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014153632704.jpg
Normal file
|
After Width: | Height: | Size: 772 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014153800800.jpg
Normal file
|
After Width: | Height: | Size: 714 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014153839977.jpg
Normal file
|
After Width: | Height: | Size: 610 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014153918385.jpg
Normal file
|
After Width: | Height: | Size: 689 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014153957167.jpg
Normal file
|
After Width: | Height: | Size: 709 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014154037360.jpg
Normal file
|
After Width: | Height: | Size: 647 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014154121106.jpg
Normal file
|
After Width: | Height: | Size: 738 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014154203390.jpg
Normal file
|
After Width: | Height: | Size: 628 KiB |
BIN
ailai_pc/results/result_192.168.0.234_01_20251014154328204.jpg
Normal file
|
After Width: | Height: | Size: 638 KiB |
BIN
cls_main(框架完成rknn还没训练)/12.png
Normal file
|
After Width: | Height: | Size: 5.0 MiB |
91
cls_main(框架完成rknn还没训练)/cls_inference.py
Normal 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}")
|
||||
48
cls_main(框架完成rknn还没训练)/readme.md
Normal 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("夹具未夹紧")
|
||||
```
|
||||