先看程序实现效果

1. 设置环境

IDE:PyCharm
Python:3.8
新建 project: Yolov3

安装依赖 Package, (路径: 菜单 PyCharm > Preferences > Project: Yolov3 > Python Interpreter)

  1. numpy
  2. opencv-python

2. 下载对象名字coco.names, 参数文件cfg,和权重文件 weights

coco.names 下载
https://github.com/pjreddie/darknet/blob/master/data/coco.names

cfg和weights,这里会下载两套,一套是精准预测YOLOv3-320,一套是速度比较快不那么精准YOLOv3-tiny。 下载请点击下面的链接,注意名字要命名为

  1. yolov3-320.cfg
  2. yolov3-320.weights
  3. yolov3-tiny.cfg
  4. yolov3-tiny.weights
Model Train Test mAP FLOPS FPS Cfg Weights
YOLOv3-320 COCO trainval test-dev 51.5 38.97 Bn 45 cfg weights
YOLOv3-416 COCO trainval test-dev 55.3 65.86 Bn 35 cfg weights
YOLOv3-608 COCO trainval test-dev 57.9 140.69 Bn 20 cfg weights
YOLOv3-tiny COCO trainval test-dev 33.1 5.56 Bn 220 cfg weights

3. 代码实现

3.1 开发摄像头,持续读取图像

import cv2
import numpy as npcap = cv2.VideoCapture(0)with open(classesFile, 'rt') as f:classNames = f.read().rstrip('\n').split('\n')while True:success, img = cap.read()cv2.imshow('Image', img)cv2.waitKey(1)

3.2 读取可以识别的物体coco.names, 权重和参数

import cv2
import numpy as npcap = cv2.VideoCapture(0)classesFile = 'coco.names'
classNames = []
with open(classesFile, 'rt') as f:classNames = f.read().rstrip('\n').split('\n')
# print(classNames)
# print(len(classNames))modelConfiguration = 'yolov3-320.cfg'
modelWeights = 'yolov3-320.weights'# modelConfiguration = 'yolov3-tiny.cfg'
# modelWeights = 'yolov3-tiny.weights'net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)


3.3 cnn神经网络前向传播,计算预测值,有3个输出layers

import cv2
import numpy as npcap = cv2.VideoCapture(0)
whT = 320
confThreshold = 0.5
nmsThreshold = 0.3# ...while True:success, img = cap.read()blob = cv2.dnn.blobFromImage(img, 1/255, (whT, whT), [0,0,0], 1, crop=False)net.setInput(blob)layerNames = net.getLayerNames()# print(layerNames)outputNames = [layerNames[i[0]-1] for i in net.getUnconnectedOutLayers()]# print(outputNames)# print(net.getUnconnectedOutLayers())outputs = net.forward(outputNames)# print(outputs[0].shape)# print(outputs[1].shape)# print(outputs[2].shape)# print(outputs[0][0])#...cv2.imshow('Image', img)cv2.waitKey(1)


三个layers 输出参数shape如下:

  1. (300, 85)
  2. (1200, 85)
  3. (4800, 85)

    80个数据分类:
  4. 位置(0~3):cx,cy,w,h
  5. 检测到物体的概率(4):confidence
  6. 每一个物体的概率比如(5~79):比如car 0.93

3.4 检测物体,去除概率比较低的检测,只留下最大的,添加物体框,物体名字,概率

def findObjects(outputs, img):hT, wT, cT = img.shapebbox = []classIds = []confs = []for output in outputs:for det in output:scores = det[5:]classId = np.argmax(scores)confidence = scores[classId]if confidence > confThreshold:w, h = int(det[2]*wT), int(det[3]*hT)x, y = int((det[0]*wT) - w/2), int((det[1]*hT) - h/2)bbox.append([x,y,w,h])classIds.append(classId)confs.append(float(confidence))#print(len(bbox))indices = cv2.dnn.NMSBoxes(bbox,confs,confThreshold,nmsThreshold)# print(indices)for i in indices:i = i[0]box = bbox[i]x,y,w,h = box[0], box[1], box[2], box[3]cv2.rectangle(img,(x,y),(x+w, y+h),(255,0,255),2)cv2.putText(img,f'{classNames[classIds[i]].upper()} {int(confs[i]*100)}%', (x,y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6,(255,0,255),2)

