小工具知识网

当前位置: 首页 > 行业知识 > yolov8 opencv模型部署(C++版)

行业知识

yolov8 opencv模型部署(C++版)

2026-11-03 21:03:09 来源:互联网转载
  • TensorRT系列之 Windows10下yolov8 tensorrt模型加速部署

  • TensorRT系列之 Linux下 yolov8 tensorrt模型加速部署

  • TensorRT系列之 Linux下 yolov7 tensorrt模型加速部署

  • TensorRT系列之 Linux下 yolov6 tensorrt模型加速部署

  • TensorRT系列之 Linux下 yolov5 tensorrt模型加速部署

  • TensorRT系列之 Linux下 yolox tensorrt模型加速部署

  • TensorRT系列之 Linux下 u2net tensorrt模型加速部署

  • 更多(点我进去)…

    文章目录

    • yolov8 opencv模型部署(C++ 版)
      • 一、安装yolov8
      • 二、导出onnx
      • 三、基于opencv CPP推理onnx

        yolov8 opencv模型部署(C++ 版)

        使用opencv推理yolov8模型,仅依赖opencv,无需其他库,以yolov8s为例子,注意:

        • 使用opencv4.8.1 !
        • 使用opencv4.8.1 !
        • 使用opencv4.8.1 !

          如果你使用别的版本,例如opencv4.5,可能会出现以下错误。

          20231206新增推理效果,代码修改。

          一、安装yolov8

          conda create -n yolov8 python=3.9 -yconda activate yolov8pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple

          二、导出onnx

          导出onnx格式模型的时候,注意,如果你是自己训练的模型,只需要把以下代码中yolov8s.pt修改为自己的模型即可,如best.pt。如果是下面代码中默认的模型,并且你没有下载到本地,系统会自动下载,我这里在文章末尾提供了下载链接。

          将以下代码创建、拷贝到yolov8根目录下。

          具体代码my_export.py:

          from ultralytics import YOLO# Load a modelmodel = YOLO('yolov8n.pt')  # load an official model# Export the modelmodel.export(format='onnx', imgsz=[480, 640], opset=12) # 导出一定不要修改这里参数

          执行导出命令:

          python my_export.py

          输出如下图信息,表明onnx格式的模型被成功导出,保存在my_export.py同一级目录。

          三、基于opencv CPP推理onnx

          使用opencv4.8.0,linux和windows都可以,下面以windows为例子。注:运行代码需要onnx模型 + 一张图,文末给了下载链接,classes.txt不需要。

          以下是主函数文件main.cpp:

          #include #include #include #include "inference.h"using namespace std;using namespace cv;int main(int argc, char **argv){    bool runOnGPU = false;    // 1. 设置你的onnx模型    // Note that in this example the classes are hard-coded and 'classes.txt' is a place holder.    Inference inf("D:/CodePython/ultralytics/yolov8s.onnx", cv::Size(640, 480), "classes.txt", runOnGPU); // classes.txt 可以缺失    // 2. 设置你的输入图片    std::vector imageNames;    imageNames.push_back("bus.jpg");    //imageNames.push_back("zidane.jpg");    for (int i = 0; i  modelScoreThreshold)                {                    confidences.push_back(confidence);                    class_ids.push_back(class_id.x);                    float x = data[0];                    float y = data[1];                    float w = data[2];                    float h = data[3];                    int left = int((x - 0.5 * w) * x_factor);                    int top = int((y - 0.5 * h) * y_factor);                    int width = int(w * x_factor);                    int height = int(h * y_factor);                    boxes.push_back(cv::Rect(left, top, width, height));                }            }        }        data += dimensions;    }    std::vector nms_result;    cv::dnn::NMSBoxes(boxes, confidences, modelScoreThreshold, modelNMSThreshold, nms_result);    std::vector detections{};    for (unsigned long i = 0; i 

上一篇:怎么管理龙管家服务器账号?

下一篇:windows nginx开机自启