OpenVINO 2022.3实战一:Window 10 环境下用 OpenVINO 2022.3部署yolov5 7.0

news2024/10/7 6:45:36

Window 10 环境下用 OpenVINO 2022.3部署yolov5_7.0

1 下载并解压 OpenVINO Runtime

OpenVINO™ Runtime 2022.3 以压缩包 (OpenVINO Archives) 的形式提供。
下载地址: storage.openvinotoolkit.org
下载后解压到 C:\Intel\openvino_2022.3.0
在这里插入图片描述
配置环境:

C:\Intel\openvino_2022.3.0\setupvars.bat

其中 OpenVINO C++ 推理程序所必需的文件在runtime目录下:

  • 头文件:include 文件夹
  • lib 文件:lib 文件夹
  • 可执行文件 (*.exe) 所需的动态链接库文件:bin 文件夹
  • OpenVINO runtime 第三方依赖库文件:3rdparty 文件夹

在这里插入图片描述

2 下载并编译 OpenCV

下载地址:_opencv
在这里插入图片描述

2-1 下载预编译OpenCV

直接下载 windows 编译版本,下载后解压到 E:\opencv455目录下即可

2-2 编译与OpenVINO对应的OpenCV

下载 Sources源码到本地, 解压到E:\opencv-4.5.5

mkdir "mybuild" && cd "mybuild"

cmake 编译项设置
test选项 不选
在这里插入图片描述
python 选项 不选
在这里插入图片描述
OPENCV_GENERATE_SETUPVARS 不选
在这里插入图片描述
WITH_OPENMP 选中
在这里插入图片描述
WITH_IPP 选中
在这里插入图片描述
BUILD_opencv_world 选中
在这里插入图片描述
OPENCV_EXTRA_MODULES_PATH 设置 E:/opencv_contrib-4.5.5/modules
在这里插入图片描述
cmake编译可以参考
Windows10+Cmake+VS2019编译opencv(超级详细)_vs编译opencv_乐安世家的博客-CSDN博客
win10+vs2017+opencv4.5.0+opencv_contrib-4.5.0+cuda源码编译详细教程_vs2017 源码编译opencv_Bubble_water的博客-CSDN博客
opencv4.2.0 源码编译,win7+VS2015,DNN模块支持cuda加速_蜡笔小心点的博客-CSDN博客

3 在 Visual Studio 中配置项目属性

Release:
属性 --> VC++ 目录 --> 包含目录

C:\Intel\openvino_2022.3.0\runtime\include
E:\opencv455\build\include

属性 --> VC++ 目录 --> 库目录

C:\Intel\openvino_2022.3.0\runtime\lib\intel64\Release
E:\opencv455\build\x64\vc15\lib

属性 --> 链接器 --> 输入 --> 附加依赖项

openvino.lib
opencv_world455.lib

动态链接库 配置
将 C:\Intel\openvino_2022.3.0\runtime\bin\intel64\Release目录下的

openvino.dll
openvino_intel_cpu_plugin.dll
openvino_ir_frontend.dll
plugins.xml

将 C:\Intel\openvino_2022.3.0\runtime\3rdparty\tbb\bin目录下的

tbb.dll

将 E:\opencv455\mybuild\x64\vc15\bin 目录下的

opencv_world455.dll

移动到 可执行文件目录 或者将三个路径加入系统目录。
Debug:
属性 --> VC++ 目录 --> 包含目录

C:\Intel\openvino_2022.3.0\runtime\include
E:\opencv455\build\include

属性 --> VC++ 目录 --> 库目录

C:\Intel\openvino_2022.3.0\runtime\lib\intel64\Debug
E:\opencv455\build\x64\vc15\lib

属性 --> 链接器 --> 输入 --> 附加依赖项

openvinod.lib
opencv_world455d.lib

动态链接库 配置
将 C:\Intel\openvino_2022.3.0\runtime\bin\intel64\Debug目录下的

openvinod.dll
openvino_intel_cpu_plugind.dll
openvino_ir_frontendd.dll
plugins.xml

将 C:\Intel\openvino_2022.3.0\runtime\3rdparty\tbb\bin目录下的

tbb.dll

