从 streamlit file_uploader 获取错误

Posted

技术标签:

【中文标题】从 streamlit file_uploader 获取错误【英文标题】:getting an error from streamlit file_uploader 【发布时间】:2021-08-22 06:39:10 【问题描述】:

我在尝试从 streamlit file_uploader 加载图像来预测图像时收到此错误。当我尝试直接在 ide 中加载图像时,该过程工作正常。但问题出在 streamlit file_uploader。我无法确定streamlit 以哪种文件类型上传文件。请帮助我了解如何上传自定义图像并使用 Keras 模型进行预测。

它显示了这个错误。

ValueError: Attempt to convert a value (<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=474x266 at 0x1C7B84190A0>) with an unsupported type (<class 'PIL.JpegImagePlugin.JpegImageFile'>) to a Tensor.

我的代码是

import tensorflow as tf
import streamlit as st
from PIL import  Image

class_names = ['apple_pie',....'] # total 101 food name.

# loading the model
model = tf.keras.models.load_model('effi_080_second.h5') 

file = st.file_uploader('Upload a file', type='jpg') # asking for file

image = Image.open(file)
st.image(image) # showing the image.


img = tf.io.read_file(file)
img = tf.image.decode_image(img)
# rsize the image
img = tf.image.resize(image, size=(224,224))
img = tf.expand_dims(img, axis=0)
pred = model.predict(img)

pred_cls = class_names[pred.argmax()] # gettting the class name index
st.write(pred_cls) # writting the class name```



The full error is
   `ValueError: Attempt to convert a value (<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=474x266 at 0x1C7B84190A0>) with an unsupported type (<class 'PIL.JpegImagePlugin.JpegImageFile'>) to a Tensor.
Traceback:
File "e:\anaconda3\envs\webapp_streamlit\lib\site-packages\streamlit\script_runner.py", line 338, in _run_script
    exec(code, module.__dict__)
File "E:\WebApp_streamlit\image_process.py", line 120, in <module>
    img = tf.image.resize(image, size=(224,224))
File "e:\anaconda3\envs\webapp_streamlit\lib\site-packages\tensorflow\python\util\dispatch.py", line 201, in wrapper
    return target(*args, **kwargs)
File "e:\anaconda3\envs\webapp_streamlit\lib\site-packages\tensorflow\python\ops\image_ops_impl.py", line 1540, in resize_images_v2
    return _resize_images_common(
File "e:\anaconda3\envs\webapp_streamlit\lib\site-packages\tensorflow\python\ops\image_ops_impl.py", line 1210, in _resize_images_common
    images = ops.convert_to_tensor(images, name='images')
File "e:\anaconda3\envs\webapp_streamlit\lib\site-packages\tensorflow\python\framework\ops.py", line 1499, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "e:\anaconda3\envs\webapp_streamlit\lib\site-packages\tensorflow\python\framework\constant_op.py", line 338, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
File "e:\anaconda3\envs\webapp_streamlit\lib\site-packages\tensorflow\python\framework\constant_op.py", line 263, in constant
    return _constant_impl(value, dtype, shape, name, verify_shape=False,
File "e:\anaconda3\envs\webapp_streamlit\lib\site-packages\tensorflow\python\framework\constant_op.py", line 275, in _constant_impl
    return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
File "e:\anaconda3\envs\webapp_streamlit\lib\site-packages\tensorflow\python\framework\constant_op.py", line 300, in _constant_eager_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
File "e:\anaconda3\envs\webapp_streamlit\lib\site-packages\tensorflow\python\framework\constant_op.py", line 98, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)`


Please help me with this, I want to know how to predict an image from streamlit file uploader and then predict it from the keras model.

【问题讨论】:

【参考方案1】:

由于您已经在缓冲区中有图像,您可以试试这个。

import streamlit as st
from PIL import Image
import numpy as np
import tensorflow as tf

file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])

if file is not None:
    image = Image.open(file)

    st.image(
        image,
        caption=f"You amazing image has shape",
        use_column_width=True,
    )

    img_array = np.array(image)
    img = tf.image.resize(img_array, size=(224,224))
    img = tf.expand_dims(img, axis=0)

【讨论】:

以上是关于从 streamlit file_uploader 获取错误的主要内容,如果未能解决你的问题,请参考以下文章

上传 .gpx 文件 Streamlit Python

在 Streamlit 中放置 sqlite3 db 文件的问题

如何从 Javascript 向 Streamlit 发送数据?

Streamlit 浏览器应用程序无法从 Sagemaker 终端打开

Streamlit 按钮只能使用一次

Streamlit:如何将变量的值存储在缓存中