从 streamlit file_uploader 中获取错误
getting an error from streamlit file_uploader
我在尝试预测从 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.
由于缓冲区中已有图像,您可以试试这个。
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 加载图像时遇到此错误。当我尝试直接加载 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.
由于缓冲区中已有图像,您可以试试这个。
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)