如何通过TensorFlow读取TGA图像文件

时间:2019-04-18 12:23:10

标签: tensorflow

在创建读取TGA图像文件的输入管道时遇到麻烦。

tf.decode_image()用于解码jpeg/png/bmp/gif图像。因此,我尝试通过tf.py _ function通过tf.placeholder传递图像路径来解决此问题,如下所示。但这是行不通的。当我直接通过图像路径时,没有问题。

def myfunc(f_p):
    im = np.asarray(Image.open(f_p), dtype='float32')
    im /= 255
    return im

f_p_ph = tf.placeholder(tf.string, name='file_path')
image = tf.py_function(myfunc, [f_p_ph], tf.float32, name='image')
with tf.Session() as sess:
    filename = r'xxx.tga'
    img = sess.run(image, feed_dict={f_p_ph: filename})
    print(img)

但可以如下所示:

filename = r'xxx.tga'

def myfunc():
    im = np.asarray(Image.open(filename), dtype='float32')
    im /= 255
    return im

f_p_ph = tf.placeholder(tf.string, name='file_path')
image = tf.py_function(myfunc, [f_p_ph], tf.float32, name='image')
with tf.Session() as sess:
    img = sess.run(image)
    print(img)

错误消息是

Traceback (most recent call last):

  File "D:\programs\anaconda3\lib\site-packages\PIL\Image.py", line 2638, in open
    fp.seek(0)

AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'seek'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\ops\script_ops.py", line 205, in __call__
    return func(device, token, args)

  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\ops\script_ops.py", line 107, in __call__
    ret = self._func(*args)

  File "E:/code/python/Test/example/test.py", line 108, in myfunc
    im = np.asarray(Image.open(f_p), dtype='float32')

  File "D:\programs\anaconda3\lib\site-packages\PIL\Image.py", line 2640, in open
    fp = io.BytesIO(fp.read())

AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'read'


Traceback (most recent call last):
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1334, in _do_call
    return fn(*args)
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1319, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1407, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.UnknownError: AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'read'
Traceback (most recent call last):

  File "D:\programs\anaconda3\lib\site-packages\PIL\Image.py", line 2638, in open
    fp.seek(0)

AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'seek'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\ops\script_ops.py", line 205, in __call__
    return func(device, token, args)

  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\ops\script_ops.py", line 107, in __call__
    ret = self._func(*args)

  File "E:/code/python/Test/example/test.py", line 108, in myfunc
    im = np.asarray(Image.open(f_p), dtype='float32')

  File "D:\programs\anaconda3\lib\site-packages\PIL\Image.py", line 2640, in open
    fp = io.BytesIO(fp.read())

AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'read'


     [[{{node image}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/code/python/Test/example/test.py", line 121, in <module>
    test()
  File "E:/code/python/Test/example/test.py", line 116, in test
    img = sess.run(image, feed_dict={f_p_ph: filename})
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
    run_metadata_ptr)
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1152, in _run
    feed_dict_tensor, options, run_metadata)
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1328, in _do_run
    run_metadata)
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1348, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.UnknownError: AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'read'
Traceback (most recent call last):

  File "D:\programs\anaconda3\lib\site-packages\PIL\Image.py", line 2638, in open
    fp.seek(0)

AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'seek'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\ops\script_ops.py", line 205, in __call__
    return func(device, token, args)

  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\ops\script_ops.py", line 107, in __call__
    ret = self._func(*args)

  File "E:/code/python/Test/example/test.py", line 108, in myfunc
    im = np.asarray(Image.open(f_p), dtype='float32')

  File "D:\programs\anaconda3\lib\site-packages\PIL\Image.py", line 2640, in open
    fp = io.BytesIO(fp.read())

AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'read'


     [[node image (defined at E:/code/python/Test/example/test.py:113) ]]

Caused by op 'image', defined at:
  File "E:/code/python/Test/example/test.py", line 121, in <module>
    test()
  File "E:/code/python/Test/example/test.py", line 113, in test
    image = tf.py_function(myfunc, [f_p_ph], tf.float32, name='image')
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\ops\script_ops.py", line 389, in eager_py_func
    return _internal_py_func(func=func, inp=inp, Tout=Tout, eager=True, name=name)
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\ops\script_ops.py", line 278, in _internal_py_func
    input=inp, token=token, Tout=Tout, name=name)
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\ops\gen_script_ops.py", line 73, in eager_py_func
    "EagerPyFunc", input=input, token=token, Tout=Tout, name=name)
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 788, in _apply_op_helper
    op_def=op_def)
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3300, in create_op
    op_def=op_def)
  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1801, in __init__
    self._traceback = tf_stack.extract_stack()

UnknownError (see above for traceback): AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'read'
Traceback (most recent call last):

  File "D:\programs\anaconda3\lib\site-packages\PIL\Image.py", line 2638, in open
    fp.seek(0)

AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'seek'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\ops\script_ops.py", line 205, in __call__
    return func(device, token, args)

  File "D:\programs\anaconda3\lib\site-packages\tensorflow\python\ops\script_ops.py", line 107, in __call__
    ret = self._func(*args)

  File "E:/code/python/Test/example/test.py", line 108, in myfunc
    im = np.asarray(Image.open(f_p), dtype='float32')

  File "D:\programs\anaconda3\lib\site-packages\PIL\Image.py", line 2640, in open
    fp = io.BytesIO(fp.read())

AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'read'


     [[node image (defined at E:/code/python/Test/example/test.py:113) ]]


Process finished with exit code 1

0 个答案:

没有答案