Fastai Image crop

how can i crop a fastai image by the coordinates of a bounding box ?

I am detection objects, but would like to crop the images and save them.


1 Like

I imagine you could utilize the PIL library to do this. I did this for YOLOv3 when I wanted to do the exact same thing. If you are given the coordinates you can go into PIL and save the new image based on the crop.

1 Like

can you share a code snippet, i am getting a little bit confused by what is what in fastai

I have this image object i would like to crop

torch.Size([512, 512])

How are you getting the coordinates? Via learn.predict()? Or where are they coming from

yes exactly, i’m following these examples here:

Ah I see now!
Okay say we have the coordinates as top-left followed by bottom-right. You could do the following:
img ="example.png")
img2 = img.crop((x1,y1,x2,y2))

Does this help?

1 Like

i wanted to do it on the fastai.Image object , is that possible, do i have to go back and reopen the image using PIL ?

The fastai image object should be a PIL image if I am not mistaken.

Edit: it is. So you should be able to repeat the above with it. Let me know if you cannot

i wasn’t able to repeat the above, i am stuck on this for a few hours now :slight_smile:

it might be two reasons img.crop() might be using the fastai crop() which is different from the PIL crop or i have the coordinates mixed up, i get confused when its center,w,h when its top left, bottom right, etc…

Ah wait! On your image. do im. followed by a tab and look through the options. I believe one of them should be the actual image, it converts it to a tensor. I can verify this when I am at a computer next.

1 Like or img.px i guess.

no worries, thanks for your help, will let you know if get somewhere with this.

1 Like

got it !!! :smiley: :smiley:

happy days !


img.px = img.px[:, 295:295+77,262:252+94]


torch.Size([3, 512, 512])
torch.Size([3, 77, 84])

How do you convert predicted bounding box (which has values [-1;1]) into these numbers [:, 295:295+77,262:252+94]?


How did you get the predicted values?