使用openvino编写YOLOv10的推理代码(c++版)

1.yolov10推理代码

#include <iostream>
#include <string>
#include <vector>
#include <openvino/openvino.hpp> 
#include <opencv2/opencv.hpp> 
 std::vector<cv::Scalar> colors = { cv::Scalar(0, 0, 255) , cv::Scalar(0, 255, 0) , cv::Scalar(255, 0, 0) ,
                                   cv::Scalar(255, 100, 50) , cv::Scalar(50, 100, 255) , cv::Scalar(255, 50, 100) };
const std::vector<std::string> class_names = {
        "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
        "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
        "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
        "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
        "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
        "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
        "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
        "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear",
        "hair drier", "toothbrush" };
using namespace cv;
using namespace dnn;

Mat letterbox(const cv::Mat& source)
{
    int col = source.cols;
    int row = source.rows;
    int _max = MAX(col, row);
    Mat result = Mat::zeros(_max, _max, CV_8UC3);
    source.copyTo(result(Rect(0, 0, col, row)));
    return result;
}
int main(int argc, char* argv[])
{
    int64 start = cv:: getTickCount();
    ov::Core core;
    auto compiled_model = core.compile_model("/home/master/yolov10/yolov10x.xml");
    ov::InferRequest infer_request = compiled_model.create_infer_request();
    Mat img = cv::imread("/home/master/yolov10/12.jpg");
    Mat letterbox_img = letterbox(img);
    float scale = letterbox_img.size[0] / 640.0;
    Mat blob = blobFromImage(letterbox_img, 1.0 / 255.0, Size(640, 640), Scalar(), true);
    auto input_port = compiled_model.input();
    ov::Tensor input_tensor(input_port.get_element_type(), input_port.get_shape(), blob.ptr(0));
    infer_request.set_input_tensor(input_tensor);
    infer_request.infer();
    auto output = infer_request.get_output_tensor(0);
    auto output_shape = output.get_shape();
    std::cout << "The shape of output tensor:" << output_shape << std::endl;
    int rows = output_shape[2];        
    int dimensions = output_shape[1];  
    float* data = output.data<float>();
    Mat output_buffer(output_shape[1], output_shape[2], CV_32F, data);
    float score_threshold = 0.15;
    std::vector<int> class_ids;
    std::vector<float> class_scores;
    std::vector<Rect> boxes;
    for (int i = 0; i < output_buffer.rows; i++) {
        float confidence = output_buffer.at<float>(i, 4);
        if (confidence > score_threshold) {
            int xmin = output_buffer.at<float>(i, 0) * scale;
            int ymin = output_buffer.at<float>(i, 1) * scale;
            int xmax = output_buffer.at<float>(i, 2) * scale;
            int ymax = output_buffer.at<float>(i, 3) * scale;
            int width = xmax - xmin;
            int height = ymax - ymin;
            int ID = (int)output_buffer.at<float>(i, 5);
            boxes.push_back(Rect(xmin, ymin, width, height));
            class_scores.push_back(confidence);
            class_ids.push_back(ID);
        }
    }
    for (size_t i = 0; i < boxes.size(); i++)
    {
        rectangle(img, boxes[i], colors[class_ids[i] % 6], 2, 8);
        std::string label = class_names[class_ids[i]] + ":" + std::to_string(class_scores[i]).substr(0, 4);
        Size textSize = cv::getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, 0);
        Rect textBox(boxes[i].tl().x, boxes[i].tl().y - 15, textSize.width, textSize.height+5);
        cv::rectangle(img, textBox, colors[class_ids[i] % 6], FILLED);
        putText(img, label, Point(boxes[i].tl().x, boxes[i].tl().y - 5), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255));
    }
    int64 end = cv::getTickCount();
    double frequency = cv::getTickFrequency();
    double duration = (end - start) / frequency;
    std::cout << "Execution time: " << duration << " seconds" << std::endl;
    namedWindow("result", WINDOW_AUTOSIZE);
    cv::imwrite("../resultv10.jpg", img);
    imshow("result", img);
    waitKey(0);
    destroyAllWindows();
    return 0;
}

2.CMakeLists

cmake_minimum_required(VERSION 3.0.0)
project(yolo5 VERSION 0.1.0 LANGUAGES C CXX)

include(CTest)
enable_testing()

set(INC_DIR /opt/intel/openvino/runtime/include)
set(LINK_DIR /opt/intel/openvino/runtime/lib/intel64)
set(CMAKE_CXX_STANDARD 17)
include_directories(${INC_DIR})
link_directories(${LINK_DIR})
link_libraries(libopenvino.so)

add_executable(yolo10 yolo.cpp)

