Spaces:
Build error
Build error
File size: 1,481 Bytes
6790860 2c82d30 6790860 13e198b 6790860 13e198b 6790860 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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()
|