使用了 OpenCV 提供的 cv2.dnn 模块来加载 YOLO 模型、处理图像,并解析网络输出。代码首先加载了预训练的 YOLO 模型的权重和配置文件,并获取了输出层的名称。接着加载了类别标签,并读取待检测的图像。图像预处理部分使用了 `cv2.dnn
import cv2
import numpy as np
# 加载预训练的模型权重和配置文件
net = cv2.dnn.readNetFromDarknet("yolov3.cfg", "yolov3.weights")
# 获取网络模型的输出层
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
# 加载类别标签
classes = []
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# 加载图像
image = cv2.imread("image.jpg")
height, width, channels = image.shape
# 图像预处理
blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
# 将预处理后的图像输入网络
net.setInput(blob)
outs = net.forward(output_layers)
# 解析网络输出,提取检测结果
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
# 目标框的中心坐标、宽度和高度
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# 目标框的左上角坐标
x = int(center_x - w / 2)
y = int(center_y - h / 2)
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([x, y, w, h])
# 使用非极大值抑制 (NMS) 进行目标框筛选
indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
for i in indices:
i = i[0]
box = boxes[i]
x, y, w, h = box
label = f"{classes[class_ids[i]]}: {confidences[i]:.2f}"
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# 显示检测结果
cv2.imshow("YOLO Object Detection", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
该代码示例展示了如何利用OpenCV的cv2.dnn模块加载预训练的YOLO模型,对图像进行处理和目标检测。首先加载YOLO的权重和配置文件,然后获取输出层名称,加载类别标签,读取图像。接着,对图像进行预处理,输入网络并解析输出,提取检测到的目标及其信息,最后应用非极大值抑制(NMS)筛选目标框并在图像上显示结果。
1265






