Replies: 2 comments
-
将PaddleOCR的检测模型转换为ONNX模型后,使用ONNXRuntime进行推理后处理的步骤包括解析模型输出、可视化和保存结果。以下是基于提供文档总结的具体后处理步骤: 1. 推理结果的后处理ONNX模型推理的输出通常是检测框(bounding boxes)的坐标信息和置信度分数。以下是后处理的步骤: 1.1 解析检测结果
代码示例: import numpy as np
# 假设推理返回的结果存储在变量 results
dt_boxes = results[0] # 第一个输出通常是检测框
# 将预测的归一化坐标转化为图像实际尺寸
image_height, image_width = original_image.shape[:2]
dt_boxes = np.array(dt_boxes) * [image_width, image_height, image_width, image_height] 1.2 筛选有效检测框
filtered_boxes = [box for box in dt_boxes if box[4] > confidence_threshold] 2. 可视化检测结果为了直观查看检测效果,可以将检测框绘制到原始图像上。使用常见的可视化工具如 代码示例: import cv2
# 读取原始图像
image = cv2.imread("input_image.jpg")
# 绘制检测框
for box in dt_boxes:
x_min, y_min, x_max, y_max = map(int, box[:4])
cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2) # 绘制矩形框
# 保存可视化结果
cv2.imwrite("visualized_result.jpg", image) 3. 保存检测结果将检测结果保存为JSON文件或者文本文件,以便后续分析或复用。 代码示例: import json
# 将检测框和置信度保存为JSON
output_data = [{"box": box[:4].tolist(), "confidence": box[4]} for box in dt_boxes]
with open("detection_results.json", "w") as f:
json.dump(output_data, f, indent=4) 4. 整合推理与后处理通常,ONNXRuntime的推理与后处理可以整合在一个脚本中。以下是一个完整的流程: import onnxruntime
import numpy as np
import cv2
import json
# 加载ONNX模型
model_path = "path_to_model.onnx"
session = onnxruntime.InferenceSession(model_path)
# 加载输入图片并预处理
image = cv2.imread("input_image.jpg")
input_image = cv2.resize(image, (640, 640)) # 根据模型要求调整尺寸
input_image = input_image.transpose(2, 0, 1).astype('float32') # HWC -> CHW
input_image = np.expand_dims(input_image, axis=0) # 增加batch维度
# 推理
input_name = session.get_inputs()[0].name
outputs = session.run(None, {input_name: input_image})
# 后处理
dt_boxes = outputs[0]
image_height, image_width = image.shape[:2]
dt_boxes = np.array(dt_boxes) * [image_width, image_height, image_width, image_height]
# 可视化
for box in dt_boxes:
x_min, y_min, x_max, y_max = map(int, box[:4])
cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
cv2.imwrite("visualized_result.jpg", image)
# 保存结果
output_data = [{"box": box[:4].tolist(), "confidence": box[4]} for box in dt_boxes]
with open("detection_results.json", "w") as f:
json.dump(output_data, f, indent=4) 注意事项
通过以上步骤,您可以完成ONNXRuntime推理结果的后处理、可视化和保存操作。 Response generated by feifei-bot | chatgpt-4o-latest |
Beta Was this translation helpful? Give feedback.
0 replies
-
参见我维护的RapidOCR项目,就是这么做的。 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
把检测模型转为了onnx模型后使用onnxruntime模型对图片进行推理,返回的结果怎么进行后处理,可视化,并保存。
Beta Was this translation helpful? Give feedback.
All reactions