detect_plate/detect_test.py
2024-08-07 09:32:38 +08:00

28 lines
861 B
Python

import torch
from ultralytics import YOLO
# 加载预训练的模型
model = YOLO('weights/plate_detect.pt')
# 设置图像路径
image_path = r'D:\Project\ChePai\test\images\val\20230331163841.jpg'
# 进行推理
results = model(image_path)
# 解析结果
for r in results:
boxes = r.boxes # 包含检测结果的Boxes对象
# 获取边界框坐标
box_coordinates = boxes.xyxy.cpu().numpy()
# 获取置信度分数
confidences = boxes.conf.cpu().numpy()
# 获取类别标签
labels = boxes.cls.cpu().numpy().astype(int)
# 打印检测结果
for i in range(len(box_coordinates)):
x1, y1, x2, y2 = box_coordinates[i]
confidence = confidences[i]
label = model.names[labels[i]]
print(f'Object: {label}, Confidence: {confidence:.2f}, Bounding Box: ({x1:.2f}, {y1:.2f}, {x2:.2f}, {y2:.2f})')