将 E:\opencv455\mybuild\x64\vc15\bin 目录下的

opencv_world455d.dll

移动到 可执行文件目录 或者将三个路径加入系统目录。

4 导出onnx模型

下载yolov5代码 ultralytics/yolov5

python export.py --weights yolov5s.pt --include torchscript onnx openvino

导出模型为 yolov5s_openvino_model
在这里插入图片描述

5 代码

yolov5_openvino.cpp

// yolov5_openvino.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma warning(disable:4996)

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


using namespace std;


const float SCORE_THRESHOLD = 0.2;
const float NMS_THRESHOLD = 0.4;
const float CONFIDENCE_THRESHOLD = 0.4;


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


struct ResizeImage
{
    cv::Mat img;
    int dw;
    int dh;
};


ResizeImage 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));
    ResizeImage resizedImg;
    cv::resize(img, resizedImg.img, cv::Size(new_unpadW, new_unpadH), 0, 0, cv::INTER_AREA);

    resizedImg.dw = new_shape.width - new_unpadW;
    resizedImg.dh = new_shape.height - new_unpadH;
    cv::Scalar color = cv::Scalar(100, 100, 100);
    cv::copyMakeBorder(resizedImg.img, resizedImg.img, 0, resizedImg.dh, 0, resizedImg.dw, cv::BORDER_CONSTANT, color);

    return resizedImg;
}


int main() {

    // Step 1. Initialize OpenVINO Runtime core
    ov::Core core;
    // Step 2. Read a model
    std::shared_ptr<ov::Model> model = core.read_model("E:\\python_code\\yolov5\\weights\\openvino\\yolov5s_openvino_model\\yolov5s.xml");


    // Step 3. Read input image
    cv::Mat img = cv::imread("E:\\cpp_code\\images\\zidane.jpg");
    // resize image
    ResizeImage res = resize_and_pad(img, cv::Size(640, 640));


    // 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();
    ov::CompiledModel compiled_model = core.compile_model(model, "CPU");


    // Step 5. Create tensor from image
    float *input_data = (float *)res.img.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>();


    // Step 8. Postprocessing including NMS  
    std::vector<cv::Rect> boxes;
    vector<int> class_ids;
    vector<float> confidences;

    for (int i = 0; i < output_shape[1]; i++) {
        float *detection = &detections[i * output_shape[2]];

        float confidence = detection[4];
        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];
                float y = detection[1];
                float w = detection[2];
                float h = detection[3];

                float xmin = x - (w / 2);
                float ymin = y - (h / 2);

                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];
        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;
        float rx = (float)img.cols / (float)(res.img.cols - res.dw);
        float ry = (float)img.rows / (float)(res.img.rows - res.dh);
        box.x = rx * box.x;
        box.y = ry * box.y;
        box.width = rx * box.width;
        box.height = ry * box.height;
        cout << "Bbox" << i + 1 << ": Class: " << classId << " "
            << "Confidence: " << confidence << " Scaled coords: [ "
            << "x: " << (float)box.x << ", "
            << "y: " << (float)box.y << ", "
            << "w: " << (float)box.width << ", "
            << "h: " << (float)box.height << " ]" << 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));
    }
    cv::imwrite("detection.png", img);

    return 0;
}

常见问题

1 error C4996: ‘ov::Node::evaluate_upper’: This method is deprecated and will be removed soon. Please use evaluate_upper with ov::Tensor instead

解决方法: 代码中加入 #pragma warning(disable:4996) 即可

2 DNN: CUDA backend requires CUDA Toolkit. Please resolve dependency or disable OPENCV_DNN_CUDA=OFF"

解决方法:https://github.com/opencv/opencv/issues/18528

参考资料:

1 Installing Intel® Distribution of OpenVINO™ Toolkit — OpenVINO™ documentation

2 How to use OpenCV with OpenVINO - OpenCV

3 BuildOpenCV4OpenVINO · opencv/opencv Wiki · GitHub

4 TFLite, ONNX, CoreML, TensorRT Export - Ultralytics YOLOv8 Docs

5 基于OpenVINO™ 2022.1实现YOLOv5推理程序 | 开发者实战

