| import io |
| import os |
|
|
| import django |
| import numpy as np |
| import pyrebase |
| import streamlit as st |
| import tensorflow as tf |
| from PIL import Image |
| from streamlit_option_menu import option_menu |
|
|
| from utils import clean_image, get_prediction, make_results |
|
|
| menu_choice = option_menu( |
| menu_title=None, |
| options=["Home", "Login/Logout", "History"], |
| icons=["house", "box-arrow-in-right", "clock-history"], |
| menu_icon="cast", |
| default_index=0, |
| orientation="horizontal", |
| ) |
|
|
| if menu_choice == "Home": |
| |
| @st.cache(allow_output_mutation=True) |
| def load_model(path): |
| |
| xception_model = tf.keras.models.Sequential([ |
| tf.keras.applications.xception.Xception(include_top=False, weights='imagenet', input_shape=(512, 512, 3)), |
| tf.keras.layers.GlobalAveragePooling2D(), |
| tf.keras.layers.Dense(4,activation='softmax') |
| ]) |
|
|
|
|
| |
| densenet_model = tf.keras.models.Sequential([ |
| tf.keras.applications.densenet.DenseNet121(include_top=False, weights='imagenet',input_shape=(512, 512, 3)), |
| tf.keras.layers.GlobalAveragePooling2D(), |
| tf.keras.layers.Dense(4,activation='softmax') |
| ]) |
|
|
| |
| inputs = tf.keras.Input(shape=(512, 512, 3)) |
|
|
| xception_output = xception_model(inputs) |
| densenet_output = densenet_model(inputs) |
|
|
| outputs = tf.keras.layers.average([densenet_output, xception_output]) |
|
|
|
|
| model = tf.keras.Model(inputs=inputs, outputs=outputs) |
|
|
| |
| model.load_weights(path) |
| |
| return model |
|
|
| |
| st.image('https://raw.githubusercontent.com/tanujdargan/plantbay/main/assets/plantbay.png?token=GHSAT0AAAAAABSBHTQM2WJJ5O7UQFBB2M5MYWPHMCQ', width=550) |
| st.write('Welcome to PlantBay!', 'Your Personal Plant Assistant!') |
|
|
| option = st.selectbox( |
| 'How would you like to detect a disease?', |
| ('Upload an Image', 'Camera')) |
| if option == 'Camera': |
| uploaded_file = st.camera_input("Take a picture") |
| if uploaded_file != None: |
| st.success('File Upload Success!!') |
| elif option == 'Upload an Image': |
| uploaded_file = st.file_uploader("Choose a Image file", type=["png", "jpg","jpeg"]) |
| |
| model = load_model('model_final.h5') |
| if model != None: |
| st.text("Keras Model Loaded") |
| if uploaded_file != None: |
| |
| |
| progress = st.text("Crunching Image") |
| my_bar = st.progress(0) |
| i = 0 |
| |
| |
| image = Image.open(io.BytesIO(uploaded_file.read())) |
| st.image(np.array(Image.fromarray( |
| np.array(image)).resize((700, 400), Image.ANTIALIAS)), width=None) |
| my_bar.progress(i + 40) |
| |
| |
| image = clean_image(image) |
| |
| |
| predictions, predictions_arr = get_prediction(model, image) |
| my_bar.progress(i + 30) |
| |
| |
| result = make_results(predictions, predictions_arr) |
| |
| |
| my_bar.progress(i + 30) |
| progress.empty() |
| i = 0 |
| my_bar.empty() |
| |
| |
| st.subheader(f"The plant{result['status']} with a prediction probability of {result['prediction']}.") |
|
|
| '''if menu_choice == "Login": |
| from streamlit_login_auth_ui.widgets import __login__ |
| |
| __login__obj = __login__(auth_token = "pk_prod_T3JEHHA0FTMDBNHXGENTAXXMXHAC", |
| company_name = "PlantBay", |
| width = 200, height = 250, |
| logout_button_name = 'Logout', hide_menu_bool = False, |
| hide_footer_bool = False, |
| lottie_url = 'https://assets2.lottiefiles.com/packages/lf20_jcikwtux.json') |
| |
| LOGGED_IN = __login__obj.build_login_ui() |
| |
| # Removing Menu |
| hide_streamlit_style = """ |
| <style> |
| #MainMenu {visibility: hidden;} |
| footer {visibility: hidden;} |
| </style> |
| """ |
| st.markdown(hide_streamlit_style, unsafe_allow_html=True) |
| |
| if menu_choice == "History": |
| st.write("History")''' |