luisotorres's picture
Update app.py
8e7d5e1 verified
raw
history blame contribute delete
No virus
1.65 kB
import os
import gradio as gr
import tensorflow as tf
import numpy as np
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
# Loading saved model
model = tf.keras.models.load_model('plant_disease_classifier.h5')
def predict(input_image):
try:
# Preprocessing
input_image = tf.convert_to_tensor(input_image)
input_image = tf.image.resize(input_image, [256, 256])
input_image = tf.expand_dims(input_image, 0) / 255.0
predictions = model.predict(input_image)
labels = ['Healthy', 'Powdery', 'Rust']
class_idx = np.argmax(predictions)
class_label = labels[class_idx]
confidence = np.round(predictions[0][class_idx] * 100, 3)
return f"Predicted Class: {class_label}. Confidence Score: {confidence}%"
except Exception as e:
return f"An error occurred: {e}"
examples = ["Healthy.png", "Powdery.png", "Rust.png"]
iface = gr.Interface(
fn=predict,
inputs=gr.Image(),
outputs="text",
title="🌿 Plant Disease Detection",
description='<br> This is a specialized Image Classification model engineered to identify the health status of plants, specifically detecting conditions of Powdery Mildew or Rust. <br> \
This model is based on a Convolutional Neural Network that I have trained, evaluated, and validated on my Kaggle Notebook: <a href="https://www.kaggle.com/code/lusfernandotorres/convolutional-neural-network-from-scratch">🧠 Convolutional Neural Network From Scratch</a>. <br> \
<br> Upload a photo of a plant to see how the model classifies its status!',
examples=examples
)
iface.launch(share=True)