0

I'm trying to use the ssd mobilenetv3 object detection model, I found this tutorial I tried running the python file on my device using an image I snapped with my phone

but I keep getting this error Error message

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/Users/nombaadmin/Documents/GitHub/Object-Sorting-Robot/object_sorting.ipynb Cell 17 in <cell line: 29>()
     46     class_id = int(detection[1])
     47     # print (class_id)
     48     # draw the name of the predicted object along with the probability
---> 49     label = f"{class_names[class_id - 1].upper()} {probability * 100:.2f}%"
     50     cv2.putText(image, label, (box[0], box[1] + 15),
     51                 cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
     53 cv2.imshow('Image', image)

IndexError: list index out of range

This is the code from the tutorial

import cv2

image = cv2.imread('Images/IMG20221220090901.jpg')
image = cv2.resize(image, (640, 480))
h = image.shape[0]
w = image.shape[1]

# path to the weights and model files
weights = "frozen_inference_graph.pb"
model = "ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"
# load the MobileNet SSD model trained  on the COCO dataset
net = cv2.dnn.readNetFromTensorflow(weights, model)

# load the class labels the model was trained on
class_names = []
with open("labels.txt", "r") as f:
    class_names = f.read().strip().split("\n")

# print(class_names)

# create a blob from the image
blob = cv2.dnn.blobFromImage(
    image, 1.0/127.5, (320, 320), [127.5, 127.5, 127.5])
# pass the blog through our network and get the output predictions
net.setInput(blob)
output = net.forward()  # shape: (1, 1, 100, 7)

# loop over the number of detected objects
for detection in output[0, 0, :, :]:  # output[0, 0, :, :] has a shape of: (100, 7)
    # the confidence of the model regarding the detected object
    probability = detection[2]

    # if the confidence of the model is lower than 50%,
    # we do nothing (continue looping)
    if probability < 0.5:
        continue

    # perform element-wise multiplication to get
    # the (x, y) coordinates of the bounding box
    box = [int(a * b) for a, b in zip(detection[3:7], [w, h, w, h])]
    box = tuple(box)
    # draw the bounding box of the object
    cv2.rectangle(image, box[:2], box[2:], (0, 255, 0), thickness=2)

    # extract the ID of the detected object to get its name
    class_id = int(detection[1])
    # print (class_id)
    # draw the name of the predicted object along with the probability
    label = f"{class_names[class_id - 1].upper()} {probability * 100:.2f}%"
    cv2.putText(image, label, (box[0], box[1] + 15),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

cv2.imshow('Image', image)
cv2.waitKey()

I have tried to check the value of class_id its value is 87 which is above the limit with is 1 to 80

I inputed the image of a spoon I snapped with my phone

2
  • It looks like class_names is populated from labels.txt. How many lines are in that file? Commented Dec 20, 2022 at 17:01
  • 80 lines It's from this document github.com/pjreddie/darknet/blob/master/data/coco.names Commented Dec 20, 2022 at 17:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.