使用YOLO目标检测时‘list’对象无‘render’属性错误的解决方法
AttributeError: 'list' object has no attribute 'render' in YOLOv8目标检测代码
问题核心是YOLOv8的model()方法返回的是Result对象的列表——哪怕仅传入单张图片,也会返回包含单个元素的列表。直接对列表调用render()方法,必然触发AttributeError。
修正后的代码
from ultralytics import YOLO import numpy as np from matplotlib import pyplot as plt def main(): model = YOLO('yolov8n.pt') # 调用model后得到Result对象列表,取出对应单张图片的结果元素 results = model('E:/03_files/programing/Neural_Networks/APEX_Aimbot/test.jpg') result = results[0] # 对单个Result对象调用render(),生成带边界框的图像数组 plotted_image = np.squeeze(result.render()) plt.imshow(plotted_image) plt.show() if __name__ == '__main__': main()
关键说明
- 列表索引取值:
model()返回的列表中,每个元素对应一张输入图片的检测结果。单张图片取results[0],多张图片则需遍历列表处理每个Result对象。 render()方法作用:单个Result对象的render()方法会生成带有检测边界框、类别标签和置信度的图像数组,可直接用plt.imshow()显示。- 版本差异:你参考的YouTube教程可能使用了旧版本YOLOv8,当时
model()直接返回单个Result对象而非列表,这是新版本库的API变更导致的兼容问题。
内容的提问来源于stack exchange,提问作者Oscar Gratton
相关产品推荐
相关产品推荐

