原文出处: https://blog.xugaoxiang.com/ai/use-yolov3-with-opencv-in-python.html

软硬件环境

  • 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://blog.xugaoxiang.com/ai/darknet.html,里面有比较详细的阐述。

准备工作

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

wget https://pjreddie.com/media/files/yolov3.weights
wget https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg?raw=true -O ./yolov3.cfg
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等。

# -*- coding: utf-8 -*-
# @time    : 18-10-26 下午4:47
# @author  : xugaoxiang
# @email   : djstava@gmail.com
# @website : https://xugaoxiang.com
# @file    : opencv_yolov3.py
# @software: PyCharm# Usage example: python3 opencv_yolov3.py --image=test.pngimport sys
import cv2
import argparse
import numpy as np
import os.path# 参数初始化
# 相似度阈值
confThreshold = 0.5  # Confidence threshold# NMS算法阈值
nmsThreshold = 0.4# 输入图片的宽和高
inpWidth = 416
inpHeight = 416parser = argparse.ArgumentParser(description = 'Object detection using YOLOv3 in opencv')
parser.add_argument('--image', help = 'Path to image file.')
args = parser.parse_args()# 导入物体类别class文件,默认支持80种
classesFile = "coco.names"
classes = None
with open(classesFile, 'rt') as f :classes = f.read().rstrip('\n').split('\n')# yolo v3的配置及weights文件
modelConfiguration = "yolov3.cfg"
modelWeights = "yolov3.weights"# opencv读取外部模型
net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
# 这里使用CPU,如果想使用GPU的话,参数是DNN_TARGET_OPENCL, 但是当前版本只支持interl GPU,如果是其它GPU的话,会自动切换到CPU模式
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)# Get the names of the output layers
def getOutputsNames(net) :# Get the names of all the layers in the networklayersNames = net.getLayerNames()# Get the names of the output layers, i.e. the layers with unconnected outputsreturn [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]# 画bounding box
def drawPred(classId, conf, left, top, right, bottom) :# Draw a bounding box.cv2.rectangle(frame, (left, top), (right, bottom), (255, 178, 50), 3)label = '%.2f' % conf# Get the label for the class name and its confidenceif classes :assert (classId < len(classes))label = '%s:%s' % (classes[classId], label)# Display the label at the top of the bounding boxlabelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)top = max(top, labelSize[1])cv2.rectangle(frame, (left, top - round(1.5 * labelSize[1])), (left + round(1.5 * labelSize[0]), top + baseLine),(255, 255, 255), cv2.FILLED)cv2.putText(frame, label, (left, top), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)# 使用NMS算法,丢弃低相似度的bounding box
def postprocess(frame, outs) :frameHeight = frame.shape[0]frameWidth = frame.shape[1]classIds = []confidences = []boxes = []# 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.classIds = []confidences = []boxes = []for out in outs :for detection in out :scores = detection[5 :]classId = np.argmax(scores)confidence = scores[classId]if confidence > confThreshold :center_x = int(detection[0] * frameWidth)center_y = int(detection[1] * frameHeight)width = int(detection[2] * frameWidth)height = int(detection[3] * frameHeight)left = int(center_x - width / 2)top = int(center_y - height / 2)classIds.append(classId)confidences.append(float(confidence))boxes.append([left, top, width, height])# Perform non maximum suppression to eliminate redundant overlapping boxes with# lower confidences.indices = cv2.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)for i in indices :i = i[0]box = boxes[i]left = box[0]top = box[1]width = box[2]height = box[3]drawPred(classIds[i], confidences[i], left, top, left + width, top + height)# Process inputs
winName = 'Deep learning object detection in OpenCV'
cv2.namedWindow(winName, cv2.WINDOW_NORMAL)if (args.image) :if not os.path.isfile(args.image) :print('Input image file {} does not exist.'.format(args.image))sys.exit(1)frame = cv2.imread(args.image, cv2.IMREAD_ANYCOLOR)outputFile = args.image[:-4] + '_yolov3_out.png'# Create a 4D blob from a frame.blob = cv2.dnn.blobFromImage(frame, 1 / 255, (inpWidth, inpHeight), [0, 0, 0], 1, crop = False)# Sets the input to the networknet.setInput(blob)# Runs the forward pass to get output of the output layersouts = net.forward(getOutputsNames(net))# Remove the bounding boxes with low confidencepostprocess(frame, outs)cv2.imshow(winName, frame)cv2.imwrite(outputFile, frame)cv2.destroyAllWindows()

测试程序输出

参考资料

  • https://github.com/pjreddie/darknet/wiki/YOLO:-Real-Time-Object-Detection
  • https://docs.opencv.org/3.4.3/da/d9d/tutorial_dnn_yolo.html
  • https://blog.xugaoxiang.com/ai/darknet.html

