origin
This commit is contained in:
		
							
								
								
									
										302
									
								
								YOLOPv2.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										302
									
								
								YOLOPv2.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,302 @@
 | 
			
		||||
#include "YOLOPv2.h"
 | 
			
		||||
#include <QLoggingCategory>
 | 
			
		||||
 | 
			
		||||
YOLOPv2::YOLOPv2(Net_config config)
 | 
			
		||||
{
 | 
			
		||||
    this->confThreshold = config.confThreshold;
 | 
			
		||||
    this->nmsThreshold  = config.nmsThreshold;
 | 
			
		||||
    //string model_path   = config.modelpath;
 | 
			
		||||
    //std::wstring widestr = std::wstring(model_path.begin(), model_path.end());
 | 
			
		||||
 | 
			
		||||
    //CUDA option set
 | 
			
		||||
    OrtCUDAProviderOptions cuda_option;
 | 
			
		||||
    cuda_option.device_id                 = 0;
 | 
			
		||||
    cuda_option.arena_extend_strategy     = 0;
 | 
			
		||||
    cuda_option.cudnn_conv_algo_search    = OrtCudnnConvAlgoSearchExhaustive;
 | 
			
		||||
    cuda_option.gpu_mem_limit             = SIZE_MAX;
 | 
			
		||||
    cuda_option.do_copy_in_default_stream = 1;
 | 
			
		||||
    //CUDA 加速
 | 
			
		||||
    sessionOptions.SetIntraOpNumThreads(1);//设置线程数
 | 
			
		||||
    sessionOptions.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL); //函数用于设置在使用 ORT 库执行模型时应用的图优化级别。ORT_ENABLE_ALL 选项启用所有可用的优化,这可以导致更快速和更高效的执行。
 | 
			
		||||
    sessionOptions.AppendExecutionProvider_CUDA(cuda_option);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    const char  *modelpath = "/home/wuxianfu/Projects/Fast-YolopV2/build/onnx/yolopv2_192x320.onnx" ;
 | 
			
		||||
    ort_session = new Session(env, modelpath, sessionOptions);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    size_t numInputNodes  = ort_session->GetInputCount();
 | 
			
		||||
    size_t numOutputNodes = ort_session->GetOutputCount();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    AllocatorWithDefaultOptions allocator;
 | 
			
		||||
    for (int i = 0; i < numInputNodes; i++)
 | 
			
		||||
    {
 | 
			
		||||
        //input_names.push_back(ort_session->GetInputName(i, allocator));
 | 
			
		||||
        AllocatedStringPtr input_name_Ptr = ort_session->GetInputNameAllocated(i, allocator);
 | 
			
		||||
        input_names.push_back(input_name_Ptr.get());
 | 
			
		||||
        qDebug() << "Input Name: " << input_name_Ptr.get();
 | 
			
		||||
        Ort::TypeInfo input_type_info = ort_session->GetInputTypeInfo(i);
 | 
			
		||||
        auto input_tensor_info = input_type_info.GetTensorTypeAndShapeInfo();
 | 
			
		||||
        auto input_dims = input_tensor_info.GetShape();
 | 
			
		||||
        input_node_dims.push_back(input_dims);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i < numOutputNodes; i++)
 | 
			
		||||
    {
 | 
			
		||||
        //output_names.push_back(ort_session->GetOutputName(i, allocator));
 | 
			
		||||
        AllocatedStringPtr output_name_Ptr= ort_session->GetOutputNameAllocated(i, allocator);
 | 
			
		||||
        output_names.push_back(output_name_Ptr.get());
 | 
			
		||||
        qDebug() << "Output Name: " << output_name_Ptr.get();
 | 
			
		||||
        Ort::TypeInfo output_type_info = ort_session->GetOutputTypeInfo(i);
 | 
			
		||||
        auto output_tensor_info = output_type_info.GetTensorTypeAndShapeInfo();
 | 
			
		||||
        auto output_dims = output_tensor_info.GetShape();
 | 
			
		||||
        output_node_dims.push_back(output_dims);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    this->inpHeight = input_node_dims[0][2];
 | 
			
		||||
    this->inpWidth = input_node_dims[0][3];
 | 
			
		||||
 | 
			
		||||
    string classesFile = "/home/wuxianfu/Projects/Fast-YolopV2/build/coco.names";
 | 
			
		||||
    ifstream ifs(classesFile.c_str());
 | 
			
		||||
    string line;
 | 
			
		||||
    while (getline(ifs, line)) this->class_names.push_back(line);
 | 
			
		||||
    this->num_class = class_names.size();
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void YOLOPv2::normalize_(Mat img)
 | 
			
		||||
{
 | 
			
		||||
    //    img.convertTo(img, CV_32F);
 | 
			
		||||
    int row = img.rows;
 | 
			
		||||
    int col = img.cols;
 | 
			
		||||
    this->input_image_.resize(row * col * img.channels());
 | 
			
		||||
    for (int c = 0; c < 3; c++)
 | 
			
		||||
    {
 | 
			
		||||
        for (int i = 0; i < row; i++)
 | 
			
		||||
        {
 | 
			
		||||
            for (int j = 0; j < col; j++)
 | 
			
		||||
            {
 | 
			
		||||
                float pix = img.ptr<uchar>(i)[j * 3 + 2 - c];
 | 
			
		||||
                this->input_image_[c * row * col + i * col + j] = pix / 255.0;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void YOLOPv2::nms(vector<BoxInfo>& input_boxes)
 | 
			
		||||
{
 | 
			
		||||
    sort(input_boxes.begin(), input_boxes.end(), [](BoxInfo a, BoxInfo b) { return a.score > b.score; });
 | 
			
		||||
    vector<float> vArea(input_boxes.size());
 | 
			
		||||
    for (int i = 0; i < int(input_boxes.size()); ++i)
 | 
			
		||||
    {
 | 
			
		||||
        vArea[i] = (input_boxes.at(i).x2 - input_boxes.at(i).x1 + 1)
 | 
			
		||||
            * (input_boxes.at(i).y2 - input_boxes.at(i).y1 + 1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    vector<bool> isSuppressed(input_boxes.size(), false);
 | 
			
		||||
    for (int i = 0; i < int(input_boxes.size()); ++i)
 | 
			
		||||
    {
 | 
			
		||||
        if (isSuppressed[i]) { continue; }
 | 
			
		||||
        for (int j = i + 1; j < int(input_boxes.size()); ++j)
 | 
			
		||||
        {
 | 
			
		||||
            if (isSuppressed[j]) { continue; }
 | 
			
		||||
            float xx1 = (max)(input_boxes[i].x1, input_boxes[j].x1);
 | 
			
		||||
            float yy1 = (max)(input_boxes[i].y1, input_boxes[j].y1);
 | 
			
		||||
            float xx2 = (min)(input_boxes[i].x2, input_boxes[j].x2);
 | 
			
		||||
            float yy2 = (min)(input_boxes[i].y2, input_boxes[j].y2);
 | 
			
		||||
 | 
			
		||||
            float w = (max)(float(0), xx2 - xx1 + 1);
 | 
			
		||||
            float h = (max)(float(0), yy2 - yy1 + 1);
 | 
			
		||||
            float inter = w * h;
 | 
			
		||||
            float ovr = inter / (vArea[i] + vArea[j] - inter);
 | 
			
		||||
 | 
			
		||||
            if (ovr >= this->nmsThreshold)
 | 
			
		||||
            {
 | 
			
		||||
                isSuppressed[j] = true;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // return post_nms;
 | 
			
		||||
    int idx_t = 0;
 | 
			
		||||
    input_boxes.erase(remove_if(input_boxes.begin(), input_boxes.end(), [&idx_t, &isSuppressed](const BoxInfo& f) { return isSuppressed[idx_t++]; }), input_boxes.end());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline float sigmoid(float x)
 | 
			
		||||
{
 | 
			
		||||
    return 1.0 / (1 + exp(-x));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Mat YOLOPv2::detect(Mat& frame)
 | 
			
		||||
{
 | 
			
		||||
    Mat dstimg;
 | 
			
		||||
    resize(frame, dstimg, Size(this->inpWidth, this->inpHeight));
 | 
			
		||||
    this->normalize_(dstimg);
 | 
			
		||||
    array<int64_t, 4> input_shape_{ 1, 3, this->inpHeight, this->inpWidth };
 | 
			
		||||
 | 
			
		||||
    auto allocator_info = MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
 | 
			
		||||
    Value input_tensor_ = Value::CreateTensor<float>(allocator_info, input_image_.data(), input_image_.size(), input_shape_.data(), input_shape_.size());
 | 
			
		||||
 | 
			
		||||
    // 开始推理
 | 
			
		||||
 | 
			
		||||
    /*qDebug() << " output_names size: " << output_names.size()<< " sec \n";
 | 
			
		||||
    qDebug() << " input_names: " << input_names[0]<< " sec \n";
 | 
			
		||||
    qDebug() << " output_names: " << output_names[1]<< " sec \n";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    vector<Value> ort_outputs = ort_session->Run(RunOptions{nullptr}, input_names.data(), &input_tensor_, 1, output_names.data(), output_names.size());*/
 | 
			
		||||
 | 
			
		||||
    const char* inputNames[]  = { "input" };//这两个值是根据netron查看onnx格式得到的输入输出名称
 | 
			
		||||
    const char* outputNames[] = { "seg" ,  "ll" ,  "pred0" ,  "pred1" ,  "pred2" , };
 | 
			
		||||
    vector<Value> ort_outputs = ort_session->Run(RunOptions{nullptr}, inputNames, &input_tensor_, 1, outputNames, 5);
 | 
			
		||||
 | 
			
		||||
                                                                                                                                                 /////generate proposals
 | 
			
		||||
 | 
			
		||||
    vector<BoxInfo> generate_boxes;
 | 
			
		||||
    float ratioh = (float)frame.rows / this->inpHeight, ratiow = (float)frame.cols / this->inpWidth;
 | 
			
		||||
    int n = 0, q = 0, i = 0, j = 0, nout = this->class_names.size() + 5, c = 0, area = 0;
 | 
			
		||||
    for (n = 0; n < 3; n++)   ///尺度
 | 
			
		||||
    {
 | 
			
		||||
        int num_grid_x = (int)(this->inpWidth / this->stride[n]);
 | 
			
		||||
        int num_grid_y = (int)(this->inpHeight / this->stride[n]);
 | 
			
		||||
        area = num_grid_x * num_grid_y;
 | 
			
		||||
        const float* pdata = ort_outputs[n + 2].GetTensorMutableData<float>();
 | 
			
		||||
        for (q = 0; q < 3; q++)    ///anchor数
 | 
			
		||||
        {
 | 
			
		||||
            const float anchor_w = this->anchors[n][q * 2];
 | 
			
		||||
            const float anchor_h = this->anchors[n][q * 2 + 1];
 | 
			
		||||
            for (i = 0; i < num_grid_y; i++)
 | 
			
		||||
            {
 | 
			
		||||
                for (j = 0; j < num_grid_x; j++)
 | 
			
		||||
                {
 | 
			
		||||
                    float box_score = sigmoid(pdata[4 * area + i * num_grid_x + j]);
 | 
			
		||||
                    if (box_score > this->confThreshold)
 | 
			
		||||
                    {
 | 
			
		||||
                        float max_class_socre = -100000, class_socre = 0;
 | 
			
		||||
                        int max_class_id = 0;
 | 
			
		||||
                        for (c = 0; c < this->class_names.size(); c++) //// get max socre
 | 
			
		||||
                        {
 | 
			
		||||
                            class_socre = pdata[(c + 5) * area + i * num_grid_x + j];
 | 
			
		||||
                            if (class_socre > max_class_socre)
 | 
			
		||||
                            {
 | 
			
		||||
                                max_class_socre = class_socre;
 | 
			
		||||
                                max_class_id = c;
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                        max_class_socre = sigmoid(max_class_socre) * box_score;
 | 
			
		||||
                        if (max_class_socre > this->confThreshold)
 | 
			
		||||
                        {
 | 
			
		||||
                            float cx = (sigmoid(pdata[i * num_grid_x + j]) * 2.f - 0.5f + j) * this->stride[n];  ///cx
 | 
			
		||||
                            float cy = (sigmoid(pdata[area + i * num_grid_x + j]) * 2.f - 0.5f + i) * this->stride[n];   ///cy
 | 
			
		||||
                            float w = powf(sigmoid(pdata[2 * area + i * num_grid_x + j]) * 2.f, 2.f) * anchor_w;   ///w
 | 
			
		||||
                            float h = powf(sigmoid(pdata[3 * area + i * num_grid_x + j]) * 2.f, 2.f) * anchor_h;  ///h
 | 
			
		||||
 | 
			
		||||
                            float xmin = (cx - 0.5*w)*ratiow;
 | 
			
		||||
                            float ymin = (cy - 0.5*h)*ratioh;
 | 
			
		||||
                            float xmax = (cx + 0.5*w)*ratiow;
 | 
			
		||||
                            float ymax = (cy + 0.5*h)*ratioh;
 | 
			
		||||
 | 
			
		||||
                            generate_boxes.push_back(BoxInfo{ xmin, ymin, xmax, ymax, max_class_socre, max_class_id });
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            pdata += area * nout;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    nms(generate_boxes);
 | 
			
		||||
 | 
			
		||||
    Mat outimg = frame.clone();
 | 
			
		||||
    for (size_t i = 0; i < generate_boxes.size(); ++i)
 | 
			
		||||
    {
 | 
			
		||||
        int xmin = int(generate_boxes[i].x1);
 | 
			
		||||
        int ymin = int(generate_boxes[i].y1);
 | 
			
		||||
        rectangle(outimg, Point(xmin, ymin), Point(int(generate_boxes[i].x2), int(generate_boxes[i].y2)), Scalar(0, 0, 255), 2);
 | 
			
		||||
        string label = format("%.2f", generate_boxes[i].score);
 | 
			
		||||
        label = this->class_names[generate_boxes[i].label-1] + ":" + label;
 | 
			
		||||
        putText(outimg, label, Point(xmin, ymin - 5), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 255, 0), 1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const float* pdrive_area = ort_outputs[0].GetTensorMutableData<float>();
 | 
			
		||||
    const float* plane_line = ort_outputs[1].GetTensorMutableData<float>();
 | 
			
		||||
    area = this->inpHeight*this->inpWidth;
 | 
			
		||||
    int min_y = -1;
 | 
			
		||||
    vector<Point2f> points_L, points_R;
 | 
			
		||||
    for (i = 0; i < frame.rows; i++)
 | 
			
		||||
    {
 | 
			
		||||
        bool flg = false;
 | 
			
		||||
        int left = -1, right = -1;
 | 
			
		||||
        for (j = 0; j < frame.cols; j++)
 | 
			
		||||
        {
 | 
			
		||||
            const int x = int(j / ratiow);
 | 
			
		||||
            const int y = int(i / ratioh);
 | 
			
		||||
            if (pdrive_area[y * this->inpWidth + x] < pdrive_area[area + y * this->inpWidth + x])
 | 
			
		||||
            {
 | 
			
		||||
                outimg.at<Vec3b>(i, j)[0] = 0;
 | 
			
		||||
                outimg.at<Vec3b>(i, j)[1] = 255;
 | 
			
		||||
                outimg.at<Vec3b>(i, j)[2] = 0;
 | 
			
		||||
            }
 | 
			
		||||
            if (plane_line[y * this->inpWidth + x] > 0.5)
 | 
			
		||||
            {
 | 
			
		||||
                outimg.at<Vec3b>(i, j)[0] = 255;
 | 
			
		||||
                outimg.at<Vec3b>(i, j)[1] = 0;
 | 
			
		||||
                outimg.at<Vec3b>(i, j)[2] = 0;
 | 
			
		||||
                if (!flg && j >= frame.cols / 2 && right == -1) { // 记录图像右半部分最靠左的车道线的左边缘坐标
 | 
			
		||||
                    right = j;
 | 
			
		||||
                }
 | 
			
		||||
                flg = true;
 | 
			
		||||
            } else {
 | 
			
		||||
                if (flg && j - 1 < frame.cols / 2) { //记录图像左半部分最靠右的车道线的右边缘坐标
 | 
			
		||||
                    left = j - 1;
 | 
			
		||||
                }
 | 
			
		||||
                flg = false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (min_y == -1 && (left != -1 || right != -1)) {
 | 
			
		||||
            min_y = i;
 | 
			
		||||
        }
 | 
			
		||||
        if (left != -1){
 | 
			
		||||
            points_L.push_back(Point2f(left, i));
 | 
			
		||||
        }
 | 
			
		||||
        if (right != -1){
 | 
			
		||||
            points_R.push_back(Point2f(right, i));
 | 
			
		||||
        }
 | 
			
		||||
        //若左右参考车道线均存在,计算并标记中心点
 | 
			
		||||
        if (left > -1 && right > -1) {
 | 
			
		||||
            int mid = (left + right) / 2;
 | 
			
		||||
            for (int k = -5; k <= 5; k++) {
 | 
			
		||||
                outimg.at<Vec3b>(i, mid+k)[0] = 255;
 | 
			
		||||
                outimg.at<Vec3b>(i, mid+k)[1] = 255;
 | 
			
		||||
                outimg.at<Vec3b>(i, mid+k)[2] = 0;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
//(需要考虑的问题 1.双车道3条线 2.拐角处曲线 3.近处显示不全 4.两条线粘连)
 | 
			
		||||
    }
 | 
			
		||||
    //备选方案,对左右车道线分别拟合直线并计算中心线解析式 泛化 鲁棒 (目前有bug
 | 
			
		||||
    if (points_L.size() && points_R.size()) {
 | 
			
		||||
        Vec4f line_L, line_R;
 | 
			
		||||
        float kL, bL, kR, bR, kM, bM; // x=ky+b
 | 
			
		||||
        fitLine(points_L, line_L, DIST_WELSCH, 0, 0.01, 0.01);
 | 
			
		||||
        fitLine(points_R, line_R, DIST_WELSCH, 0, 0.01, 0.01);
 | 
			
		||||
        kL = line_L[0] / line_L[1];
 | 
			
		||||
        bL = line_L[2] - kL * line_L[3];
 | 
			
		||||
        kR = line_R[0] / line_R[1];
 | 
			
		||||
        bR = line_R[2] - kR * line_R[3];
 | 
			
		||||
        kM = (kL + kR) / 2;
 | 
			
		||||
        bM = (bL + bR) / 2;
 | 
			
		||||
        for (int i = min_y; i < frame.rows; i++) {
 | 
			
		||||
            int mid = round(kM * i + bM);
 | 
			
		||||
            for (int k = -5; k <= 5; k++) {
 | 
			
		||||
                outimg.at<Vec3b>(i, mid+k)[0] = 255;
 | 
			
		||||
                outimg.at<Vec3b>(i, mid+k)[1] = 0;
 | 
			
		||||
                outimg.at<Vec3b>(i, mid+k)[2] = 255;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return outimg;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										62
									
								
								YOLOPv2.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								YOLOPv2.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,62 @@
 | 
			
		||||
#ifndef YOLOPV2_H
 | 
			
		||||
#define YOLOPV2_H
 | 
			
		||||
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <opencv2/imgproc.hpp>
 | 
			
		||||
#include <opencv2/highgui.hpp>
 | 
			
		||||
#include <onnxruntime_cxx_api.h>
 | 
			
		||||
 | 
			
		||||
using namespace cv;
 | 
			
		||||
using namespace Ort;
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
struct Net_config
 | 
			
		||||
{
 | 
			
		||||
    float confThreshold; // Confidence threshold
 | 
			
		||||
    float nmsThreshold;  // Non-maximum suppression threshold
 | 
			
		||||
    string modelpath;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct BoxInfo
 | 
			
		||||
{
 | 
			
		||||
    float x1;
 | 
			
		||||
    float y1;
 | 
			
		||||
    float x2;
 | 
			
		||||
    float y2;
 | 
			
		||||
    float score;
 | 
			
		||||
    int label;
 | 
			
		||||
} BoxInfo;
 | 
			
		||||
 | 
			
		||||
class YOLOPv2
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    YOLOPv2(Net_config config);
 | 
			
		||||
    Mat detect(Mat& frame);
 | 
			
		||||
private:
 | 
			
		||||
    int inpWidth;
 | 
			
		||||
    int inpHeight;
 | 
			
		||||
    int nout;
 | 
			
		||||
    int num_proposal;
 | 
			
		||||
    vector<string> class_names;
 | 
			
		||||
    int num_class;
 | 
			
		||||
 | 
			
		||||
    float confThreshold;
 | 
			
		||||
    float nmsThreshold;
 | 
			
		||||
    vector<float> input_image_;
 | 
			
		||||
    void normalize_(Mat img);
 | 
			
		||||
    void nms(vector<BoxInfo>& input_boxes);
 | 
			
		||||
    const float anchors[3][6] = { {12, 16, 19, 36, 40, 28}, {36, 75, 76, 55, 72, 146},{142, 110, 192, 243, 459, 401} };
 | 
			
		||||
    const float stride[3] = { 8.0, 16.0, 32.0 };
 | 
			
		||||
 | 
			
		||||
    Env env = Env(ORT_LOGGING_LEVEL_ERROR, "YOLOPv2");
 | 
			
		||||
    Ort::Session *ort_session = nullptr;
 | 
			
		||||
    SessionOptions sessionOptions = SessionOptions();
 | 
			
		||||
    vector<char*> input_names;
 | 
			
		||||
    vector<char*> output_names;
 | 
			
		||||
    vector<vector<int64_t>> input_node_dims; // >=1 outputs
 | 
			
		||||
    vector<vector<int64_t>> output_node_dims; // >=1 outputs
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // YOLOPV2_H
 | 
			
		||||
							
								
								
									
										61
									
								
								build/ui_mainwindow.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								build/ui_mainwindow.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,61 @@
 | 
			
		||||
/********************************************************************************
 | 
			
		||||
** Form generated from reading UI file 'mainwindow.ui'
 | 
			
		||||
**
 | 
			
		||||
** Created by: Qt User Interface Compiler version 5.15.3
 | 
			
		||||
**
 | 
			
		||||
** WARNING! All changes made in this file will be lost when recompiling UI file!
 | 
			
		||||
********************************************************************************/
 | 
			
		||||
 | 
			
		||||
#ifndef UI_MAINWINDOW_H
 | 
			
		||||
#define UI_MAINWINDOW_H
 | 
			
		||||
 | 
			
		||||
#include <QtCore/QVariant>
 | 
			
		||||
#include <QtWidgets/QApplication>
 | 
			
		||||
#include <QtWidgets/QMainWindow>
 | 
			
		||||
#include <QtWidgets/QMenuBar>
 | 
			
		||||
#include <QtWidgets/QStatusBar>
 | 
			
		||||
#include <QtWidgets/QWidget>
 | 
			
		||||
 | 
			
		||||
QT_BEGIN_NAMESPACE
 | 
			
		||||
 | 
			
		||||
class Ui_MainWindow
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    QWidget *centralwidget;
 | 
			
		||||
    QMenuBar *menubar;
 | 
			
		||||
    QStatusBar *statusbar;
 | 
			
		||||
 | 
			
		||||
    void setupUi(QMainWindow *MainWindow)
 | 
			
		||||
    {
 | 
			
		||||
        if (MainWindow->objectName().isEmpty())
 | 
			
		||||
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
 | 
			
		||||
        MainWindow->resize(800, 600);
 | 
			
		||||
        centralwidget = new QWidget(MainWindow);
 | 
			
		||||
        centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
 | 
			
		||||
        MainWindow->setCentralWidget(centralwidget);
 | 
			
		||||
        menubar = new QMenuBar(MainWindow);
 | 
			
		||||
        menubar->setObjectName(QString::fromUtf8("menubar"));
 | 
			
		||||
        MainWindow->setMenuBar(menubar);
 | 
			
		||||
        statusbar = new QStatusBar(MainWindow);
 | 
			
		||||
        statusbar->setObjectName(QString::fromUtf8("statusbar"));
 | 
			
		||||
        MainWindow->setStatusBar(statusbar);
 | 
			
		||||
 | 
			
		||||
        retranslateUi(MainWindow);
 | 
			
		||||
 | 
			
		||||
        QMetaObject::connectSlotsByName(MainWindow);
 | 
			
		||||
    } // setupUi
 | 
			
		||||
 | 
			
		||||
    void retranslateUi(QMainWindow *MainWindow)
 | 
			
		||||
    {
 | 
			
		||||
        MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr));
 | 
			
		||||
    } // retranslateUi
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
namespace Ui {
 | 
			
		||||
    class MainWindow: public Ui_MainWindow {};
 | 
			
		||||
} // namespace Ui
 | 
			
		||||
 | 
			
		||||
QT_END_NAMESPACE
 | 
			
		||||
 | 
			
		||||
#endif // UI_MAINWINDOW_H
 | 
			
		||||
							
								
								
									
										51
									
								
								fast-yolopv2.pro
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								fast-yolopv2.pro
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
			
		||||
QT       += core gui widgets
 | 
			
		||||
 | 
			
		||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 | 
			
		||||
 | 
			
		||||
CONFIG      += c++11
 | 
			
		||||
DEFINES     += OPENCV
 | 
			
		||||
DEFINES     += GPU
 | 
			
		||||
DEFINES     += CUDNN
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# You can make your code fail to compile if it uses deprecated APIs.
 | 
			
		||||
# In order to do so, uncomment the following line.
 | 
			
		||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
 | 
			
		||||
 | 
			
		||||
# 包含 OpenCV 的头文件路径
 | 
			
		||||
INCLUDEPATH += /usr/local/Opencv-4.10.0/include/opencv4
 | 
			
		||||
# 包含 Cuda 的头文件路径
 | 
			
		||||
INCLUDEPATH += /usr/local/cuda-12.6/include
 | 
			
		||||
# 包含 onnx 的头文件路径
 | 
			
		||||
INCLUDEPATH += /usr/local/include/onnxruntime
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# 链接到 OpenCV 的库文件
 | 
			
		||||
LIBS        += -L/usr/local/Opencv-4.10.0/lib \
 | 
			
		||||
               -lopencv_core \
 | 
			
		||||
               -lopencv_imgproc \
 | 
			
		||||
               -lopencv_highgui \
 | 
			
		||||
               -lopencv_imgcodecs \
 | 
			
		||||
               -lopencv_videoio
 | 
			
		||||
 | 
			
		||||
# 链接到 onnx 的库文件
 | 
			
		||||
LIBS        += -L/usr/local/lib  -lonnxruntime
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
SOURCES += \
 | 
			
		||||
    YOLOPv2.cpp \
 | 
			
		||||
    main.cpp \
 | 
			
		||||
    mainwindow.cpp
 | 
			
		||||
 | 
			
		||||
HEADERS += \
 | 
			
		||||
    YOLOPv2.h \
 | 
			
		||||
    mainwindow.h
 | 
			
		||||
 | 
			
		||||
FORMS += \
 | 
			
		||||
    mainwindow.ui
 | 
			
		||||
 | 
			
		||||
# Default rules for deployment.
 | 
			
		||||
qnx: target.path = /tmp/$${TARGET}/bin
 | 
			
		||||
else: unix:!android: target.path = /opt/$${TARGET}/bin
 | 
			
		||||
!isEmpty(target.path): INSTALLS += target
 | 
			
		||||
							
								
								
									
										11
									
								
								main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								main.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
#include "mainwindow.h"
 | 
			
		||||
 | 
			
		||||
#include <QApplication>
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
    QApplication a(argc, argv);
 | 
			
		||||
    MainWindow w;
 | 
			
		||||
    w.show();
 | 
			
		||||
    return a.exec();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										201
									
								
								mainwindow.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										201
									
								
								mainwindow.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,201 @@
 | 
			
		||||
#include "mainwindow.h"
 | 
			
		||||
#include "ui_mainwindow.h"
 | 
			
		||||
#include "YOLOPv2.h"
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <QFile>
 | 
			
		||||
#include <QLoggingCategory>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
string gstreamer_pipeline (int capture_width, int capture_height, int display_width, int display_height, int framerate, int flip_method)
 | 
			
		||||
{
 | 
			
		||||
    return "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)" + to_string(capture_width) + ", height=(int)" +
 | 
			
		||||
           to_string(capture_height) + ", format=(string)NV12, framerate=(fraction)" + to_string(framerate) +
 | 
			
		||||
           "/1 ! nvvidconv flip-method=" + to_string(flip_method) + " ! video/x-raw, width=(int)" + to_string(display_width) + ", height=(int)" +
 | 
			
		||||
           to_string(display_height) + ", format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
MainWindow::MainWindow(QWidget *parent)
 | 
			
		||||
    : QMainWindow(parent)
 | 
			
		||||
    , ui(new Ui::MainWindow)
 | 
			
		||||
{
 | 
			
		||||
    ui->setupUi(this);
 | 
			
		||||
    //ShowImage();
 | 
			
		||||
    ShowVideo();
 | 
			
		||||
    //OpenCSICamera();
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
MainWindow::~MainWindow()
 | 
			
		||||
{
 | 
			
		||||
    delete ui;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void MainWindow::ShowImage()
 | 
			
		||||
{
 | 
			
		||||
    Net_config YOLOPv2_nets = { 0.5, 0.5, "/home/wuxianfu/Projects/Fast-YolopV2/build/onnx/yolopv2_192x320.onnx" };   ////choices = onnx文件夹里的文件
 | 
			
		||||
    YOLOPv2 net(YOLOPv2_nets);
 | 
			
		||||
    string imgpath = "/home/wuxianfu/Projects/Fast-YolopV2/build/images/3c0e7240-96e390d2.jpg";
 | 
			
		||||
    static const string kWinName = "Deep learning object detection in ONNXRuntime";
 | 
			
		||||
    namedWindow(kWinName, WINDOW_NORMAL);
 | 
			
		||||
 | 
			
		||||
    Mat srcimg = imread(imgpath);
 | 
			
		||||
    imshow(kWinName, srcimg);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    Mat outimg = net.detect(srcimg);
 | 
			
		||||
 | 
			
		||||
    imshow(kWinName, outimg);
 | 
			
		||||
    waitKey(0);
 | 
			
		||||
    destroyAllWindows();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MainWindow::ShowVideo()
 | 
			
		||||
{
 | 
			
		||||
    Net_config YOLOPv2_nets = { 0.5, 0.5, "/home/wuxianfu/Projects/Fast-YolopV2/build/onnx/yolopv2_736x1280.onnx" };   ////choices = onnx文件夹里的文件
 | 
			
		||||
    YOLOPv2 net(YOLOPv2_nets);
 | 
			
		||||
    static const string kWinName = "Deep learning object detection in ONNXRuntime";
 | 
			
		||||
    namedWindow(kWinName, WINDOW_NORMAL);
 | 
			
		||||
 | 
			
		||||
    cv::VideoCapture cap("/home/wuxianfu/Projects/Fast-YolopV2/build/videos/566a351c29c00924a337e91e85fa7dec.mp4");
 | 
			
		||||
    if (!cap.isOpened()) {
 | 
			
		||||
        std::cerr << "Error opening video stream" << std::endl;
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cv::Mat frame;
 | 
			
		||||
    while (true) {
 | 
			
		||||
        cap >> frame; // 读取一帧
 | 
			
		||||
        if (frame.empty()) {
 | 
			
		||||
            std::cerr << "Error reading frame" << std::endl;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        auto start = std::chrono::steady_clock::now();
 | 
			
		||||
        Mat outimg = net.detect(frame);
 | 
			
		||||
        auto end = std::chrono::steady_clock::now();
 | 
			
		||||
        std::chrono::duration<double> spent = end - start;
 | 
			
		||||
        qDebug()<< " Time: " << spent.count() << " sec \n";
 | 
			
		||||
 | 
			
		||||
        imshow(kWinName, outimg);
 | 
			
		||||
        if (cv::waitKey(5) >= 0) break; // 按任意键退出循环
 | 
			
		||||
    }
 | 
			
		||||
    cap.release(); // 释放资源
 | 
			
		||||
    cv::destroyAllWindows(); // 关闭所有OpenCV窗口
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void MainWindow::OpenUSBCamera() {
 | 
			
		||||
 | 
			
		||||
    Net_config YOLOPv2_nets = { 0.5, 0.5, "/home/wuxianfu/Projects/Fast-YolopV2/build/onnx/yolopv2_192x320.onnx" };   ////choices = onnx文件夹里的文件
 | 
			
		||||
    YOLOPv2 net(YOLOPv2_nets);
 | 
			
		||||
    cv::VideoCapture cap(1); // 使用默认的摄像头索引(通常是0)
 | 
			
		||||
    if (!cap.isOpened()) {
 | 
			
		||||
        std::cerr << "Error opening video stream" << std::endl;
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cv::Mat frame;
 | 
			
		||||
    while (true) {
 | 
			
		||||
        cap >> frame; // 读取一帧
 | 
			
		||||
        if (frame.empty()) {
 | 
			
		||||
            std::cerr << "Error reading frame" << std::endl;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        Mat outimg = net.detect(frame);
 | 
			
		||||
        cv::imshow("USB Camera", outimg); // 显示帧
 | 
			
		||||
        if (cv::waitKey(10) >= 0) break; // 按任意键退出循环
 | 
			
		||||
    }
 | 
			
		||||
    cap.release(); // 释放资源
 | 
			
		||||
    cv::destroyAllWindows(); // 关闭所有OpenCV窗口
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MainWindow::OpenCSICamera() {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    Net_config YOLOPv2_nets = { 0.5, 0.5, "/home/wuxianfu/Projects/Fast-YolopV2/build/onnx/yolopv2_736x1280.onnx" };   ////choices = onnx文件夹里的文件
 | 
			
		||||
    YOLOPv2 net(YOLOPv2_nets);
 | 
			
		||||
    string imgpath = "/home/wuxianfu/Projects/Fast-YolopV2/build/images/0ace96c3-48481887.jpg";
 | 
			
		||||
    Mat srcimg = imread(imgpath);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    int capture_width  = 3280 ;
 | 
			
		||||
    int capture_height = 1848 ;
 | 
			
		||||
    int display_width  = 3280 ;
 | 
			
		||||
    int display_height = 1848 ;
 | 
			
		||||
 | 
			
		||||
    //3280*2464最大支持21帧
 | 
			
		||||
    //3280*1848最大支持28帧
 | 
			
		||||
    //1920*1080最大支持29帧
 | 
			
		||||
    //1640*1232最大支持29帧
 | 
			
		||||
    //1280*720 最大支持59帧
 | 
			
		||||
    int framerate      = 10;
 | 
			
		||||
    int flip_method    = 0;
 | 
			
		||||
 | 
			
		||||
    //创建管道
 | 
			
		||||
    string pipeline = gstreamer_pipeline(capture_width,
 | 
			
		||||
    capture_height,
 | 
			
		||||
    display_width,
 | 
			
		||||
    display_height,
 | 
			
		||||
    framerate,
 | 
			
		||||
    flip_method);
 | 
			
		||||
    std::cout << "使用gstreamer管道: \n\t" << pipeline << "\n";
 | 
			
		||||
 | 
			
		||||
    //管道与视频流绑定
 | 
			
		||||
    VideoCapture cap(pipeline, CAP_GSTREAMER);
 | 
			
		||||
    if(!cap.isOpened())
 | 
			
		||||
    {
 | 
			
		||||
        std::cout<<"打开摄像头失败."<<std::endl;
 | 
			
		||||
        return ;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qDebug()<<"打开摄像头成功.";
 | 
			
		||||
 | 
			
		||||
    //创建显示窗口
 | 
			
		||||
    namedWindow("CSI Camera", WINDOW_AUTOSIZE);
 | 
			
		||||
    Mat img;
 | 
			
		||||
 | 
			
		||||
    //逐帧显示
 | 
			
		||||
    while(true)
 | 
			
		||||
    {
 | 
			
		||||
        qDebug()<<"开始捕获摄像头.";
 | 
			
		||||
        auto start = std::chrono::steady_clock::now();
 | 
			
		||||
        if (!cap.read(img))
 | 
			
		||||
        {
 | 
			
		||||
            std::cout<<"捕获失败"<<std::endl;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        int new_width,new_height,width,height;
 | 
			
		||||
        width=img.cols;
 | 
			
		||||
        height=img.rows;
 | 
			
		||||
 | 
			
		||||
        //调整图像大小
 | 
			
		||||
        new_width=1000;
 | 
			
		||||
        if(width>800)
 | 
			
		||||
        {
 | 
			
		||||
            new_height=int(new_width*1.0/width*height);
 | 
			
		||||
        }
 | 
			
		||||
        cv::resize(img, img, cv::Size(new_width, new_height));
 | 
			
		||||
 | 
			
		||||
        Mat outimg = net.detect(img);
 | 
			
		||||
        auto end = std::chrono::steady_clock::now();
 | 
			
		||||
        std::chrono::duration<double> spent = end - start;
 | 
			
		||||
        qDebug()<< " Time: " << spent.count() << " sec \n";
 | 
			
		||||
 | 
			
		||||
        imshow("CSI Camera",outimg);
 | 
			
		||||
 | 
			
		||||
        int keycode = cv::waitKey(2) & 0xff ; //ESC键退出
 | 
			
		||||
        if (keycode == 27) break ;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cap.release();
 | 
			
		||||
    destroyAllWindows() ;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										32
									
								
								mainwindow.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								mainwindow.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
#ifndef MAINWINDOW_H
 | 
			
		||||
#define MAINWINDOW_H
 | 
			
		||||
 | 
			
		||||
#include <QMainWindow>
 | 
			
		||||
 | 
			
		||||
QT_BEGIN_NAMESPACE
 | 
			
		||||
namespace Ui { class MainWindow; }
 | 
			
		||||
QT_END_NAMESPACE
 | 
			
		||||
 | 
			
		||||
class Detector;
 | 
			
		||||
 | 
			
		||||
class MainWindow : public QMainWindow
 | 
			
		||||
{
 | 
			
		||||
    Q_OBJECT
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    MainWindow(QWidget *parent = nullptr);
 | 
			
		||||
    ~MainWindow();
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    void OpenUSBCamera();
 | 
			
		||||
    void OpenCSICamera();
 | 
			
		||||
    void ShowImage();
 | 
			
		||||
    void ShowVideo();
 | 
			
		||||
 | 
			
		||||
    std::vector<std::string>  m_vObjects_Names;  //检测目标名称
 | 
			
		||||
    Detector *m_pDetector;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    Ui::MainWindow *ui;
 | 
			
		||||
};
 | 
			
		||||
#endif // MAINWINDOW_H
 | 
			
		||||
							
								
								
									
										22
									
								
								mainwindow.ui
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								mainwindow.ui
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<ui version="4.0">
 | 
			
		||||
 <class>MainWindow</class>
 | 
			
		||||
 <widget class="QMainWindow" name="MainWindow">
 | 
			
		||||
  <property name="geometry">
 | 
			
		||||
   <rect>
 | 
			
		||||
    <x>0</x>
 | 
			
		||||
    <y>0</y>
 | 
			
		||||
    <width>800</width>
 | 
			
		||||
    <height>600</height>
 | 
			
		||||
   </rect>
 | 
			
		||||
  </property>
 | 
			
		||||
  <property name="windowTitle">
 | 
			
		||||
   <string>MainWindow</string>
 | 
			
		||||
  </property>
 | 
			
		||||
  <widget class="QWidget" name="centralwidget"/>
 | 
			
		||||
  <widget class="QMenuBar" name="menubar"/>
 | 
			
		||||
  <widget class="QStatusBar" name="statusbar"/>
 | 
			
		||||
 </widget>
 | 
			
		||||
 <resources/>
 | 
			
		||||
 <connections/>
 | 
			
		||||
</ui>
 | 
			
		||||
		Посилання в новій задачі
	
	Block a user