源码地址
- 本人使用的opencv c++ github代码,代码作者非本人
使用github源码结合自己导出的onnx模型推理自己的视频
推理条件
windows 10
Visual Studio 2019
Nvidia GeForce GTX 1070
opencv4.7.0 (opencv4.5.5在别的地方看到不支持yolov8的推理,所以只使用opencv4.7.0)
导出yolov8模型
yolov8版本: version = ‘8.0.110’
首先将default.yaml中的一些配置修改以下,将只修改的部分贴上去,注意下面的batch一定要设置为1
task: detect # YOLO task, i.e. detect, segment, classify, pose
mode: export # YOLO mode, i.e. train, val, predict, export, track, benchmark# Train settings -------------------------------------------------------------------------------------------------------
# model: C:\Users\HUST\Desktop\yolov8_ultralytics\ultralytics\models\v8\yolov8.yaml # path to model file, i.e. yolov8n.pt, yolov8n.yaml
model: C:\Users\Administrator\Desktop\yolov8_ultralytics\runs\detect\yolov8n\weights\best.pt # path to model file, i.e. yolov8n.pt, yolov8n.yaml
data: C:\Users\Administrator\Desktop\yolov8_ultralytics/ultralytics/datasets/custom.yaml # path to data file, i.e. coco128.yaml
weights: yolov8n.pt
epochs: 1 # number of epochs to train for
patience: 50 # epochs to wait for no observable improvement for early stopping of training
batch: 1 # number of images per batch (-1 for AutoBatch)
default.yaml中的export部分的配置也需要修改
# Export settings ------------------------------------------------------------------------------------------------------
format: onnx # format to export to
keras: False # use Keras
optimize: False # TorchScript: optimize for mobile
int8: False # CoreML/TF INT8 quantization
dynamic: False # ONNX/TF/TensorRT: dynamic axes
simplify: False # ONNX: simplify model
opset: 12 # ONNX: opset version (optional)
workspace: 4 # TensorRT: workspace size (GB)
nms: False # CoreML: add NMS
然后直接运行ultralytics/yolo/engine/exporter.py
测试一下导出的best.onnx可不可用,直接正常的val即可
将best.onnx模型放入netron中,onnx的输入和输出如下图1所示
图 1 图1 图1
c++部署
先将源码复制到下图位置中
环境和代码的大致步骤跟yolov5 opencv dnn部署 github代码一样
由于源码中使用的输入尺寸如图2是640 * 480的,我导出模型时使用的模型的输入如图1是640 * 640,所以需要对尺寸的那一部分需要进行修改,修改为640 * 640
const float INPUT_WIDTH = 640.0;
const float INPUT_HEIGHT = 640.0;
const float SCORE_THRESHOLD = 0.45;
const float NMS_THRESHOLD = 0.5;
const float CONFIDENCE_THRESHOLD = 0.25;
图 2 图2 图2在进行修改之后,就可以直接运行yolo.cpp
c++推理结果
yolov8_deploy_fire