在OpenCV中使用YOLO v3进行物体检测相关推荐

  1. OpenCV中使用YOLO对象检测

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文转自:opencv学堂 OpenCV在3.3.1的版本中开始正 ...

  2. yolo v3做行人检测+alexnet做上衣颜色识别

    参考链接: 1)alexnet做上衣颜色识别 2)keras训练自己的数据集 前言: 本项目是基于yolo v3已经训练好的coco数据集的模型进行行人检测的,然后将检测出来的结果传入到alexnet ...

  3. OpenCV中的几种角点检测方法

    1.Harris角点检测 import numpy as np import cv2 as cv import matplotlib.pyplot as pltplt.rcParams['font.s ...

  4. 如何在OpenCV中使用YOLO

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 今天,我们将研究如何在OpenCV框架中使用YOLO.YOLO于2 ...

  5. 目标检测 YOLO v3 训练 人脸检测模型

    YOLO,是You Only Look Once的缩写,一种基于深度卷积神经网络的物体检测算法,YOLO v3是YOLO的第3个版本,检测算法更快更准. 本文源码:https://github.com ...

  6. .pth转.weights/openCV-python + YOLO v3实现目标检测

    文章目录 前言 一.基于Pytorch框架的YOLO v3 二.openCV-python 三..pth转.weights 四 模型部署 总结 前言   毕设做的是水面目标的目标检测,因为要用Tkin ...

  7. OpenCV中基于LBP算法的人脸检测测试代码

    下面是OpenCV 3.3中基于CascadeClassifier类的LBP算法实现的人脸检测,从结果上看,不如其它开源库效果好,如libfacedetection,可参考 https://blog. ...

  8. TensorFlow + Keras 实战 YOLO v3 目标检测图文并茂教程

    作者 | fendouai 编辑 | 安可 [导读]:本文介绍了目标检测算法yolov3的keras实战.欢迎大家点击上方蓝字关注我们的公众号:深度学习与计算机视觉. YOLO 是一种非常流行的目标检 ...

  9. 探索 YOLO v3 实现细节 - 第6篇 预测 (完结)

    YOLO,即You Only Look Once的缩写,是一个基于卷积神经网络(CNN)的物体检测算法.而YOLO v3是YOLO的第3个版本,即YOLO.YOLO 9000.YOLO v3,检测效果 ...

  10. 对象检测目标小用什么模型好_自动驾驶目标检测- YOLO v3 深入解析

    从2016年 Joseph Redmon 发布第一代YOLO开始,YOLO已经更新四代了,凭借着在一个网络模型中完成对图像中所有对象边界框和类别预测的独特创新,成为现今使用最广泛也是最快的对象检测算法 ...

最新文章

  1. 4 信道编码之循环码
  2. shell脚本[] [[]] -n -z 的含义解析
  3. LeetCode - 785. Is Graph Bipartite?
  4. http --- 混合加密的具体过程
  5. leetcode 491. 递增子序列 思考分析
  6. Linux系统管理(6)——Linux下启动Redis服务的几种方法
  7. mount: none already mounted or /cgroup busy
  8. SHT30温湿度传感器使用记录(AVR atmega128)
  9. 结构仿真实验,Midas多跨超静定连续梁手算电算分析
  10. 小米手机安装欧洲版系统(MIUI12) 详细安装教程
  11. 阿里巴巴十周年有感----宗教的盛宴
  12. 虚幻引擎 4 渲染流程分析
  13. Ubuntu 18.04 服务器版安装桌面及软件
  14. linux PWM驱动屏幕亮度及pwm子系统框架(Linux驱动开发篇)
  15. 8种基本数据类型的分类
  16. javascript制作钟表
  17. Android实现仿支付宝流水
  18. 用腾讯云阿里云搭建自己的个人网站
  19. 用 QGIS 画矢量交通路线图
  20. springboot集成阿里MNS消息队列发布订阅消息功能

热门文章

  1. studio one 3 机架声道设置_雅马哈Yamaha AG03/AG06声卡直播机架跳线教程
  2. 研磨设计模式读书笔记
  3. 互联网日报 | 腾讯地图全国上线聚合打车服务;国庆档首日票房突破7亿;特斯拉国产Model3再降价...
  4. 软件流程和管理(四):PMP Stakeholder Management
  5. 基于行政区划的百度个性化地图及遥感影像栅格瓦片下载合成器(可叠加行政区划,适合用作科研遥感影像附图)
  6. 2018美赛 A 题
  7. Ubuntu安装sqlmap
  8. ◎Vbs调用MsAgent组件,很有趣
  9. BlackBerry模拟器中文转换
  10. 新手如何备考GRE考试作文