|
|
|
|
|
import gradio as gr |
|
|
import tensorflow as tf |
|
|
import numpy as np |
|
|
from datasets import load_dataset |
|
|
from PIL import Image |
|
|
|
|
|
|
|
|
def preprocess_inference_image(image): |
|
|
|
|
|
image = tf.convert_to_tensor(image, dtype=tf.float32) |
|
|
|
|
|
if tf.shape(image)[-1] == 4: |
|
|
image = image[..., :3] |
|
|
|
|
|
image = tf.image.convert_image_dtype(image, tf.float32) |
|
|
|
|
|
image = tf.image.resize(image, [224, 224]) |
|
|
|
|
|
image = tf.expand_dims(image, axis=0) |
|
|
return image |
|
|
|
|
|
|
|
|
model = tf.keras.models.load_model('maize_disease_model.keras') |
|
|
|
|
|
|
|
|
ds = load_dataset("aquib1011/maize-leaf-disease", cache_dir=None) |
|
|
label_names = ds['train'].features['label'].names |
|
|
|
|
|
def predict_maize_disease(image): |
|
|
|
|
|
image = np.array(image) |
|
|
|
|
|
processed_image = preprocess_inference_image(image) |
|
|
|
|
|
|
|
|
predictions = model.predict(processed_image) |
|
|
|
|
|
|
|
|
return {label_names[i]: float(predictions[0][i]) for i in range(len(label_names))} |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=predict_maize_disease, |
|
|
inputs=gr.Image(type="pil"), |
|
|
outputs=gr.Label(), |
|
|
title="Maize Leaf Disease Classifier", |
|
|
description="Upload an image of a maize leaf to get a prediction of the disease." |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
iface.launch() |
|
|
|