使用 Python 和 OpenCV 进行实时目标检测的详解
import cv2
# 初始化摄像头和 OpenCV 窗口
cap = cv2.VideoCapture(0)
cv2.namedWindow('Realtime Object Detection', cv2.WINDOW_NORMAL)
# 加载预训练的深度学习目标检测模型
net = cv2.dnn.readNet('model_data/yolov3.weights', 'model_data/yolov3.cfg')
# 读取分类标签
with open('model_data/coco.names', 'r') as f:
labels = [line.strip() for line in f.readlines()]
# 进行目标检测的循环
while True:
ret, frame = cap.read()
if not ret:
break
# 获取网络输入尺寸
blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
# 设置网络输入并进行前向传播
net.setInput(blob)
outputs = net.forward(net.getUnconnectedOutLayersNames())
# 解析检测结果
for output in outputs:
for detection in output:
# 忽略置信度低的检测结果
if detection[2] > 0.5:
# 获取类别索引、置信度、坐标
class_id = detection[0]
confidence = detection[2]
box = detection[3:7] * np.array([frame.shape[1], frame.shape[0], frame.shape[1], frame.shape[0]])
start_x, start_y, end_x, end_y = box.astype(np.int)
# 绘制矩形框和标签
cv2.rectangle(frame, (start_x, start_y), (end_x, end_y), (255, 0, 0), 2)
cv2.putText(frame, f"{labels[class_id]}: {confidence * 100:.2f}%", (start_x, start_y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
# 显示结果
cv2.imshow('Realtime Object Detection', frame)
# 按 'q' 退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放摄像头资源并关闭所有窗口
cap.release()
cv2.destroyAllWindows()
这段代码使用了OpenCV的深度学习模块来进行实时目标检测。首先,它初始化了摄像头和OpenCV窗口,然后加载了预训练好的YOLOv3模型和相关的配置文件。接着,它在一个循环中不断地从摄像头中读取帧,并通过YOLOv3模型进行目标检测。检测结果会被绘制在帧上,并且在窗口中实时显示。用户可以通过按'q'键退出程序。
评论已关闭