c++windows+yolov5-6.2+openvino模型部署超详细

这篇具有很好参考价值的文章主要介绍了c++windows+yolov5-6.2+openvino模型部署超详细。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

自我记录:代码是根据自己的项目需求,进行了修改,主要是需要检测的图片非常大,目标小,所以对图片进行了分割再检测。下载完配置好环境之后可以直接跑。

我的环境是:windows+vs2019+openvino2022.2+opencv4.5.5+cmake3.14.0

步骤:

1、下载openvino,我用的版本是2022.2

官网网址:https://docs.openvino.ai/latest/index.html

c++windows+yolov5-6.2+openvino模型部署超详细,openvino+yolov5模型部署,openvino,人工智能,c++,opencv

 c++windows+yolov5-6.2+openvino模型部署超详细,openvino+yolov5模型部署,openvino,人工智能,c++,opencv

c++windows+yolov5-6.2+openvino模型部署超详细,openvino+yolov5模型部署,openvino,人工智能,c++,opencv

 就是这个链接:https://www.intel.com/content/www/us/en/developer/tools/openvino-toolkit/download.html

 c++windows+yolov5-6.2+openvino模型部署超详细,openvino+yolov5模型部署,openvino,人工智能,c++,opencv

 解压之前记得给电脑设置一下,启动长路径,很简单,教程在这儿:https://blog.csdn.net/weixin_46356818/article/details/121029550

 解压后,配置电脑系统变量,下面是我的:

c++windows+yolov5-6.2+openvino模型部署超详细,openvino+yolov5模型部署,openvino,人工智能,c++,opencv

 下面是代码:

h文件

#pragma once
#include <opencv2/dnn.hpp>
#include <openvino/openvino.hpp>
#include <opencv2/opencv.hpp>

using namespace std;

class YOLO_OPENVINO
{
public:
	YOLO_OPENVINO();
	~YOLO_OPENVINO();

public:
	struct Detection 
	{
		int class_id;
		float confidence;
		cv::Rect box;
	};

	struct Resize 
	{
		cv::Mat resized_image;
		int dw;
		int dh;
	};

	Resize resize_and_pad(cv::Mat& img, cv::Size new_shape);
	void yolov5_compiled(std::string xml_path, ov::CompiledModel &compiled_model);
	void yolov5_detector(ov::CompiledModel compiled_model, cv::Mat input_detect_img, cv::Mat output_detect_img, vector<cv::Rect>& nms_box);
	
private:
	
	const float SCORE_THRESHOLD = 0.4;
	const float NMS_THRESHOLD = 0.4;
	const float CONFIDENCE_THRESHOLD = 0.4;

	vector<cv::Mat>images;//图像容器 
	vector<cv::Rect> boxes;
	vector<int> class_ids;
	vector<float> confidences;
	vector<cv::Rect>output_box;
	Resize resize;

};


cpp文件

#include"yolo_openvino.h"

YOLO_OPENVINO::YOLO_OPENVINO()
{
}

YOLO_OPENVINO::~YOLO_OPENVINO()
{
}


YOLO_OPENVINO::Resize YOLO_OPENVINO::resize_and_pad(cv::Mat& img, cv::Size new_shape)
{
    float width = img.cols;
    float height = img.rows;
    float r = float(new_shape.width / max(width, height));
    int new_unpadW = int(round(width * r));
    int new_unpadH = int(round(height * r));
    
    cv::resize(img, resize.resized_image, cv::Size(new_unpadW, new_unpadH), 0, 0, cv::INTER_AREA);

    resize.dw = new_shape.width - new_unpadW;//w方向padding值 
    resize.dh = new_shape.height - new_unpadH;//h方向padding值 
    cv::Scalar color = cv::Scalar(100, 100, 100);
    cv::copyMakeBorder(resize.resized_image, resize.resized_image, 0, resize.dh, 0, resize.dw, cv::BORDER_CONSTANT, color);

    return resize;
}

void YOLO_OPENVINO::yolov5_compiled(std::string xml_path, ov::CompiledModel& compiled_model)
{
    // Step 1. Initialize OpenVINO Runtime core
    ov::Core core;
    // Step 2. Read a model
    //std::shared_ptr<ov::Model> model = core.read_model("best.xml");
    std::shared_ptr<ov::Model> model = core.read_model(xml_path);
    // Step 4. Inizialize Preprocessing for the model 初始化模型的预处理
    ov::preprocess::PrePostProcessor ppp = ov::preprocess::PrePostProcessor(model);
    // Specify input image format 指定输入图像格式
    ppp.input().tensor().set_element_type(ov::element::u8).set_layout("NHWC").set_color_format(ov::preprocess::ColorFormat::BGR);
    // Specify preprocess pipeline to input image without resizing 指定输入图像的预处理管道而不调整大小
    ppp.input().preprocess().convert_element_type(ov::element::f32).convert_color(ov::preprocess::ColorFormat::RGB).scale({ 255., 255., 255. });
    //  Specify model's input layout 指定模型的输入布局
    ppp.input().model().set_layout("NCHW");
    // Specify output results format 指定输出结果格式
    ppp.output().tensor().set_element_type(ov::element::f32);
    // Embed above steps in the graph 在图形中嵌入以上步骤
    model = ppp.build();
    compiled_model = core.compile_model(model, "CPU");
}

