文章目录

  • 1.MobileNetV1轻量化网络结构
  • 2.MobileNetV2轻量化网络结构
  • 3.前置准备
    • (1)MobileNetSSD_300x300.prototxt描述文件下载
    • (2)MobileNet_SSD.caffemodel下载
  • 3.正文
    • (1)初始化操作
    • (2)预测类别
    • (3)读取相关文件
    • (4)对图像进行预处理和设置网络的输入
    • (5)对图像进一步处理
    • (6)遍历预测的结果
    • (7)对单张图片进行预测
    • (8)实时检测
    • (9)完整代码

1.MobileNetV1轻量化网络结构

https://mydreamambitious.blog.csdn.net/article/details/124560414


2.MobileNetV2轻量化网络结构

https://mydreamambitious.blog.csdn.net/article/details/124617584


3.前置准备

(1)MobileNetSSD_300x300.prototxt描述文件下载

注:虽然我们这里使用的python中的opencv来实现GoogleNet图像分类,可是我们需要GoogleNet模型的描述文件和分类文件,所以我们这里需要下载Opencv-3-3-0,从里面获取描述文件和分类文件:
https://www.raoyunsoft.com/opencv/opencv-3.3.0/

下载好Opencv-3-3-0压缩包之后,解压,打开以下路径即可找到MobileNetSSD_300x300.prototxt

(2)MobileNet_SSD.caffemodel下载

git clone https://github.com/chuanqi305/MobileNet-SSD.git

或者从百度网盘下载亦可以:
链接:https://pan.baidu.com/s/1S9GrYB-G_iS1wodrYjsdbw
提取码:gqpu

3.正文

(1)初始化操作

import os
import cv2
import cvzone
import numpy as np#设置图片的宽度和高度
img_width,img_heigth=300,300
#得到图像的高宽比
WHRatio=img_width/float(img_heigth)
#设置图片的缩放因子
ScaleFactor=0.007843
#设置平均数
meanVal=127.5
#设置置信度阈值
threshod=0.2

(2)预测类别

#mobileNetSSD可以检测类别数21=20+1(背景)
classNames = ['background','aeroplane', 'bicycle', 'bird', 'boat','bottle', 'bus', 'car', 'cat', 'chair','cow', 'diningtable', 'dog', 'horse','motorbike', 'person', 'pottedplant','sheep', 'sofa', 'train', 'tvmonitor']

(3)读取相关文件

#加载文件
net=cv2.dnn.readNetFromCaffe(prototxt='modelCaffe//MobileNetSSD_300x300.prototxt',caffeModel='modelCaffe//mobilenet_iter_73000.caffemodel')

(4)对图像进行预处理和设置网络的输入

# 对图片进行预处理blob = cv2.dnn.blobFromImage(image=imgSize, scalefactor=ScaleFactor,size=(img_width, img_heigth), mean=meanVal)# 设置网络的输入并进行前向传播net.setInput(blob)detections = net.forward()

(5)对图像进一步处理

# 对图像进行按比例裁剪height,width,channel=np.shape(imgSize)if width/float(height)>WHRatio:cropSize=(int(height*WHRatio),height)else:cropSize = (width,int(width / WHRatio))y1=int((height-cropSize[1])/2)y2=int(y1+cropSize[1])x1=int((width-cropSize[0])/2)x2=int(x1+cropSize[0])imgSize=imgSize[y1:y2,x1:x2]height,width,channel=np.shape(imgSize)

(6)遍历预测的结果


打开文件:MobileNetSSD_300x300.prototxt末尾上面的第一个参数之所以为0,表示背景。

#遍历检测的目标print('detection.shape: {}'.format(detections.shape))print('detection: {}'.format(detections))for i in range(detections.shape[2]):#预测的置信度保留两位小数confidence=round(detections[0,0,i,2]*100,2)if confidence>threshod:#预测类别的idclass_id=int(detections[0,0,i,1])xLeftBottom=int(detections[0,0,i,3]*width)yLeftBottom=int(detections[0,0,i,4]*height)xRightTop=int(detections[0,0,i,5]*width)yRightTop=int(detections[0,0,i,6]*height)cv2.rectangle(img=imgSize,pt1=(xLeftBottom,yLeftBottom),pt2=(xRightTop,yRightTop),color=(0,255,0),thickness=2)label=classNames[class_id]+": "+str(confidence)labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)cvzone.putTextRect(img=imgSize,text=label,pos=(xLeftBottom+9,yLeftBottom-12),scale=1,thickness=1,colorR=(0,255,0))return imgSize

