rice / app.py
Bishal Sharma
2nd update
ec2844c verified
raw
history blame contribute delete
990 Bytes
import gradio as gr
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import numpy as np
import pickle
# Load model and class indices
model = tf.keras.models.load_model("rice_disease_detector.h5")
with open("class_indices.pkl", "rb") as f:
class_indices = pickle.load(f)
class_names = list(class_indices.keys())
def predict_rice_disease(img):
img = img.resize((224, 224))
img_array = image.img_to_array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
predictions = model.predict(img_array)[0] # shape: (num_classes,)
# Map class names to confidence scores
confidence_scores = {class_names[i]: float(f"{predictions[i]:.4f}") for i in range(len(class_names))}
return confidence_scores
iface = gr.Interface(
fn=predict_rice_disease,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=len(class_names)), # shows all class scores nicely
title="Rice Disease Classifier"
)
iface.launch()