当前位置:   article > 正文

OpenCV+yolov3实现目标检测(C++,Python)_yolo实时检测c++怎么看fps

yolo实时检测c++怎么看fps

OpenCV+yolov3实现目标检测(C++,Python)


    目标检测算法主要分为两类:一类是基于Region Proposal(候选区域)的算法,如R-CNN系算法(R-CNN,Fast R-CNN, Faster R-CNN),它们是two-stage(两步法)的,需要先使用Selective search或者CNN网络(RPN)产生Region Proposal,然后再在Region Proposal上做分类与回归。而另一类是Yolo,SSD这类one-stage算法(一步法),其仅仅使用一个CNN网络直接预测不同目标的类别与位置。第一类方法是准确度高一些,但是速度慢,而第二类算法是速度快,但是准确性要低一些。

    YOLO是一种比SSD还要快的目标检测网络模型,作者在其论文中说FPS是Fast R-CNN的100倍,这里首先简单的介绍一下YOLO网络基本结构,然后通过OpenCV C++调用Darknet的,实现目标检测。OpenCV在3.3.1的版本中开始正式支持Darknet网络框架并且支持YOLO1与YOLO2以及YOLO Tiny网络模型的导入与使用。后面测试,OpenCV3.4.2也支持YOLO3 。另外,OpenCV dnn模块目前支持Caffe、TensorFlow、Torch、PyTorch等深度学习框架,关于《OpenCV调用TensorFlow预训练模型》可参考鄙人的另一份博客:https://blog.csdn.net/guyuealian/article/details/80570120

    关于《OpenCV+yolov2-tiny实现目标检测(C++)》请参考我的另一篇博客:https://blog.csdn.net/guyuealian/article/details/82950283

    本博客源码都放在Github上:https://github.com/PanJinquan/opencv-learning-tutorials/tree/master/dnn_tutorial,麻烦给个“Star”哈

参考资料:

Deep Learning based Object Detection using YOLOv3 with OpenCV ( Python / C++ )》:

官网博客:https://www.learnopencv.com/deep-learning-based-object-detection-using-yolov3-with-opencv-python-c/

YOLOv3 + OpenCV 实现目标检测(Python / C ++)》:https://blog.csdn.net/haoqimao_hard/article/details/82081285

 Github参考源码:https://github.com/spmallick/learnopencv/tree/master/ObjectDetection-YOLO

 darknt yolo官网:https://pjreddie.com/darknet/yolo/


目录

OpenCV+yolov3实现目标检测(C++,Python)

1、YOLO网络

(1)YOLO网络结构

2、OpenCV使用YOLO v3实现目标检测

2.1 C++代码

2.2 Python代码 

3、YOLO的缺点

4、参考资料:


1、YOLOv3网络

相比YOLOv2和YOLOv1,YOLOv3最大的变化包括两点:使用残差模型和采用FPN架构。YOLOv3的特征提取器是一个残差模型,因为包含53个卷积层,所以称为Darknet-53,从网络结构上看,相比Darknet-19网络使用了残差单元,所以可以构建得更深。另外一个点是采用FPN架构(Feature Pyramid Networks for Object Detection)来实现多尺度检测

YOLOv3是到目前为止,速度和精度最均衡的目标检测网络。通过多种先进方法的融合,将YOLO系列的短板(速度很快,不擅长检测小物体等)全部补齐。


1.1 YOLOv3网络结构

参考资料:

《深入理解目标检测与YOLO(从v1到v3)》:https://blog.csdn.net/qq_39521554/article/details/80694512 

https://blog.csdn.net/leviopku/article/details/82660381

 


 

2、OpenCV使用YOLO v3实现目标检测

    yolov3模型下载地址:链接: https://pan.baidu.com/s/1TugNSWRZaJz1R6IejRtNiA 提取码: 46mh 

