python - tensorflow 检查图像在阅读器中

标签 python image tensorflow

当输出仅为:Tensor("DecodeJpeg:0", shape=TensorShape([Dimension(None), Dimension(None), Dimension(无)]), dtype=uint8)

如何显示张量对象的图像和标签?

(部分代码来自:Tensorflow read images with labels)

import tensorflow as tf
from PIL import Image
import numpy as np
from os.path import join

KNOWN_HEIGHT = 812
KNOWN_WIDTH = 812

def read_my_file_format(self, filename_and_label_tensor):
    """Consumes a single filename and label as a ' '-delimited string.

    Args:
      filename_and_label_tensor: A scalar string tensor.

    Returns:
      Two tensors: the decoded image, and the string label.
    """
    filename, label = tf.decode_csv(filename_and_label_tensor, [[""], [""]], ",")
    file_contents = tf.read_file(filename)
    image = tf.image.decode_jpeg(file_contents)
    #image.set_shape([KNOWN_HEIGHT, KNOWN_WIDTH, 3])

    return image, label

string = ['test.jpg,m', 'test2.jpg,f']
filepath_queue = tf.train.string_input_producer(string)

image, label = read_my_file_format(filepath_queue.dequeue())

print(image)
# Output: Tensor("DecodeJpeg:0", shape=TensorShape([Dimension(None), Dimension(None), Dimension(None)]), dtype=uint8)

print(label)
# Output: Tensor("DecodeCSV:1", shape=TensorShape([]), dtype=string)

如何在 image 中显示实际图像并显示 label?因为图像是否确实在同一个文件夹中不会改变 imagelabel 的输出。

编辑

以下代码(部分来自:Tensorflow image reading & display)在我 friend 的 Mac 上显示了一个图像,但在我的 Ubuntu 14.04 上没有:

# Test show image
images = []
with tf.Session() as sess:

    # Start populating the filename queue.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    if len(string) > 0:
      for i in range(len(string)):
        plaatje = result.image.eval()
        images.append(plaatje)

    Image._showxv(Image.fromarray(np.asarray(plaatje)))

    coord.request_stop()
    coord.join(threads)
    print("tf.session success")

这会导致以下错误:

W tensorflow/core/common_runtime/executor.cc:1076] 0x7fa3940cb950 Compute status: Cancelled: Enqueue operation was cancelled
 [[Node: input_producer/input_producer_EnqueueMany = QueueEnqueueMany[Tcomponents=[DT_STRING], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](input_producer, input_producer/RandomShuffle)]]
I tensorflow/core/kernels/queue_base.cc:278] Skipping cancelled enqueue attempt

最佳答案

您的第二个代码部分似乎省略了代码,不是吗?

尝试建议的解决方案 here .在启动队列运行器之前,您必须初始化占位符。这为我解决了类似的问题。

我想您已经知道必须对张量调用 .eval() 才能获得其实际值。看这个question I made

关于python - tensorflow 检查图像在阅读器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34654220/

相关文章:

python - 循环中断而不执行最后一个打印语句

python - 如何正确地逐行调整 pandas 数据框

objective-c - 使用 QTCaptureView 拍照

python - 在线性回归中获得非常高的值

python - 从unittest.TestCase切换到tf.test.TestCase后的幻像测试

python - 将表头分配为 Python 字典中的键

python - Pandas - 拆分大型 excel 文件

c++ - 无法从压缩纹理数据创建图像 (S3TC)

ios - Obj-C 检查图像是否已存在于照片库中

python - 使用干净和退化的照片训练深度学习模型