国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Ubuntu環(huán)境下C++使用onnxruntime和Opencv進行YOLOv8模型部署

這篇具有很好參考價值的文章主要介紹了Ubuntu環(huán)境下C++使用onnxruntime和Opencv進行YOLOv8模型部署。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

目錄

環(huán)境配置

系統(tǒng)環(huán)境

項目文件路徑?

文件環(huán)境

?config.txt

?CMakeLists.txt

type.names

?讀取config.txt配置文件

修改圖片尺寸格式

讀取缺陷標志文件

生成缺陷隨機顏色標識

模型推理

推理結(jié)果獲取

缺陷信息還原并顯示

總代碼


環(huán)境配置

系統(tǒng)環(huán)境

Ubuntu18.04

onnxruntime-linux-x64 1.12.1:https://github.com/microsoft/onnxruntime/releases

opencv 3.4.3

cmake?3.10.2

項目文件路徑?

1.? bin:存放可執(zhí)行程序和識別結(jié)果
2.? data:存放數(shù)據(jù)集
3.? src:存放源程序
4.? include:存放頭文件
5.? config.txt:配置文件,內(nèi)容分別是模型相對路徑、圖片相對路徑、缺陷標識文件相對路徑、缺陷識別閾值、缺陷重疊閾值
6.? type.names:缺陷標識文件,內(nèi)容和模型識別的缺陷標識順序需要一致

ubuntu onnx,深度學(xué)習(xí),YOLO,深度學(xué)習(xí),c++,ubuntu

文件環(huán)境

?config.txt

分別表示模型相對路徑、圖片相對路徑、缺陷標識文件相對路徑、缺陷識別閾值、缺陷重疊閾值

../models/best.onnx
../data/2.bmp
../type.names
0.4
0.4

?CMakeLists.txt

需要更改的地方已經(jīng)在里面標注好了

# 項目名稱,隨便寫
PROJECT(image_onnx)
# cmake版本,根據(jù)自己的寫
cmake_minimum_required(VERSION 3.10)

# 編譯好的可執(zhí)行文件放置的位置
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${image_onnx_SOURCE_DIR}/bin)

# find required opencv
find_package(OpenCV REQUIRED)
# directory of opencv headers
include_directories(${OpenCV_INCLUDE_DIRS})

# 根據(jù)自己的onnxruntime存放路徑編寫
set(ONNXRUNTIME_ROOT_PATH /home/ebaina/onnxruntime-linux-x64-1.12.1/)
set(ONNXRUNTIME_INCLUDE_DIRS ${ONNXRUNTIME_ROOT_PATH}/include/)
set(ONNXRUNTIME_LIB ${ONNXRUNTIME_ROOT_PATH}lib/libonnxruntime.so)

# 需要編譯的cpp文件所在路徑,前面是編譯好的可執(zhí)行文件名
add_executable(image_onnx src/main_image.cpp
src/change_image.cpp
src/adjust_result.cpp)
# directory of opencv library
link_directories(${OpenCV_LIBRARY_DIRS})
# opencv libraries
target_link_libraries(image_onnx ${OpenCV_LIBS})

include_directories(${ONNXRUNTIME_INCLUDE_DIRS})
target_link_libraries(image_onnx ${ONNXRUNTIME_LIB})

# include
target_include_directories(image_onnx
    PRIVATE 
        ${PROJECT_SOURCE_DIR}/include
)

type.names

缺陷標志文件,內(nèi)容和模型識別的缺陷標識順序需要一致,模型識別網(wǎng)站:Netron

ubuntu onnx,深度學(xué)習(xí),YOLO,深度學(xué)習(xí),c++,ubuntu

burr
cbreakage
inbreakage
bpulp
corrode文章來源地址http://www.zghlxwxcb.cn/news/detail-796807.html

