yolov8旋转目标检测部署教程(附代码c++/python)

news/2025/6/13 8:35:12/

为了编写一个详细的YOLOv8旋转目标检测ONNX部署教程,我们需要考虑几个关键点:模型转换为ONNX格式、ONNX模型的部署以及后处理逻辑。由于YOLOv8本身还未发布,我们将基于现有的知识和技术来进行推断。

以下是部署YOLOv8旋转目标检测模型到ONNX的步骤,包括代码示例。请注意,这只是一个假设性的教程,因为YOLOv8的具体细节尚未公开。
在这里插入图片描述

1. 准备环境

确保安装了以下依赖:

  • Python 3.7+
  • PyTorch 1.10+
  • torchvision
  • OpenCV
  • numpy
  • onnx
  • onnxruntime
  • tqdm

安装所需的库:

pip install torch torchvision opencv-python numpy onnx onnxruntime tqdm

2. 模型转换为ONNX格式

假设你已经有了一个经过训练的YOLOv8旋转目标检测模型,接下来将其转换为ONNX格式。

导入库
import torch
import onnx
from onnxsim import simplify
转换为ONNX
def convert_to_onnx(model, input_size=(640, 640), output_file="yolov8.onnx"):dummy_input = torch.randn(1, 3, *input_size)  # 1 batch, 3 channels, input sizeinput_names = ["input"]output_names = ["output"]torch.onnx.export(model,dummy_input,output_file,export_params=True,opset_version=11,do_constant_folding=True,input_names=input_names,output_names=output_names,dynamic_axes={"input": {0: "batch_size"}, "output": {0: "batch_size"}})print(f"Model has been converted to ONNX format and saved to {output_file}")# Simplify the ONNX modelonnx_model = onnx.load(output_file)model_simplified, check = simplify(onnx_model)assert check, "Simplified ONNX model could not be validated"onnx.save(model_simplified, output_file)print(f"Simplified ONNX model saved to {output_file}")

3. ONNX模型部署

接下来,我们将使用ONNX Runtime来加载和运行ONNX模型。

导入库
import cv2
import numpy as np
import onnxruntime
加载ONNX模型
def load_onnx_model(model_path):sess = onnxruntime.InferenceSession(model_path, providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])input_name = sess.get_inputs()[0].nameoutput_name = sess.get_outputs()[0].namereturn sess, input_name, output_name
预处理
def preprocess_image(image_path, input_size=(640, 640)):img = cv2.imread(image_path)img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)img = cv2.resize(img, input_size)img = img.astype(np.float32)img /= 255.0img = np.transpose(img, (2, 0, 1))  # HWC -> CHWimg = np.expand_dims(img, axis=0)  # Add batch dimensionreturn img
后处理
def postprocess(output, image_shape, input_size=(640, 640)):# 假设输出包含旋转框的坐标和角度detections = output[0]boxes = detections[:, :5]  # x, y, width, height, anglescores = detections[:, 5]labels = detections[:, 6]# 调整检测框到原始图像尺寸scale_x = image_shape[1] / input_size[1]scale_y = image_shape[0] / input_size[0]boxes[:, 0] *= scale_xboxes[:, 1] *= scale_yboxes[:, 2] *= scale_xboxes[:, 3] *= scale_yreturn boxes, scores, labels
推理过程
def detect_rotated_boxes(image_path, sess, input_name, output_name, input_size=(640, 640)):img = preprocess_image(image_path, input_size)outputs = sess.run([output_name], {input_name: img})boxes, scores, labels = postprocess(outputs[0], cv2.imread(image_path).shape, input_size)return boxes, scores, labels
可视化结果
def visualize(image_path, boxes, scores, labels):img = cv2.imread(image_path)for box, score, label in zip(boxes, scores, labels):x, y, w, h, angle = box# 使用OpenCV绘制旋转矩形box_points = cv2.boxPoints(((x, y), (w, h), angle))box_points = np.int0(box_points)cv2.drawContours(img, [box_points], 0, (0, 0, 255), 2)cv2.putText(img, f"{label} {score:.2f}", (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)cv2.imshow("Rotated Object Detection", img)cv2.waitKey(0)cv2.destroyAllWindows()

