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实现目标检测_第1张图片

摄像头实时目标检测

除了对图像进行目标检测,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)  

你可能感兴趣的:(计算机视觉,Python,opencv,目标检测,计算机视觉)