Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import joblib
|
4 |
+
from tensorflow.keras.utils import load_img, img_to_array
|
5 |
+
|
6 |
+
# Load the pre-trained model
|
7 |
+
model = joblib.load("flower.pkl")
|
8 |
+
|
9 |
+
# Define the class names (must match the training dataset order)
|
10 |
+
class_names = ['Daisy', 'Rose', 'Sunflower', 'Tulip', 'Dandelion']
|
11 |
+
|
12 |
+
# Function to preprocess the image and make predictions
|
13 |
+
def predict_flower(image):
|
14 |
+
# Resize and preprocess the image
|
15 |
+
img = image.resize((150, 150)) # Resize image to match the model input
|
16 |
+
img_array = np.array(img) / 255.0 # Normalize pixel values to [0, 1]
|
17 |
+
img_array = img_array.reshape((1, 150, 150, 3)) # Add batch dimension
|
18 |
+
|
19 |
+
# Make prediction
|
20 |
+
predictions = model.predict(img_array)
|
21 |
+
predicted_class = class_names[np.argmax(predictions)]
|
22 |
+
return f"The predicted flower is: {predicted_class}"
|
23 |
+
|
24 |
+
# Create the Gradio interface
|
25 |
+
title = "Flower Classification"
|
26 |
+
description = "Upload an image of a flower, and the model will predict the type of flower (Daisy, Rose, Sunflower, Tulip, or Dandelion)."
|
27 |
+
|
28 |
+
gr_interface = gr.Interface(
|
29 |
+
fn=predict_flower, # Function to process predictions
|
30 |
+
inputs=gr.Image(type="pil"), # Input: Image (PIL format)
|
31 |
+
outputs=gr.Textbox(), # Output: Textbox for predicted class
|
32 |
+
title=title,
|
33 |
+
description=description
|
34 |
+
)
|
35 |
+
|
36 |
+
# Launch the Gradio app
|
37 |
+
if __name__ == "__main__":
|
38 |
+
gr_interface.launch()
|