4. 运行检测

编译和运行
1)编译cd examples/rknn_yolov8_obb_demobash build-linux_RK3588.sh2)运行cd install/rknn_yolov8obb_demo_Linux./rknn_yolov8obb_demo

结果展示

类别:

CLASSES = ['plane', 'ship', 'storage tank', 'baseball diamond', 'tennis court', 'basketball court','ground track field', 'harbor', 'bridge', 'large vehicle', 'small vehicle', 'helicopter', 'roundabout','soccer ball field', 'swimming pool']

在这里插入图片描述
最后:计算机视觉、图像处理、毕业辅导、作业帮助、代码获取,远程协助,代码定制,私聊会回复!


http://www.ppmy.cn/news/1506809.html

相关文章

VisionPro二次开发学习笔记11-使用 Caliper和Fixture定位Blob工具检测方块

该示例演示了如何使用卡尺工具和夹具工具来固定 Blob 工具。示例代码将检测图像上部区域中小方块的存在。当点击“运行”按钮时,将读取一张新图像。卡尺工具将被运行,卡尺工具的输出 Y 信息将传递给夹具工具。夹具工具使用来自卡尺工具的 Y 信息和新图像…

GeoServer+Postgis发布存储在Postgis中的栅格数据

这两天在研究GeoServer发布Postgis中存储的栅格数据,网上寻找的方法没有很哪个很顺利的成功了的(很多坑都踩了一遍),今天终于成功了,把发布过程记录一下,并分享给有需要的人。 前期准备 版本推荐&#xf…

onlyoffice使用Https访问

开发服务器用的是http,一切正常使用,部署到服务器后,由于服务器使用了Https,导致访问onlyoffice时控制台报错。Mixed Content: The page at http://xxxxx// was loaded over HTTPS, but requested an insecure frame http://xxxxx…

Java | Leetcode Java题解之第330题按要求补齐数组

题目&#xff1a; 题解&#xff1a; class Solution {public int minPatches(int[] nums, int n) {int patches 0;long x 1;int length nums.length, index 0;while (x < n) {if (index < length && nums[index] < x) {x nums[index];index;} else {x *…

【json解析】控制台打印json字符串格式正确,但json.loads()解析失败问题解决

问题为控制台打印json字符串格式正确&#xff0c;但json.loads()解析失败。看似简单的问题&#xff0c;却又折腾了好一会&#xff0c;因此记录一下解决方法&#xff01; 出现这个问题的原因&#xff1a;眼见不一定为实&#xff0c;控制台打印的json字符串并不一定是实际的json字…

Jar工具完全指南:从入门到精通

Jar工具完全指南&#xff1a;从入门到精通的详尽教程 前言 欢迎来到Jar工具的完全指南&#xff01;无论你是Java编程的初学者&#xff0c;还是经验丰富的开发者&#xff0c;掌握Jar工具都是必不可少的。Jar&#xff08;Java Archive&#xff09;是Java生态系统中的一个核心组…

Cocos通过Electron打包web应用后,在触屏一体机设备触摸滑动无效问题解决

Cocos通过Electron打包web应用后&#xff0c;在触屏一体机设备触摸滑动无效问题解决 已经很晚了&#xff0c;刚刚解决这个问题&#xff0c;还是想记录一下&#xff0c;因为刚刚接触 cocos 没多久&#xff0c;这个问题困扰了我很久。 背景 接手了一个答题小游戏&#xff0c;由于…

技术周总结 08.05-08.11周日

文章目录 一、08.06 周二1.1) 问题01 mac安装 scala:1. 使用 Homebrew2. 使用 SDKMAN!其他注意事项1. 确认 Scala 安装位置2. 设置 PATH 环境变量对于 zsh (macOS Catalina 及更高版本默认使用 zsh):对于 bash (如果您使用的是 bash shell): 3. 验证安装 二、08.09 周五2.1&…