Spaces:
Runtime error
Runtime error
Download app.py from danushrajaram/matlab: direct link, hf CLI and curl.
- Browser
- Download file 1.64 kB
-
https://huggingface.co/spaces/danushrajaram/matlab/resolve/main/app.py
- Command line
-
hf download hf://spaces/danushrajaram/matlab/app.py
-
curl -L -o app.py https://huggingface.co/spaces/danushrajaram/matlab/resolve/main/app.py
1.64 kB
| import streamlit as st | |
| from tensorflow.keras.models import load_model | |
| from tensorflow.keras.preprocessing.image import load_img, img_to_array | |
| import numpy as np | |
| import pickle | |
| # Load the saved model | |
| model = load_model("best_model.h5") | |
| # Load the class labels from a pickle file | |
| with open("mod_class_labels.pkl", "rb") as f: | |
| class_indices = pickle.load(f) | |
| # Function to preprocess the image | |
| def preprocess_image(image): | |
| image = load_img(image, target_size=(256, 256)) # Load the image with target size | |
| image = img_to_array(image) # Convert the image to array | |
| image = np.expand_dims(image, axis=0) # Expand dimensions to match the input shape | |
| image = image / 255.0 # Rescale the image | |
| return image | |
| # Function to make a prediction and get the label | |
| def predict_image(image): | |
| image = preprocess_image(image) | |
| prediction = model.predict(image) | |
| predicted_class = np.argmax(prediction, axis=1)[0] | |
| predicted_label = class_indices[predicted_class] | |
| return predicted_label | |
| # Streamlit App | |
| st.title("Rice Leaf Disease Classification") | |
| st.write("Upload an image of a rice leaf and the model will predict its disease category.") | |
| # File uploader | |
| uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| # Display the uploaded image | |
| image = load_img(uploaded_file, target_size=(256, 256)) | |
| st.image(image, caption='Uploaded Image', use_column_width=True) | |
| st.write("") | |
| st.write("Classifying...") | |
| # Make a prediction | |
| predicted_label = predict_image(uploaded_file) | |
| st.write(f"Predicted label: {predicted_label}") | |