【目標檢測】Windows版本Darknet編譯+darknet_no_gpu.exe編譯與測試+動態鏈接庫yolo_cpp_dll編譯+測試實例+QT封裝

測試一下YOLO Darknet C++接口,後面可以做基於darknet的目標檢測框架

源碼下載地址:https://github.com/AlexeyAB/darknet

作者的QT封裝版本地址:暫時不發佈(有需要可以評論聯繫博主)

作者yolo_cpp_dll測試源碼:見下文(完整源碼可以評論聯繫博主)

一、編譯運行darknet_no_gpu.sln(不需要的,可以直接看第二部分)

1.下載後解壓,就得到下圖中的文件

2.點擊加入build->darknet目錄下

3.在上面的目錄下加入opencv工程屬性表Opencv3_4_0.props

4.打開darknet_no_gpu.sln,先配置好Opencv環境(bin,include,lib),這裏我就直接添加屬性表Opencv3_4_0.props

5.重新生成解決方案

然後就編譯成功了,其實是沒什麼困難的到這裏

在x64文件夾下可以找到編譯好的darknet_no_gpu.exe,接下來就是測試啦!

6.測試darknet_no_gpu.exe

darknet_no_gpu.exe的執行指令有很多,這裏參考https://blog.csdn.net/u011609063/article/details/84477500

單張圖片檢測的通用模板:
darknet_no_gpu.exe detector test data_file_path_and_file cfg_file_path_and_name weights_file_path_and_name
[-i int_number] [-thresh thresh_value] photo_path_and_name [-ext_output]

舉例: 
darknet_no_gpu.exe detector test data/coco.data cfg/yolov3.cfg yolov3.weights -i 0 -thresh 0.25 data/dog.jpg -ext_output


視頻檢測的通用模板
darknet_no_gpu.exe detector demo data_file_path_and_name cfg_file_path_and_name weights_file_path_and_name video_name_path_and_file [-i int_number] [-out_filename output_path_and_name]

舉例:

【不保存視頻】darknet_no_gpu.exe detector demo data/coco.data cfg/yolov3.cfg yolov3.weights data/test.mp4 -i 0

【保存視頻】darknet_no_gpu.exe detector demo data/coco.data cfg/yolov3.cfg yolov3.weights data/test.mp4 -i 0 -out_filename res.MP4


如果你只是想跑個例子,做一下測試的話,到這裏也就結束

但是我想要應用它,讓它成爲我工程中的一個接口,那麼我就需要生成動態鏈接庫yolo_cpp_dll

 

二、編譯yolo_cpp_dll,並測試

1.我編譯的是cpu版本的,所以第一步是打開yolo_cpp_dll_no_gpu.sln

修改一下,Release版本是會生成yolo_cpp_dll.dll,

Debug版本是會生成yolo_cpp_dll_no_gpu.dll,

2.測試yolo_cpp_dll.dll

這裏參考https://blog.csdn.net/weixinhum/article/details/81475548

新建工程test_yolo_cpp_dll.sln

將下圖中這些文件添加到工程目錄下

#include <iostream>

#ifdef _WIN32
#define OPENCV
#define GPU
#endif

#include "yolo_v2_class.hpp"	// imported functions from DLL
#include <opencv2/opencv.hpp>	// C++
#include "opencv2/highgui/highgui.hpp"  

#pragma comment(lib, "yolo_cpp_dll.lib")//引入鏈接庫

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);
		}
	}
}


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;
}

int main()
{
	std::string  names_file = "coco.names";
	std::string  cfg_file = "yolov3.cfg";
	std::string  weights_file = "yolov3.weights";
	Detector detector(cfg_file, weights_file);//初始化檢測器
	auto obj_names = objects_names_from_file(names_file);//獲得分類對象名稱

	cv::VideoCapture capture;
	capture.open("test.mp4");
	if (!capture.isOpened())
	{
		printf("文件打開失敗");
	}
	//獲取整個幀數  
	long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
	cv::Mat frame;
	for (size_t i = 0; i < totalFrameNumber; i++)
	{
		capture >> frame;
		std::vector<bbox_t> result_vec = detector.detect(frame);
		draw_boxes(frame, result_vec, obj_names);
		cv::imshow("window name1", frame);
		cv::waitKey(3);
	}
	return 0;
}

 

可能報錯:

嚴重性    代碼    說明    項目    文件    行    禁止顯示狀態
錯誤    C4996    'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    test_yolo_cpp_dll    e:\hellodarknet\test_yolo_cpp_dll\test_yolo_cpp_dll\yolo_v2_class.hpp    190    


解決方法:

 添加預處理指令:_CRT_SECURE_NO_WARNINGS

 

 

後面我還自己用qt封裝出操作界面

有需要的,可以私信博主

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章