6 使用OpenVINO™ 预处理API进一步提升YOLOv5推理性能 | 开发者实战

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/499519.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

5月5日 8H25min|5月6日 3H10min|时间轴复盘

7:30-8:00 起床洗漱吃饭 8:00-8:30 背书 【30min】 8:30-9:40 对话单词 【1h10min】 9:45-11:30 听力精听 【2h-15min】 11:30-12:10 吃午饭吃水果 12:10-12:50 继续吃饭之前没完成的 【40min】 13:00-14:30 健身 14:35-14:43 语法 【1…

asdfghasdfghjkl

PDL1检测&#xff1a; 肿瘤细胞高表达PD-L1分子&#xff0c;与肿瘤部位浸润T淋巴细胞表面的PD-1分子结合后&#xff0c;抑制T细胞活性&#xff0c;实现肿瘤的免疫逃避。而目前PD-1/PD-L1抑制剂均是检测PD-L1的表达。 目前在NSCLC治疗中&#xff0c;对于每个PD-1/PD-L1抑制剂&a…

【Hive大数据】Hive分区表与分桶表使用详解

目录 一、分区概念产生背景 二、分区表特点 三、分区表类型 3.1 单分区 3.2 多分区 四、动态分区与静态分区 4.1 静态分区【静态加载】 4.1.1 操作演示 4.2 多重分区 4.2.1 操作演示 4.3 分区数据动态加载 4.3.1 分区表数据加载 -- 动态分区 4.3.2 操作演示 五、…

mysql事务及搜索引擎

mysql事务后半部分 加快查询速度索引会自动排序&#xff0c;&#xff08;升序&#xff09; select * from t1&#xff1b;全盘扫描 where可以索引查找show create table 索引是一个排序的列表&#xff0c;包含字段值和相应行数据的物理地址 事务是一种机制&#xff0c;一个…

Misc小总结

Misc分类 个人认为Misc中的题目可分为七大类&#xff0c;图片隐写&#xff0c;音视频隐写&#xff0c;其它隐写(PPT、word文档等隐写)&#xff0c;压缩包破解&#xff0c;流量分析&#xff0c;取证&#xff0c;编码或密码。这里面涉及的知识点当然是很多的&#xff0c;有很多你…

大学毕业设计使用python制作

前言&#xff1a;相信看到这篇文章的小伙伴都或多或少有一些编程基础&#xff0c;懂得一些linux的基本命令了吧&#xff0c;本篇文章将带领大家服务器如何部署一个使用django框架开发的一个网站进行云服务器端的部署。 文章使用到的的工具 Python&#xff1a;一种编程语言&…

Python标准数据类型-字符串常用方法(上)【文末送书】

✅作者简介&#xff1a;CSDN内容合伙人、阿里云专家博主、51CTO专家博主、新星计划第三季python赛道Top1 &#x1f4c3;个人主页&#xff1a;hacker707的csdn博客 &#x1f525;系列专栏&#xff1a;零基础入门篇 &#x1f4ac;个人格言&#xff1a;不断的翻越一座又一座的高山…

经验总结:(Redis NoSQL数据库快速入门)

一、Nosql概述 为什么使用Nosql 1、单机Mysql时代 90年代,一个网站的访问量一般不会太大&#xff0c;单个数据库完全够用。随着用户增多&#xff0c;网站出现以下问题 数据量增加到一定程度&#xff0c;单机数据库就放不下了数据的索引&#xff08;B Tree&#xff09;,一个机…

【Linux】进程的终止,等待(不包含进程的程序替换)

信号的部分会在后面仔细讲&#xff0c;本文不涉及 目录 1.进程终止以及退出码的理解 2.进程退出 3.进程等待 1.进程终止以及退出码的理解 1.情况分类 &#xff08;1&#xff09;正常执行完 a.结果正确 b.结果不正确 反思为什么&#xff1f; &#xff08;2&#xff…

CesiumForUnreal去掉左下角的Ion Logo

文章目录 1.实现目标2.实现过程3.参考资料1.实现目标 记录一下使用CesiumForUnreal插件过程中如何清除左下角的Cesium Ion Logo,清除前后的对比截图如下所示。 原始样式去除后2.实现过程 记录一下实现的过程(含踩坑记录,可能有一点啰嗦)。 (1)首先看一下是哪个蓝图添加的…