3.5 为了更快检测,可以替换参数pkg和权重weights为tiny

 modelConfiguration = 'yolov3-tiny.cfg'modelWeights = 'yolov3-tiny.weights'

4. 完整代码如下

import cv2
import numpy as npcap = cv2.VideoCapture(0)
whT = 320
confThreshold = 0.5
nmsThreshold = 0.3classesFile = 'coco.names'
classNames = []
with open(classesFile, 'rt') as f:classNames = f.read().rstrip('\n').split('\n')
# print(classNames)
# print(len(classNames))modelConfiguration = 'yolov3-320.cfg'
modelWeights = 'yolov3-320.weights'# modelConfiguration = 'yolov3-tiny.cfg'
# modelWeights = 'yolov3-tiny.weights'net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)def findObjects(outputs, img):hT, wT, cT = img.shapebbox = []classIds = []confs = []for output in outputs:for det in output:scores = det[5:]classId = np.argmax(scores)confidence = scores[classId]if confidence > confThreshold:w, h = int(det[2]*wT), int(det[3]*hT)x, y = int((det[0]*wT) - w/2), int((det[1]*hT) - h/2)bbox.append([x,y,w,h])classIds.append(classId)confs.append(float(confidence))#print(len(bbox))indices = cv2.dnn.NMSBoxes(bbox,confs,confThreshold,nmsThreshold)# print(indices)for i in indices:i = i[0]box = bbox[i]x,y,w,h = box[0], box[1], box[2], box[3]cv2.rectangle(img,(x,y),(x+w, y+h),(255,0,255),2)cv2.putText(img,f'{classNames[classIds[i]].upper()} {int(confs[i]*100)}%', (x,y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6,(255,0,255),2)while True:success, img = cap.read()blob = cv2.dnn.blobFromImage(img, 1/255, (whT, whT), [0,0,0], 1, crop=False)net.setInput(blob)layerNames = net.getLayerNames()# print(layerNames)outputNames = [layerNames[i[0]-1] for i in net.getUnconnectedOutLayers()]# print(outputNames)# print(net.getUnconnectedOutLayers())outputs = net.forward(outputNames)# print(outputs[0].shape)# print(outputs[1].shape)# print(outputs[2].shape)# print(outputs[0][0])findObjects(outputs, img)cv2.imshow('Image', img)cv2.waitKey(1)

参考

  1. https://www.youtube.com/watch?v=GGeF_3QOHGE&ab_channel=Murtaza%27sWorkshop-RoboticsandAI
  2. https://www.youtube.com/watch?v=9AycYn9gj1U&ab_channel=Murtaza%27sWorkshop-RoboticsandAI
  3. https://www.youtube.com/watch?v=xK4li3jinSw&ab_channel=Murtaza%27sWorkshop-RoboticsandAI
  4. https://www.youtube.com/watch?v=xK4li3jinSw&ab_channel=Murtaza%27sWorkshop-RoboticsandAI
  5. https://pjreddie.com/darknet/yolo/

