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包含了一些常见的目标,如
1 | person |
图像目标检测
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)
参数说明:
- 图片
- 要添加的文字
- 文字添加的位置
- 字体
- 字体大小
- 字体颜色
- 字体粗细
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28import 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摄像头
1 | import cv2 |
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.