Windows下QT调用YOLO-V3(Darknet)

本文档介绍了在Windows系统中,使用CUDA8.0、CUDNN6.0、VS13和QT5.3.0环境,如何将YOLO-V3模型与Darknet编译为动态库,并在QT工程中进行调用实现图片检测。详细步骤包括新建QT工程、配置环境、添加代码以及展示检测结果。通过这个过程,旨在理解QT的工作原理并将YOLO-V3模型工程化,以便后续实际应用。

环境:Windows、CUDA8.0、CUDNN6.0、VS13、QT5.3.0

概述:

1.YOLO-V3:根据Darknet编译得到对应的yolo_cpp_dll.lib和yolo_cpp_dll.dll

     C++ YOLOV3工程化下载地址:

https://download.youkuaiyun.com/download/yangjayhui/11688131

2.了解学习QT5;

3.完成QT下调用YOLO-V3,工程化便于后期应用;

步骤:

一.在VS13下新建QT工程,整个解决方案如下:

UI界面如下:

二.配置相应的环境:

将所需的dll库放入Release下:

三.配置好环境直接上代码:

(1)在Detection.h中添加以下代码:

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_Detection.h"

class Detection : public QMainWindow
{
	Q_OBJECT

public:
	Detection(QWidget *parent = Q_NULLPTR);
	int flag = 0;
private slots :
	void demo();
	
private:
	Ui::DetectionClass ui;
};

(2)yolo_v2_class.hpp可直接复制Darknet下的,没有做任何改动;

(3)在Detection.cpp中添加以下代码:
     

#include "Detection.h"

#include <qfiledialog.h>

#include <iostream>
#include <fstream>
#include <windows.h>

#define OPENCV
#define GPU

#include "yolo_v2_class.hpp" //引用动态链接库中的头文件
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
using namespace std;



Detection::Detection(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(demo()));
}


void draw_boxes(cv::Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std::string> obj_names,
	int current_det_fps = -1, int current_cap_fps = -1)
{
	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);
		}
	}
	if (current_det_fps >= 0 && current_cap_fps >= 0) {
		std::string fps_str = "FPS detection: " + std::to_string(current_det_fps) + "   FPS capture: " + std::to_string(current_cap_fps);
		putText(mat_img, fps_str, cv::Point2f(10, 20), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, cv::Scalar(50, 255, 0), 2);
	}
}

std::vector<std::string> 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;
}



void Detection::demo()
{
	
	std::string names_file = "D://darknet//yolodemo//cfg//surface//surface.names";
	std::string cfg_file = "D://darknet//yolodemo//cfg//surface//yolov3-voc-surface.cfg";
	std::string weights_file = "D://darknet//yolodemo//cfg//surface//yolov3-voc-surface_24000.weights";
	Detector detector(cfg_file, weights_file, 1); //初始化检测器

	
	std::vector<std::string> obj_names;
	std::ifstream ifs(names_file.c_str());
	std::string line;
	while (getline(ifs, line)) obj_names.push_back(line);

	cv::Mat frame;

	QString filename;
	filename = QFileDialog::getOpenFileName(this, tr("选择图片"), "", tr("Images(*.png *.bmp *.jpg)"));
	string str = filename.toStdString();
	frame = cv::imread(str);
	


	double t1 = GetTickCount();
	std::vector<bbox_t> result_vec = detector.detect(frame);

	for (auto &i : result_vec)
	{
		cv::Scalar color = obj_id_to_color(i.obj_id);
		cv::rectangle(frame, cv::Rect(i.x, i.y, i.w, i.h), color, 2);
	}
	double t2 = GetTickCount()-t1;
	QString timecost = QString::number(t2);
	ui.text_time->setText(timecost);

	QImage img1 = QImage((const unsigned char*)(frame.data), frame.cols, frame.rows, frame.cols*frame.channels(), QImage::Format_RGB888);
	img1 = img1.scaled(ui.label_image->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
	ui.label_image->setPixmap(QPixmap::fromImage(img1));
		
}

(4)QT界面如下:左边可显示结果图

(5)整个工程的下载地址:

https://download.youkuaiyun.com/download/yangjayhui/11688011

四.总结:

1.主要是为了了解QT的机制;

2.将YOLO-V3 工程化,便于应用;

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值