使用OpenCV进行对象检测

项目所需

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)

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值