?讀取config.txt配置文件

    // 自動讀取模型路徑,圖片路徑,缺陷閾值,重疊閾值
    std::string model_path_;
    std::string imgPath;
    std::string namesPath;
    float threshold;
    float nms_threshold;
        // 打開配置文件并讀取配置
    std::ifstream configFile("../config.txt");
    if (configFile.is_open()) {
        configFile >> model_path_ >> imgPath >> namesPath >> threshold >> nms_threshold;
        configFile.close();

        std::cout << "Model Path: " << model_path_ << std::endl;
        std::cout << "Image Path: " << imgPath << std::endl;
        std::cout << "Names Path: " << namesPath << std::endl;
        std::cout << "Threshold: " << threshold << std::endl;
        std::cout << "NMS Threshold: " << nms_threshold << std::endl;
    } else
        std::cerr << "Failed to open config file." << std::endl;
    const char* model_path = model_path_.c_str();

修改圖片尺寸格式

    // 圖片變換
    cv::Mat inputImage = cv::imread(imgPath);
    if (inputImage.empty()) {
        std::cerr << "Failed to load image." << std::endl;
        return 1;
    }
        // 獲取圖片尺寸
    int y = inputImage.rows;
    int x = inputImage.cols;
        // 圖片尺寸變換
    cv::Mat image0 = resizeImage(inputImage, y, x);
        // 圖像歸一化
    std::vector<float> input_image_ = nchwImage(image0);

讀取缺陷標志文件

    // 讀取缺陷標志文件
    std::ifstream inputFile(namesPath);
    if (!inputFile.is_open()) {
        std::cerr << "Failed to open the file." << std::endl;
        return 1;
    }
    std::vector<std::string> typeNames;
    std::string line;
    while (std::getline(inputFile, line)) 
        typeNames.push_back(line);
    inputFile.close();

生成缺陷隨機顏色標識

    // 缺陷顏色標識隨機
    int numColors = typeNames.size();
    std::vector<std::vector<int>> colors;
    for (int i = 0; i < numColors; ++i) 
        colors.push_back(generateRandomColor());
    //     // 打印顏色種類
    // for (const auto &color : colors) 
    //     std::cout << "R: " << color[0] << ", G: " << color[1] << ", B: " << color[2] << std::endl;

模型推理

    // 模型設(shè)置和推理結(jié)果
    Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "Default");
        // CPU
    Ort::Session session_{env, model_path, Ort::SessionOptions{nullptr}}; 
        // 模型輸入尺寸
    static constexpr const int height_ = 640; //model input height
    static constexpr const int width_ = 640; //model input width
    Ort::Value input_tensor_{nullptr};
    std::array<int64_t, 4> input_shape_{1, 3, height_, width_}; //mode input shape NCHW = 1x3xHxW
        // 模型輸出尺寸
    Ort::Value output_tensor_{nullptr};
    std::array<int64_t, 3> output_shape_{1, 9, 8400}; //model output shape,
    std::array<_Float32, 9*8400> results_{};

        // 模型輸入輸出張量設(shè)置
    auto memory_info = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
    input_tensor_ = Ort::Value::CreateTensor<float>(memory_info, input_image_.data(), input_image_.size(), input_shape_.data(), input_shape_.size());
    output_tensor_ = Ort::Value::CreateTensor<float>(memory_info, results_.data(), results_.size(), output_shape_.data(), output_shape_.size());
        // 查看模型輸入輸出的名稱
    const char* input_names[] = {"images"};
    const char* output_names[] = {"output0"};
        // 推理
    session_.Run(Ort::RunOptions{nullptr}, input_names, &input_tensor_, 1, output_names, &output_tensor_, 1);
    float* out = output_tensor_.GetTensorMutableData<float>();

