|
|
import streamlit as st |
|
|
import tensorflow as tf |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
import io |
|
|
import numpy as np |
|
|
|
|
|
|
|
|
@st.cache_resource |
|
|
|
|
|
def load_model(): |
|
|
MODEL_PATH = "./model_v1.keras" |
|
|
model = tf.keras.models.load_model(MODEL_PATH) |
|
|
return model |
|
|
|
|
|
|
|
|
model = load_model() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class_names = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy'] |
|
|
|
|
|
IMAGE_SIZE = 256 |
|
|
|
|
|
def predict(model, img): |
|
|
img = img.resize((IMAGE_SIZE, IMAGE_SIZE)) |
|
|
img_array = tf.keras.preprocessing.image.img_to_array(img) |
|
|
img_array = tf.expand_dims(img_array, 0) |
|
|
|
|
|
predictions = model.predict(img_array) |
|
|
|
|
|
predicted_class = class_names[np.argmax(predictions[0])] |
|
|
confidence = round(100 * (np.max(predictions[0])), 2) |
|
|
return predicted_class, confidence |
|
|
|
|
|
|
|
|
st.title("Potato Disease Classification") |
|
|
|
|
|
|
|
|
option = st.radio("Select input method:", ('Upload Image', 'Take Picture')) |
|
|
|
|
|
if option == 'Upload Image': |
|
|
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
|
|
if uploaded_file is not None: |
|
|
image = Image.open(uploaded_file).convert('RGB') |
|
|
st.image(image, caption='Uploaded Image', use_column_width=True) |
|
|
|
|
|
|
|
|
predicted_class, confidence = predict(model, image) |
|
|
|
|
|
|
|
|
st.write(f"Predicted class: {predicted_class}") |
|
|
st.write(f"Confidence: {confidence:.2f}") |
|
|
|
|
|
elif option == 'Take Picture': |
|
|
picture = st.camera_input("Take a picture") |
|
|
if picture is not None: |
|
|
image = Image.open(picture).convert('RGB') |
|
|
st.image(image, caption='Captured Image', use_column_width=True) |
|
|
|
|
|
|
|
|
predicted_class, confidence = predict(model, image) |
|
|
|
|
|
|
|
|
st.write(f"Predicted class: {predicted_class}") |
|
|
st.write(f"Confidence: {confidence:.2f}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|