convert-the-model-to-rknn/006验证PaddleOCR_ONNX模型.py

28 lines
1.1 KiB
Python

import onnxruntime as ort
import cv2
import numpy as np
# 加载 ONNX 模型(检测 / 识别)
det_session = ort.InferenceSession("/home/admin-root/haotian/康达瑞贝斯机器狗/det_shape.onnx", providers=['CPUExecutionProvider'])
rec_session = ort.InferenceSession("/home/admin-root/haotian/康达瑞贝斯机器狗/rec_shape.onnx", providers=['CPUExecutionProvider'])
# 示例预处理函数(根据你的模型需要调整)
def preprocess(img_path, target_size=(640, 640)):
img = cv2.imread(img_path)
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, target_size)
img = img.astype('float32') / 255.0
img = img.transpose(2, 0, 1) # HWC → CHW
img = np.expand_dims(img, axis=0)
return img
img_path = "/home/admin-root/haotian/康达瑞贝斯机器狗/data_image/001读表图片/3aee64cc1f90d93a5a45979f7b17cb4b_frame_001460.jpg"
input_blob = preprocess(img_path)
# 推理检测结果
det_out = det_session.run(None, {det_session.get_inputs()[0].name: input_blob})
print("Detection ONNX outputs:", det_out)
print("Detection ONNX outputs shape:", det_out[0].shape)
# 对应地,用识别模型预测