SUBHABRATA NATH
SUBHABRATA NATH

Reputation: 165

Getting an error from streamlit file_uploader

I am getting this error when trying to predict the image from loading it from streamlit file_uploader. the process is working fine when I try directly loading the image in ide. but the problem is arising with streamlit file_uploader. i can't figure in which file type the streamlit is uploading the file. Please help me with how I can upload a custom image and predict it with the Keras model.

It is showing this error.

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.

My code is

import numpy as np
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()] # getting 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
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.

Upvotes: 0

Views: 2986

Answers (1)

Mohan Radhakrishnan
Mohan Radhakrishnan

Reputation: 3197

Since you already have the image in the buffer you can try this.

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)

Upvotes: 0

Related Questions