推理結(jié)果獲取

        // 推理結(jié)果獲取
    int rows = 9;      // 第二維度大小,即行數(shù)
    int cols = 8400;   // 第三維度大小,即列數(shù)
    std::vector<std::vector<float>> matrix(rows, std::vector<float>(cols));
    for (int row = 0; row < rows; ++row) 
        for (int col = 0; col < cols; ++col) 
            matrix[row][col] = out[row * cols + col];
        // 9,8400數(shù)組轉(zhuǎn)置為8400,9
    std::vector<std::vector<float>> tran_matrix = transpose(matrix);
    //     // 顯示缺陷篩選結(jié)果
    // std::vector<std::vector<float>> num = tran_matrix;
    // for (size_t n = 0; n < num.size(); ++n) {
    //     bool aboveThreshold = false;
    //     for (size_t col = 4; col <= 8; ++col)
    //         if (num[n][col] > threshold) {
    //             aboveThreshold = true;
    //             break;
    //         }
        
    //     if (aboveThreshold) {
    //         std::cout << "Row " << n << ": ";
    //         for (const auto& val : num[n]) 
    //             std::cout << val << " ";
                
    //         std::cout << std::endl;
    //     }
    // }

缺陷信息還原并顯示

    // 缺陷還原
    std::vector<std::vector<double>> select_matrix;
    select_matrix = select(tran_matrix, threshold, cols,rows);
        // 缺陷位置信息還原
    select_matrix = return_(select_matrix, y, x);
        // 缺陷位置信息篩選
    select_matrix = nms_(select_matrix, nms_threshold);
    //     // 打印數(shù)組的內(nèi)容
    // for (const auto& row : select_matrix){
    //     for (const auto& value : row) {
    //         std::cout << value << " ";
    //     }
    //     std::cout << std::endl;
    // }
        // 繪制識別框
    cv::Mat outputImage = draw_image(select_matrix, inputImage, typeNames, colors);
    
    // 自定義窗口大小
    int windowWidth = 1200;
    int windowHeight = 900;

    // 調(diào)整窗口大小
    cv::namedWindow("Image with Bounding Boxes", cv::WINDOW_NORMAL);
    cv::resizeWindow("Image with Bounding Boxes", windowWidth, windowHeight);
    cv::imshow("Image with Bounding Boxes", outputImage);
    cv::imwrite("marked_image.jpg", outputImage);
    cv::waitKey(0);

main代碼(關(guān)注取源碼!)

#include <assert.h>
#include <random>
#include <onnxruntime_cxx_api.h>
#include "cpu_provider_factory.h"
#include <adjust_result.h>

// 隨機生成顏色
std::vector<int> generateRandomColor() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<double> dis(0.0, 1.0);

    std::vector<int> color(3);
    for (int i = 0; i < 3; ++i) {
        color[i] = static_cast<int>(dis(gen) * 255);
    }

    return color;
}

