Can I do lesson 2 on real time system?

I trained my data at kaggle and ı download my trained model . When ı try to predict one of frame ,python gives me an error : AttributeError: ‘numpy.ndarray’ object has no attribute ‘apply_tfms’ . I am using opencv for capturing frames . Is it wrong data type or something like that ?

Use the pil2tensor method

t = pil2tensor(arr, dtype=np.uint8)
learn.predict(Image(t))
1 Like

so what is t ? Is it my captured frame ? What has to be arr at code ? arguments ?

arr is a numpy array.

What you are currently passing to predict

t is the tensor created from your numpy array that is given to you from opencv

1 Like

Thanks ı will try.

I’m actually having a problem getting this code to work for me. I am also using opencv but the images are coming out strange

p = PIL.Image.fromarray(n)
t = pil2tensor(p, dtype=np.uint8)
im = Image(t)
learn.predict(im)
1 Like

what is “n”?

learn = load_learner('E:\İndirmeler', 'trained_model.pkl')
im = cv2.imread("One_8.jpg")
learn.predict(im)

AttributeError: ‘numpy.ndarray’ object has no attribute ‘apply_tfms’

As @baz says, you need

  1. convert numpy image to torch.Tensor
  2. reorder channels
  3. create fast.ai “Image”
learn = load_learner('E:\İndirmeler', 'trained_model.pkl')
p = cv2.imread("One_8.jpg") # p is numpy array with shape (height,width,channels)
t = pil2tensor(p, dtype=np.uint8) # converts to numpy tensor
t = t.permute(2,0,1) # Move num_channels as first dimension
im = Image(t) # Convert to fastAi Image - this class has "apply_tfms" 
learn.predict(im)

NB: you should check the channels order: as I remember opencv has BGR and we use RGB!

Reference for OpenCv image shape:

2 Likes
learn = load_learner('E:\İndirmeler', 'trained_model.pkl')

p = cv2.imread("One_8.jpg",0) # p is numpy array with shape (height,width,channels)


t = pil2tensor(p, dtype=np.uint8) # converts to numpy tensor

t = t.permute(2,0,1) # Move num_channels as first dimension

im = Image(t) # Convert to fastAi Image - this class has "apply_tfms"

im.show()

I am geting this error: TypeError: Invalid dimensions for image data

Try to tranpose the numpy array

learn = load_learner('E:\İndirmeler', 'trained_model.pkl')
p = cv2.imread("One_8.jpg",0) # p is numpy array with shape (height,width,channels)
p = p.transpose(2,0,1) # Move num_channels as first dimension
t = pil2tensor(p, dtype=np.uint8) # converts to numpy tensor
im = Image(t) # Convert to fastAi Image - this class has "apply_tfms"
im.show()
1 Like

which one ? transpose the im ?

Notice I’ve changed the code slightly, adding p = p.transpose(2,0,1) and removing t = t.permute(2, 0, 1)

1 Like

I tried it . Following error is :
p = p.transpose(2,0,1) # Move num_channels as first dimension
ValueError: axes don’t match array

Ok this is working for me:

p = cv2.imread("/home/h/Desktop/a.png") # p is numpy array with shape (height,width,channels)
print(p.shape)
t = pil2tensor(p, dtype=np.uint8) # converts to numpy tensor
im = Image(t) # Convert to fastAi Image - this class has "apply_tfms"
im.show()
1 Like

I tried it . There is no error or warnings . Ok .But there is nothing showed .

Are you running it in a notebook?

1 Like

Nope. I tried it at python IDE Pycharm

That will be why then :wink:

Is it problem with pycharm or with all of IDE’s ?