类型错误:“NoneType”对象不可下标

时间:2021-04-13 05:49:31

标签: python cv2

我在 colab 上尝试代码,这是我代码的一部分

    while True:
            ret, frame = cap.read()
            if ret:
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                cls_idx = int(frames_label[frames_label['frame'] == i]['label'])
    
                if annot:
                    bb = np.array(annot.iloc[i-1, 2:].astype(int))
                else:
                     
                    bb = detector.detect(frame)[0, :4].numpy().astype(int)
                bb[:2] = np.maximum(0, bb[:2] - 5)
                bb[2:] = np.minimum(frame_size, bb[2:] + 5) if bb[2:].any() != 0 else bb[2:]

我也用imshow()在我的屏幕上显示图片(由于代码的长度,它不在上面的代码中),图片最初可以正确显示,但它会停在特定的图片上,并引发

Type error: 'NoneType' object is not subscriptable at 

bb = detector.detect(frame)[0, :4].numpy().astype(int)

附言bb是bounding box的缩写

错误图片:

Error Image

有人可以帮忙吗?谢谢

1 个答案:

答案 0 :(得分:-1)

我无法完全测试这个,但进行这些更改并让我知道它是否有效:

try:
    bb = detector.detect(frame)[0, :4].numpy().astype(int)
except Exception as e:
    print("An error occurred:", e)
    continue