void YOLO_OPENVINO::yolov5_detector(ov::CompiledModel compiled_model, cv::Mat input_detect_img, cv::Mat output_detect_img, vector<cv::Rect>& nms_box)
{
    // Step 3. Read input image
    cv::Mat img = input_detect_img.clone();
    int img_height = img.rows;
    int img_width = img.cols;
    if (img_height < 5000 && img_width < 5000)
    {
        images.push_back(img);
    }
    else
    {
        images.push_back(img(cv::Range(0, 0.6 * img_height), cv::Range(0, 0.6 * img_width)));
        images.push_back(img(cv::Range(0, 0.6 * img_height), cv::Range(0.4 * img_width, img_width)));
        images.push_back(img(cv::Range(0.4 * img_height, img_height), cv::Range(0, 0.6 * img_width)));
        images.push_back(img(cv::Range(0.4 * img_height, img_height), cv::Range(0.4 * img_width, img_width)));
    }

    for (int m = 0; m < images.size(); m++)
    {
        // resize image
        Resize res = resize_and_pad(images[m], cv::Size(1280, 1280));
        // Step 5. Create tensor from image
        float* input_data = (float*)res.resized_image.data;//缩放后图像数据
        ov::Tensor input_tensor = ov::Tensor(compiled_model.input().get_element_type(), compiled_model.input().get_shape(), input_data);


        // Step 6. Create an infer request for model inference 
        ov::InferRequest infer_request = compiled_model.create_infer_request();
        infer_request.set_input_tensor(input_tensor);
        infer_request.infer();


        //Step 7. Retrieve inference results 
        const ov::Tensor& output_tensor = infer_request.get_output_tensor();
        ov::Shape output_shape = output_tensor.get_shape();
        float* detections = output_tensor.data<float>();

        for (int i = 0; i < output_shape[1]; i++)//遍历所有框
        {
            float* detection = &detections[i * output_shape[2]];//bbox(x y w h obj cls)

            float confidence = detection[4];//当前bbox的obj
            if (confidence >= CONFIDENCE_THRESHOLD) //判断是否为前景
            {
                float* classes_scores = &detection[5];
                cv::Mat scores(1, output_shape[2] - 5, CV_32FC1, classes_scores);
                cv::Point class_id;
                double max_class_score;
                cv::minMaxLoc(scores, 0, &max_class_score, 0, &class_id);//返回最大得分和最大类别

                if (max_class_score > SCORE_THRESHOLD)//满足得分
                {
                    confidences.push_back(confidence);

                    class_ids.push_back(class_id.x);

                    float x = detection[0];//框中心x 
                    float y = detection[1];//框中心y 
                    float w = detection[2];//49
                    float h = detection[3];//50

                    float rx = (float)images[m].cols / (float)(res.resized_image.cols - res.dw);//x方向映射比例
                    float ry = (float)images[m].rows / (float)(res.resized_image.rows - res.dh);//y方向映射比例

                    x = rx * x;
                    y = ry * y;
                    w = rx * w;
                    h = ry * h;

                    if (m == 0)
                    {
                        x = x;
                        y = y;
                    }
                    else if (m == 1)
                    {
                        x = x + 0.4 * img_width;
                        y = y;

                    }
                    else if (m == 2)
                    {
                        x = x;
                        y = y + 0.4 * img_height;
                    }
                    else if (m == 3)
                    {
                        x = x + 0.4 * img_width;
                        y = y + 0.4 * img_height;
                    }

                    float xmin = x - (w / 2);//bbox左上角x
                    float ymin = y - (h / 2);//bbox左上角y
                    boxes.push_back(cv::Rect(xmin, ymin, w, h));
                }
            }
        }
    }

    std::vector<int> nms_result;
    cv::dnn::NMSBoxes(boxes, confidences, SCORE_THRESHOLD, NMS_THRESHOLD, nms_result);
    std::vector<Detection> output;
    
    for (int i = 0; i < nms_result.size(); i++)
    {
        Detection result;
        int idx = nms_result[i];
        result.class_id = class_ids[idx];
        result.confidence = confidences[idx];
        result.box = boxes[idx];
        nms_box.push_back(result.box);//传给Qt NMS后的box
        output.push_back(result);
    }

    // Step 9. Print results and save Figure with detections
    for (int i = 0; i < output.size(); i++)
    {
        auto detection = output[i];
        auto box = detection.box;
        auto classId = detection.class_id;
        auto confidence = detection.confidence;

        /*cout << "Bbox" << i + 1 << ": Class: " << classId << " "
            << "Confidence: " << confidence << " Scaled coords: [ "
            << "cx: " << (float)(box.x + (box.width / 2)) / img.cols << ", "
            << "cy: " << (float)(box.y + (box.height / 2)) / img.rows << ", "
            << "w: " << (float)box.width / img.cols << ", "
            << "h: " << (float)box.height / img.rows << " ]" << endl;*/
        float xmax = box.x + box.width;
        float ymax = box.y + box.height;
        
        cv::rectangle(img, cv::Point(box.x, box.y), cv::Point(xmax, ymax), cv::Scalar(0, 255, 0), 3);
        cv::rectangle(img, cv::Point(box.x, box.y - 20), cv::Point(xmax, box.y), cv::Scalar(0, 255, 0), cv::FILLED);
        cv::putText(img, std::to_string(classId), cv::Point(box.x, box.y - 5), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
        img.copyTo(output_detect_img);
    }

    cv::imwrite("./fz.jpg", output_detect_img);

}


main.cpp

#include"yolo_openvino.h"

using namespace std;

YOLO_OPENVINO yolo_openvino;
std::string path = "./best.xml";
ov::CompiledModel model;
cv::Mat input_img, output_img;
vector<cv::Rect>output_box;
int main()
{
    input_img = cv::imread("140_0_0.jpg");
    yolo_openvino.yolov5_compiled(path, model);
    yolo_openvino.yolov5_detector(model, input_img, output_img, output_box);
/*    for (int i = 0; i < output_box.size(); i++)
    {
        cv::rectangle(input_img, cv::Point(output_box[i].x, output_box[i].y), cv::Point(output_box[i].x + output_box[i].width, output_box[i].y + output_box[i].height), cv::Scalar(0, 255, 0), 3);
    }
    cv::imshow("a", input_img);
    cv::waitKey(0)*/;
	return 0;
}

接下来配置项目的包含目录、库目录、附加依赖项
c++windows+yolov5-6.2+openvino模型部署超详细,openvino+yolov5模型部署,openvino,人工智能,c++,opencv

c++windows+yolov5-6.2+openvino模型部署超详细,openvino+yolov5模型部署,openvino,人工智能,c++,opencv

c++windows+yolov5-6.2+openvino模型部署超详细,openvino+yolov5模型部署,openvino,人工智能,c++,opencv

 2、下载cmake3.14.0

这个下完之后解压,然后配置个环境变量就行,不下cmake应该也是可以的。

 c++windows+yolov5-6.2+openvino模型部署超详细,openvino+yolov5模型部署,openvino,人工智能,c++,opencv

 3、跑代码:

放一个onnx转xml、bin文件的方法,现在可以直接从Yolov5中用export_openvino直接导出,其导出函数定义为:

def export_openvino(model, im, file, prefix=colorstr('OpenVINO:')):
    # YOLOv5 OpenVINO export
    try:
        check_requirements(('openvino-dev',))  # requires openvino-dev: https://pypi.org/project/openvino-dev/
        import openvino.inference_engine as ie
 
        LOGGER.info(f'\n{prefix} starting export with openvino {ie.__version__}...')
        f = str(file).replace('.pt', '_openvino_model' + os.sep)
 
        cmd = f"mo --input_model {file.with_suffix('.onnx')} --output_dir {f}"
        subprocess.check_output(cmd, shell=True)
 
        LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
    except Exception as e:
        LOGGER.info(f'\n{prefix} export failure: {e}')

所以只需要在yolov5里面运行下面命令:

“mo --input_model {file.with_suffix('.onnx')} --output_dir {f}”

///然后代码里的模型路径改成你自己的,就可以跑了。文章来源地址https://www.toymoban.com/news/detail-670272.html

到了这里,关于c++windows+yolov5-6.2+openvino模型部署超详细的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • openvino部署yolov8 检测、分割、分类及姿态模型实例详解

    本文重点参考:https://github.com/openvino-book/yolov8_openvino_cpp/tree/main 文中代码为简便版本,如果要使用请自行修改并封装 openvnio部署模型比较方便和简单,而且不易出错,就是速度慢了点! 下边分别给出 部署源码

    2024年02月16日
    浏览(36)
  • [C#]winform部署yolov8图像分类的openvino格式的模型

    【官方框架地址】 https://github.com/ultralytics/ultralytics 【openvino介绍】 OpenVINO是一个针对Intel硬件优化的开源工具包,用于优化和部署深度学习模型。以下是OpenVINO部署模型的主要优点: 高性能:OpenVINO提供了一系列性能优化工具,如模型量化和剪枝等,可以在Intel硬件平台上实现

    2024年01月21日
    浏览(37)
  • 详细介绍 Yolov5 转 ONNX模型 + 使用ONNX Runtime 的 Python 部署(包含官方文档的介绍)

    对ONNX的介绍强烈建议看,本文做了很多参考:模型部署入门教程(一):模型部署简介 模型部署入门教程(三):PyTorch 转 ONNX 详解 以及Pytorch的官方介绍:(OPTIONAL) EXPORTING A MODEL FROM PYTORCH TO ONNX AND RUNNING IT USING ONNX RUNTIME C++的部署:详细介绍 Yolov5 转 ONNX模型 + 使用 ONNX Runti

    2024年02月01日
    浏览(39)
  • 人工智能任务4-读懂YOLOv5模型的几个灵魂拷问问题,深度理解 YOLOv5模型架构

    大家好,我是微学AI,今天给大家介绍一下人工智能任务4-读懂YOLOv5模型的几个灵魂拷问问题,深度理解 YOLOv5模型架构。YOLOv5是一种高效且精确的目标检测模型,由ultralytics团队开发。它采用了轻量级的网络结构,能够在保持高性能的同时降低计算复杂度。模型由三个主要部分

    2024年01月16日
    浏览(34)
  • 人工智能图像识别分析之——Yolov5模型训练

    上一课讲述了Yolov5模型环境搭建的过程 这一课讲Yolov5模型训练的过程 进行模型训练前,首先要先进行样本标注,标注后产生标注文件,将图片源文件和标注文件进行文件划分,本文以2000张负样本进行训练。 1.新建三级目录datasets/images/train、datasets/images/val 2.新建三级目录da

    2024年02月01日
    浏览(60)
  • Windows 上使用LabVIEW AI 工具包 for OpenVINO™部署YOLOv9实现实时目标检测

    作者: 英特尔边缘计算创新大使 王立奇 YOLOv9 引入了可编程梯度信息 (PGI) 和广义高效层聚合网络 (GELAN) 等开创性技术,不仅增强了模型的学习能力,还确保了在整个检测过程中保留关键信息,从而实现了卓越的准确性和性能。该模型在效率、准确性和适应性方面都有显著提

    2024年04月12日
    浏览(46)
  • YOLOV5 自动瞄准(OpenVino)(附源码)

    参考文章:基于OpenVINOTM2022.2和蝰蛇峡谷优化并部署YOLOv5模型_openvino 前篇文章:基于YOLOV5的自动瞄准(附代码)_yolov5 自瞄_RedWhiteLuo的博客 之前已经通过Pytroch 调用 NVIDIA GPU 进行推理,但是在实际使用中,独显肯定是最好空闲出来的, 因此我们可以利用闲置的硬件——核显,

    2024年02月08日
    浏览(46)
  • yolov5-6.0项目部署+自用Pytorch模型转换rknn模型并在RK3568 linux(Debian)平台上使用qt部署使用NPU推理加速摄像头目标识别详细新手教程

    1 我们打开yolov的官网,Tags选择6.0版本 2. 下载该压缩包并解压到工程目录下 3. 我们这里使用pycharm,专门针对python的IDE,用起来非常方便,下载方式就是官网直接下载,用的是社区版 4. 我们需要安装环境,这里我推荐安装Anaconda在电脑上,这是一个非常方便的包管理工具,可

    2024年02月05日
    浏览(52)
  • 无人机上仅使用CPU实时运行Yolov5(OpenVINO实现)(上篇)

    I ntel CPU 在运行 视觉导航 等算法时实时性要优于Nvidia等平台,如Jetson Tx2,NX。而 Nvidia平台 在运行 深度学习算法 方面具有很大优势,两种平台各有利弊。但是,Intel OpenVINO的推出允许NUC平台实时运行深度学习模型,如目前最流行的目标检测程序Yolov5,这样就太好了, 仅使用

    2024年02月10日
    浏览(27)
  • 树莓派部署YOLOv5模型

    本文章是关于树莓派部署YOLOv5s模型,实际测试效果的FPS仅有0.15,不够满足实际检测需要,各位大佬可以参考参考。 1、在树莓派中安装opencv(默认安装好python3) 2、导出onnx模型 从YOLOv5官网下载源代码和YOLOv5s.pt文件 YOLOv5官网 YOLOv5s.pt下载 按照作者提示安装环境,使用它自带

    2024年02月11日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包