1、下载YOLOv8模型文件
2、编写python脚本 aaa.py
import cv2
import numpy as np
from ultralytics import YOLO
import matplotlib.pyplot as plt
def plot_detection(image, boxes, scores, class_ids, class_names, conf_threshold=0.5):
"""
将检测结果绘制到图像上
"""
# 创建图像的副本用于绘制
img_with_detections = image.copy()
# 定义颜色列表,为不同类别使用不同颜色
colors = [(0, 255, 0), (255, 0, 0), (0, 0, 255),
(255, 255, 0), (255, 0, 255), (0, 255, 255)]
# 遍历所有检测结果
for i, (box, score, class_id) in enumerate(zip(boxes, scores, class_ids)):
# 过滤低置信度的检测结果
if score < conf_threshold:
continue
# 获取框的坐标 (x1, y1, x2, y2)
x1, y1, x2, y2 = map(int, box)
# 选择颜色 (循环使用颜色列表)
color = colors[class_id % len(colors)]
# 1. 绘制边界框
cv2.rectangle(img_with_detections, (x1, y1), (x2, y2), color, 2)
# 2. 绘制标签背景
label = f"{class_names[class_id]}: {score:.2f}"
(label_width, label_height), baseline = cv2.getTextSize(
label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
cv2.rectangle(img_with_detections,
(x1, y1 - label_height - 5),
(x1 + label_width, y1),
color, -1) # -1 表示填充矩形
# 3. 绘制标签文本
cv2.putText(img_with_detections, label,
(x1, y1 - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
return img_with_detections
def main():
# 1. 加载预训练的 YOLOv8n 模型
print("正在加载 YOLOv8n 模型...")
model = YOLO('yolov8n.pt') # 会自动下载模型如果不存在
# 2. 加载要检测的图像
image_path = '123.jpg' # 替换为你的图片路径
print(f"正在加载图像: {image_path}")
image = cv2.imread(image_path)
if image is None:
print(f"错误: 无法加载图像 {image_path}")
return
# 3. 进行推理
print("正在进行推理...")
results = model(image, verbose=False) # verbose=False 减少输出信息
# 4. 提取检测结果
# 获取第一个结果(因为只输入了一张图片)
result = results[0]
# 提取边界框信息
boxes = result.boxes.xyxy.cpu().numpy() # 边界框坐标 [x1, y1, x2, y2]
scores = result.boxes.conf.cpu().numpy() # 置信度分数
class_ids = result.boxes.cls.cpu().numpy().astype(int) # 类别ID
# 获取类别名称
class_names = result.names
print(class_names)
print(f"检测到 {len(boxes)} 个对象")
for i, (box, score, class_id) in enumerate(zip(boxes, scores, class_ids)):
print(f"对象 {i+1}: {class_names[class_id]} (置信度: {score:.3f})")
# 5. 将检测结果绘制到图像上
print("正在绘制检测结果...")
result_image = plot_detection(image, boxes, scores, class_ids, class_names)
# 6. 保存和显示结果
output_path = 'detection_result123-x.jpg'
cv2.imwrite(output_path, result_image)
print(f"结果已保存到: {output_path}")
# 使用 matplotlib 显示结果(支持中文显示)
plt.figure(figsize=(12, 8))
# 将 BGR 转换为 RGB 用于 matplotlib 显示
result_image_rgb = cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)
plt.imshow(result_image_rgb)
plt.title('YOLOv8 result')
plt.axis('off')
plt.tight_layout()
#plt.savefig('detection_result_plt.jpg', dpi=300, bbox_inches='tight')
plt.show()
def detect_specific_classes():
"""
示例:只检测特定的类别(如可能类似于电表的物体)
"""
# 可能类似于电表的 COCO 类别
target_classes = {
67: 'cell phone', # 手机 - 最可能检测到数字屏幕
62: 'tvmonitor', # 显示器
75: 'remote', # 遥控器
74: 'clock' # 时钟
}
# 加载模型和图像
model = YOLO('yolov8n.pt')
image = cv2.imread('your_meter_image.jpg')
# 推理
results = model(image, verbose=False)
result = results[0]
# 提取结果
boxes = result.boxes.xyxy.cpu().numpy()
scores = result.boxes.conf.cpu().numpy()
class_ids = result.boxes.cls.cpu().numpy().astype(int)
class_names = result.names
# 过滤出目标类别
filtered_boxes = []
filtered_scores = []
filtered_class_ids = []
for box, score, class_id in zip(boxes, scores, class_ids):
if class_id in target_classes and score > 0.3: # 降低阈值
filtered_boxes.append(box)
filtered_scores.append(score)
filtered_class_ids.append(class_id)
print(f"检测到可能的目标: {target_classes[class_id]} (置信度: {score:.3f})")
# 绘制结果
if filtered_boxes:
result_image = plot_detection(image, filtered_boxes, filtered_scores,
filtered_class_ids, class_names, conf_threshold=0.3)
cv2.imwrite('meter_detection_result.jpg', result_image)
print("电表检测结果已保存")
else:
print("未检测到可能的目标物体")
if __name__ == "__main__":
# 运行主函数进行通用物体检测
main()
# 或者运行特定类别检测(用于电表检测尝试)
# detect_specific_classes()
3、运行python脚本
python aaa.py
正在加载 YOLOv8n 模型...
正在加载图像: 10.jpg
正在进行推理...
检测品类:{0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train',
7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12:
'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep',
19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25:
'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31:
'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36:
'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41:
'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48:
'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54:
'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining
table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard',
67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72:
'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78:
'hair drier', 79: 'toothbrush'}
检测到 2 个对象
对象 1: chair (置信度: 0.968)
对象 2: chair (置信度: 0.946)
正在绘制检测结果...
结果已保存到: detection_result10.jpg
python脚本、模型文件、图片文件在一个目录中