(7)对单张图片进行预测

#对单张图片进行检测
def SignalDetect(img_path='images//6.png'):imgSize=cv2.imread(img_path)imgSize=processImage(imgSize)cv2.imshow('imgSize', imgSize)cv2.waitKey(0)cv2.destroyAllWindows()

(8)实时检测

#实时检测
def detectTime():cap=cv2.VideoCapture(0)while cap.isOpened():ret,frame=cap.read()frame=cv2.resize(src=frame,dsize=(520,520))frame=cv2.flip(src=frame,flipCode=2)frame=processImage(frame)cv2.imshow('frame',frame)key=cv2.waitKey(1)if key==27:breakcap.release()cv2.destroyAllWindows()

(9)完整代码

import os
import cv2
import cvzone
import numpy as np#设置图片的宽度和高度
img_width,img_heigth=300,300
#得到图像的高宽比
WHRatio=img_width/float(img_heigth)
#设置图片的缩放因子
ScaleFactor=0.007843
#设置平均数
meanVal=127.5
#设置置信度阈值
threshod=0.2#mobileNetSSD可以检测类别数21=20+1(背景)
classNames = ['background','aeroplane', 'bicycle', 'bird', 'boat','bottle', 'bus', 'car', 'cat', 'chair','cow', 'diningtable', 'dog', 'horse','motorbike', 'person', 'pottedplant','sheep', 'sofa', 'train', 'tvmonitor']#加载文件
net=cv2.dnn.readNetFromCaffe(prototxt='modelCaffe//MobileNetSSD_300x300.prototxt',caffeModel='modelCaffe//mobilenet_iter_73000.caffemodel')#对图片进行处理和设置网络的输入同时进行前向传播
def processImage(imgSize):# 对图片进行预处理blob = cv2.dnn.blobFromImage(image=imgSize, scalefactor=ScaleFactor,size=(img_width, img_heigth), mean=meanVal)# 设置网络的输入并进行前向传播net.setInput(blob)detections = net.forward()# 对图像进行按比例裁剪height,width,channel=np.shape(imgSize)if width/float(height)>WHRatio:#裁剪多余的宽度cropSize=(int(height*WHRatio),height)else:# 裁剪多余的高度cropSize = (width,int(width / WHRatio))y1=int((height-cropSize[1])/2)y2=int(y1+cropSize[1])x1=int((width-cropSize[0])/2)x2=int(x1+cropSize[0])imgSize=imgSize[y1:y2,x1:x2]height,width,channel=np.shape(imgSize)#遍历检测的目标# print('detection.shape: {}'.format(detections.shape))# print('detection: {}'.format(detections))for i in range(detections.shape[2]):#保留两位小数confidence=round(detections[0,0,i,2]*100,2)if confidence>threshod:class_id=int(detections[0,0,i,1])xLeftBottom=int(detections[0,0,i,3]*width)yLeftBottom=int(detections[0,0,i,4]*height)xRightTop=int(detections[0,0,i,5]*width)yRightTop=int(detections[0,0,i,6]*height)cv2.rectangle(img=imgSize,pt1=(xLeftBottom,yLeftBottom),pt2=(xRightTop,yRightTop),color=(0,255,0),thickness=2)label=classNames[class_id]+": "+str(confidence)labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)cvzone.putTextRect(img=imgSize,text=label,pos=(xLeftBottom+9,yLeftBottom-12),scale=1,thickness=1,colorR=(0,255,0))# cv2.rectangle(imgSize, (xLeftBottom, yLeftBottom - labelSize[1]),#               (xLeftBottom + labelSize[0], yLeftBottom + baseLine),#               (255, 255, 255), cv2.FILLED)# cv2.putText(imgSize, label, (xLeftBottom, yLeftBottom),#             cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))return imgSize#对单张图片进行检测
def SignalDetect(img_path='images//8.png'):imgSize=cv2.imread(img_path)imgSize=processImage(imgSize)cv2.imshow('imgSize', imgSize)cv2.waitKey(0)cv2.destroyAllWindows()#实时检测
def detectTime():cap=cv2.VideoCapture(0)while cap.isOpened():ret,frame=cap.read()frame=cv2.resize(src=frame,dsize=(520,520))frame=cv2.flip(src=frame,flipCode=2)frame=processImage(frame)cv2.imshow('frame',frame)key=cv2.waitKey(1)if key==27:breakcap.release()cv2.destroyAllWindows()if __name__ == '__main__':print('Pycharm')# SignalDetect()detectTime()

