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

为了编写一个详细的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 size
    input_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 model
    onnx_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].name
    output_name = sess.get_outputs()[0].name
    return 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.0
    img = np.transpose(img, (2, 0, 1))  # HWC -> CHW
    img = np.expand_dims(img, axis=0)  # Add batch dimension
    return img
后处理
def postprocess(output, image_shape, input_size=(640, 640)):
    # 假设输出包含旋转框的坐标和角度
    detections = output[0]
    boxes = detections[:, :5]  # x, y, width, height, angle
    scores = detections[:, 5]
    labels = detections[:, 6]
    
    # 调整检测框到原始图像尺寸
    scale_x = image_shape[1] / input_size[1]
    scale_y = image_shape[0] / input_size[0]
    boxes[:, 0] *= scale_x
    boxes[:, 1] *= scale_y
    boxes[:, 2] *= scale_x
    boxes[:, 3] *= scale_y
    
    return 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_demo

bash build-linux_RK3588.sh

2)运行

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']

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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿利同学

一角两角不嫌少

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值