CoolFace
Apppublic

Shru/Rice_Disease_Classifier

sourceHugging Facegpl-3.0updated 2y agoView on Hugging Face
0likes
Model_2.py103 linesDownload Raw Back to pages
1# Trained on 80 epochs at Batch size 256 with Learning Rate of 0.001 under 50 seconds!!2from PIL import Image, ImageOps3import numpy as np4import pandas as pd5import streamlit as st6import tensorflow as tf7 8# Suppress warnings9import warnings10warnings.filterwarnings('ignore', category=UserWarning)11warnings.filterwarnings('ignore', category=FutureWarning)12 13# Disable scientific notation for clarity14np.set_printoptions(suppress=True)15 16def load_model_teachable():17    # Load the model18    model = tf.keras.models.load_model("pages/keras_model.h5")19    return model20 21with st.spinner('Model is being loaded..'):22    model = load_model_teachable()23 24st.write("""25         # Teachable Machine Model 26         """27         )28 29st.sidebar.info("You should be happy if it classifies as - Healthy plant ๐Ÿ™‚")30 31file = st.file_uploader("Upload the image to be classified", type=["jpg", "png"])32st.set_option('deprecation.showfileUploaderEncoding', False)33 34 35def upload_predict_teachable(image, model, class_names):36 37    # Create the array of the right shape to feed into the keras model38    # The 'length' or number of images you can put into the array is39    # determined by the first position in the shape tuple, in this case 140    data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)41 42    # Replace this with the path to your image43    image_RGB = image.convert("RGB")44 45    # resizing the image to be at least 224x224 and then cropping from the center46    image_resized = image_RGB.resize((224, 224), resample=Image.LANCZOS)47 48    # turn the image into a numpy array49    image_array = np.asarray(image_resized)50 51    # Normalize the image52    normalized_image_array = (image_array.astype(np.float32) / 127.5) - 153 54    # Load the image into the array55    data[0] = normalized_image_array56 57    # Predicts the model58    prediction = model.predict(data)59    return prediction60 61 62if file is None:63    st.text("Please upload an image file")64else:65    image = Image.open(file)66    st.image(image, use_column_width=True)67    68    # Load the labels69    class_names = open("pages/labels.txt", "r").readlines()70    prediction = upload_predict_teachable(image, model,class_names)71    index = np.argmax(prediction)72    class_name = class_names[index]73    confidence_score = prediction[0][index]74 75    # Print prediction and confidence score76    result = f"Your plant is suffering from: {class_name[2:]}"77    result_score = "โœ… Accurate prediction score is: {} / 100".format("%.2f"  %  confidence_score)78    st.success(result)79    st.info(result_score)80 81 82st.markdown("""83    #### Know more about this disease and suggestions to prevent them from spreading.84 85    - *Just copy the disease name (result) from above & paste it in the below input box as it is!*86""")87 88# Loading diseases info & management strategies89df = pd.read_csv('pages/Disease_solutions.csv')90disease = st.text_input('Enter the disease name below ๐Ÿ‘‡', '')91 92# Selecting rows based on condition93about = df.loc[df['Diseases'] == disease, ['Type', 'Description']]94strategies = df.loc[df['Diseases'] == disease, 'Management Strategies']95 96# Print diseases' other info97tab1, tab2 = st.tabs(["๐Ÿ‘€ Description", "โญ Solution"])98 99with tab1:100    st.dataframe(about, use_container_width=True)101        102with tab2:103    st.dataframe(strategies, use_container_width=True)