VS+Yolov3+QT+opencv(GPU版)

环境: 系统:win10 (显卡:NVIDIA GTX 950M) Yolo版本:V3 cuda:9.0 cudnn:9.0 opencv:3.4.9 VS:2015

1、环境准备:

1.1、下载Yolov3(darknet)

下载链接:https://github.com/AlexeyAB/darknet VS+Yolov3+QT+opencv(GPU版)

1.2、下载权重文件(yolov3.weights)

下载链接:https://link.csdn.net/?target=https%3A%2F%2Fpjreddie.com%2Fmedia%2Ffiles%2Fyolov3.weights 下载完成后,将yolov3.weights文件复制到E:\yolo\darknet-master\darknet-master\build\darknet\x64目录下。

1.3、下载CUDA 9.0

下载链接:链接:https://pan.baidu.com/s/1LLMmFVOCSLvaY4GzTuVYcg 提取码:kkvb 安装过程网上搜教程就行了,挺简单的(PS:CUDA版本需根据自己电脑配置选择,我电脑显卡是:NVIDIA GTX 950M的,一开始我装CUDA10.0和11.0都不行,后来改为9.0可以了,也是一波三折)

1.4、下载CUDNN 9.0

下载链接:链接:https://pan.baidu.com/s/1d-yigqIoy9q6Ha5-fdJkSA 提取码:74kw VS+Yolov3+QT+opencv(GPU版) 将里面3个文件夹里的每个文件复制到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0目录下对应的文件夹中即可。 VS+Yolov3+QT+opencv(GPU版)

1.4、opencv3.4.9和VS2015

网上搜索安装即可并将opencv配置到VS2015中

2、编译Yolo

2.1、编译darknet.exe

2.1.1、找到darknet.vcxproj用EditPlus打开CTRL+F搜索CUDA 11.1全部改为9.0然后保存。共2处

VS+Yolov3+QT+opencv(GPU版)   VS+Yolov3+QT+opencv(GPU版)

2.1.2、打开darknet.sln

修改为Release X64模式,然后生成即可。

3、运行yolov3

双击darknet_yolo_v3.cmd识别图片实例;而darknet_web_cam_voc.cmd是打开笔记本摄像头动态检测识别。 VS+Yolov3+QT+opencv(GPU版)

4、编译Yolov3动态链接库

打开yolo_cpp_dll.sln在Release X64模式下生成(也要修改yolo_cpp_dll.vcxproj中的cuda版本) VS+Yolov3+QT+opencv(GPU版) 会生成yolo_cpp_dll.dll和yolo_cpp_dll.lib的库文件 VS+Yolov3+QT+opencv(GPU版)

5、使用yolo的动态链接库进行开发

VS+Yolov3+QT+opencv(GPU版) 使用yolo编译好的yolo_cpp_dll.dll和yolo_cpp_dll.lib的库文件和API:yolo_v2_class.hpp头文件 mainwindow.h
#pragma once
//这段代码一定要加,这是在yolo_v2_class.hpp文件中使用opencv函数
#ifdef _WIN32
#define OPENCV
#define GPU
#endif

#include <QtWidgets/QMainWindow>
#include "ui_mianwindow.h"
#include "yolo_v2_class.hpp"
#include "opencv\highgui.h"
#include "opencv2\opencv.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include <iostream>
#include <stdio.h>

#pragma comment(lib, "yolo_cpp_dll.lib")//引入yolo链接库
#pragma execution_character_set("utf-8")
using namespace cv;

class mianwindow : public QMainWindow
{
    Q_OBJECT

public:
    mianwindow(QWidget *parent = Q_NULLPTR);

    void draw_boxes(Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std::string> obj_names);

    std::vector<std::string> objects_names_from_file(std::string const filename);
    QImage cvMat2QImage(const Mat& mat);
    Mat QImage2cvMat(QImage image);


private:
    Ui::mianwindowClass ui;
};

mainwindow.cpp

