OpenCV实现目标检测

OpenCV实现目标检测

目标检测是计算机视觉领域的一大重要分支,在自动驾驶等领域发挥着重大作用。本文将介绍如何通过OpenCV实现简单的目标检测。

环境准备

创建并进入虚拟环境

conda create -n opencv python=3.9
conda activate opencv

安装依赖

pip install opencv-python

下载其他文件

链接:https://pan.baidu.com/s/1nW_WE6PqIEmY78gnjmhE7Q
提取码:4d5o
网盘中包含coco.names、权重文件和配置文件
coco.nams包含了一些常见的目标,如

person
bicycle
car
motorcycle
airplane
bus
train
truck
boat
traffic light

图像目标检测

cv2.dnn_DetectionModel()

定义目标检测模型,并可设置权重文件和配置文件

cv2.dnn_DetectionModel(weightsPath,configPath)

net.detect()

进行目标检测

classIds, confs, bbox = net.detect(img, confThreshold=0.5)

cv2.rectangle()

绘制矩形

cv2.rectangle(img, box, color=(0, 255, 0), thickness=2)

cv2.putText()

添加文字

cv2.putText(image, text, (5,50 ), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
参数说明:

  • 图片
  • 要添加的文字
  • 文字添加的位置
  • 字体
  • 字体大小
  • 字体颜色
  • 字体粗细
import cv2

classNames = []
classFile = 'coco.names'
with open(classFile,'rt') as f:
    classNames = f.read().rstrip('\n').split('\n')

# print(classNames)
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'

net = cv2.dnn_DetectionModel(weightsPath,configPath)
net.setInputSize(320,320)
net.setInputScale(1.0/ 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)

img = cv2.imread('1.jpg')

classIds, confs, bbox = net.detect(img, confThreshold=0.5)
# print(classIds, bbox)
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].upper(), (box[0] + 10, box[1] + 30),
                cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)  

cv2.imshow('res', img)
cv2.waitKey(0)

摄像头实时目标检测

除了对图像进行目标检测,OpenCV也可以开启摄像头进行实时目标检测。

cv2.VideoCapture()

开启摄像头
cv2.VideoCapture(0):开启笔记本自带摄像头
cv2.VideoCapture(1):开启USB摄像头

import cv2

thres = 0.45 # Threshold to detect object

cap = cv2.VideoCapture(1)
cap.set(3,1280)
cap.set(4,720)
cap.set(10,70)

classNames= []
classFile = 'coco.names'
with open(classFile,'rt') as f:
    classNames = f.read().rstrip('\n').split('\n')

configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'

net = cv2.dnn_DetectionModel(weightsPath,configPath)
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()
    classIds, confs, bbox = net.detect(img,confThreshold=thres)
    print(classIds,bbox)

    if len(classIds) != 0:
        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].upper(),(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)  
  • 4
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值