samir1120's picture
Update app.py
13e198b verified
import gradio as gr
import numpy as np
import joblib
from tensorflow.keras.utils import load_img, img_to_array
# Load the pre-trained model
model = joblib.load("flower1.pkl")
# Define the class names (must match the training dataset order)
class_names = ["Rose", "Tulip", "Daisy", "Sunflower", "Daffodil","Lily","Orchid","Lotus","Jasmine","Marigold","Hibiscus","Tube rose","Gladiolus","Frangipani","Iris","Dahlia"]
# Function to preprocess the image and make predictions
def predict_flower(image):
# Resize and preprocess the image
img = image.resize((150, 150)) # Resize image to match the model input
img_array = np.array(img) / 255.0 # Normalize pixel values to [0, 1]
img_array = img_array.reshape((1, 150, 150, 3)) # Add batch dimension
# Make prediction
predictions = model.predict(img_array)
predicted_class = class_names[np.argmax(predictions)]
return f"The predicted flower is: {predicted_class}"
# Create the Gradio interface
title = "Flower Classification"
description = "Upload an image of a flower, and the model will predict the type of flower (Daisy, Rose, Sunflower, Tulip etc.)."
gr_interface = gr.Interface(
fn=predict_flower, # Function to process predictions
inputs=gr.Image(type="pil"), # Input: Image (PIL format)
outputs=gr.Textbox(), # Output: Textbox for predicted class
title=title,
description=description
)
# Launch the Gradio app
if __name__ == "__main__":
gr_interface.launch()