YOLOv3在OpenCV4.0.0/OpenCV3.4.2上的C++ demo实现
YOLOv3在OpenCV4.0.0/OpenCV3.4.2上的C++ demo实现
2018年11月20日 15:53:05 Andyoyo007 阅读数:1650
参考:
[1] https://pjreddie.com/darknet/yolo/
[2] https://www.learnopencv.com/deep-learning-based-object-detection-using-yolov3-with-opencv-python-c/
1. 运行环境:
Ubuntu16.04+OpenCV3.4.2/OpenCV4.0.0
Intel® Core™ i7-8700K CPU @ 3.70GHz × 12
GeForce GTX 1060 5GB/PCIe/SSE2
注:未使用GPU,在CPU上运行,大约160ms/帧图像,貌似OpenCV对其做了优化,我尝试过直接编译darknet的源码用CPU运行,每张图片需要30s+。
2. yolo_opencv.cpp
对参考网址上的代码进行了整合和修改,能够读取图片、视频文件、摄像头。
//YOLOv3 on OpenCV
//reference:https://www.learnopencv.com/deep-learning-based-object-detection-using-yolov3-with-opencv-python-c/
//by:Andyoyo@swust
//data:2018.11.20
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/dnn/shape_utils.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <fstream>
// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess(cv::Mat& frame, std::vector<cv::Mat>& outs);
// Get the names of the output layers
std::vector<cv::String> getOutputsNames(const cv::dnn::Net& net);
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, cv::Mat& frame);
// Initialize the parameters
float confThreshold = 0.5; // 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
static const char* about =
"This sample uses You only look once (YOLO)-Detector (https://arxiv.org/abs/1612.08242) to detect objects on camera/video/image.\n"
"Models can be downloaded here: https://pjreddie.com/darknet/yolo/\n"
"Default network is 416x416.\n"
"Class names can be downloaded here: https://github.com/pjreddie/darknet/tree/master/data\n";
static const char* params =
"{ help | false | ./yolo_opencv -source=../data/3.avi }"
"{ source | ../data/dog.jpg | image or video for detection }"
"{ device | 0 | video for detection }"
"{ save | false | save result }";
std::vector<std::string> classes;
int main(int argc, char** argv)
{
cv::CommandLineParser parser(argc, argv, params);
// Load names of classes
std::string classesFile = "../coco.names";
std::ifstream classNamesFile(classesFile.c_str());
if (classNamesFile.is_open())
{
std::string className = "";
while (std::getline(classNamesFile, className))
classes.push_back(className);
}
else{
std::cout<<"can not open classNamesFile"<<std::endl;
}
// Give the configuration and weight files for the model
cv::String modelConfiguration = "../yolov3.cfg";
cv::String modelWeights = "../yolov3.weights";
// Load the network
cv::dnn::Net net = cv::dnn::readNetFromDarknet(modelConfiguration, modelWeights);
std::cout<<"Read Darknet..."<<std::endl;
net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);
cv::String outputFile = "../data/yolo_out_cpp.avi";
std::string str;
cv::VideoCapture cap;
double frame_count;
if (parser.get<bool>("help"))
{
std::cout << about << std::endl;
parser.printMessage();
return 0;
}
if (parser.get<cv::String>("source").empty())
{
int cameraDevice = parser.get<int>("device");
cap = cv::VideoCapture(cameraDevice);
if (!cap.isOpened())
{
std::cout << "Couldn't find camera: " << cameraDevice << std::endl;
return -1;
}
}
else
{
str=parser.get<cv::String>("source");
cap.open(str);
if (!cap.isOpened())
{
std::cout << "Couldn't open image or video: " << parser.get<cv::String>("video") << std::endl;
return -1;
}
frame_count=cap.get(cv::CAP_PROP_FRAME_COUNT);
std::cout<<"frame_count:"<<frame_count<<std::endl;
}
// Get the video writer initialized to save the output video
cv::VideoWriter video;
if (parser.get<bool>("save"))
{
if(frame_count>1)
{
video.open(outputFile, cv::VideoWriter::fourcc('M','J','P','G'), 28, cv::Size(cap.get(cv::CAP_PROP_FRAME_WIDTH),cap.get(cv::CAP_PROP_FRAME_HEIGHT)));
}
else
{
str.replace(str.end()-4, str.end(), "_yolo_out.jpg");
outputFile = str;
}
}
// Process frames.
std::cout <<"Processing..."<<std::endl;
cv::Mat frame;
while (1)
{
// get frame from the video
cap >> frame;
// Stop the program if reached end of video
if (frame.empty()) {
std::cout << "Done processing !!!" << std::endl;
if(parser.get<bool>("save"))
std::cout << "Output file is stored as " << outputFile << std::endl;
std::cout << "Please enter Esc to quit!" << std::endl;
if(cv::waitKey(0)==27)
break;
}
//show frame
cv::imshow("frame",frame);
// Create a 4D blob from a frame.
cv::Mat blob;
cv::dnn::blobFromImage(frame, blob, 1/255.0, cv::Size(inpWidth, inpHeight), cv::Scalar(0,0,0), true, false);
//Sets the input to the network
net.setInput(blob);
// Runs the forward pass to get output of the output layers
std::vector<cv::Mat> outs;
net.forward(outs, getOutputsNames(net));
// Remove the bounding boxes with low confidence
postprocess(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)
std::vector<double> layersTimes;
double freq = cv::getTickFrequency() / 1000;
double t = net.getPerfProfile(layersTimes) / freq;
std::string label = cv::format("Inference time for a frame : %.2f ms", t);
cv::putText(frame, label, cv::Point(0, 15), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 255));
// Write the frame with the detection boxes
cv::Mat detectedFrame;
frame.convertTo(detectedFrame, CV_8U);
//show detectedFrame
cv::imshow("detectedFrame",detectedFrame);
//save result
if(parser.get<bool>("save"))
{
if(frame_count>1)
{
video.write(detectedFrame);
}
else
{
cv::imwrite(outputFile, detectedFrame);
}
}
if(cv::waitKey(10)==27)
{
break;
}
}
std::cout<<"Esc..."<<std::endl;
return 0;
}
// Get the names of the output layers
std::vector<cv::String> getOutputsNames(const cv::dnn::Net& net)
{
static std::vector<cv::String> names;
if (names.empty())
{
//Get the indices of the output layers, i.e. the layers with unconnected outputs
std::vector<int> outLayers = net.getUnconnectedOutLayers();
//get the names of all the layers in the network
std::vector<cv::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;
}
// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess(cv::Mat& frame, std::vector<cv::Mat>& outs)
{
std::vector<int> classIds;
std::vector<float> confidences;
std::vector<cv::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)
{
cv::Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
cv::Point classIdPoint;
double confidence;
// Get the value and location of the maximum score
cv::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(cv::Rect(left, top, width, height));
}
}
}
// Perform non maximum suppression to eliminate redundant overlapping boxes with
// lower confidences
std::vector<int> indices;
cv::dnn::NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
for (size_t i = 0; i < indices.size(); ++i)
{
int idx = indices[i];
cv::Rect box = boxes[idx];
drawPred(classIds[idx], confidences[idx], box.x, box.y,
box.x + box.width, box.y + box.height, frame);
}
}
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, cv::Mat& frame)
{
//Draw a rectangle displaying the bounding box
cv::rectangle(frame, cv::Point(left, top), cv::Point(right, bottom), cv::Scalar(0, 0, 255));
//Get the label for the class name and its confidence
std::string label = cv::format("%.2f", conf);
if (!classes.empty())
{
CV_Assert(classId < (int)classes.size());
label = classes[classId] + ":" + label;
}
else
{
std::cout<<"classes is empty..."<<std::endl;
}
//Display the label at the top of the bounding box
int baseLine;
cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
top = std::max(top, labelSize.height);
cv::putText(frame, label, cv::Point(left, top), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255,255,255));
}
3. CMakeLists.txt
本人使用的环境安装了OpenCV3.4.2和4.0.0两个版本,均调试通过,不用修改源码,只需在CMakeLists.txt文件中指定OpenCV版本即可。
cmake_minimum_required(VERSION 2.8)
#OpenCV4 must enable c++11
add_definitions(-std=c++11)
#setOpenCV_DIR
#set(OpenCV_DIR "/home/andyoyo/opencv/opencv-4.0.0-beta/build")
project(yolo_opencv)
find_package(OpenCV 4 REQUIRED)
#print OpenCV_VERSION on terminal
message(STATUS "OpenCV_VERSION:" ${OpenCV_VERSION})
file(GLOB native_srcs "src/*.cpp")
add_executable(${PROJECT_NAME} ${native_srcs})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} )
4. 其他说明
运行前先下载yolov3的配置文件等,包括:coco.names,yolov3.cfg,yolov3.weights三个文件,可通过wget下载。
wget https://github.com/pjreddie/darknet/blob/master/data/coco.names?raw=true -O ./coco.names
wget https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg?raw=true -O ./yolov3.cfg
wget https://pjreddie.com/media/files/yolov3.weights
5.运行效果:
YOLOv3在OpenCV4.0.0/OpenCV3.4.2上的C++ demo实现相关推荐
- ubuntu16.0.4 opencv4.0.0 yolov3测试
https://github.com/spmallick/learnopencv/tree/master/ObjectDetection-YOLO 硬件信息 8 Intel® Core™ i7-479 ...
- Ros noetic opencv4.2.0 cv_bridge 冲突 适配Opencv3.2.0完成自己的USB_Camera 跑通 Orb_slam2
前言: 本文附上使用自己的USB_Cam 跑通Orb_Slam2 算法的过程,以及解决cv_bridge冲突的方案 Orb_Slam2 需要opencv3.2.0 的cv版本 但是ubantu20.0 ...
- VS2019中配置opencv4.3.0(亲测有效)
写在前面:之前一直使用vs2017+opencv的配置,现在体验vs2019+opencv 4.3.0的配置.由于之前的配置相隔很久,忘记很多东西,如今重新配置还是踩了很多坑,记录如下,希望对读者有帮 ...
- Qt Creator5.12配置OpenCV4.3.0和opencv_contrib扩展包(亲测有效)
本文结构 第一部分 只安装Qt Creator和配置OpenCV 1.Qt Creator5.12.2下载与安装 2.Cmake下载与安装 3.OpenCV下载 4.编译OpenCV 5.测试Qt程序 ...
- linux下安装opencv4.4.0
简介 opencv4.4.0和opencv_contrib-4.4.0以及编译过程中缺少的文件 链接:https://pan.baidu.com/s/11D6G3TbRY_-oNYlP4FDnTA ...
- Anaconda Python3.6 OpenCV4.1.0 Ubuntu 16.04源码编译
Anaconda Python3.6 OpenCV4.1.0 Ubuntu 16.04源码编译 转载于:https://blog.csdn.net/phdsky/article/details/782 ...
- opencv:centos7中安装opencv4.3.0环境
准备 安装依赖 sudo yum -y install epel-release # 安装epel扩展源 sudo yum -y install git gcc gcc-c++ cmake3 sudo ...
- windows11编译OpenCV4.5.0 with CUDA(附注意事项)
windows11编译OpenCV4.5.0 with CUDA 从OpenCV4.2.0 版本开始允许使用 Nvidia GPU 来加速推理.本文介绍最近使用windows11系统编译带CUDA的O ...
- 【安装教程】Ubuntu18.04中用CMake-gui安装OpenCV4.1.0和OpenCV_contrib-4.1.0(图文)
目录 一.简要说明 二.下载和添加依赖包 三.配置OpenCV 四.配置环境变量 新建一个 opecv.pc (勾选了就跳过此步骤,勾选生成的opencv4.pc 我印象中好像不用修改的,不放心可以打 ...
最新文章
- 【强化学习篇】--强化学习从初识到应用
- OpenOffce在Centos7安装和使用
- 使用 Bamboo 构建项目的 CICD 过程文档
- C++直接初始化与复制初始化的区别深入解析
- spring中AOP动态代理的两种方式
- 图表插件Highcharts的动态化赋值,实现图表数据的动态化设置显示
- has_a php,PHP has encountered a Stack overflow问题解决方法
- android:showAsAction 无效
- 7-161 梅森数 (20 分)
- 页面中动态画有超连接的图
- 每天一道剑指offer-二叉搜索数的后序遍历序列
- 2021-05-15 SqlServer面试题 通用篇
- Facebook全新数字货币Libra引发关注 数字货币国际化逐渐发展
- 《MLB棒球创造营》:走近棒球运动·纽约扬基队
- python学习第二天——编写名片
- 打破定制化语音技术落地怪圈?从讲一口标准英音的语音助手说起
- java affinity_sched_setaffinity()如何工作?
- Pytorch构建Transformer实现英文翻译
- win10系统日语输入法只能打出英文字母无法切换微软输入法无法使用
- 最小均方算法二分类(基于双月数据集)
热门文章
- OpenStack在dashboard界面点击管理员网络,服务器页面出错
- 最大似然估计学习总结
- hadoop启动页面_轻松搞定Windows下的Hadoop环境安装
- linux通过tftp下载的文件大小为0,linux 通过 tftp下载文件
- java 文本压缩_[Java基础]Java使用GZIP进行文本压缩
- easyexcel导入固定sheet_easyexcel指定多个sheet导excel数据
- Linux安装redis最新版5.0.8
- 2020黑群晖最稳定版本_打造完美6.2.3黑群晖,正确显示 CPU,支持Nvme缓存
- 计算机word做课程表实验报告,word制作课程表.doc
- 线程 信号量 java_JAVA多线程-Semaphore信号量