我已经使用 YOLOv8 训练了一个自定义对象检测模型来检测图像中的号码/车牌。当我使用命令行界面测试模型时,它工作正常并为检测到的板提供正确的边界框坐标。但是,当我使用 Python 脚本获取边界框坐标并裁剪图像时,模型无法正确检测坐标。
当我使用 YOLOv8 的 CLI 命令检测车牌时,它工作正常。
yolo predict detect model=best.pt source=photo1.jpg save=True
# Output
Ultralytics YOLOv8.0.53 🚀 Python-3.9.16 torch-2.1.0.dev20230315 CPU
Model summary (fused): 168 layers, 11125971 parameters, 0 gradients, 28.4 GFLOPs
image 1/1
416x640 1 np, 115.0ms
Speed: 0.7ms preprocess, 115.0ms inference, 8.6ms postprocess per image at shape (1, 3, 640, 640)
Results saved to runs/detect/predict
存储输出:
当我使用以下 python 脚本时 - 它没有提取正确的图像坐标。我正在使用 OpenCV 裁剪车牌 - 但模型没有提供可以裁剪图像的正确坐标。
import cv2
from ultralytics import YOLO
model = YOLO('best.pt')
image = cv2.imread('photo1.jpg')
result = model(image)
result = result[0].boxes
result = result.numpy()
print(result)
# print(result.boxes) # print the box coordinates
[x,y,w,h] = result.xyxy.tolist()[0] # print the box coordinates
print(x,y,w,h)
x, y, w, h = int(x), int(y), int(w), int(h)
object = image[y:y+h, x:x+w]
cv2.imshow('Object', object)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Output
0: 416x640 1 np, 130.3ms
Speed: 0.9ms preprocess, 130.3ms inference, 7.4ms postprocess per image at shape (1, 3, 640, 640)
[[ 155.34 71.656 245.1 146.62 0.88172 0]]
155.33755493164062 71.65567779541016 245.10494995117188 146.61927795410156
裁剪图像:
请帮我找出这个问题的根源。
基于这行代码:
[x,y,w,h] = result.xyxy.tolist()[0]
我认为它没有映射到
x, y, w, h
,而是映射到 xmin, ymin, xmax, ymax
,因为您从结果对象调用 .xyxy
属性。
所以,这就是为什么你的左上角是正确的,但右下角太远了。要解决这个问题,您可以修改该行和这一行:
object = image[ymin:ymax, xmin:xmax]
希望有帮助!