带有对象检测标签的 IndexError

问题描述

我使用 this 模型从视频中检测到物体。该模型检测三类:自行车、汽车和人。我只想要汽车和人,这就是我得到的,直到图片中出现自行车。然后我收到错误 IndexError: list out of range

这是我的代码

CLASSES = ["bicycle","car","person"]

for i in np.arange(0,detections.shape[2]):
    confidence = detections[0,i,2]
    if confidence > 0.4:
       idx = int(detections[0,1])
       if CLASSES[idx] == "person" or CLASSES[idx] == "car":
          print("CLASSES[idx]: ",CLASSES[idx])

错误指向行:if CLASSES[idx] == "person" or CLASSES[idx] == "car":。我已经尝试过像这样检查其他索引(自行车):

if CLASSES[idx] == "person" or CLASSES[idx] == "car":
    print("CLASSES[idx]: ",CLASSES[idx])

else:
    print("Not a car/person")
    continue

错误依旧,else一直没有执行。欢迎提出任何建议。

编辑。我做了一些日志记录,发现索引有时是 3,这就是脚本失败的原因。模型文档说:

The net outputs blob with shape: [1,1,N,7],where N is the number of detected bounding Boxes. Each detection has the format [image_id,label,conf,x_min,y_min,x_max,y_max],where:

image_id - ID of the image in the batch
label - predicted class ID
conf - confidence for the predicted class
(x_min,y_min) - coordinates of the top left bounding Box corner
(x_max,y_max) - coordinates of the bottom right bounding Box corner.

那么我是否以错误的方式进行检测?

这就是我获得检测结果的方式:

net = cv2.dnn.readNet(
    "models/person-vehicle-bike-detection-crossroad-0078.xml","models/person-vehicle-bike-detection-crossroad-0078.bin")

detections = net.forward()

解决方法

由于您使用的是 OpenVINO 预训练模型,我建议您试试这个示例应用程序:Pedestrian Tracker Demo

您应该得到类似于所附照片的内容。我使用的模型与您使用的模型相同,person-vehicle-bike-detection-crossroad-0078 和 person-reidentification-retail-0277

完成后,尝试研究代码并相应地实现自己的代码。

enter image description here