2.1 C++代码

  1. // This code is written at BigVision LLC. It is based on the OpenCV project.
  2. //It is subject to the license terms in the LICENSE file found in this distribution and at http://opencv.org/license.html
  3. // Usage example: ./object_detection_yolo.out --video=run.mp4
  4. // ./object_detection_yolo.out --image=bird.jpg
  5. #include <fstream>
  6. #include <sstream>
  7. #include <iostream>
  8. #include <opencv2/dnn.hpp>
  9. #include <opencv2/imgproc.hpp>
  10. #include <opencv2/highgui.hpp>
  11. ;
  12. using namespace cv;
  13. using namespace dnn;
  14. using namespace std;
  15. string pro_dir = "E:/opencv-learning-tutorials/"; //项目根目录
  16. // Initialize the parameters
  17. float confThreshold = 0.5; // Confidence threshold
  18. float nmsThreshold = 0.4; // Non-maximum suppression threshold
  19. int inpWidth = 416; // Width of network's input image
  20. int inpHeight = 416; // Height of network's input image
  21. vector<string> classes;
  22. // Remove the bounding boxes with low confidence using non-maxima suppression
  23. void postprocess(Mat& frame, const vector<Mat>& out);
  24. // Draw the predicted bounding box
  25. void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);
  26. // Get the names of the output layers
  27. vector<String> getOutputsNames(const Net& net);
  28. void detect_image(string image_path, string modelWeights, string modelConfiguration, string classesFile);
  29. void detect_video(string video_path, string modelWeights, string modelConfiguration, string classesFile);
  30. int main(int argc, char** argv)
  31. {
  32. // Give the configuration and weight files for the model
  33. String modelConfiguration = pro_dir + "data/models/yolov3/yolov3.cfg";
  34. String modelWeights = pro_dir + "data/models/yolov3/yolov3.weights";
  35. string image_path = pro_dir + "data/images/bird.jpg";
  36. string classesFile = pro_dir + "data/models/yolov3/coco.names";// "coco.names";
  37. //detect_image(image_path, modelWeights, modelConfiguration, classesFile);
  38. string video_path = pro_dir + "data/images/run.mp4";
  39. detect_video(video_path, modelWeights, modelConfiguration, classesFile);
  40. cv::waitKey(0);
  41. return 0;
  42. }
  43. void detect_image(string image_path, string modelWeights, string modelConfiguration, string classesFile) {
  44. // Load names of classes
  45. ifstream ifs(classesFile.c_str());
  46. string line;
  47. while (getline(ifs, line)) classes.push_back(line);
  48. // Load the network
  49. Net net = readNetFromDarknet(modelConfiguration, modelWeights);
  50. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  51. net.setPreferableTarget(DNN_TARGET_OPENCL);
  52. // Open a video file or an image file or a camera stream.
  53. string str, outputFile;
  54. cv::Mat frame = cv::imread(image_path);
  55. // Create a window
  56. static const string kWinName = "Deep learning object detection in OpenCV";
  57. namedWindow(kWinName, WINDOW_NORMAL);
  58. // Stop the program if reached end of video
  59. // Create a 4D blob from a frame.
  60. Mat blob;
  61. blobFromImage(frame, blob, 1 / 255.0, cvSize(inpWidth, inpHeight), Scalar(0, 0, 0), true, false);
  62. //Sets the input to the network
  63. net.setInput(blob);
  64. // Runs the forward pass to get output of the output layers
  65. vector<Mat> outs;
  66. net.forward(outs, getOutputsNames(net));
  67. // Remove the bounding boxes with low confidence
  68. postprocess(frame, outs);
  69. // Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
  70. vector<double> layersTimes;
  71. double freq = getTickFrequency() / 1000;
  72. double t = net.getPerfProfile(layersTimes) / freq;
  73. string label = format("Inference time for a frame : %.2f ms", t);
  74. putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
  75. // Write the frame with the detection boxes
  76. imshow(kWinName, frame);
  77. cv::waitKey(30);
  78. }
  79. void detect_video(string video_path, string modelWeights, string modelConfiguration, string classesFile) {
  80. string outputFile = "./yolo_out_cpp.avi";;
  81. // Load names of classes
  82. ifstream ifs(classesFile.c_str());
  83. string line;
  84. while (getline(ifs, line)) classes.push_back(line);
  85. // Load the network
  86. Net net = readNetFromDarknet(modelConfiguration, modelWeights);
  87. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  88. net.setPreferableTarget(DNN_TARGET_CPU);
  89. // Open a video file or an image file or a camera stream.
  90. VideoCapture cap;
  91. //VideoWriter video;
  92. Mat frame, blob;
  93. try {
  94. // Open the video file
  95. ifstream ifile(video_path);
  96. if (!ifile) throw("error");
  97. cap.open(video_path);
  98. }
  99. catch (...) {
  100. cout << "Could not open the input image/video stream" << endl;
  101. return ;
  102. }
  103. // Get the video writer initialized to save the output video
  104. //video.open(outputFile,
  105. // VideoWriter::fourcc('M', 'J', 'P', 'G'),
  106. // 28,
  107. // Size(cap.get(CAP_PROP_FRAME_WIDTH), cap.get(CAP_PROP_FRAME_HEIGHT)));
  108. // Create a window
  109. static const string kWinName = "Deep learning object detection in OpenCV";
  110. namedWindow(kWinName, WINDOW_NORMAL);
  111. // Process frames.
  112. while (waitKey(1) < 0)
  113. {
  114. // get frame from the video
  115. cap >> frame;
  116. // Stop the program if reached end of video
  117. if (frame.empty()) {
  118. cout << "Done processing !!!" << endl;
  119. cout << "Output file is stored as " << outputFile << endl;
  120. waitKey(3000);
  121. break;
  122. }
  123. // Create a 4D blob from a frame.
  124. blobFromImage(frame, blob, 1 / 255.0, cvSize(inpWidth, inpHeight), Scalar(0, 0, 0), true, false);
  125. //Sets the input to the network
  126. net.setInput(blob);
  127. // Runs the forward pass to get output of the output layers
  128. vector<Mat> outs;
  129. net.forward(outs, getOutputsNames(net));
  130. // Remove the bounding boxes with low confidence
  131. postprocess(frame, outs);
  132. // Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
  133. vector<double> layersTimes;
  134. double freq = getTickFrequency() / 1000;
  135. double t = net.getPerfProfile(layersTimes) / freq;
  136. string label = format("Inference time for a frame : %.2f ms", t);
  137. putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
  138. // Write the frame with the detection boxes
  139. Mat detectedFrame;
  140. frame.convertTo(detectedFrame, CV_8U);
  141. //video.write(detectedFrame);
  142. imshow(kWinName, frame);
  143. }
  144. cap.release();
  145. //video.release();
  146. }
  147. // Remove the bounding boxes with low confidence using non-maxima suppression
  148. void postprocess(Mat& frame, const vector<Mat>& outs)
  149. {
  150. vector<int> classIds;
  151. vector<float> confidences;
  152. vector<Rect> boxes;
  153. for (size_t i = 0; i < outs.size(); ++i)
  154. {
  155. // Scan through all the bounding boxes output from the network and keep only the
  156. // ones with high confidence scores. Assign the box's class label as the class
  157. // with the highest score for the box.
  158. float* data = (float*)outs[i].data;
  159. for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
  160. {
  161. Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
  162. Point classIdPoint;
  163. double confidence;
  164. // Get the value and location of the maximum score
  165. minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
  166. if (confidence > confThreshold)
  167. {
  168. int centerX = (int)(data[0] * frame.cols);
  169. int centerY = (int)(data[1] * frame.rows);
  170. int width = (int)(data[2] * frame.cols);
  171. int height = (int)(data[3] * frame.rows);
  172. int left = centerX - width / 2;
  173. int top = centerY - height / 2;
  174. classIds.push_back(classIdPoint.x);
  175. confidences.push_back((float)confidence);
  176. boxes.push_back(Rect(left, top, width, height));
  177. }
  178. }
  179. }
  180. // Perform non maximum suppression to eliminate redundant overlapping boxes with
  181. // lower confidences
  182. vector<int> indices;
  183. NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
  184. for (size_t i = 0; i < indices.size(); ++i)
  185. {
  186. int idx = indices[i];
  187. Rect box = boxes[idx];
  188. drawPred(classIds[idx], confidences[idx], box.x, box.y,
  189. box.x + box.width, box.y + box.height, frame);
  190. }
  191. }
  192. // Draw the predicted bounding box
  193. void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame)
  194. {
  195. //Draw a rectangle displaying the bounding box
  196. rectangle(frame, Point(left, top), Point(right, bottom), Scalar(255, 178, 50), 3);
  197. //Get the label for the class name and its confidence
  198. string label = format("%.2f", conf);
  199. if (!classes.empty())
  200. {
  201. CV_Assert(classId < (int)classes.size());
  202. label = classes[classId] + ":" + label;
  203. }
  204. //Display the label at the top of the bounding box
  205. int baseLine;
  206. Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  207. top = max(top, labelSize.height);
  208. rectangle(frame, Point(left, top - round(1.5*labelSize.height)), Point(left + round(1.5*labelSize.width), top + baseLine), Scalar(255, 255, 255), FILLED);
  209. putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 0), 1);
  210. }
  211. // Get the names of the output layers
  212. vector<String> getOutputsNames(const Net& net)
  213. {
  214. static vector<String> names;
  215. if (names.empty())
  216. {
  217. //Get the indices of the output layers, i.e. the layers with unconnected outputs
  218. vector<int> outLayers = net.getUnconnectedOutLayers();
  219. //get the names of all the layers in the network
  220. vector<String> layersNames = net.getLayerNames();
  221. // Get the names of the output layers in names
  222. names.resize(outLayers.size());
  223. for (size_t i = 0; i < outLayers.size(); ++i)
  224. names[i] = layersNames[outLayers[i] - 1];
  225. }
  226. return names;
  227. }

