当前位置:   article > 正文

c++调用YOLOv3模型批量测试目标检测结果_yolov3 ++c

yolov3 ++c
#include <fstream>
#include <sstream>
#include <iostream>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include<vector>
#include<string>


using namespace std;
using namespace cv;
using namespace dnn;

vector<string> classes;

vector<String> getOutputsNames(Net&net)
{
	static vector<String> names;
	if (names.empty())
	{
		//Get the indices of the output layers, i.e. the layers with unconnected outputs
		vector<int> outLayers = net.getUnconnectedOutLayers();

		//get the names of all the layers in the network
		vector<String> layersNames = net.getLayerNames();

		// Get the names of the output layers in names
		names.resize(outLayers.size());
		for (size_t i = 0; i < outLayers.size(); ++i)
			names[i] = layersNames[outLayers[i] - 1];
	}
	return names;
}


void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame)
{
	//Draw a rectangle displaying the bounding box
	rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 0, 255), 1.5);//矩形框大小及颜色

	//Get the label for the class name and its confidence
	string label = format("%.3f", conf);   //预测值保留小数点后两位
	if (!classes.empty())
	{
		CV_Assert(classId < (int)classes.size());
		label = classes[classId] + ":" + label;
	}

	//Display the label at the top of the bounding box
	int baseLine;
	Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0, 1, &baseLine);   //0表示预测框上面的文本条大小,0表示无
	top = max(top, labelSize.height);
	rectangle(frame, Point(left, top - round(0.5*labelSize.height)), Point(left + round(0.5*labelSize.width), top + baseLine), Scalar(255, 255, 255), FILLED);
	//putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 0, 0), 3);  //0.4表示预测字体的大小,1表示字体的粗细
	putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.4, Scalar(255, 0, 0), 1.4);  //0.4表示预测字体的大小,1表示字体的粗细
}



void postprocess(Mat& frame, const vector<Mat>& outs, float confThreshold, float nmsThreshold)
{
	vector<int> classIds;
	vector<float> confidences;
	vector<Rect> boxes;

	for (size_t i = 0; i < outs.size(); ++i)
	{
		// Scan through all the bounding boxes output from the network and keep only the
		// ones with high confidence scores. Assign the box's class label as the class
		// with the highest score for the box.
		float* data = (float*)outs[i].data;
		for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
		{
			Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
			Point classIdPoint;
			double confidence;
			// Get the value and location of the maximum score
			minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
			if (confidence > confThreshold)
			{
				int centerX = (int)(data[0] * frame.cols);
				int centerY = (int)(data[1] * frame.rows);
				int width = (int)(data[2] * frame.cols);
				int height = (int)(data[3] * frame.rows);
				int left = centerX - width / 2;
				int top = centerY - height / 2;

				classIds.push_back(classIdPoint.x);
				confidences.push_back((float)confidence);
				boxes.push_back(Rect(left, top, width, height));
			}
		}
	}

	// Perform non maximum suppression to eliminate redundant overlapping boxes with
	// lower confidences
	vector<int> indices;
	NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);

	for (size_t i = 0; i < indices.size(); ++i)
	{
		int idx = indices[i];
		Rect box = boxes[idx];
		drawPred(classIds[idx], confidences[idx], box.x, box.y,
			box.x + box.width, box.y + box.height, frame);

	}
}

int main()
{

	string names_file = "D:\\PointerImg\\darknet-half-pointer\\data\\voc.names";
	String model_def = "D:\\PointerImg\\darknet-half-pointer\\cfg\\yolov3-voc.cfg";
	String weights = "D:\\PointerImg\\darknet-half-pointer\\backup\\tiny1\\yolov3-voc_last.weights";


	int in_w, in_h;
	double thresh = 0.5;
	double nms_thresh = 0.25;
	in_w = in_h = 416;


	string path = "D:/PointerImg/darknet-half-pointer/data/meter/reality/";
	String dest = "D:/PointerImg/darknet-half-pointer/data/predicts/pre2/";

	String savedfilename;
	int len = path.length();
	vector<cv::String> filenames;

	cv::glob(path, filenames);
	for (int i = 0; i < filenames.size(); i++) {


		//read names

		ifstream ifs(names_file.c_str());
		string line;
		while (getline(ifs, line)) classes.push_back(line);

		//init model
		Net net = readNetFromDarknet(model_def, weights);
		net.setPreferableBackend(DNN_BACKEND_OPENCV);
		net.setPreferableTarget(DNN_TARGET_CPU);

		//read image and forward
		VideoCapture capture(2);// VideoCapture:OENCV中新增的类,捕获视频并显示出来
		/*while (1)
		{*/
			Mat frame, blob;
			capture >> frame;

			frame = imread(filenames[i]);
			blobFromImage(frame, blob, 1 / 255.0, Size(in_w, in_h), Scalar(), true, false);

			vector<Mat> mat_blob;
			imagesFromBlob(blob, mat_blob);

			//Sets the input to the network
			net.setInput(blob);

			// Runs the forward pass to get output of the output layers
			vector<Mat> outs;
			net.forward(outs, getOutputsNames(net));

			postprocess(frame, outs, thresh, nms_thresh);

			vector<double> layersTimes;
			double freq = getTickFrequency() / 1000;
			double t = net.getPerfProfile(layersTimes) / freq;
			string label = format("Inference time for a frame : %.2f ms", t);
			putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0));
			printf("Inference time for a frame : %.2f ms", t);
			//savedfilename = dest + filenames[i].substr(54);   //path的字符串长度
			savedfilename = dest + filenames[i].substr(len);
			cout << savedfilename << endl;
			imwrite(savedfilename, frame);
			//imwrite("D:\\PointerImg\\darknet-master-meter_pointer\\data\\predicts\\1.jpg", frame);
			//imshow("res", frame);

			//waitKey(0);
		/*}*/
	}
	return 0;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/736289
推荐阅读
相关标签
  

闽ICP备14008679号