使用MobileNet_SSD进行目标检测相关推荐

  1. 微调torchvision 0.3的目标检测模型

    微调torchvision 0.3的目标检测模型 本文将微调在 Penn-Fudan 数据库中对行人检测和分割的已预先训练的 Mask R-CNN 模型.它包含170个图像和345个行人实例,说明如何 ...

  2. 部署可扩展的目标检测管道:推理过程(下)

    部署可扩展的目标检测管道:推理过程(下) 融合 感兴趣的目标可以被遮挡.有时只能看到目标的一小部分(少至几个像素). • 图19.车辆和交通信号灯被遮挡. • 图20:阻塞了总线. • 图21:左侧的 ...

  3. 目标检测推理部署:优化和部署

    目标检测推理部署:优化和部署 本文简要介绍了端对端推理管道的优化技术和部署. 将在以下三个方面研究推理优化过程:硬件优化,软件优化和模型优化.推理优化的关键指标如下: • 吞吐量(未推理图像/秒) • ...

  4. 部署可扩展的目标检测管道:推理过程(上)

    部署可扩展的目标检测管道:推理过程(上) 基于YOLOv3的目标检测推理过程的所有代码都可以在eriklindernoren/PyTorch-YOLOv3 GitHub repo找到. 为了进行审查, ...

  5. 大数据目标检测推理管道部署

    大数据目标检测推理管道部署 本文提供了一个用于对象检测的深度学习推理的概述. 自主车辆软件开发需要大规模的数据.计算和算法创新,这些都是gpu实现的.一组神经网络构成了感知和决策系统的基础.神经网络的 ...

  6. GPU端到端目标检测YOLOV3全过程(下)

    GPU端到端目标检测YOLOV3全过程(下) Ubuntu18.04系统下最新版GPU环境配置 · 安装显卡驱动 · 安装Cuda 10.0 · 安装cuDNN 1.安装显卡驱动 (1)这里采用的是P ...

  7. GPU端到端目标检测YOLOV3全过程(上)

    GPU端到端目标检测YOLOV3全过程(上) Basic Parameters: Video: mp4, webM, avi Picture: jpg, png, gif, bmp Text: doc ...

  8. Mask R-CNN用于目标检测和分割代码实现

    Mask R-CNN用于目标检测和分割代码实现 Mask R-CNN for object detection and instance segmentation on Keras and Tenso ...

  9. 目标检测数据集The Object Detection Dataset

    目标检测数据集The Object Detection Dataset 在目标检测领域,没有像MNIST或Fashion MNIST这样的小数据集.为了快速测试模型,我们将组装一个小数据集.首先,我们 ...

最新文章

  1. 关于MATLAB中xlswrite函数写数据出现服务器异常情况的解决办法
  2. 实习总结之jquery实例
  3. ionic开发ios app
  4. 【最优解法】1030 完美数列 (25分)_23行代码AC
  5. 【POJ - 2631】Roads in the North (树的直径,模板)
  6. java 执行shell 卡住_Aid learning/Termux之Jupyter的Java编程高级篇——包管理
  7. STM8单片机ADC带缓存的连续采样模式
  8. 易天40G QSFP+光模块的规格参数
  9. 【Leetcode】98. 验证二叉搜索树
  10. ios 初体验窗口的创建
  11. 【平面设计】ACDSee5.0软件安装教程
  12. 几款引擎比较 BigWorld Unreal CryEngine等
  13. android 添加pdf字体,为Android添加多国语言包.pdf
  14. 使用 Google Chrome 数据抓包方式免费下载收费音乐
  15. jpi多表联查_多表连接查询详解
  16. 一本《Redis 深度历险》,我能躺挣多少钱?
  17. 中国太平人寿保险承保系统采用ILOG JRules
  18. java 判断字符串重排后是否等于另一个字符串,包括空格符
  19. 郁闷,做了很多无用功
  20. 动态壁纸:酷炫手机壁纸

热门文章

  1. 理光Ricoh Aficio MP 2014 一体机驱动
  2. appletviewer命令详解
  3. 游戏本推荐排行榜哪款好?开箱这款有答案
  4. DX11小知识1-Direct 3D
  5. 深入理解EMA和SMA
  6. 反渗透设备:反渗透纯净水处理设备特点
  7. jsp taglib指令_JSP Taglib指令
  8. FPGA入门(FPGA结构、Verilog编程基础)
  9. LVDS转MIPI 视频旋转芯片 POL8901 图像处理芯片
  10. 企业如何摆脱低效的客户服务,从建立客服中心知识库开始,让企业客服更科学、更智能!