windows环境下的YOLO3入门,及opencv344配置
首先
建议使用vs2017以及opencv3.4以上的版本,因为yolo3只有opencv3.4.3(我记得这这个版本以上。。。)以上的版本才能正常调用。
关于opencv3.4以上版本的配置问题,可以参考这篇博文https://blog.csdn.net/biaobro/article/details/79141868
我们可以把目标检测看成是目标定位和目标识别的结合。
在传统的计算机视觉方法中,采用滑动窗口查找不同区域和大小的目标。因为这是消耗量较大的算法,通常假定目标的纵横比是固定的。
早期的基于深度学习的目标检测算法,如R-CNN和快速R-CNN,采用选择型搜索(Selective Search)来缩小必须测试的边界框的数量(本文的边界框指的是,在预测到疑似所识别到的目标后,在图片上把对象框出的一个矩形)。
另外一种称为Overfeat的方法,通过卷积地计算滑动窗口,以多个尺度扫描了图像。
然后有人提出了快速R-CNN算法,使用Region Proposal Network(RPN)区别将要测试的边界框。通过巧妙的设计,用于目标识别的特征点,也被RPN用于提出潜在的边界框,因此节省了大量的计算。
然而,YOLO使用了完全不同的方法解决目标检测问题。它将图像进行神经网络的一次性正向处理。SSD是另外一种将图像进行神经网络一次性正向处理的方法,但是YOLOv3比SSD实现了更高的精度,同时又较快的运算速度。YOLOv3在M40,TitanX和1080Ti这类GPU上实时效果更好。
为什么选择OpenCV的YOLO
这里有三个理由。
容易整合到现有的OpenCV程序中:如果应用程序已经使用了OpenCV,并想简单地使用YOLOv3,完全不需要担心Darknet源代码的编译和建立。
OpenCV的CPU版本的运算速度比Darknet+OpenMP快9倍:OpenCV的DNN模块,其CPU运行是十分快的。举个例子,当用了OpenMP的Darknet在CPU上处理一张图片消耗2秒,OpenCV的实现只需要0.22秒。具体请看下面的表格。
支持Python。Darknet是用C语言写的,因此并不支持Python。相反,OpenCV是支持Python的。会有支持Darknet的编程接口。
接下来就是以具体配置了
配置完成opencv后,下面是yolo3的源代码,感谢yolo的作者开源了代码。
#include "pch.h"
#include <iostream>
// This code is written at BigVision LLC. It is based on the OpenCV project.
//It is subject to the license terms in the LICENSE file found in this distribution and at http://opencv.org/license.html
// Usage example: ./object_detection_yolo.out --video=run.mp4
// ./object_detection_yolo.out --image=bird.jpg
#include <fstream>
#include <sstream>
#include <iostream>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
;
using namespace cv;
using namespace dnn;
using namespace std;
string pro_dir = "E:/opencv-learning-tutorials/"; //项目根目录
// 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
vector<string> classes;
// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess(Mat& frame, const vector<Mat>& out);
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);
// Get the names of the output layers
vector<String> getOutputsNames(const Net& net);
void detect_image(string image_path, string modelWeights, string modelConfiguration, string classesFile);
void detect_video(string video_path, string modelWeights, string modelConfiguration, string classesFile);
int main(int argc, char** argv)
{
// Give the configuration and weight files for the model
String modelConfiguration = pro_dir + "data/models/yolov3/yolov3.cfg";
String modelWeights = pro_dir + "data/models/yolov3/yolov3.weights";
string image_path = pro_dir + "data/images/bird.jpg";
string classesFile = pro_dir + "data/models/yolov3/coco.names";// "coco.names";
//detect_image(image_path, modelWeights, modelConfiguration, classesFile);
string video_path = pro_dir + "data/images/run.mp4";
detect_video(video_path, modelWeights, modelConfiguration, classesFile);
cv::waitKey(0);
return 0;
}
void detect_image(string image_path, string modelWeights, string modelConfiguration, string classesFile) {
// Load names of classes
ifstream ifs(classesFile.c_str());
string line;
while (getline(ifs, line)) classes.push_back(line);
// Load the network
Net net = readNetFromDarknet