当前位置: 首页 > news >正文

C++调用yolov3模型-opencv3.4.2

介绍

基本思想:通过darknet在线下进行训练,生成yolov3.weights文件,然后opencv通过线上进行调用,模型可以落地了~~~

源代码

#include <fstream>
#include <sstream>
#include <iostream>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include<vector>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 outputsvector<int> outLayers = net.getUnconnectedOutLayers();//get the names of all the layers in the networkvector<String> layersNames = net.getLayerNames();// Get the names of the output layers in namesnames.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 boxrectangle(frame, Point(left, top), Point(right, bottom), Scalar(255, 178, 50), 3);//Get the label for the class name and its confidencestring label = format("%.5f", conf);if (!classes.empty()){CV_Assert(classId < (int)classes.size());label = classes[classId] + ":" + label;}//Display the label at the top of the bounding boxint baseLine;Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);top = max(top, labelSize.height);rectangle(frame, Point(left, top - round(1.5*labelSize.height)), Point(left + round(1.5*labelSize.width), top + baseLine), Scalar(255, 255, 255), FILLED);putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 0), 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 scoreminMaxLoc(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 confidencesvector<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 = "/home/oliver/darknet-master/data/coco.names";String model_def = "/home/oliver/darknet-master/cfg/yolov3.cfg";String weights = "/home/oliver/darknet-master/yolov3.weights";int in_w, in_h;double thresh = 0.5;double nms_thresh = 0.25;in_w = in_h = 608;string img_path = "/home/oliver/darknet/data/dog.jpg";//read namesifstream ifs(names_file.c_str());string line;while (getline(ifs, line)) classes.push_back(line);//init modelNet net = readNetFromDarknet(model_def, weights);net.setPreferableBackend(DNN_BACKEND_OPENCV);net.setPreferableTarget(DNN_TARGET_CPU);//read image and forwardVideoCapture capture(2);// VideoCapture:OENCV中新增的类,捕获视频并显示出来while (1){Mat frame, blob;capture >> frame;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 networknet.setInput(blob);// Runs the forward pass to get output of the output layersvector<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, 0, 255));imshow("res", frame);waitKey(10);}return 0;
}

http://www.taodudu.cc/news/show-1782133.html

相关文章:

  • 【physx/wasm】在physx中添加自定义接口并重新编译wasm
  • excel---常用操作
  • Lora训练Windows[笔记]
  • linux基础指令讲解(ls、pwd、cd、touch、mkdir)
  • InnoDB 事务处理机制
  • 启明云端ESP32 C3 模组WT32C3通过 MQTT 连接 AWS
  • Ubuntu18.04 + anaconda3 +python3.6+ 安装labelImg 标注
  • pcl学习之kd-tree
  • PCL中的点云ICP配准(附源代码和数据)
  • PCL对点云进行滤波处理并进行颜色可视化
  • PCL中的关键点
  • PCL点云参数估计算法之RANSAC和LMEDS
  • PCL计算点云的法线
  • PCL中的点云分割算法
  • PCL中把点云拟合成曲面(附源代码)
  • 环形链表 II
  • C++调用caffe分类模型-Opencv3.4.3
  • C++调用SSD caffe模型进行物体检测-Opencv3.4.3
  • C++调用tensorflow训练好的SSD物体检测模型-opencv3.4.3
  • C++调用mask rcnn进行实时检测--opencv4.0
  • tensorflow线下训练SSD深度学习物体检测模型,C++线上调用模型进行识别定位(干货满满)
  • python训练Faster RCNNC++调用训练好的模型进行物体检测-基于opencv3.4.3(超详细)
  • mask rcnn数据转换为tfrecord数据
  • opencv3.4.2调用训练好的Openpose模型
  • python训练mask rcnn模型C++调用训练好的模型--基于opencv4.0(干货满满)
  • leetcode之移除链表的元素
  • leetcode之奇偶链表
  • leetcode之回文链表
  • ubuntu16.04下安装配置caffe2和detectron(亲测有效,非常简单)
  • leetcode之字符串中的第一个唯一字符
  • 哈希表中处理冲突的方法
  • SENet算法解析
  • SNIP物体检测算法理解
  • yolov3聚类自己数据的anchor box
  • Ubuntu下安装qt57creator-plugin-ros,在QT中进行ROS开发(亲测有效)
  • ROS下面调用自定义的头文件和.cpp/.so文件(亲测有效)