Fastai Image crop

(John) #1

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.

thanks,

0 Likes

(Zachary Mueller) #2

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

(John) #3

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

img.size
torch.Size([512, 512])
type(img)
fastai.vision.image.Image

0 Likes

(Zachary Mueller) #4

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

0 Likes

(John) #5

yes exactly, i’m following these examples here:

0 Likes

(Zachary Mueller) #6

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

Does this help?

1 Like

(John) #7

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 ?

0 Likes

(Zachary Mueller) #8

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

0 Likes

(John) #9

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…

0 Likes

(Zachary Mueller) #10

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

(John) #11

img.data or img.px i guess.

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

1 Like

(John) #12

got it !!! :smiley: :smiley:

happy days !

print(img.px.shape)

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

print(img.px.shape)

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

(Tantrum) #13

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

0 Likes