import streamlit as st import tensorflow as tf from PIL import Image import io import numpy as np from streamlit_option_menu import option_menu # Function to add a background image def add_bg_from_local(image_path): with open(image_path, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) st.markdown( f""" """, unsafe_allow_html=True ) # Add background image add_bg_from_local('flower.png') def load_image(): uploaded_file = st.file_uploader(label='Pick an image to test') if uploaded_file is not None: image_data = uploaded_file.getvalue() st.image(image_data) img = Image.open(io.BytesIO(image_data)) img = img.resize((224, 224)) return img else: return None def load_model(): model_name = 'Model/model.h5' model = tf.keras.models.load_model(model_name) return model def load_labels(): with open('Oxford-102_Flower_dataset_labels.txt', 'r') as file: data = file.read().splitlines() return data def predict(model, labels, img): img_array = tf.keras.preprocessing.image.img_to_array(img) img_array = tf.expand_dims(img_array, 0) # Create a batch prediction = model.predict(img_array) predicted_class = np.argmax(prediction[0], axis=-1) flower = labels[predicted_class] closeness = np.round(prediction[0][predicted_class] * 100, 2) return flower, closeness def main(): with st.sidebar: selected = option_menu( menu_title="Main Menu", options=["About", "Local Classifier", "Extensive Classifier", "Project Details"], icons=["info-circle", "camera", "search", "clipboard-list"], menu_icon="cast", default_index=0, ) if selected == "About": st.title('About') st.markdown(""" ### NAME: TOLULOPE ### CLASS: HND2 ### LEVEL: 400L """) elif selected == "Local Classifier": st.title('Local Classifier') st.write("This is a demo of an image classification model trained on the Oxford Flower Dataset.") st.write("To test the model, upload an image of a flower and click the 'Run on image' button.") model = load_model() labels = load_labels() image = load_image() result = st.button('Run on image') if result and image is not None: st.markdown('**_Calculating results..._**') flower, closeness = predict(model, labels, image) st.markdown(f'