int main(int argc, char* argv[]) {
    // // 模型路徑,圖片路徑,缺陷閾值,重疊閾值
    // const char* model_path = "../models/best.onnx";
    // std::string imgPath = "../data/3.bmp";
    // std::string namesPath = "../type.names";
    // float threshold = 0.4;
    // float nms_threshold = 0.4;
    // 自動讀取模型路徑,圖片路徑,缺陷閾值,重疊閾值
    std::string model_path_;
    std::string imgPath;
    std::string namesPath;
    float threshold;
    float nms_threshold;
        // 打開配置文件并讀取配置
    std::ifstream configFile("../config.txt");
    if (configFile.is_open()) {
        configFile >> model_path_ >> imgPath >> namesPath >> threshold >> nms_threshold;
        configFile.close();

        std::cout << "Model Path: " << model_path_ << std::endl;
        std::cout << "Image Path: " << imgPath << std::endl;
        std::cout << "Names Path: " << namesPath << std::endl;
        std::cout << "Threshold: " << threshold << std::endl;
        std::cout << "NMS Threshold: " << nms_threshold << std::endl;
    } else
        std::cerr << "Failed to open config file." << std::endl;
    const char* model_path = model_path_.c_str();

    // 圖片變換
    cv::Mat inputImage = cv::imread(imgPath);
    if (inputImage.empty()) {
        std::cerr << "Failed to load image." << std::endl;
        return 1;
    }
        // 獲取圖片尺寸
    int y = inputImage.rows;
    int x = inputImage.cols;
        // 圖片尺寸變換
    cv::Mat image0 = resizeImage(inputImage, y, x);
        // 圖像歸一化
    std::vector<float> input_image_ = nchwImage(image0);
    
    // 讀取缺陷標志文件
    std::ifstream inputFile(namesPath);
    if (!inputFile.is_open()) {
        std::cerr << "Failed to open the file." << std::endl;
        return 1;
    }
    std::vector<std::string> typeNames;
    std::string line;
    while (std::getline(inputFile, line)) 
        typeNames.push_back(line);
    inputFile.close();
    //     // 打印缺陷標志文件內(nèi)容
    // std::cout << "Number of elements: " << typeNames.size() << std::endl;
    // for (const std::string &typeName : typeNames) 
    //     std::cout << typeName << std::endl;

    // 缺陷顏色標識隨機
    int numColors = typeNames.size();
    std::vector<std::vector<int>> colors;
    for (int i = 0; i < numColors; ++i) 
        colors.push_back(generateRandomColor());
    //     // 打印顏色種類
    // for (const auto &color : colors) 
    //     std::cout << "R: " << color[0] << ", G: " << color[1] << ", B: " << color[2] << std::endl;

    // 模型設(shè)置和推理結(jié)果
    Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "Default");
        // CPU
    Ort::Session session_{env, model_path, Ort::SessionOptions{nullptr}}; 
        // 模型輸入尺寸
    static constexpr const int height_ = 640; //model input height
    static constexpr const int width_ = 640; //model input width
    Ort::Value input_tensor_{nullptr};
    std::array<int64_t, 4> input_shape_{1, 3, height_, width_}; //mode input shape NCHW = 1x3xHxW
        // 模型輸出尺寸
    Ort::Value output_tensor_{nullptr};
    std::array<int64_t, 3> output_shape_{1, 9, 8400}; //model output shape,
    std::array<_Float32, 9*8400> results_{};

        // 模型輸入輸出張量設(shè)置
    auto memory_info = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
    input_tensor_ = Ort::Value::CreateTensor<float>(memory_info, input_image_.data(), input_image_.size(), input_shape_.data(), input_shape_.size());
    output_tensor_ = Ort::Value::CreateTensor<float>(memory_info, results_.data(), results_.size(), output_shape_.data(), output_shape_.size());
        // 查看模型輸入輸出的名稱
    const char* input_names[] = {"images"};
    const char* output_names[] = {"output0"};
        // 推理
    session_.Run(Ort::RunOptions{nullptr}, input_names, &input_tensor_, 1, output_names, &output_tensor_, 1);
    float* out = output_tensor_.GetTensorMutableData<float>();

        // 推理結(jié)果獲取
    int rows = 9;      // 第二維度大小,即行數(shù)
    int cols = 8400;   // 第三維度大小,即列數(shù)
    std::vector<std::vector<float>> matrix(rows, std::vector<float>(cols));
    for (int row = 0; row < rows; ++row) 
        for (int col = 0; col < cols; ++col) 
            matrix[row][col] = out[row * cols + col];
        // 9,8400數(shù)組轉(zhuǎn)置為8400,9
    std::vector<std::vector<float>> tran_matrix = transpose(matrix);
    //     // 顯示缺陷篩選結(jié)果
    // std::vector<std::vector<float>> num = tran_matrix;
    // for (size_t n = 0; n < num.size(); ++n) {
    //     bool aboveThreshold = false;
    //     for (size_t col = 4; col <= 8; ++col)
    //         if (num[n][col] > threshold) {
    //             aboveThreshold = true;
    //             break;
    //         }
        
    //     if (aboveThreshold) {
    //         std::cout << "Row " << n << ": ";
    //         for (const auto& val : num[n]) 
    //             std::cout << val << " ";
                
    //         std::cout << std::endl;
    //     }
    // }

    // 缺陷還原
    std::vector<std::vector<double>> select_matrix;
    select_matrix = select(tran_matrix, threshold, cols,rows);
        // 缺陷位置信息還原
    select_matrix = return_(select_matrix, y, x);
        // 缺陷位置信息篩選
    select_matrix = nms_(select_matrix, nms_threshold);
    //     // 打印數(shù)組的內(nèi)容
    // for (const auto& row : select_matrix){
    //     for (const auto& value : row) {
    //         std::cout << value << " ";
    //     }
    //     std::cout << std::endl;
    // }
        // 繪制識別框
    cv::Mat outputImage = draw_image(select_matrix, inputImage, typeNames, colors);
    
    // 自定義窗口大小
    int windowWidth = 1200;
    int windowHeight = 900;

    // 調(diào)整窗口大小
    cv::namedWindow("Image with Bounding Boxes", cv::WINDOW_NORMAL);
    cv::resizeWindow("Image with Bounding Boxes", windowWidth, windowHeight);
    cv::imshow("Image with Bounding Boxes", outputImage);
    cv::imwrite("marked_image.jpg", outputImage);
    cv::waitKey(0);

    return 0;
}

