YOLOv8推理结果加载报错:results[0].boxes.boxes无法正常使用
解决YOLOv8更新依赖后
results[0].boxes.boxes报错问题 你遇到的问题和TensorFlow降级无关,核心是Ultralytics YOLOv8的API在版本更新中发生了变更:旧版本里results[0].boxes.boxes用来获取检测框的原始数据,但新版本中boxes属性已被移除,取而代之的是data或numpy()方法。
修复步骤
直接修改检测结果获取的代码行:
将原来的:
pred = results[0].boxes.boxes
替换为以下二者之一:
- 获取PyTorch张量格式的检测数据:
pred = results[0].boxes.data
- 获取NumPy数组格式的检测数据(和原代码逻辑完全兼容):
pred = results[0].boxes.numpy()
修改后的完整代码
import cv2 from ultralytics import YOLO import numpy as np import pickle # Load your YOLOv8 model model = YOLO('yolov8s.pt') # Define class IDs CAR_CLASS = 2 # Load parking spot coordinates from the pickle file pickle_file_path = 'D:\\Projekti\\parking spot detection\\regions.p' with open(pickle_file_path, 'rb') as file: parking_spots = pickle.load(file) cap = cv2.VideoCapture(r'D:\Projekti\parking spot detection\videji\parking1.mp4') while True: ret, frame = cap.read() if not ret: break # Perform inference with your YOLOv8 model results = model.predict(frame) # 替换为兼容新版本的API pred = results[0].boxes.numpy() # Iterate through each parking spot for spot_number, parking_spot in enumerate(parking_spots, 1): # Draw a border around the parking spot cv2.polylines(frame, [parking_spot], isClosed=True, color=(0, 0, 255), thickness=2) # Check for cars in the current parking spot for det in pred: class_id, confidence, x_min, y_min, x_max, y_max = int(det[5]), det[4], det[0], det[1], det[2], det[3] if confidence > 0.5 and class_id == CAR_CLASS: if x_min > parking_spot[:, 0].min() and x_max < parking_spot[:, 0].max() and \ y_min > parking_spot[:, 1].min() and y_max < parking_spot[:, 1].max(): print(f"A car is in parking spot {spot_number}!") label = f"car: {confidence:.2f}" color = (0, 255, 0) # Green for cars thickness = 2 cv2.rectangle(frame, (int(x_min), int(y_min)), (int(x_max), int(y_max)), color, thickness) cv2.putText(frame, label, (int(x_min), int(y_min) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, thickness) # Display spot number next to the border spot_text = f"Spot {spot_number}" cv2.putText(frame, spot_text, (int(parking_spot[:, 0].mean()), int(parking_spot[:, 1].mean())), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cv2.imshow("Object Detection", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
补充说明
Ultralytics YOLOv8默认基于PyTorch运行,和TensorFlow的版本关联度极低。你更新依赖后触发报错,是因为环境更新时顺带升级了YOLOv8版本,导致API不兼容。无需降级TensorFlow,只需适配YOLOv8的新API即可。
内容的提问来源于stack exchange,提问作者Shime
相关产品推荐
相关产品推荐

