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)
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’
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:
stackoverflow.com
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()
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()