#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
using namespace dnn;
vector<string> classes;
vector<String> getOutputsNames(Net&net)
{
static vector<String> names;
if (names.empty())
{
vector<int> outLayers = net.getUnconnectedOutLayers();
vector<String> layersNames = net.getLayerNames();
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)
{
rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 0, 255), 1.5);
string label = format("%.3f", conf);
if (!classes.empty())
{
CV_Assert(classId < (int)classes.size());
label = classes[classId] + ":" + label;
}
int baseLine;
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0, 1, &baseLine);
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, 0.4, Scalar(255, 0, 0), 1.4);
}
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)
{
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;
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));
}
}
}
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++) {
ifstream ifs(names_file.c_str());
string line;
while (getline(ifs, line)) classes.push_back(line);
Net net = readNetFromDarknet(model_def, weights);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);
VideoCapture capture(2);
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);
net.setInput(blob);
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(len);
cout << savedfilename << endl;
imwrite(savedfilename, frame);
}
return 0;
}