Hi,
I have this code which works fine in my jupyter notebook.
def load_image(image):
image = image.resize((224,224))
img_array = np.array(image)/255 # a normalised 2D array
img_array = img_array.reshape(-1, 224, 224, 3) # to shape as (1, 224, 224, 3)
return img_array
im_path = 'C:\\Users\\..\\oneman\\image21.jpg'
img = load_image(Image.open(im_path))
model_cnn = load_model("C:/Users/.../Saved_models/cnn_model.h5")
model_cnn_ = tf.keras.models.Model(model_cnn.inputs, model_cnn.outputs)
pred_label = model_cnn_.predict(img)[0]
if pred_label>0.5:
print('Human is detected')
else:
print("No human is detected: ")
Output: 'Human is detected' (as expected)
However, when I try to implement the same in Streamlit with this code, I am getting an error.
import streamlit as st
import pandas as pd
import numpy as np
from PIL import Image
import tensorflow as tf
from tensorflow.keras.models import load_model
st.title("Binary Human Detection Web App")
st.markdown("Is there a human in office space? 🧍")
# loading images
def load_image(image):
image = image.resize((224,224))
img_array = np.array(image)/255 # a normalised 2D array
img_array = img_array.reshape(-1, 224, 224, 3) # to shape as (1, 224, 224, 3)
return img_array
uploaded_file = st.sidebar.file_uploader(" ",type=['jpg', 'jpeg'])
if uploaded_file is not None:
u_img = load_image(Image.open(uploaded_file))
img = st.image(u_img, 'Uploaded Image', use_column_width=True)
st.sidebar.write('\n')
if st.sidebar.button("Click Here to Predict"):
if uploaded_file is None:
st.sidebar.write("Please upload an Image to Classify")
else:
#with st.spinner('Classifying ...'):
model_cnn = load_model("C:/Users/.../Saved_models/cnn_model.h5")
model_cnn_ = tf.keras.models.Model(model_cnn.inputs, model_cnn.outputs)
pred_label = model_cnn_.predict(img)[0]
st.sidebar.header("CNN results: ")
#st.write('Human is detected') if pred_label>0.5 else st.write('No human is detected')
if pred_label>0.5:
st.write('Human is detected')
else:
st.write('No human is detected')
the error is
Can you help me?
I have uploaded cnn_ltsm.h5, image20.jpg and image21.jpg to my github, if someone wants to reproduce my problem. GitHub - bluetail14/Thermal_data_images_project
I am running Streamlit via Anaconda in Google Chrome, Python 3.9.12 , keras 2.9.0, tensorflow 2.9.1, cudnn 8.1.0.77, cudatoolkit 11.2.2, streamlit 1.11.1, windows 64.
Thank you!