【STM32CubeMX】F103窗口看门狗

前言 本文记录了我学习STM32CubeMX的过程&#xff0c;方便以后回忆。我们使用的开发板是基于STM32F103C6T6的。本章记录了窗口看门狗的使用配置。要学习的话&#xff0c;注意流程一说&#xff0c;省略的内容。 基础 窗口看门狗(WWDG)属于APB1上外设。窗口看门狗(WWDG)的时钟源…

【小结】git合并分支总结

首先理清以下几个关系&#xff1a; 1、git有本地库和远程库。 ①本地仓库&#xff1a;也就是电脑上存储的代码&#xff0c;本地代码&#xff0c;一般在某个盘中。 ②远程仓库&#xff1a;是云上的库&#xff0c;比如gitee,github等等。 2、分支&#xff1a;分为本地分支和远…

手机摄影(三)

第七章 构图&#xff0c;用光与色彩 构图的原则&#xff1a; 画面简洁 突出主体 陪体和主体&#xff1a;如果没有枯叶做前景&#xff0c;画面的空间感和深秋氛围会大打折扣。 看到一张你认为很美的照片时&#xff0c;要问自己几个问题&#xff1a; • 这张照片的主体是什么…

jvm之启动参数

写在前面 本文一起看下jvm启动参数相关内容&#xff0c;通过本文希望我自己也希望大家能够真正的应用到实际的工作中。 1&#xff1a;基本内容介绍 一般我们启动java程序有两种方式&#xff0c;一种是直接运行一个有main函数的class&#xff0c;第二种是运行一个在MANIFEST文…

安装mysql

1、环境&#xff08;虚拟机新安装的Redhat&#xff09;&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1jjdimuq-TrS8RASqGiU5Xg 提取码&#xff1a;2hef 2、检查系统是否已经安装了 MySQL rpm -aq | grep mysql 如果有旧版本就需要像下面卸载mariadb一样卸载掉。M…

希尔密码,云影密码 及例题

希尔密码 云影密码 题一 [HDCTF2023]–爬过小山去看云 题目描述&#xff1a; 密文&#xff1a;ymyvzjtxswwktetpyvpfmvcdgywktetpyvpfuedfnzdjsiujvpwktetpyvnzdjpfkjssvacdgywktetpyvnzdjqtincduedfpfkjssne 在山的那头&#xff0c;有3个人&#xff0c;4只鸟&#xff0c;19只…

Keepalived概念与安装部署过程

前言 目前互联网上主流高可用方案软件有keepalived、heartbeat&#xff0c;其中heartbeat是比较早期用来实现高可用软件的&#xff0c;而keepalived是目前轻量级&#xff0c;并且管理方便、易使用的高可用解决方案。 1.1 Keeplived高可用的功能 Keepalived是一个类似于工作在…

半监督学习经典工作:边缘生成对抗网络(MarginGAN)

来源&#xff1a;投稿 作者&#xff1a;小灰灰 编辑&#xff1a;学姐 论文标题&#xff1a;MarginGAN: Adversarial Training in Semi-Supervised Learning 论文链接: https://papers.nips.cc/paper/2019/file/517f24c02e620d5a4dac1db388664a63-Paper.pdf 代码链接&#xf…

ShardingCore安装笔记

由于本人采用Visual Studio的nuget管理器安装ShardingCore经常出现网络错误的问题&#xff0c;所以采用离线包的方式安装插件。 nueget包下载地址&#xff1a;NuGet Gallery | ShardingCore 7.7.1.8 ShardingCore使用版本7.7.1.7 1、下载各种依赖文件&#xff0c;并存放到系…

openTCS分析

一、openTCS概览 1. openTCS操作流程 打开ModelEditor设置点、路线、装货卸货等信息并保存模型打开Kernel和KernelControlCenter在ModelEditor上传模型打开KernelControlCenter设置车辆信息打开OperationsDesk打开OperationsDesk设置小车模式为可利用&#xff0c;并创建订单 …