树莓派路牌识别报错:'Prediction'对象无'label'属性求解决方案
解决方案
在Roboflow 1.1.12版本中,Prediction对象的类别名称属性为class,但由于class是Python保留关键字,无法直接通过detection.class调用,需用以下两种方式获取:
- 字典索引方式:
class_name = detection['class']
getattr()函数调用:
class_name = getattr(detection, 'class')
修改后的代码片段(循环检测部分)
for detection in detections: class_name = detection['class'] confidence = detection.confidence box = detection.bounding_box # 在图像上绘制检测框和类别信息 x1, y1 = int(box.x), int(box.y) x2, y2 = int(box.x + box.width), int(box.y + box.height) cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(image, f"{class_name}: {round(confidence, 2)}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
说明
Roboflow Python SDK 1.x版本对Prediction对象的属性做了调整,旧版本中的label属性在1.x中被替换为class,但因class是Python关键字,需通过上述两种方式访问。
内容的提问来源于stack exchange,提问作者Jackob Loko
相关产品推荐
相关产品推荐

