导包
import cv2
import numpy as np
import mxnet as mx
import onnxruntime as ort
图片处理
img = cv2.imread(image_path)
方案1:
img = cv2.resize(img, (192, 192))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = np.transpose(img, (2, 0, 1)) HWC->CHW
input_blob = np.expand_dims(img, axis=0).astype(np.float32) # NCHW
#方案2:
input_std = 128.0
input_mean = 127.5
input_size=(192,192) #H W
input_blob = cv2.dnn.blobFromImage(img, 1.0/input_std, input_size, (input_mean, input_mean, input_mean), swapRB=True)
加载模型
onnx_model_path = "./2d106det.onnx"
ort_session = ort.InferenceSession(onnx_model_path)
#输入输出层名字(固定写法)
onnx_input_name = ort_session.get_inputs()[0].name
#输出层名字,可能有多个
onnx_outputs_names = ort_session.get_outputs()
output_names = []
for o in onnx_outputs_names:
output_names.append(o.name)
推理过程
onnx_result = ort_session.run(outputs_names, input_feed={onnx_input_name: input_blob})
onnx_result = onnx_result[0] #这里只有一个输出
网友评论