c++ 调用yolo4训练后的模型

3 篇文章 0 订阅

搭配数据

1. darknet-master\build\darknet\x64\cfg\my_yolov4.cfg(修改参数的文件)

2. darknet-master\build\darknet\x64\data\myData.names(此为你的存标签类别的文件)

3. darknet-master\build\darknet\x64\backup\my_yolov4_last.weights(训练完成后生成的模型)

 

#include <iostream>
#include <fstream>
#ifdef _WIN32
#define OPENCV
#define GPU
#endif
#include <typeinfo>
#include <sstream>
#include <opencv2/opencv.hpp>
#include "darknet/yolo_v2_class.hpp" //引用动态链接库中的头文件
#include "opencv2/highgui/highgui.hpp"
#include <fstream>

#pragma comment(lib, "yolo_cpp_dll.lib") //引入YOLO动态链接库
//#pragma comment(lib, "opencv_world340d.lib") //引入OpenCV链接库,我在vs中配置过了,如果没配置请对应修改

using namespace cv;
using namespace std;


//以下两段代码来自yolo_console_dll.sln
void draw_boxes(Mat mat_img, vector<bbox_t> result_vec, vector<string> obj_names,
	int current_det_fps = -1, int current_cap_fps = -1)
{
	char destination[10];

	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) {
		Scalar color = obj_id_to_color(i.obj_id);
		rectangle(mat_img, Rect(i.x, i.y, i.w, i.h), color, 2);
		if (obj_names.size() > i.obj_id) {
			string obj_name = obj_names[i.obj_id];
			if (i.track_id > 0) obj_name += " - " + to_string(i.track_id);
			Size const text_size = getTextSize(obj_name, FONT_HERSHEY_COMPLEX_SMALL, 1.2, 2, 0);
			int const max_width = (text_size.width > i.w + 2) ? text_size.width : (i.w + 2);
			rectangle(mat_img, Point2f(max((int)i.x - 1, 0), max((int)i.y - 30, 0)),
				Point2f(min((int)i.x + max_width, mat_img.cols - 1), min((int)i.y, mat_img.rows - 1)),
				color, -1, 8, 0);
			putText(mat_img, obj_name, Point2f(i.x, i.y - 10), FONT_HERSHEY_COMPLEX_SMALL, 1.2, Scalar(255, 255, 255), 2);
			sprintf(destination, "%.3f", i.prob);
			putText(mat_img, destination, Point2f(i.x, i.y + 20), FONT_HERSHEY_COMPLEX_SMALL, 1.5, Scalar(255, 255, 255), 2);

		}
	}
	if (current_det_fps >= 0 && current_cap_fps >= 0) {
		string fps_str = "FPS detection: " + to_string(current_det_fps) + "   FPS capture: " + to_string(current_cap_fps);
		putText(mat_img, fps_str, Point2f(10, 20), FONT_HERSHEY_COMPLEX_SMALL, 1.2, Scalar(50, 255, 0), 2);

	}
}


vector<string> objects_names_from_file(string const filename) {
	ifstream file(filename);
	vector<string> file_lines;
	if (!file.is_open()) return file_lines;
	for (string line; getline(file, line);) file_lines.push_back(line);
	cout << "object names loaded \n";
	return file_lines;
}

int main()
{
	void getFiles(const string & path, vector<string> & files);
	string names_file = "D:/Q/c++/Project1/yolov4/myData.names";
	string cfg_file = "D:/Q/c++/Project1/yolov4/7/my_yolov4.cfg";
	string weights_file = "D:/Q/c++/Project1/yolov4/7/my_yolov4_last.weights";
	Detector detector(cfg_file, weights_file, 0); //初始化检测器
	vector<string> obj_names;
	ifstream ifs(names_file.c_str());
	//cout << ifs.size() << "__________" << endl;
	string line;
	while (getline(ifs, line)) obj_names.push_back(line);

	/*cout << "______________________________" << endl;
	cout << detector.cur_gpu_id << endl;
	cout << "______________________________" << endl;*/
	const string path = "D:/a";
	vector<string> files;
	getFiles(path, files);
	float unprint = 0.0;
	float stain = 0.0;
	float hole = 0.0;
	int unprint_count = 0;
	int stain_count = 0;
	int hole_count = 0;
	for (int i = 0; i < files.size(); i++)
	{
		//namedWindow("test", WINDOW_NORMAL);
		Mat frame = imread(files[i], -1);
		vector<bbox_t> result_vec = detector.detect(frame);
		draw_boxes(frame, result_vec, obj_names);
		for(int j = 0; j < result_vec.size(); j++)
		{ 
			if (obj_names[result_vec[j].obj_id] == "unprint")
			{
				unprint = unprint + result_vec[j].prob;
				unprint_count = unprint_count + 1;
				//cout << "unprint+++  " << unprint << endl;
				//cout << result_vec[j].prob << "***" << obj_names[result_vec[j].obj_id] << endl;
			}
			else if (obj_names[result_vec[j].obj_id] == "stain")
			{
				stain = stain + result_vec[j].prob;
				stain_count = stain_count + 1;
				cout << "stain+++  " << stain << endl;
				//cout << result_vec[j].prob << "***" << obj_names[result_vec[j].obj_id] << endl;
			}
			else {
				hole = hole + result_vec[j].prob;
				hole_count = hole_count + 1;
				cout << "hole+++  " << hole << endl;
				//cout << result_vec[j].prob << "***" << obj_names[result_vec[j].obj_id] << endl;
			}

			//cout << "______________________  " << endl;
			
			//cout << result_vec[j].prob << "***" << obj_names[result_vec[j].obj_id] << endl;
		}
		/*imshow("test", frame);
		waitKey();
		destroyAllWindows();*/
	}

	float unprint_accuracy = unprint / (float)unprint_count;
	float stain_accuracy = stain / (float)stain_count;
	float hole_accuracy = hole / (float)hole_count;
	cout << "unprint_accuracy: " << unprint_accuracy << endl;
	cout << "stain_accuracy: " << stain_accuracy << endl;
	cout << "hole_accuracy: " << hole_accuracy << endl;
	cout << "*****************************************************" << endl;
	return 0;
}

 

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值