I am trying to write a web app which would offer the user to upload an image, select one of the three deep learning models through pressing the âSelect a Modelâ button, and then output either âHuman presence detectedâ or âNo human presence detectedâ for a binomial image classification problem.
I am getting this error
my code is like this:
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")
# loading images
def load_image(uploaded_file):
image = uploaded_file.resize((224,224))
im_array = np.array(image)/255 # a normalised 2D array
im_array = im_array.reshape(-1, 224, 224, 3) # to shape as (1, 224, 224, 3)
return im_array
st.sidebar.subheader("Select a Model")
model_name = st.sidebar.selectbox("Model", ("CNN", "ResNet50", "VGG16"))
if st.button("Try with the Default Image"):
image = Image.open('C:/Users/maria/Jupiter_Notebooks/Dataset_Thermal_Project/Camera_videos/Images_3sec_newdata_v2/oneman/image21.jpg')
st.subheader("Human is detected")
st.image(image)
# predicting images
if model_name == 'CNN':
st.write("Try out the CNN model with the default image or upload an image")
if st.sidebar.button("Get prediction", key='predict'):
st.subheader("Upload an image file")
uploaded_file = st.file_uploader("Upload a JPG image file", type=["jpg", "jpeg"])
if uploaded_file is not None:
image = load_image(Image.open(uploaded_file))
st.image(image)
st.subheader("CNN Results")
model_cnn = load_model("C:/Users/.../Camera_videos/Saved_models/cnn_model.h5")
model_cnn_ = tf.keras.models.Model(model_cnn.inputs, model_cnn.outputs)
pred_label = model_cnn_.predict(image)[0]
if model_name == 'ResNet50':
st.write("Try out the ResNet50 model with the default image or upload an image")
if st.sidebar.button("Get prediction", key='predict'):
st.subheader("Upload an image file")
uploaded_file = st.file_uploader("Upload a JPG image file", type=["jpg", "jpeg"])
if uploaded_file is not None:
image = load_image(Image.open(uploaded_file))
st.image(image)
st.subheader("ResNet50 Results")
model_resnet = load_model("C:/Users/.../Camera_videos/Saved_models/model_resnet.h5")
model_resnet_ = tf.keras.models.Model(model_resnet.inputs, model_resnet.outputs)
pred_label = model_resnet_.predict(image)[0]
st.write('Human is detected') if pred_label>0.5 else st.write('No human is detected')
#if model_name == 'VGG16':
else:
st.write("Try out the VGG16 model with the default image or upload an image")
if st.sidebar.button("Get prediction", key='predict'):
st.subheader("Upload an image file")
uploaded_file = st.file_uploader("Upload a JPG image file", type=["jpg", "jpeg"])
if uploaded_file is not None:
image = load_image(Image.open(uploaded_file))
st.image(image)
st.subheader("VGG16 Results")
model_vgg16 = load_model("C:/Users/.../Camera_videos/Saved_models/model_vgg16.h5")
model_vgg16_ = tf.keras.models.Model(model_vgg16.inputs, model_resnet.vgg16)
pred_label = model_resnet_.predict(image)[0]
st.write('Human is detected') if pred_label>0.5 else st.write('No human is detected')
I am a beginner with Streamlit, and its my first app.
I also have another version of my code, but here I am struggling to pass the âuploaded_fileâ i.e. the file supplied by the user, into my initialize_model() function.
import streamlit as st
import pandas as pd
import numpy as np
from numpy import vstack
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(uploaded_file):
image = uploaded_file.resize((224,224))
im_array = np.array(image)/255 # a normalised 2D array
im_array = im_array.reshape(-1, 224, 224, 3) # to shape as (1, 224, 224, 3)
return im_array
st.sidebar.subheader("Select a NN Model")
model_name = st.sidebar.selectbox("Model", ("CNN", "ResNet50", "VGG16"))
# predicting images
def initialize_model(model_name, image):
if model_name == 'CNN':
st.write("Try out the CNN model with the default image or upload an image")
if st.sidebar.button("Get prediction", key='predict'):
st.subheader("CNN Results")
model_cnn = load_model("C:/Users/.../Camera_videos/Saved_models/cnn_model.h5")
model_cnn_ = tf.keras.models.Model(model_cnn.inputs, model_cnn.outputs)
# image = load_image(Image.open(image))
pred_label = model_cnn_.predict(image)[0]
if model_name == 'ResNet50':
if st.sidebar.button("Get prediction", key='predict'):
st.subheader("ResNet50 Results")
model_resnet = load_model("C:/Users/.../Camera_videos/Saved_models/model_resnet.h5")
model_resnet_ = tf.keras.models.Model(model_resnet.inputs, model_resnet.outputs)
#image = load_image(uploaded_file)
pred_label = model_resnet_.predict(image)[0]
if model_name == 'VGG16':
if st.sidebar.button("Get prediction", key='predict'):
st.subheader("VGG16 Results")
model_vgg16 = load_model("C:/Users/.../Camera_videos/Saved_models/model_vgg16.h5")
model_vgg16_ = tf.keras.models.Model(model_vgg16.inputs, model_resnet.vgg16)
#image = load_image(uploaded_file)
pred_label = model_resnet_.predict(image)[0]
return print('Human is detected') if pred_label>0.5 else print('No human is detected')
if st.button("Try with the Default Image"):
d_image = Image.open('C:/Users/maria/Jupiter_Notebooks/Dataset_Thermal_Project/Camera_videos/Images_3sec_newdata_v2/oneman/image21.jpg')
st.image(d_image)
st.subheader("Human is detected")
st.image(initialize_model(model_name,d_image))
st.subheader("Upload an image file")
uploaded_file = st.file_uploader("Upload a JPG image file", type=["jpg", "jpeg"])
if uploaded_file is not None:
sel_image = load_image(Image.open(uploaded_file))
st.image(sel_image)
Can you advise me what Iâm doing wrong and how to achieve the task above?
Thank you.
s for answers to questions.