I detec objects from a video with this model. The model detects three classes: bicycles, cars and persons. I only want cars and persons and thats's what I get until there are bikes in the picture. Then I get an error IndexError: list out of range
.
This is my code:
CLASSES = ["bicycle", "car", "person"]
for i in np.arange(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > 0.4:
idx = int(detections[0, 0, i, 1])
if CLASSES[idx] == "person" or CLASSES[idx] == "car":
print("CLASSES[idx]: ", CLASSES[idx])
The error points to the line: if CLASSES[idx] == "person" or CLASSES[idx] == "car":
. I already tried to check for other indexes (bicycle) like this:
if CLASSES[idx] == "person" or CLASSES[idx] == "car":
print("CLASSES[idx]: ", CLASSES[idx])
else:
print("Not a car/person")
continue
The error was still the same and the else was never executed. Any advices what is wrong here are welcome.
Edit. I did some logging and found out that the index is sometimes 3 and that's why the script fails. The models documents says:
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.
So am I going through the detections in a wrong way?
This is how I get the detections:
net = cv2.dnn.readNet(
"models/person-vehicle-bike-detection-crossroad-0078.xml",
"models/person-vehicle-bike-detection-crossroad-0078.bin")
detections = net.forward()