target_link_libraries(yolo10 libopenvino.so)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
SET(CPACK_PROJECT_VERSION ${PROJECT_VERSION})

include(CPack)
find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(yolo10 ${OpenCV_LIBS})

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: YOLOv5是一种基于深度学习的目标检测算法,而OpenVINO是英特尔开发的用于神经网络模型优化和部署的工具集。 首先,YOLOv5可以在训练过程中生成模型权重,这些权重包含了检测目标的特征信息。然后,使用OpenVINO可以将YOLOv5训练得到的模型进行优化和部署。 在YOLOv5 c OpenVINO部署过程中,首先需要将YOLOv5的模型转换为OpenVINO支持的IR(Intermediate Representation)格式。这可以通过OpenVINO提供的Model Optimizer工具来完成,该工具可以将YOLOv5模型的权重和配置文件转换为OpenVINO可用的中间表示格式。 转换完成后,就可以使用OpenVINO进行模型的部署。OpenVINO提供了一系列可以用于优化和加速神经网络的计算库和工具。例如,可以使用OpenVINO的Inference Engine库来加载和运行转换后的模型,并利用英特尔的硬件加速功能提高推理速度和效率。 此外,OpenVINO还提供了一些用于适配不同硬件平台的示例代码和优化技术。例如,可以使用OpenVINO的Hardware-Aware Optimizations工具来针对特定的硬件平台进行优化,以实现更好的性能表现。 总结来说,YOLOv5 c OpenVINO部署是将基于深度学习的目标检测算法YOLOv5转换为OpenVINO支持的中间表示格式,并使用OpenVINO进行模型的优化和部署。通过利用OpenVINO提供的硬件加速和优化技术,可以提高模型的推理速度和效率。 ### 回答2: YOLOv5是一个用于目标检测的深度学习模型,而OpenVINO是一种用于模型部署和优化的开源工具包。将YOLOv5模型部署到OpenVINO中,可以提高模型的推理性能并适应不同的硬件环境。 首先,要将YOLOv5模型转换为OpenVINO支持的IR格式。IR(中间表示)格式是一种中间表达,能够将深度学习模型转换为可在不同硬件上执行的优化形式。可以使用OpenVINO提供的Model Optimizer工具来进行模型转换。该工具将根据模型的结构和权重生成IR文件。 接下来,在部署模型之前,需要选择适当的推理引擎。OpenVINO提供了多种推理引擎,例如CPU、GPU、VPU等。根据硬件环境的特性选择最优的推理引擎。 在进行实际的部署前,需要编写一个推理脚本来加载IR文件并执行推理操作。脚本需要指定输入图像、处理结果以及模型的基本参数,例如置信度阈值、IOU阈值等。可以使用OpenVINO提供的Python API来编写推理脚本。 最后,可以在硬件设备上运行推理脚本来进行目标检测。通过OpenVINO的优化,模型的推理速度可以得到明显的提升,并且可以在不同硬件环境中进行部署,如服务器、边缘设备等。 总之,将YOLOv5模型部署到OpenVINO需要进行模型转换、选择推理引擎、编写推理脚本等步骤。这样可以提高模型的推理性能,并使其适应不同的硬件环境。 ### 回答3: YOLOv5是一个流行的目标检测算法,其在计算机视觉领域具有广泛的应用。OpenVINO是英特尔开发的一个工具套件,用于优化和部署深度学习模型。 YOLOv5-C是YOLOv5系列中的一个变种,其相对较小而轻量化,适合在资源受限的设备上进行部署。OpenVINO可以用于将训练好的YOLOv5-C模型进行加速和优化,并将其部署到不同的设备上。 在使用OpenVINO部署YOLOv5-C模型时,首先需要使用OpenVINO的Model Optimizer工具将模型转换为OpenVINO可识别的格式。这个工具可以自动进行网络层级别的优化和压缩,以减少模型的大小和计算量。 转换完成后,可以使用OpenVINO的Inference Engine进行模型推理。Inference Engine是一个优化的推理引擎,可以在各种硬件设备上高效地运行深度学习模型。它提供了API和示例代码,使得在不同平台上进行部署变得更加简单和便捷。 通过使用OpenVINO部署YOLOv5-C模型,可以获得更高的推理性能和更低的延迟。这对于一些应用场景,如实时目标检测和视频分析,非常重要。此外,OpenVINO还支持边缘设备、嵌入式系统和云计算平台,使得模型可以在多种场景下灵活部署和应用。 总而言之,使用OpenVINO部署YOLOv5-C模型可以实现对模型的优化和加速,使得在各种设备上实现高效的目标检测应用成为可能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

mmasterer

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值