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

Opencv3.4.2调用yolov2进行物体检测源代码

源代码

#include<opencv2/opencv.hpp>
#include<opencv2/dnn.hpp>
#include <iostream>
#include<string>using namespace std;
using namespace cv;
using namespace dnn;int main()
{String modelConfiguration = "/home/oliver/darknet-master/cfg/yolov2.cfg";String modelBinary = "/home/oliver/darknet-master/yolov2.weights";dnn::Net net = readNetFromDarknet(modelConfiguration, modelBinary);if (net.empty()){printf("Could not load net...\n");return 0;}vector<string> classNamesVec;ifstream classNamesFile("/home/oliver/darknet-master/data/coco.names");if (classNamesFile.is_open()){string className = "";while (std::getline(classNamesFile, className))classNamesVec.push_back(className);}// 加载图像VideoCapture capture(2);// VideoCapture:OENCV中新增的类,捕获视频并显示出来while (1){Mat frame;capture >> frame;Mat inputBlob = blobFromImage(frame, 1 / 255.F, Size(608, 608), Scalar(), true, false);net.setInput(inputBlob, "data");// 检测Mat detectionMat = net.forward("detection_out");vector<double> layersTimings;double freq = getTickFrequency() / 1000;double time = net.getPerfProfile(layersTimings) / freq;ostringstream ss;ss << "detection time: " << time << " ms";putText(frame, ss.str(), Point(20, 20), 0, 0.5, Scalar(0, 0, 255));// 输出结果for (int i = 0; i < detectionMat.rows; i++){const int probability_index = 5;const int probability_size = detectionMat.cols - probability_index;float *prob_array_ptr = &detectionMat.at<float>(i, probability_index);size_t objectClass = max_element(prob_array_ptr, prob_array_ptr + probability_size) - prob_array_ptr;float confidence = detectionMat.at<float>(i, (int)objectClass + probability_index);if (confidence > 0.5){float x = detectionMat.at<float>(i, 0);float y = detectionMat.at<float>(i, 1);float width = detectionMat.at<float>(i, 2);float height = detectionMat.at<float>(i, 3);int xLeftBottom = static_cast<int>((x - width / 2) * frame.cols);int yLeftBottom = static_cast<int>((y - height / 2) * frame.rows);int xRightTop = static_cast<int>((x + width / 2) * frame.cols);int yRightTop = static_cast<int>((y + height / 2) * frame.rows);Rect object(xLeftBottom, yLeftBottom,xRightTop - xLeftBottom,yRightTop - yLeftBottom);rectangle(frame, object, Scalar(0, 0, 255), 2, 8);if (objectClass < classNamesVec.size()){ss.str("");ss << confidence;String conf(ss.str());String label = String(classNamesVec[objectClass]) + ": " + conf;int baseLine = 0;Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom),Size(labelSize.width, labelSize.height + baseLine)),Scalar(255, 255, 255), CV_FILLED);putText(frame, label, Point(xLeftBottom, yLeftBottom + labelSize.height),FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));}}}cv::namedWindow("YOLO-Detections",0);cv::imshow("YOLO-Detections", frame);waitKey(30);}return 0;
}

实验结果

在这里插入图片描述在这里插入图片描述


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

相关文章:

  • 【physx/wasm】在physx中添加自定义接口并重新编译wasm
  • excel---常用操作
  • Lora训练Windows[笔记]
  • linux基础指令讲解(ls、pwd、cd、touch、mkdir)
  • InnoDB 事务处理机制
  • 启明云端ESP32 C3 模组WT32C3通过 MQTT 连接 AWS
  • C++调用yolov3模型-opencv3.4.2
  • 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开发(亲测有效)