欢迎访问我的网站,希望内容对您有用,感兴趣的可以加入我们的社群。

在OpenCV中使用YOLOv3进行物体检测

OpenCV 迷途小书童 5年前 (2019-12-16) 5691次浏览 2个评论

软硬件环境

  • ubuntu 18.04 64bit
  • NVIDIA GTX 1070Ti 8G
  • anaconda with python 3.6
  • opencv 3.4.3
  • cuda 9.0
  • YOLO v3

前言

下图是近年来物体检测领域算法的演化,YOLO是目前公认的比较准确的物体检测算法,已经发展到了第三个版本。关于darknet(实现YOLO检测的开源项目)的基本情况,参考之前的博文 https://xugaoxiang.com/2019/12/16/darknet-basic/,里面有比较详细的阐述。

state-of-the-art-in-object-detection

准备工作

下载YOLO检测需要用到的配置文件、weights模型文件及物体类型class文件

  1. wget https://pjreddie.com/media/files/yolov3.weights
  2. wget https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg?raw=true -O ./yolov3.cfg
  3. wget https://github.com/pjreddie/darknet/blob/master/data/coco.names?raw=true -O ./coco.names

YOLO的基本原理

一般来讲,物体检测由两部分组成,物体定位(object locator)和物体识别(object recognizer)。下面以图片为例来讲YOLO的实现原理

  1. 将图片分割成13x13大小的网格单元, 以一张416x416像素大小的图片为例,会有1024个网格单元,模型会在每个cell上预测bounding box
  2. 每个cell有可能会被预测出多个bounding box,大部分bounding box最后都会被清除,因为它们的相似度太低。这里有个算法叫Non-Maximum Suppression,翻译过来叫非极大值抑制,可参考论文Efficient-Non-Maximum-Suppression, NMS算法是用来提取相似度最高的

python代码

opencv 3.4.2及以上的版本已经支持darknet,同时也支持使用其他常见深度学习框架的模型,如torchtensorflowcaffe等。

  1. # -*- coding: utf-8 -*-
  2. # @time : 18-10-26 下午4:47
  3. # @author : xugaoxiang
  4. # @email : xugx.ai@gmail.com
  5. # @website : https://xugaoxiang.com
  6. # @file : opencv_yolov3.py
  7. # @software: PyCharm
  8. # Usage example: python3 opencv_yolov3.py --image=test.png
  9. import sys
  10. import cv2
  11. import argparse
  12. import numpy as np
  13. import os.path
  14. # 参数初始化
  15. # 相似度阈值
  16. confThreshold = 0.5 # Confidence threshold
  17. # NMS算法阈值
  18. nmsThreshold = 0.4
  19. # 输入图片的宽和高
  20. inpWidth = 416
  21. inpHeight = 416
  22. parser = argparse.ArgumentParser(description = 'Object detection using YOLOv3 in opencv')
  23. parser.add_argument('--image', help = 'Path to image file.')
  24. args = parser.parse_args()
  25. # 导入物体类别class文件,默认支持80种
  26. classesFile = "coco.names"
  27. classes = None
  28. with open(classesFile, 'rt') as f :
  29. classes = f.read().rstrip('\n').split('\n')
  30. # yolo v3的配置及weights文件
  31. modelConfiguration = "yolov3.cfg"
  32. modelWeights = "yolov3.weights"
  33. # opencv读取外部模型
  34. net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
  35. net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
  36. # 这里使用CPU,如果想使用GPU的话,参数是DNN_TARGET_OPENCL, 但是当前版本只支持interl GPU,如果是其它GPU的话,会自动切换到CPU模式
  37. net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
  38. # Get the names of the output layers
  39. def getOutputsNames(net) :
  40. # Get the names of all the layers in the network
  41. layersNames = net.getLayerNames()
  42. # Get the names of the output layers, i.e. the layers with unconnected outputs
  43. return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]
  44. # 画bounding box
  45. def drawPred(classId, conf, left, top, right, bottom) :
  46. # Draw a bounding box.
  47. cv2.rectangle(frame, (left, top), (right, bottom), (255, 178, 50), 3)
  48. label = '%.2f' % conf
  49. # Get the label for the class name and its confidence
  50. if classes :
  51. assert (classId < len(classes))
  52. label = '%s:%s' % (classes[classId], label)
  53. # Display the label at the top of the bounding box
  54. labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
  55. top = max(top, labelSize[1])
  56. cv2.rectangle(frame, (left, top - round(1.5 * labelSize[1])), (left + round(1.5 * labelSize[0]), top + baseLine),
  57. (255, 255, 255), cv2.FILLED)
  58. cv2.putText(frame, label, (left, top), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)
  59. # 使用NMS算法,丢弃低相似度的bounding box
  60. def postprocess(frame, outs) :
  61. frameHeight = frame.shape[0]
  62. frameWidth = frame.shape[1]
  63. classIds = []
  64. confidences = []
  65. boxes = []
  66. # Scan through all the bounding boxes output from the network and keep only the
  67. # ones with high confidence scores. Assign the box's class label as the class with the highest score.
  68. classIds = []
  69. confidences = []
  70. boxes = []
  71. for out in outs :
  72. for detection in out :
  73. scores = detection[5 :]
  74. classId = np.argmax(scores)
  75. confidence = scores[classId]
  76. if confidence > confThreshold :
  77. center_x = int(detection[0] * frameWidth)
  78. center_y = int(detection[1] * frameHeight)
  79. width = int(detection[2] * frameWidth)
  80. height = int(detection[3] * frameHeight)
  81. left = int(center_x - width / 2)
  82. top = int(center_y - height / 2)
  83. classIds.append(classId)
  84. confidences.append(float(confidence))
  85. boxes.append([left, top, width, height])
  86. # Perform non maximum suppression to eliminate redundant overlapping boxes with
  87. # lower confidences.
  88. indices = cv2.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)
  89. for i in indices :
  90. i = i[0]
  91. box = boxes[i]
  92. left = box[0]
  93. top = box[1]
  94. width = box[2]
  95. height = box[3]
  96. drawPred(classIds[i], confidences[i], left, top, left + width, top + height)
  97. # Process inputs
  98. winName = 'Deep learning object detection in OpenCV'
  99. cv2.namedWindow(winName, cv2.WINDOW_NORMAL)
  100. if (args.image) :
  101. if not os.path.isfile(args.image) :
  102. print('Input image file {} does not exist.'.format(args.image))
  103. sys.exit(1)
  104. frame = cv2.imread(args.image, cv2.IMREAD_ANYCOLOR)
  105. outputFile = args.image[:-4] + '_yolov3_out.png'
  106. # Create a 4D blob from a frame.
  107. blob = cv2.dnn.blobFromImage(frame, 1 / 255, (inpWidth, inpHeight), [0, 0, 0], 1, crop = False)
  108. # Sets the input to the network
  109. net.setInput(blob)
  110. # Runs the forward pass to get output of the output layers
  111. outs = net.forward(getOutputsNames(net))
  112. # Remove the bounding boxes with low confidence
  113. postprocess(frame, outs)
  114. cv2.imshow(winName, frame)
  115. cv2.imwrite(outputFile, frame)
  116. cv2.destroyAllWindows()

测试程序输出

opencv_yolov3_bird

opencv_yolov3_person

参考资料

喜欢 (1)

您必须 登录 才能发表评论!

(2)个小伙伴在吐槽
  1. 您好,为什么我的窗口关闭特别快呢,没有报错,是opencv版本的问题吗
    小枫2023-12-18 13:55