ImageBBox.create Error - TypeError: slice indices must be integers or None or have an __index__ method

Here is another error that took me a while to solve. Using the new ImageBBox.create, I was running through everything and I entered the bounding box as prescribes [top, left, bottom, right] which for me was [y-min, x-min, y-min+height, x-min+width] in case that helps anybody at any point. But I got this error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-135-f101155ece2c> in <module>()
----> 1 ImageBBox.create([[bbox[1],bbox[0],bbox[1]+bbox[3],bbox[0]+bbox[2]]], *img.size)

~/fastai_v1/fastai/kaggle/Competitions/fastai/vision/image.py in create(cls, bboxes, h, w, labels, pad_idx)
    220         pxls = torch.zeros(len(bboxes),h, w).long()
    221         for i,bbox in enumerate(bboxes):
--> 222             pxls[i,bbox[0]:bbox[2]+1,bbox[1]:bbox[3]+1] = 1
    223         bbox = cls(pxls.float())
    224         bbox.labels,bbox.pad_idx = labels,pad_idx

TypeError: slice indices must be integers or None or have an __index__ method

The issue was I was passing floats as my bounding box instead of ints, so the solution was to cast my bounding box as ints:

ImageBBox.create([[int(bbox[1]),int(bbox[0]),int(bbox[1]+bbox[3]),int(bbox[0]+bbox[2])]], *img.size)

Maybe this is something that could be done behind the scenes in the future, but that seems to fix the issue.

That are is still a work in progress. Yes, casting to int behind the scenes is a good idea!

1 Like

It’s done know.

1 Like