tensorrt-yolov8实践

项目工程:https://github.com/Monday-Leo/YOLOv8_Tensorrt

此链接有视频教程,根据此视频能够初步实现部署
主要记录以下自己修改的代码


filesystem  这个基本库需要在项目配置中将语言标准调成c++17

如图所示

 

#include <opencv2/opencv.hpp>
#include "yolo.hpp"
#include <filesystem>  //c++17


#include <iostream>
#include <vector>

using namespace std;
namespace fs = std::filesystem;

static const char* mylabels[] = { "feather", "grass", "other", "paper", "plastic" };

// 结构体用于存储检测到的目标信息
struct DetectedObject {
    string label;
    float confidence;
    cv::Rect bbox;
    cv::Point center; // 中心点
};

// 将图像路径转换为完整的输出路径
string get_output_path(const string& img_path, const string& output_folder) {
    fs::path img_path_obj(img_path);
    return (fs::path(output_folder) / img_path_obj.filename()).string();
}

// 目标检测函数
int detect_objects(const string& img_path, const string& model_path, const string& output_folder) {
    cv::Mat image = cv::imread(img_path);
    if (image.empty()) {
        cerr << "Error: Unable to read image " << img_path << endl;
        return 0;
    }

    float confidence_threshold = 0.25f;
    float nms_threshold = 0.5f;
    auto yolo = yolo::load(model_path, yolo::Type::V8, confidence_threshold, nms_threshold);
    if (yolo == nullptr) {
        cerr << "Error: Unable to load YOLO model from " << model_path << endl;
        return 0;
    }

    auto objs = yolo->forward(yolo::Image(image.data, image.cols, image.rows));

    // 绘制边界框、计算中心点和保存结果
    vector<DetectedObject> detected_objects; // 存储检测到的目标信息
    for (auto& obj : objs) {
        DetectedObject detected_obj;
        detected_obj.label = mylabels[obj.class_label];
        detected_obj.confidence = obj.confidence;
        detected_obj.bbox = cv::Rect(obj.left, obj.top, obj.right - obj.left, obj.bottom - obj.top);
        detected_obj.center = cv::Point(obj.left + (obj.right - obj.left) / 2, obj.top + (obj.bottom - obj.top) / 2); // 计算中心点
        detected_objects.push_back(detected_obj);

        uint8_t b, g, r;
        tie(b, g, r) = yolo::random_color(obj.class_label);
        cv::rectangle(image, cv::Point(obj.left, obj.top), cv::Point(obj.right, obj.bottom),
            cv::Scalar(b, g, r), 5);

        auto caption = cv::format("%s %.2f", detected_obj.label.c_str(), detected_obj.confidence);
        int width = cv::getTextSize(caption, 0, 1, 2, nullptr).width + 10;
        cv::rectangle(image, cv::Point(obj.left - 3, obj.top - 33),
            cv::Point(obj.left + width, obj.top), cv::Scalar(b, g, r), -1);
        cv::putText(image, caption, cv::Point(obj.left, obj.top - 5), 0, 1, cv::Scalar::all(0), 2, 16);

        // 绘制中心点
        cv::circle(image, detected_obj.center, 5, cv::Scalar(255, 255, 255), -1);
    }

    // 输出检测到的目标信息
    cout << "Detected objects in image " << img_path << ":" << endl;
    for (const auto& obj : detected_objects) {
        cout << "Class: " << obj.label << ", Confidence: " << obj.confidence << endl;
        cout << "Bounding Box: (" << obj.bbox.x << ", " << obj.bbox.y << ") - (" << obj.bbox.x + obj.bbox.width << ", " << obj.bbox.y + obj.bbox.height << ")" << endl;
        cout << "Center: (" << obj.center.x << ", " << obj.center.y << ")" << endl;
    }

    // 保存结果图像
    string output_path = get_output_path(img_path, output_folder);
    if (cv::imwrite(output_path, image)) {
        cout << "Saved result to " << output_path << ", " << detected_objects.size() << " objects detected" << endl;
    }
    else {
        cerr << "Error: Failed to save image to " << output_path << endl;
    }

    return detected_objects.size(); // 返回检测到的目标数量
}

// 主函数
int main() {
    string img_folder = "E:/daima/YOLOv8_Tensorrt-zawu/images";
    string model_path = "E:/daima/YOLOv8_Tensorrt-zawu/zawu.trt";
    string output_folder = "outputs";
    fs::create_directory(output_folder);

    int total_detected_objects = 0; // 总共检测到的目标数量
    for (const auto& entry : fs::directory_iterator(img_folder)) {
        if (entry.is_regular_file()) {
            total_detected_objects += detect_objects(entry.path().string(), model_path, output_folder);
        }
    }

    cout << "Total detected objects: " << total_detected_objects << endl;

    return 0;
}

以上代码主要添加的功能如下:

1.计算图像中心点并绘制
2.计算目标数量

  • 22
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值