项目所需
1 文件coco.names(存放我们可以检测到的类)
2 图片lena.png(一张人脸图像)
3 权重 frozen_inference_graph.pb
4 配置文件ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt
5 导入opencv-python 库
检测一张图片中的人脸
import cv2
#读入图片
img=cv2.imread('E:/graduatedata/code/OpencvPython/Resources/lena.png')
# 导入数据集
classNames=[]
classFile="E:/graduatedata/code/OpencvPython/Resources/coco.names"
with open(classFile,'rt') as f:
classNames=f.read().rstrip("\n").split("\n")
print(classNames)
# 导入配置文件
comfigPath='E:/graduatedata/code/OpencvPython/Resources/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath='E:/graduatedata/code/OpencvPython/Resources/frozen_inference_graph.pb'
# 检测模型及参数设置
net=cv2.dnn_DetectionModel(weightsPath,comfigPath)
net.setInputSize(320,320)
net.setInputScale(1.0/127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
# ID 配置 边界框
classIds,confs,bbox=net.detect(img,confThreshold=0.5)
# 利用bbox信息创建矩形框 通过ID获取类名设置文本
for classId,confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
cv2.rectangle(img,box,color=(0,255,0),thickness=2)
cv2.putText(img,classNames[classId-1],(box[0]+10,box[1]+30),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
cv2.imshow("Output",img)
cv2.waitKey(0)
通过摄像头检测
import cv2
cap=cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
# 导入数据集
classNames=[]
classFile="E:/graduatedata/code/OpencvPython/Resources/coco.names"
with open(classFile,'rt') as f:
classNames=f.read().rstrip("\n").split("\n")
#print(classNames)
# 导入配置文件
comfigPath='E:/graduatedata/code/OpencvPython/Resources/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath='E:/graduatedata/code/OpencvPython/Resources/frozen_inference_graph.pb'
# 检测模型及参数设置
net=cv2.dnn_DetectionModel(weightsPath,comfigPath)
net.setInputSize(320,320)
net.setInputScale(1.0/127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
while True:
success,img=cap.read()
# ID 配置 边界框
classIds,confs,bbox=net.detect(img,confThreshold=0.5)
# 防止检测到ID为0的对象发生错误
if len(classIds)!=0:
# 利用bbox信息创建矩形框 通过ID获取类名设置文本
for classId,confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
cv2.rectangle(img,box,color=(0,255,0),thickness=2)
cv2.putText(img,classNames[classId-1],(box[0]+10,box[1]+30),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
cv2.imshow("Output",img)
cv2.waitKey(1)
识别摄像头对象并显示对象识别的置信度
import cv2
#img=cv2.imread('E:/graduatedata/code/OpencvPython/Resources/lena.png')
cap=cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
# 导入数据集
classNames=[]
classFile="E:/graduatedata/code/OpencvPython/Resources/coco.names"
with open(classFile,'rt') as f:
classNames=f.read().rstrip("\n").split("\n")
#print(classNames)
# 导入配置文件
comfigPath='E:/graduatedata/code/OpencvPython/Resources/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath='E:/graduatedata/code/OpencvPython/Resources/frozen_inference_graph.pb'
# 检测模型及参数设置
net=cv2.dnn_DetectionModel(weightsPath,comfigPath)
net.setInputSize(320,320)
net.setInputScale(1.0/127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
while True:
success,img=cap.read()
# ID 配置 边界框
classIds,confs,bbox=net.detect(img,confThreshold=0.5)
# 防止检测到ID为0的对象发生错误
if len(classIds)!=0:
# 利用bbox信息创建矩形框 通过ID获取类名设置文本 显示置信度(四舍五入保留两位小数)
for classId,confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
cv2.rectangle(img,box,color=(0,255,0),thickness=2)
cv2.putText(img,classNames[classId-1],(box[0]+10,box[1]+30),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
cv2.putText(img, str(round(confidence*100,2)), (box[0] + 200, box[1] + 30), cv2.FONT_HERSHEY_COMPLEX, 1,
(0, 255, 0), 2)
cv2.imshow("Output",img)
cv2.waitKey(1)
添加最大抑制:使每个对象只识别出一个矩形框
import cv2
import numpy as np
# 检测器的阈值
thres=0.45
# 最大抑制的阈值 值越小 抑制更强
nms_threshold=0.2
# 摄像头
cap=cv2.VideoCapture(0)
# 高度和宽度
cap.set(3,640)
cap.set(4,480)
# 亮度
cap.set(10,150)
# 导入数据集(类名,如果没有这个文件也可以自己写入列表几个名称)
classNames=[]
classFile="E:/graduatedata/code/OpencvPython/Resources/coco.names"
with open(classFile,'rt') as f:
classNames=f.read().rstrip("\n").split("\n")
#print(classNames)
# 导入配置文件
comfigPath='E:/graduatedata/code/OpencvPython/Resources/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath='E:/graduatedata/code/OpencvPython/Resources/frozen_inference_graph.pb'
# 检测模型及参数设置
net=cv2.dnn_DetectionModel(weightsPath,comfigPath)
net.setInputSize(320,320)
net.setInputScale(1.0/127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
while True:
success,img=cap.read()
# ID 配置 边界框
classIds,confs,bbox=net.detect(img,confThreshold=thres)
bbox=list(bbox)
confs=list(np.array(confs).reshape(1,-1)[0])
confs=list(map(float,confs))
# 添加最大抑制 边框 置信度 阈值 抑制程度 该函数返回一个保留的边界框的索引列表
indices = cv2.dnn.NMSBoxes(bbox, confs, thres, nms_threshold)
for i in indices:
box=bbox[i]
x,y,w,h=box[0],box[1],box[2],box[3]
cv2.rectangle(img, (x,y),(x+w,y+h), color=(0, 255, 0), thickness=2)
cv2.putText(img, classNames[classIds[i]-1], (box[0] + 10, box[1] + 30), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0),2)
cv2.imshow("Output", img)
cv2.waitKey(1)