将图像转换为黑底白字并进行预测

时间:2019-10-22 01:55:24

标签: python python-imaging-library pytorch

我想使用here训练的LeNet模型来预测新的自定义图像。 自定义的图像是黑白的,因此我需要将它们转换为黑白的图像。 enter image description here

# Load & transform image
ori_img = Image.open('./test/2.png').convert('L')
img = np.invert(ori_img) #Transform images to white on black
t = transforms.Compose([
    transforms.Resize((32, 32)),
    transforms.ToTensor(),
    transforms.Normalize((0.1307,), (0.3081,))
])
img = torch.autograd.Variable(t(img).unsqueeze(0))
ori_img.close()

# Predict
model.eval()
output = model(img)
pred = output.data.max(1, keepdim=True)[1][0][0]
print('Prediction: {}'.format(pred))

我得到的结果:

TypeError                                 Traceback (most recent call last)
<ipython-input-182-abbffa2ce0d8> in <module>
      7     transforms.Normalize((0.1307,), (0.3081,))
      8 ])
----> 9 img = torch.autograd.Variable(t(img).unsqueeze(0))
     10 ori_img.close()

~/.local/lib/python3.6/site-packages/torchvision/transforms/transforms.py in __call__(self, img)
     59     def __call__(self, img):
     60         for t in self.transforms:
---> 61             img = t(img)
     62         return img
     63 

~/.local/lib/python3.6/site-packages/torchvision/transforms/transforms.py in __call__(self, img)
    196             PIL Image: Rescaled image.
    197         """
--> 198         return F.resize(img, self.size, self.interpolation)
    199 
    200     def __repr__(self):

~/.local/lib/python3.6/site-packages/torchvision/transforms/functional.py in resize(img, size, interpolation)
    236     """
    237     if not _is_pil_image(img):
--> 238         raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
    239     if not (isinstance(size, int) or (isinstance(size, Iterable) and len(size) == 2)):
    240         raise TypeError('Got inappropriate size arg: {}'.format(size))

TypeError: img should be PIL Image. Got <class 'numpy.ndarray'>

当我评论img = np.invert(ori_img)时,我没有出现任何错误,但所有预测结果均为2

有人可以帮忙吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

您可以执行以下功能:PIL.Image.fromarray从numpy数组创建PIL图像,然后可以使用PIL.ImageOps.invert函数对颜色进行反转。然后,您的img变量应为正确的类型并取反。