gpt4 book ai didi

python - 在tensorflow中导入图像

转载 作者:太空宇宙 更新时间:2023-11-04 10:11:05 25 4
gpt4 key购买 nike

我正在尝试导入图像并将它们转换为张量。所有其他解决方案都建议制作一个 filename_queue 并使用 tf.reader(),但我无法让它工作......因此,我只是继续基础知识。

我的桌面目录中有一个名为test.jpg 的文件,我正在运行一个linux 环境。这是我的代码:

import tensorflow as tf

image = tf.image.decode_jpeg("~/Desktop/test.jpg", channels=1)
print(image)

如您所见,一些非常简单的代码......但是它会输出

Tensor("DecodeJpeg:0", shape=(?, ?, 1), dtype=uint8)

这告诉我它没有正确读取文件。我做错了什么吗?

谢谢!

最佳答案

tf.image.decode_jpeg 函数只是构建一个图节点并将其添加到执行图中。您必须实际评估节点以运行它并获取其值作为 numpy 数组。尝试以下操作:

import tensorflow as tf

image = tf.image.decode_jpeg(tf.read_file("~/Desktop/test.jpg"), channels=1)
sess = tf.InteractiveSession()
print(sess.run(image))

请注意,tf.image.decode_jpeg 本身的参数应该是一个字符串 Tensor。值得庆幸的是,TensorFlow 有一个将文件读入 Tensor 的操作,即 tf.read_file

关于python - 在tensorflow中导入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38272699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com