opencv-python
, numpy
, matplotlib
目标检测就像教计算机"看"图片中的物体。它不仅要知道图片中有什么物体,还要知道这些物体在哪里(用方框标出来)。
类比说明:
YOLO(You Only Look Once)是一种快速的目标检测算法。它的特点是:
YOLO工作原理:
# 安装OpenCV(用于图像处理)
# 在命令行中运行:
# pip install opencv-python numpy matplotlib
import cv2
import numpy as np
# 下载YOLO的配置文件(yolov3.cfg)和权重文件(yolov3.weights)
# 可以从这里下载:https://pjreddie.com/darknet/yolo/
# 下载后放在项目文件夹的yolo/目录下
# 加载YOLO模型
net = cv2.dnn.readNet("yolo/yolov3.weights", "yolo/yolov3.cfg")
# 加载类别名称(80个类别)
with open("yolo/coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# 获取输出层名称
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
def detect_objects(img_path):
# 读取图片
img = cv2.imread(img_path)
height, width, channels = img.shape
# 预处理图片(缩放、归一化等)
blob = cv2.dnn.blobFromImage(img, 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: # 只保留置信度大于50%的检测结果
# 计算边界框坐标
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)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
# 应用非极大值抑制(NMS)去除重叠的边界框
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# 绘制检测结果
font = cv2.FONT_HERSHEY_PLAIN
colors = np.random.uniform(0, 255, size=(len(classes), 3))
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
color = colors[class_ids[i]]
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
cv2.putText(img, label, (x, y + 30), font, 3, color, 3)
# 显示结果
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 保存结果
cv2.imwrite("output/detection_result.jpg", img)
print("检测结果已保存到 output/detection_result.jpg")
# 检测示例图片(请确保data目录下有测试图片)
detect_objects("data/test_image.jpg")
项目文件结构:
lesson_31_yolo/
├── README.md
├── requirements.txt
├── main.py
├── yolo/
│ ├── yolov3.cfg
│ ├── yolov3.weights
│ └── coco.names
├── data/
│ └── test_image.jpg
└── output/
└── detection_result.jpg
opencv-python>=4.5.0
numpy>=1.19.0
matplotlib>=3.3.0
检测结果已保存到 output/detection_result.jpg
output/detection_result.jpg
: 带有检测框和标签的图片解决方法:
解决方法:
解决方法:
如遇问题,请检查: