搭配数据
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
#include
#ifdef _WIN32
#define OPENCV
#define GPU
#endif
#include
#include
#include
#include "darknet/yolo_v2_class.hpp" //引用动态链接库中的头文件
#include "opencv2/highgui/highgui.hpp"
#include
#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 result_vec, vector 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 objects_names_from_file(string const filename) {
ifstream file(filename);
vector 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 & 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 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 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 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;
}