import streamlit as st import tensorflow as tf from tensorflow.keras.applications.imagenet_utils import decode_predictions import cv2 from PIL import Image, ImageOps import numpy as np @st.cache(allow_output_mutation=True) def load_model(): model=tf.keras.models.load_model('keras.h5') return model with st.spinner('Model is being loaded..'): model=load_model() st.write(""" # Image Classification """ ) file = st.file_uploader("Upload the image to be classified U0001F447", type=["jpg", "png"]) st.set_option('deprecation.showfileUploaderEncoding', False) def upload_predict(upload_image, model): size = (224,224) image = ImageOps.fit(upload_image, size, Image.ANTIALIAS) image = np.asarray(image) img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) img_resize = cv2.resize(img, dsize=(224, 224),interpolation=cv2.INTER_CUBIC) image = np.asarray(img_resize, dtype=np.float32).reshape(1, 224, 224, 3) # Normalize the image array image = (image / 127.5) - 1 # Predicts the model prediction = model.predict(image) index = np.argmax(prediction) classname = ['Glass', 'NoGlass'] class_name = classname[index] return class_name if file is None: st.text("Please upload an image file") else: image = Image.open(file) st.image(image, use_column_width=True) predictions = upload_predict(image, model) image_class = str(predictions) st.write("The image is classified as",image_class)