【发布时间】:2020-02-01 18:42:23
【问题描述】:
我正在开发人脸识别应用程序并准备读取 jpg 图像然后训练我的神经网络的数据集。为了提高准确性,我决定将图像转换为灰度(内部映射功能)。 这是我将图像转换为张量的方式:
from PIL import Image
image_data = tf.io.decode_jpeg(
image_bytes_string,
channels=3
)
image_data = tf.reshape(image_data , (277, 370, 3))
label = parsed['label']
label = tf.reshape(label, (1,))
return image_data, label
我如何显示图像的代码:
image_data = dataset_item[0].numpy()
img = Image.fromarray(image_data)
img
更改代码转换为灰度后,显示图像的代码显示此错误:
KeyError: ((1, 1, 1), '|u1')
During handling of the above exception, another exception occurred:
Cannot handle this data type
灰度代码:
image_data = tf.io.decode_jpeg(
image_bytes_string,
channels=3
)
grayscale = tf.image.rgb_to_grayscale(
image_data
)
image_data = tf.reshape(grayscale, (277, 370, 1))
label = parsed['label']
label = tf.reshape(label, (1,))
return image_data, label
我如何将图像转换为灰度或 PIL 库有问题吗?
更新: 这是带有示例的 colab:https://colab.research.google.com/drive/1v72_C5i8HZzSLEy_p6d45EWq1byoF3fe
【问题讨论】:
-
请分享一个能重现问题的独立笔记本。
-
@BobSmith 抱歉延迟回答,你可以在这个 colab 中看到它:colab.research.google.com/drive/…
标签: python tensorflow python-imaging-library google-colaboratory