有点无聊3 2024-05-01 20:36 采纳率: 54.5%
浏览 153
已结题

YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'

使用YOLOv8obb模型获取边框坐标时报错obbs = r.boxes.xywhr.cpu().tolist()
AttributeError: 'NoneType' object has no attribute 'xywhr'

import time
import cv2
from ultralytics import YOLO
from ultralytics.utils.plotting import save_one_box as crop
import imutils
import numpy as np
import os
import shutil

model = YOLO('best.pt')

video_path ="D:/WIN_20230907_21_08_23_Pro.mp4"
cap = cv2.VideoCapture(video_path)
confi=0.2
while cap.isOpened():
    success, frame = cap.read()

    confi = round(confi, 2)
    if confi <= 0.2:
        confi = 0.2
    elif confi >= 0.8:
        confi = 0.8

    if success:
        results = model(frame,  conf=confi, save=False, show=False)
        annotated_frame = results[0].plot()
        cv2.imshow("YOLOv8 Tracking", annotated_frame)
        r=results[0]

        if results:
            obbs = r.boxes.xywhr.cpu().tolist()
            for i, obb in enumerate(obbs):
                xc, yc, w, h, angle = obb  # Unpack the OBB

        else:
            bkg = np.ones((256, 1280, 3), dtype=np.uint8)
            bkg *= 200
            annotated_frame = np.vstack((annotated_frame, bkg))

        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        # Break the loop if the end of the video is reached
        break

    # Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()

修改后不管有没有检测到物体,都会print:no objects detected
最前面有这个报错不知道有没有关系:

UserWarning: Plan failed with a cudnnException: CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR: cudnnFinalize Descriptor Failed cudnn_status: CUDNN_STATUS_NOT_SUPPORTED (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\aten\src\ATen\native\cudnn\Conv_v8.cpp:919.)
  return F.conv2d(input, weight, bias, self.stride,

img

img

  • 写回答

12条回答 默认 最新

  • 专家-皆知 2024-05-01 20:36
    关注

    参考免费微信小程序:皆我百晓生 作答,经由专家-皆知整理回复,如问题解决,望采纳;如仍有疑问,请评论回复。

    遇到这个错误AttributeError: 'NoneType' object has no attribute 'xywhr'通常意味着results[0]没有成功检测到任何对象,因此r.boxesNone,尝试访问.xywhr属性自然会引发错误。为了解决这个问题,你需要在尝试访问边界框属性之前,先检查是否有检测结果。下面是对你的代码进行修改后的版本,加入了对无检测结果情况的处理:

    import time
    import cv2
    from ultralytics import YOLO
    from ultralytics.utils.plotting import save_one_box as crop
    import imutils
    import numpy as np
    import os
    import shutil
    
    # Load the model
    model = YOLO('best.pt')
    
    video_path = "D:/WIN_20230907_21_08_23_Pro.mp4"
    cap = cv2.VideoCapture(video_path)
    confi = 0.2
    
    while cap.isOpened():
        success, frame = cap.read()
    
        # Ensure confidence is within bounds
        confi = max(min(confi, 0.8), 0.2)
    
        if success:
            results = model(frame, conf=confi, save=False, show=False)
            
            # Annotate the frame only if there are detections
            if results:
                annotated_frame = results[0].plot()
                cv2.imshow("YOLOv8 Tracking", annotated_frame)
                
                # Access boxes after confirming detections exist
                r = results[0]
                if r.boxes:
                    obbs = r.boxes.xywhr.cpu().tolist()
                    for i, obb in enumerate(obbs):
                        xc, yc, w, h, angle = obb  # Unpack the OBB
                        # Process each bounding box here...
                else:
                    print("No detections found for this frame.")
            else:
                # If no detections, create a blank background and stack it
                bkg = np.ones((256, 1280, 3), dtype=np.uint8) * 200
                annotated_frame = np.vstack((annotated_frame, bkg))
                cv2.imshow("YOLOv8 Tracking", annotated_frame)
    
            # Exit condition
            if cv2.waitKey(1) & 0xFF == ord("q"):
                break
        else:
            break
    
    # Cleanup
    cap.release()
    cv2.destroyAllWindows()
    

    这段代码中,我做了以下修改:

    1. 在调整confi值时使用了maxmin函数来确保其始终在0.2到0.8之间。
    2. 在尝试访问r.boxes.xywhr之前,首先检查了results是否非空,然后进一步检查了r.boxes是否存在,以避免尝试访问NoneType对象的属性。
    3. 对于没有检测结果的帧,添加了一条打印语句来提示,并且仍然显示了画面(即使没有实际检测结果,也通过叠加空白背景来保持界面更新)。

    这样修改后,你的程序应该能够在没有检测到任何对象时避免抛出异常,并且能继续正常运行。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月2日
  • 修改了问题 5月1日
  • 修改了问题 5月1日
  • 赞助了问题酬金15元 5月1日
  • 展开全部

悬赏问题

  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题
  • ¥15 vs2019的js智能提示
  • ¥15 关于#开发语言#的问题:FDTD建模问题图中代码没有报错,但是模型却变透明了