#include "mianwindow.h"
#include "yolo_v2_class.hpp"
#include <QDebug>
mianwindow::mianwindow(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    QImage q_image(QString("E:/TX_workCode/DataInfo/images/dog.jpg"));

    ui.label_img->setPixmap(QPixmap::fromImage(q_image));
    
    std::string names_file = "coco.names";
    std::string cfg_file = "yolov3.cfg";
    std::string weights_file = "yolov3.weights";
    Detector detector(cfg_file, weights_file); //初始化Detector

    std::vector<std::string> obj_names;
    obj_names = objects_names_from_file(names_file);
    //测试是否成功读入分类对象文件
    for (int i = 0; i < obj_names.size(); i++)
    {
        qDebug() << "第i个:" << QString(QString::fromLocal8Bit(obj_names[i].c_str()));
    }
    
    Mat frame = QImage2cvMat(q_image);
    std::vector<bbox_t> result_vec = detector.detect(frame);
    draw_boxes(frame, result_vec, obj_names);

    QImage det_image = cvMat2QImage(frame);
    ui.label_detectorImg->setPixmap(QPixmap::fromImage(det_image));

}
//以下draw_boxes和objects_names_from_file两段代码来自yolo_console_dll.sln
//给Mat类型的图像画出检测出对象框
void mianwindow::draw_boxes(cv::Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std::string> obj_names)
{
    int const colors[6][3] = { { 1,0,1 },{ 0,0,1 },{ 0,1,1 },{ 0,1,0 },{ 1,1,0 },{ 1,0,0 } };
    for (auto &i : result_vec) {
        cv::Scalar color = obj_id_to_color(i.obj_id);
        cv::rectangle(mat_img, cv::Rect(i.x, i.y, i.w, i.h), color, 2);
        if (obj_names.size() > i.obj_id) {
            std::string obj_name = obj_names[i.obj_id];
            if (i.track_id > 0) obj_name += " - " + std::to_string(i.track_id);
            cv::Size const text_size = getTextSize(obj_name, cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, 2, 0);
            int const max_width = (text_size.width > i.w + 2) ? text_size.width : (i.w + 2);
            cv::rectangle(mat_img, cv::Point2f(std::max((int)i.x - 1, 0), std::max((int)i.y - 30, 0)),
                cv::Point2f(std::min((int)i.x + max_width, mat_img.cols - 1), std::min((int)i.y, mat_img.rows - 1)),
                color, CV_FILLED, 8, 0);
            putText(mat_img, obj_name, cv::Point2f(i.x, i.y - 10), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, cv::Scalar(0, 0, 0), 2);
        }
    }

}
//读names文件中对象类别名称保存在vector<std::string>容器中
std::vector<std::string> mianwindow::objects_names_from_file(std::string const filename) {
    std::ifstream file(filename);
    std::vector<std::string> file_lines;
    if (!file.is_open()) return file_lines;
    for (std::string line; getline(file, line);) file_lines.push_back(line);
    std::cout << "object names loaded \n";
    return file_lines;
}

//---------QImage与Mat类型相互转换----------------------------------------------------------
QImage mianwindow::cvMat2QImage(const cv::Mat& mat)
{
    // 8-bits unsigned, NO. OF CHANNELS = 1  
    if (mat.type() == CV_8UC1)
    {
        QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
        // Set the color table (used to translate colour indexes to qRgb values)  
        image.setColorCount(256);
        for (int i = 0; i < 256; i++)
        {
            image.setColor(i, qRgb(i, i, i));
        }
        // Copy input Mat  
        uchar *pSrc = mat.data;
        for (int row = 0; row < mat.rows; row++)
        {
            uchar *pDest = image.scanLine(row);
            memcpy(pDest, pSrc, mat.cols);
            pSrc += mat.step;
        }
        return image;
    }
    // 8-bits unsigned, NO. OF CHANNELS = 3  
    else if (mat.type() == CV_8UC3)
    {
        // Copy input Mat  
        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat  
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
        return image.rgbSwapped();
    }
    else if (mat.type() == CV_8UC4)
    {
        qDebug() << "CV_8UC4";
        // Copy input Mat  
        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat  
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
        return image.copy();
    }
    else
    {
        qDebug() << "ERROR: Mat could not be converted to QImage.";
        return QImage();
    }
}
cv::Mat mianwindow::QImage2cvMat(QImage image)
{
    cv::Mat mat;
    qDebug() << image.format();
    switch (image.format())
    {
    case QImage::Format_ARGB32:
    case QImage::Format_RGB32:
    case QImage::Format_ARGB32_Premultiplied:
        mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
        break;
    case QImage::Format_RGB888:
        mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
        cv::cvtColor(mat, mat, CV_BGR2RGB);
        break;
    case QImage::Format_Indexed8:
        mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
        break;
    }
    return mat;
}

 

                   

报错:error MSB3721

VS+Yolov3+QT+opencv(GPU版) 解决办法: 问题产生的原因: sm_XX,compute_XX:表示自己电脑显卡的计算能力,显卡越好值越高,自己显卡对应的值可以查看英伟达官网   由于自己电脑显卡有好有坏,对应的sm_XX,compute_XX值也不一样,出现上述错误的原因主要是当前运行的程序里的sm_XX,compute_XX值选取不当。如下图是我自己程序中设置sm_XX,compute_XX的值。(我将sm_30改为了sm_35可以) VS+Yolov3+QT+opencv(GPU版)     参考链接: 1、https://blog.csdn.net/lixudem/article/details/106993430?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-6.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-6.control 2、https://blog.csdn.net/sinat_41852207/article/details/90906309?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-5.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-5.control 3、https://blog.csdn.net/alansss/article/details/104448347?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161095690516780271532753%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=161095690516780271532753&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-3-104448347.first_rank_v2_pc_rank_v29&utm_term=win10%E7%BC%96%E8%AF%91yolov3%E6%97%B6%E6%8A%A5%E9%94%99error%20MSB3721&spm=1018.2226.3001.4187 4、https://blog.csdn.net/jin739738709/article/details/90342696 5、https://blog.csdn.net/stjuliet/article/details/87884976?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161102361916780269894467%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=161102361916780269894467&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-3-87884976.first_rank_v2_pc_rank_v29&utm_term=%E4%BD%BF%E7%94%A8yolo%E7%9A%84%E5%8A%A8%E6%80%81%E9%93%BE%E6%8E%A5%E5%BA%93&spm=1018.2226.3001.4187

上一篇:python 读取Excel数据


下一篇:数据分析作业——Numpy(第4次作业)