我正在使用Ultralytics YOLO进行车牌检测,在尝试从Results.boxes对象中提取边界框坐标时遇到了问题。我已经检查了Results.boxes对象的结构,但很难正确访问边界框信息。
class ImageProcessing:
def __init__(self, model_path: Path, input_image: Path, output_image: Path):
if not isinstance(model_path, Path):
raise TypeError("model_path must be a pathlib.Path instance")
if not isinstance(input_image, Path) or not isinstance(output_image, Path):
raise TypeError("input_image and output_image must be pathlib.Path instances")
# Load the YOLO model from the provided path
self.model = YOLO(str(model_path))
self.input_image = input_image
self.output_image = output_image
def ascertain_license_plates_as_image(self, threshold: float = 0.5, fontscale: float = 1.3, color: tuple = (0, 255, 0), thickness: int = 3):
image = opencv.imread(str(self.input_image))
results = self.model(image)
# Check if results is a list and get the first result
if isinstance(results, list):
results = results[0]
# Iterate through each detected object
for box in results.boxes:
# Extract coordinates, confidence, and class ID
x1, y1, x2, y2, conf, class_id = box.data[0][0], box.data[0][1], box.data[0][2], box.data[0][3], box.conf.item(), int(box.cls.item())
if conf > threshold:
opencv.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), color, thickness)
label = results.names[class_id].upper() if results.names else f'class {class_id}'
opencv.putText(image, label, (int(x1), int(y1) - 10), opencv.FONT_HERSHEY_SIMPLEX, fontscale, color, thickness, opencv.LINE_AA)
opencv.imwrite(str(self.output_image), image)
return results
字符串
然而,我得到了一个IndexError,似乎我的索引对于这个特定的Boxes对象是不正确的。或者更糟糕的是,cv2没有突出显示车牌。
1条答案
按热度按时间dluptydi1#
for box in results.boxes
将返回一个ultralytics.engine.results.Boxes对象,该对象具有以下属性(值作为示例给出):字符串
这将是更好地调用它们,因为它是,从 Torch 拆包。Tensor在必要的地方:
型