2.2 Python代码 

    使用cv_dnn_forward获得预测输出outs是三个二维的数组,每个二维数组是一个feature_map的输出结果,feature_map中每一行是一个预测值:

outs:[507*85 =13*13*3*(5+80),
        2028*85=26*26*3*(5+80),
        8112*85=52*52*3*(5+80)]

每一个行:85=[x,y,w,h,confs,class_probs_0,class_probs_1,..,class_probs_78,class_probs_79]

  1. # -*-coding: utf-8 -*-
  2. """
  3. @Project: tensorflow-yolov3
  4. @File : opencv_dnn_yolov3.py
  5. @Author : panjq
  6. @E-mail : pan_jinquan@163.com
  7. @Date : 2019-01-28 14:36:00
  8. """
  9. import cv2 as cv
  10. import numpy as np
  11. def read_class(file):
  12. with open(file, 'rt') as f:
  13. classes = f.read().rstrip('\n').split('\n')
  14. return classes
  15. class cv_yolov3(object):
  16. def __init__(self,class_path,net_width,net_height,confThreshold,nmsThreshold):
  17. '''
  18. Initialize the parameters
  19. :param class_path:
  20. :param net_width: default 416, Width of network's input image
  21. :param net_height: default 416,Height of network's input image
  22. :param confThreshold: default 0.5, Confidence threshold
  23. :param nmsThreshold: default 0.5,Non-maximum suppression threshold
  24. '''
  25. self.classes = read_class(class_path)
  26. self.net_width=net_width
  27. self.net_height=net_height
  28. self.confThreshold=confThreshold
  29. self.nmsThreshold=nmsThreshold
  30. def cv_dnn_init(self,modelConfiguration,modelWeights):
  31. '''
  32. Give the configuration and weight files for the model and load the network using them.
  33. eg:
  34. modelConfiguration = "checkpoint-bk/yolov3.cfg";
  35. modelWeights = "checkpoint-bk/yolov3.weights";
  36. :param modelConfiguration:
  37. :param modelWeights:
  38. :return:
  39. '''
  40. self.net = cv.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
  41. self.net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
  42. self.net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)
  43. def getOutputsNames(self,net):
  44. '''
  45. Get the names of the output layers
  46. :param net:
  47. :return:
  48. '''
  49. # Get the names of all the layers in the network
  50. layersNames = net.getLayerNames()
  51. # Get the names of the output layers, i.e. the layers with unconnected outputs
  52. return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]
  53. def drawPred(self,frame,classes,classId, conf, left, top, right, bottom):
  54. '''
  55. Draw the predicted bounding box
  56. :param frame:
  57. :param classes:
  58. :param classId:
  59. :param conf:
  60. :param left:
  61. :param top:
  62. :param right:
  63. :param bottom:
  64. :return:
  65. '''
  66. # Draw a bounding box.
  67. cv.rectangle(frame, (left, top), (right, bottom), (255, 178, 50), 3)
  68. label = '%.2f' % conf
  69. # Get the label for the class name and its confidence
  70. if classes:
  71. assert (classId < len(classes))
  72. label = '%s:%s' % (classes[classId], label)
  73. # Display the label at the top of the bounding box
  74. labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
  75. top = max(top, labelSize[1])
  76. cv.rectangle(frame, (left, top - round(1.5 * labelSize[1])), (left + round(1.5 * labelSize[0]), top + baseLine),
  77. (255, 255, 255), cv.FILLED)
  78. cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)
  79. def postprocess(self,frame, classes,outs):
  80. '''
  81. Remove the bounding boxes with low confidence using non-maxima suppression
  82. :param frame:
  83. :param classes:
  84. :return: outs:[507*85 =(13*13*3)*(5+80),
  85. 2028*85=(26*26*3)*(5+80),
  86. 8112*85=(52*52*3)*(5+80)]
  87. outs中每一行是一个预测值:[x,y,w,h,confs,class_probs_0,class_probs_1,..,class_probs_78,class_probs_79]
  88. :return:
  89. '''
  90. frameHeight = frame.shape[0]
  91. frameWidth = frame.shape[1]
  92. # Scan through all the bounding boxes output from the network and keep only the
  93. # ones with high confidence scores. Assign the box's class label as the class with the highest score.
  94. classIds = []
  95. confidences = []
  96. boxes = []
  97. for out in outs:
  98. for detection in out:
  99. scores = detection[5:]
  100. classId = np.argmax(scores)
  101. confidence = scores[classId]
  102. if confidence > self.confThreshold:
  103. center_x = int(detection[0] * frameWidth)
  104. center_y = int(detection[1] * frameHeight)
  105. width = int(detection[2] * frameWidth)
  106. height = int(detection[3] * frameHeight)
  107. left = int(center_x - width / 2)
  108. top = int(center_y - height / 2)
  109. classIds.append(classId)
  110. confidences.append(float(confidence))
  111. boxes.append([left, top, width, height])
  112. # Perform non maximum suppression to eliminate redundant overlapping boxes with
  113. # lower confidences.
  114. indices = cv.dnn.NMSBoxes(boxes, confidences, self.confThreshold, self.nmsThreshold)
  115. for i in indices:
  116. i = i[0]
  117. box = boxes[i]
  118. left = box[0]
  119. top = box[1]
  120. width = box[2]
  121. height = box[3]
  122. self.drawPred(frame,classes,classIds[i], confidences[i], left, top, left + width, top + height)
  123. def cv_dnn_forward(self,frame):
  124. '''
  125. :param frame:
  126. :return: outs:[507*85 =13*13*3*(5+80),
  127. 2028*85=26*26*3*(5+80),
  128. 8112*85=52*52*3*(5+80)]
  129. '''
  130. # Create a 4D blob from a frame.
  131. blob = cv.dnn.blobFromImage(frame, 1 / 255, (self.net_width, self.net_height), [0, 0, 0], 1, crop=False)
  132. # Sets the input to the network
  133. self.net.setInput(blob)
  134. # Runs the forward pass to get output of the output layers
  135. outs = self.net.forward(self.getOutputsNames(self.net))
  136. # Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
  137. runtime, _ = self.net.getPerfProfile()
  138. return outs,runtime
  139. def yolov3_predict(self,image_path):
  140. '''
  141. :param image_path:
  142. :return:
  143. '''
  144. # Process inputs
  145. winName = 'Deep learning object detection in OpenCV'
  146. cv.namedWindow(winName, cv.WINDOW_NORMAL)
  147. frame=cv.imread(image_path)
  148. outs,runtime=self.cv_dnn_forward(frame)
  149. # Remove the bounding boxes with low confidence
  150. self.postprocess(frame, self.classes, outs)
  151. label = 'Inference time: %.2f ms' % (runtime * 1000.0 / cv.getTickFrequency())
  152. cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
  153. cv.imshow(winName, frame)
  154. cv.waitKey(0)
  155. if __name__=="__main__":
  156. confThreshold = 0.5 # Confidence threshold
  157. nmsThreshold = 0.5 # Non-maximum suppression threshold
  158. net_input_width = 416 # Width of network's input image
  159. net_input_height = 416 # Height of network's input image
  160. image_path = "./data/demo_data/dog.jpg"
  161. # anchors_path = './data/coco_anchors.txt'
  162. classesFile = './data/coco.names'
  163. modelConfiguration = "model/yolov3.cfg";
  164. modelWeights = "model/yolov3.weights";
  165. cv_model=cv_yolov3(classesFile,net_input_width,net_input_height,confThreshold,nmsThreshold)
  166. cv_model.cv_dnn_init(modelConfiguration,modelWeights)
  167. cv_model.yolov3_predict(image_path)

 


3、YOLO的缺点

  • YOLO对相互靠的很近的物体,还有很小的群体 检测效果不好,这是因为一个网格中只预测了两个框,并且只属于一类。
  • 对测试图像中,同一类物体出现的新的不常见的长宽比和其他情况是。泛化能力偏弱。
  • 由于损失函数的问题,定位误差是影响检测效果的主要原因。尤其是大小物体的处理上,还有待加强。

4、参考资料:

[1].《论文阅读笔记:You Only Look Once: Unified, Real-Time Object Detection》https://blog.csdn.net/tangwei2014/article/details/50915317

[2]. https://blog.csdn.net/xiaohu2022/article/details/79211732 

[3]. https://blog.csdn.net/u014380165/article/details/72616238 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/390988
推荐阅读
相关标签
  

闽ICP备14008679号