image prediction function throws IndexError: list index out of range

39 Views Asked by At

I am using OpenCV, specifically R-CNN.

I am trying to do object prediction on an image and it returns an indexing area. I don't know the library well enough to tell.

It feeds in image into the get prediction function and it returns a problem with indexes.

def get_prediction(img_path, threshold):
  """
  get_prediction
    parameters:
      - img_path - path of the input image
      - threshold - threshold value for prediction score
    method:
      - Image is obtained from the image path
      - the image is converted to image tensor using PyTorch's Transforms
      - image is passed through the model to get the predictions
      - class, box coordinates are obtained, but only prediction score > threshold
        are chosen.
     
  """
  img = Image.open(img_path)
  transform = T.Compose([T.ToTensor()])
  img = transform(img)
  pred = model([img])
  pred_class = [COCO_INSTANCE_CATEGORY_NAMES[i] for i in list(pred[0]['labels'].numpy())]
  pred_boxes = [[(i[0], i[1]), (i[2], i[3])] for i in list(pred[0]['boxes'].detach().numpy())]
  pred_score = list(pred[0]['scores'].detach().numpy())
  pred_t = [pred_score.index(x) for x in pred_score if x>threshold][-1]
  pred_boxes = pred_boxes[:pred_t+1]
  pred_class = pred_class[:pred_t+1]
  return pred_boxes, pred_class



def object_detection_api(img_path, threshold=0.5, rect_th=3, text_size=3, text_th=3): 
  boxes, pred_cls = get_prediction(img_path, threshold) 
  # Get predictions 
  img = cv2.imread(img_path) 
  # Read image with cv2 
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 
  # Convert to RGB 
  for i in range(len(boxes)): 
    cv2.rectangle(img, boxes[i][0], boxes[i][1],color=(0, 255, 0), thickness=rect_th) 
    # Draw Rectangle with the coordinates 
    cv2.putText(img,pred_cls[i], boxes[i][0], cv2.FONT_HERSHEY_SIMPLEX, text_size, (0,255,0),thickness=text_th) 
    # Write the prediction class 
    plt.figure(figsize=(20,30)) 
    # display the output image 
    plt.imshow(img) 
    plt.xticks([]) 
    plt.yticks([]) 
    plt.show()
    return pred_boxes, pred_class

def computeSpecificArea(imageSegment): # whatever portion of the image is held by bounding box. 
    pass
data = []  # dictionaries with all the different fields for the different images # get the path/directory
folder_dir = r"C:\Users\geniu\OneDrive\GW\Spring 2024\CSCI 6527\test_images"
for images in os.listdir(folder_dir):
 
    # check if the image ends with png
    #if (images.endswith(".png")):
        #print(images)
   print("image: ", images)
   boxes, classes =  object_detection_api(images, threshold=0.5, rect_th=3, text_size=3, text_th=3)

   print("boxes length: ", len(boxes))
   print("boxes type: ", type(boxes), " ", type(boxes))
   print("classes type: " , type(classes))

    #retrieve data from these results 

   #data.append(thisImageData)
   #thisImageData = {"quantity": 0, "area": 0}
   break



IndexError: list index out of range

I don't know anything about how openCV processes the images to find the index error.

     ---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[26], line 89
     83 for images in os.listdir(folder_dir):
     84  
     85     # check if the image ends with png
     86     #if (images.endswith(".png")):
     87         #print(images)
     88    print("image: ", images)
---> 89    boxes, classes =  object_detection_api(images, threshold=0.5, rect_th=3, text_size=3, text_th=3)
     91    print("boxes length: ", len(boxes))
     92    print("boxes type: ", type(boxes), " ", type(boxes))

Cell In[26], line 60, in object_detection_api(img_path, threshold, rect_th, text_size, text_th)
     59 def object_detection_api(img_path, threshold=0.5, rect_th=3, text_size=3, text_th=3): 
---> 60   boxes, pred_cls = get_prediction(img_path, threshold) 
     61   # Get predictions 
     62   img = cv2.imread(img_path) 

Cell In[26], line 52, in get_prediction(img_path, threshold)
     50 pred_boxes = [[(i[0], i[1]), (i[2], i[3])] for i in list(pred[0]['boxes'].detach().numpy())]
     51 pred_score = list(pred[0]['scores'].detach().numpy())
---> 52 pred_t = [pred_score.index(x) for x in pred_score if x>threshold][-1]
     53 pred_boxes = pred_boxes[:pred_t+1]
     54 pred_class = pred_class[:pred_t+1]

IndexError: list index out of range

    enter code here
0

There are 0 best solutions below