到了這里,關(guān)于Ubuntu環(huán)境下C++使用onnxruntime和Opencv進行YOLOv8模型部署的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 使用yolov8的dockerfile在ubuntu上部署環(huán)境

    使用yolov8的dockerfile在ubuntu上部署環(huán)境

    首先進入doceker文件夾 cd yolov8/ultralytics-main/docker 執(zhí)行命令 docker build -t yolov8:v1 . yolov8:v1(鏡像名稱:鏡像標簽,可以自己定義) 注意點: (1)原docekerfile中 ADD https://ultralytics.com/assets/Arial.ttf https://ultralytics.com/assets/Arial.Unicode.ttf /root/.config/Ultralytics/ 下載很慢,可以在外部下載好

    2024年02月10日
    瀏覽(21)
  • [C++]使用純opencv部署yolov8旋轉(zhuǎn)框目標檢測

    [C++]使用純opencv部署yolov8旋轉(zhuǎn)框目標檢測

    【官方框架地址】 https://github.com/ultralytics/ultralytics 【算法介紹】 YOLOv8是一種先進的對象檢測算法,它通過單個神經(jīng)網(wǎng)絡(luò)實現(xiàn)了快速的物體檢測。其中,旋轉(zhuǎn)框檢測是YOLOv8的一項重要特性,它可以有效地檢測出不同方向和角度的物體。 旋轉(zhuǎn)框檢測的原理是通過預(yù)測物體的邊界

    2024年04月26日
    瀏覽(28)
  • pytorch 42 C#使用onnxruntime部署內(nèi)置nms的yolov8模型

    pytorch 42 C#使用onnxruntime部署內(nèi)置nms的yolov8模型

    在進行目標檢測部署時,通常需要自行編碼實現(xiàn)對模型預(yù)測結(jié)果的解碼及與預(yù)測結(jié)果的nms操作。所幸現(xiàn)在的各種部署框架對算子的支持更為靈活,可以在模型內(nèi)實現(xiàn)預(yù)測結(jié)果的解碼,但仍然需要自行編碼實現(xiàn)對預(yù)測結(jié)果的nms操作。其實在onnx opset===11版本以后,其已支持將nms操

    2024年02月12日
    瀏覽(22)
  • C++模型部署:qt+yolov5/6+onnxruntime+opencv

    C++模型部署:qt+yolov5/6+onnxruntime+opencv

    作者平時主要是寫 c++ 庫的,界面方面了解不多,也沒有發(fā)現(xiàn)“美”的眼鏡,界面有點丑,大家多包涵。 本次介紹的項目主要是通過 cmake 構(gòu)建一個 基于 c++ 語言的,以 qt 為框架的,包含 opencv 第三方庫在內(nèi)的,跨平臺的,使用 ONNX RUNTIME 進行前向推理的 yolov5/6 演示平臺。文章

    2024年02月05日
    瀏覽(23)
  • [C++]使用yolov8的onnx模型僅用opencv和bytetrack實現(xiàn)目標追蹤

    [C++]使用yolov8的onnx模型僅用opencv和bytetrack實現(xiàn)目標追蹤

    【官方框架地址】 yolov8: https://github.com/ultralytics/ultralytics bytetrack: https://github.com/ifzhang/ByteTrack 【算法介紹】 隨著人工智能技術(shù)的不斷發(fā)展,目標追蹤已成為計算機視覺領(lǐng)域的重要研究方向。Yolov8和ByTetrack作為當(dāng)前先進的算法,當(dāng)它們結(jié)合使用時,能夠顯著提升目標追蹤的準

    2024年01月24日
    瀏覽(26)
  • 如何加載模型YOLOv8 ONNXRuntime

    如何加載模型YOLOv8 ONNXRuntime

    YOLOv8 是 YOLO(You Only Look Once)目標檢測系統(tǒng)的最新版本(v8)。YOLO 是一種實時、一次性目標檢測系統(tǒng),旨在在網(wǎng)絡(luò)的單次前向傳遞中執(zhí)行目標檢測,使其快速高效。YOLOv8是之前YOLO模型的改進版本,具有更高的精度和更快的推理速度。 ONNX(開放神經(jīng)網(wǎng)絡(luò)交換)是一種表示深度

    2024年02月14日
    瀏覽(22)
  • YOLOV8 進行docker環(huán)境配置

    原docekerfile中ADD https://ultralytics.com/assets/Arial.ttf https://ultralytics.com/assets/Arial.Unicode.ttf /root/.config/Ultralytics/下載很慢,可以在外部下載好,放入docker文件夾中,再將源代碼改為ADD Arial.ttf Arial.Unicode.ttf /root/.config/Ultralytics/(其它下載內(nèi)容類似修改包括yolo8.pt,) 可在RUN pip install -

    2024年02月04日
    瀏覽(53)
  • yolov8 opencv模型部署(C++版)

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

    TensorRT系列之 Windows10下yolov8 tensorrt模型加速部署 TensorRT系列之 Linux下 yolov8 tensorrt模型加速部署 TensorRT系列之 Linux下 yolov7 tensorrt模型加速部署 TensorRT系列之 Linux下 yolov6 tensorrt模型加速部署 TensorRT系列之 Linux下 yolov5 tensorrt模型加速部署 TensorRT系列之 Linux下 yolox tensorrt模型加速部

    2024年02月08日
    瀏覽(27)
  • YOLOv8-Openvino和ONNXRuntime推理【CPU】

    YOLOv8-Openvino和ONNXRuntime推理【CPU】

    CPU:i5-12500 2.1 Openvino簡介 Openvino是由Intel開發(fā)的專門用于優(yōu)化和部署人工智能推理的半開源的工具包,主要用于對深度推理做優(yōu)化。 Openvino內(nèi)部集成了Opencv、TensorFlow模塊,除此之外它還具有強大的Plugin開發(fā)框架,允許開發(fā)者在Openvino之上對推理過程做優(yōu)化。 Openvino整體框架為

    2024年02月20日
    瀏覽(27)
  • AI模型部署 | onnxruntime部署YOLOv8分割模型詳細教程

    AI模型部署 | onnxruntime部署YOLOv8分割模型詳細教程

    本文首發(fā)于公眾號【DeepDriving】,歡迎關(guān)注。 0. 引言 我之前寫的文章《基于YOLOv8分割模型實現(xiàn)垃圾識別》介紹了如何使用 YOLOv8 分割模型來實現(xiàn)垃圾識別,主要是介紹如何用自定義的數(shù)據(jù)集來訓(xùn)練 YOLOv8 分割模型。那么訓(xùn)練好的模型該如何部署呢? YOLOv8 分割模型相比檢測模型

    2024年04月24日
    瀏覽(47)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包