UNeedCryDear / yolov8-opencv-onnxruntime-cpp

yolov8 hub,cpp with onnxruntime and opencv
Apache License 2.0
319 stars 56 forks source link

Detect Failed! #9

Closed HarborC closed 1 year ago

HarborC commented 1 year ago

Hi, Thank you for your work. I modified you code as the below, and I test the segment function of yolov8_seg.h by loop using cuda. Then Error messages "Detect Failed!" appear one line apart. Could you give me some advice.

#include <iostream>
#include <string>
#include <memory>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/dnn/dnn.hpp>

#include <chrono>
#include <cstdlib>
#include <ctime>
#include <time.h>

#include "yolov8_seg.h"

class PersonSeg {
public:
    typedef std::shared_ptr<PersonSeg> Ptr;
public:
    PersonSeg(const std::string &model_path) {
        if (task_segment_onnx.ReadModel(net, model_path, true)) {
            std::cout << "read net ok!" << std::endl;
        } else {
            std::exit(0);
        }

        srand(time(0));
        for (int i = 0; i < 80; i++) {
            int b = rand() % 256;
            int g = rand() % 256;
            int r = rand() % 256;
            color.push_back(cv::Scalar(b, g, r));
        }
    }
    ~PersonSeg() {}

public:
    void segment(const cv::Mat& input_image, cv::Mat& result);

protected:
    Yolov8Seg task_segment_onnx;
    cv::dnn::Net net;
    std::vector<cv::Scalar> color;
};

void PersonSeg::segment(const cv::Mat& input_image, cv::Mat& result) {
    std::vector<OutputSeg> result2;
    if (task_segment_onnx.Detect(input_image, net, result2)) {
        DrawPred(input_image, result2, task_segment_onnx._className, color);
    } else {
        std::cout << "Detect Failed!" << std::endl;
    }
}

int main(int argc, char** argv) {
    std::string model_path = "D:/Code/onnx_sky_seg/Sky-Segmentation-and-Post-processing/skyseg.onnx";
    SkySeg sky_seg(model_path);

    std::string image_path = "D:/Code/onnx_sky_seg/Sky-Segmentation-and-Post-processing/pano_images/1679476170.981470.jpg";
    cv::Mat image = cv::imread(image_path);    
    std::string seg_model_path = "D:/Code/onnx_sky_seg/yolov8m-seg.onnx";
    PersonSeg person_seg(seg_model_path);
     for (int i = 0; i < 100; i++) {
        TicToc timer;

        cv::Mat result;
        person_seg.segment(image, result);
        cv::imwrite("D:/Code/onnx_sky_seg/yolov8-opencv-onnxruntime-cpp-main/test.jpg", image);

        std::cout << "time: " << timer.toc() << " ms" << std::endl;
    }

    return 0;
}

image

UNeedCryDear commented 1 year ago

没有检测到物体啊,确认下你的图片没问题,或者你直接在python下面用pt模型检测能检测到结果吗? 不行的话请提供一下图片,我我检查一下啥问题。

HarborC commented 1 year ago

我只读取了一张图片,然后再循环中检测同一张图像的物体,控制台显示在一开始可以检测出来,后面就都检测不出来了,图片应该是没有问题。python下面用pt模型检测能检测到结果。我用cpu运行就没有这个问题

std::string model_path = "D:/Code/onnx_sky_seg/Sky-Segmentation-and-Post-processing/skyseg.onnx";
    SkySeg sky_seg(model_path);

    std::string image_path = "D:/Code/onnx_sky_seg/Sky-Segmentation-and-Post-processing/pano_images/1679476170.981470.jpg";
    cv::Mat image = cv::imread(image_path);    
    std::string seg_model_path = "D:/Code/onnx_sky_seg/yolov8m-seg.onnx";
    PersonSeg person_seg(seg_model_path);
     for (int i = 0; i < 100; i++) {
        TicToc timer;

        cv::Mat result;
        person_seg.segment(image, result);
        cv::imwrite("D:/Code/onnx_sky_seg/yolov8-opencv-onnxruntime-cpp-main/test.jpg", image);

        std::cout << "time: " << timer.toc() << " ms" << std::endl;
    }
UNeedCryDear commented 1 year ago

如果是这样,你将读取部分也放置在循环当中,或者去掉drawPred,因为这个函数会在原来的图像上面绘制结果,会修改到原始图片导致你后面无法检测

HarborC commented 1 year ago

有效,谢谢你的帮助