下载预训练模型放入工程目录,放入run.mp4视频
yolov3.cfg
yolov3.weights
coco.names

#include <opencv2/opencv.hpp>
#include <opencv2/dnn/dnn.hpp>
#include <vector>using namespace std;
using namespace cv;
using namespace cv::dnn;// Initialize the parameters
float confThreshold = 0.2; // Confidence threshold
float nmsThreshold = 0.4;// Non-maximum suppression threshold
int inpWidth = 416;// Width of network's input image
int inpHeight = 416;// Height of network's input image
vector<string> classes;// Draw the predicted bounding box
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(0, 0, 255));//Get the label for the class name and its confidencestring label = format("%.2f", 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);putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255));}// Get the names of the output layers
vector<String> getOutputsNames(const 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;
}// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess(Mat& frame, const vector<Mat>& outs)
{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);}
}void main()
{// Load names of classesstring classesFile = "coco.names";ifstream ifs(classesFile.c_str());string line;while (getline(ifs, line)) classes.push_back(line);// Give the configuration and weight files for the modelString modelConfiguration = "yolov3.cfg";String modelWeights = "yolov3.weights";// Load the networkNet net = readNetFromDarknet(modelConfiguration, modelWeights);net.setPreferableBackend(DNN_BACKEND_OPENCV);net.setPreferableTarget(DNN_TARGET_CPU);cv::VideoCapture cap("run.mp4");cv::Mat frame, blob;while (waitKey(1) < 0){// get frame from the videocap >> frame;//std::cout << "frame.size=" << frame.size() << "\n";// Create a 4D blob from a frame.cv::Mat sml;cv::resize(frame, sml, cv::Size(0, 0), 3.0 / 2, 3 / 2.0);blobFromImage(sml, blob, 1 / 255.0, cv::Size(inpWidth, inpHeight), Scalar(0, 0, 0), true, false);//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));// Remove the bounding boxes with low confidencepostprocess(frame, outs);// Put efficiency information. The function getPerfProfile returns the// overall time for inference(t) and the timings for each of the layers(in layersTimes)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));// Write the frame with the detection boxes  Mat detectedFrame;frame.convertTo(detectedFrame, CV_8U);cv::imshow("detectedFrame", detectedFrame);//cv::waitKey(10);}}


OpenCV3.4.2-YOLOv3实现视频对象检测相关推荐

  1. YOLOV:图像对象检测器在视频对象检测方面表现也很不错

    前言 与传统的两段pipeline不同,论文提出了在一段检测之后再进行区域级的选择,避免了处理大量低质量的候选区域.此外,还构建了一个新的模块来评估目标帧与参考帧之间的关系,并指导聚合. 作者进行了大 ...

  2. ECCV 2020 论文大盘点-视频目标检测篇

    本文盘点ECCV 2020 中所有视频目标价检测(Video Object Detection)相关的论文,总计 4 篇,其中 2 篇论文代码将开源. 对视频中的目标进行检测,当然可以转化为对每帧图像 ...

  3. Dogfight :从无人机视频中检测无人机

    摘要 随着机载车辆变得越来越自主和无处不在,发展探测周围物体的能力变得至关重要.本文试图解决从其他飞行无人机中探测到无人机的问题.源无人机和目标无人机的不稳定运动.小尺寸.任意形状.大强度变化和遮挡使 ...

  4. python label显示图片_高大上的YOLOV3对象检测算法,使用python也可轻松实现

    继续我们的目标检测算法的分享,前期我们介绍了SSD目标检测算法的python实现以及Faster-RCNN目标检测算法的python实现以及yolo目标检测算法的darknet的window环境安装, ...

  5. AI队列长度检测:使用YOLO进行视频中的对象检测

    下载源219 MB 到目前为止,在本系列中,我们一直在处理静止图像数据.在本文中,我们将使用YOLO的基本实现来检测和计数视频序列中的人物. 让我们再次从导入所需的库开始. import cv2 im ...

  6. 基于yolov3的行人目标检测算法在图像和视频中识别检测

    资源下载地址:https://download.csdn.net/download/sheziqiong/85772186 资源下载地址:https://download.csdn.net/downl ...

  7. DL之Yolov3:基于深度学习Yolov3算法实现视频目标检测之对《我要打篮球》视频段进行实时目标检测

    DL之Yolov3:基于深度学习Yolov3算法实现视频目标检测之对<我要打篮球>视频段进行实时目标检测 目录 输出结果 设计思路 核心代码 相关文章 成功解决AttributeError ...

  8. DL之Yolov3:基于深度学习Yolov3算法实现视频目标检测之对《俄罗斯总统普京对沙特王储摊的“友好摊手”瞬间—东道主俄罗斯5-0完胜沙特》视频段实时检测

    DL之Yolov3:基于深度学习Yolov3算法实现视频目标检测之对<俄罗斯总统普京对沙特王储摊的"友好摊手"瞬间-东道主俄罗斯5-0完胜沙特>视频段实时检测 导读   ...

  9. 深度学习和目标检测系列教程 15-300:在 Python 中使用 OpenCV 执行 YOLOv3 对象检测

    @Author:Runsen 上次讲了yolov3,这是使用yolov3的模型通过opencv的摄像头来执行YOLOv3 对象检测. 导入所需模块: import cv2 import numpy a ...

最新文章

  1. asp连接mysql odbc,在ASP中连接MySQL数据库的方法,最好的通过ODBC方法
  2. webdriver--单选、复选及下拉框的定位
  3. 重磅!SCI顶级牛刊《Science》合集(2018~2020年度)
  4. 如何进行嵌入式系统的学习?
  5. java date传输类型错误_转换日期格式:Java中的转换错误?
  6. json 微信小程序 筛选_微信小程序学习记录
  7. 计算机资格考试中级工程师种类,中级工程师职称考试类别及注意事项
  8. 做自媒体和有没有文化没有太大关系
  9. NodeJS 加密 —— crypto 模块
  10. android audiorecord jni,Android AudioRecord初始化失败
  11. 二分查找时间复杂度分析
  12. yoga ins android.ios,to Yoga Lin:
  13. 微信小程序服务通知模板的实现
  14. 跨境电商shopee这个平台怎么样?百万shopee卖家笑了,不要再犹豫了
  15. win10如何删除计算机用户,Win10系统如何删除账户?Win10系统删除账户的方法
  16. win7无法连接打印机拒绝访问_打印机拒绝访问,小编教你打印机拒绝访问无法连接怎么解决...
  17. Android 打开相册选择图片(史上最简单)
  18. 2015蓝桥真题(A组省赛)
  19. 在Unity中创建基于Node节点的编辑器 (二) 窗口序列化
  20. 让单个单元格显示两个数据

热门文章

  1. LiteOS移植到STM32F103系列单片机,非常简单
  2. CPLD/FPGA/Verilog_Verilog指令_assign用法
  3. 【调剂】2021年上海海事大学硕士研究生复试、调剂问答
  4. 【设计模式】我终于读懂了观察者模式。。。
  5. javascript实现下拉条联动_js实现下拉框联动
  6. Python的类变量和实例变量
  7. 异常:Parameter 0 of constructor in com.ylz.supervise.childmodule.sys.log.strategy.StrategyContext req记
  8. OWASP A9 Security Logging and Monitoring Failures(安全日志和监控故障)
  9. 直流稳压稳流开关电源使用说明(LW-6020)
  10. 如何制作Win7 usb启动盘