一步一步手写实现实时监测物体YOLO v3 EASY METHOD | OpenCV Python CNN卷积神经网络相关推荐

  1. TensorFlow 2.0 mnist手写数字识别(CNN卷积神经网络)

    TensorFlow 2.0 (五) - mnist手写数字识别(CNN卷积神经网络) 源代码/数据集已上传到 Github - tensorflow-tutorial-samples 大白话讲解卷积 ...

  2. 深度学习--TensorFlow(项目)识别自己的手写数字(基于CNN卷积神经网络)

    目录 基础理论 一.训练CNN卷积神经网络 1.载入数据 2.改变数据维度 3.归一化 4.独热编码 5.搭建CNN卷积神经网络 5-1.第一层:第一个卷积层 5-2.第二层:第二个卷积层 5-3.扁 ...

  3. 深度篇—— CNN 卷积神经网络(四) 使用 tf cnn 进行 mnist 手写数字 代码演示项目

    返回主目录 返回 CNN 卷积神经网络目录 上一章:深度篇-- CNN 卷积神经网络(三) 关于 ROI pooling 和 ROI Align 与 插值 本小节,细说 使用 tf cnn 进行 mn ...

  4. 【FPGA教程案例100】深度学习1——基于CNN卷积神经网络的手写数字识别纯Verilog实现,使用mnist手写数字数据库

    FPGA教程目录 MATLAB教程目录 ---------------------------------------- 目录 1.软件版本 2.CNN卷积神经网络的原理 2.1 mnist手写数字数 ...

  5. 卷积神经网络mnist手写数字识别代码_搭建经典LeNet5 CNN卷积神经网络对Mnist手写数字数据识别实例与注释讲解,准确率达到97%...

    LeNet-5卷积神经网络是最经典的卷积网络之一,这篇文章就在LeNet-5的基础上加入了一些tensorflow的有趣函数,对LeNet-5做了改动,也是对一些tf函数的实例化笔记吧. 环境 Pyc ...

  6. Tensorflow之 CNN卷积神经网络的MNIST手写数字识别

    点击"阅读原文"直接打开[北京站 | GPU CUDA 进阶课程]报名链接 作者,周乘,华中科技大学电子与信息工程系在读. 前言 tensorflow中文社区对官方文档进行了完整翻 ...

  7. 用Keras搭建神经网络 简单模版(三)—— CNN 卷积神经网络(手写数字图片识别)...

    # -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) #for reproducibility再现性 from keras.d ...

  8. 基于CNN卷积神经网络的minst数据库手写字识别

    up目录 一.理论基础 二.核心程序 三.测试结果 一.理论基础 卷积神经网络(Convolutional Neural Networks, CNN)是一类包含卷积计算且具有深度结构的前馈神经网络(F ...

  9. CNN卷积神经网络实现手写数字识别(基于tensorflow)

    1.1卷积神经网络简介 文章目录 1.1卷积神经网络简介 1.2 神经网络 1.2.1 神经元模型 1.2.2 神经网络模型 1.3 卷积神经网络 1.3.1卷积的概念 1.3.2 卷积的计算过程 1 ...

  10. 基于tensorflow、keras利用emnist数据集构建CNN卷积神经网络进行手写字母识别

    EMNIST 数据集是一个包含手写字母,数字的数据集,它具有和MNIST相同的数据格式.The EMNIST Dataset | NIST 引用模块介绍: import tensorflow as t ...

最新文章

  1. 神操作!一行Python代码搞定一款游戏?给力!
  2. 带你深度解析Maven
  3. STM32使用DMA从串口读数据到内存
  4. 反模式? 只有模式不彻底吧
  5. C shell命令行解释器
  6. SWFTools pdf2swf 参数详解
  7. 神策数据杨宁:财富管理转型趋势下的精细化运营
  8. 一文搞定Vim/Vi编辑器
  9. 淘宝 oracle mysql_Oracle 与 MySQL 的区别-阿里云开发者社区
  10. python gis库_使用开放的python库自动化GIS和遥感工作流
  11. WebSocket使用中Stomp Client连接报ERROR CODE 200的解决办法
  12. ACCESS-类型转换函数
  13. java实现邮件定时发送
  14. 在二维数组中寻找最小鞍点1181(该行最大值,该列最小值)(2种思路)
  15. 读书:海明威的《老人与海》
  16. Win10系列:WinJS库控件
  17. 360来硬拼,云盘免费用了!新注册就能获得36T容量!
  18. 闪耀DTCC | 腾讯云——提供全球领先的专业云计算服务
  19. Mysql 语句的优化技巧
  20. 单选按钮html图片,js实现的 图片单选按钮效果

热门文章

  1. python函数作用的描述_python基础之函数内容介绍
  2. 写失败数据写入成功_深度 | 缓存与数据库一致性问题剖析
  3. linux去除内容重复行,实例详细说明linux下去除重复行命令uniq
  4. 【转】python eval
  5. springboot 程序发布到tomcat运行
  6. jvm学习--类加载器
  7. 乐在其中设计模式(C#) - 状态模式(State Pattern)
  8. 开源日志系统log4cplus(五)
  9. CSDN答疑:按行累加
  10. 在ASP.NET 2.0中实现URL重写