File size: 1,189 Bytes
c23107d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.efficientnet_v2 import preprocess_input

# Load the trained model
model = load_model("models/flower_model.h5")

# Class labels
class_names = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']

# Preprocessing + prediction
def predict_flower(img):
    img = img.resize((224, 224))
    img_array = image.img_to_array(img)  # raw [0, 255]
    img_array = preprocess_input(img_array)  # normalize to [-1, 1]
    img_array = np.expand_dims(img_array, axis=0)

    preds = model.predict(img_array)[0]

    return {
        f"{class_names[i]} ({preds[i]:.4f})": float(preds[i])
        for i in range(len(class_names))
    }

# Gradio interface
demo = gr.Interface(
    fn=predict_flower,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=5),
    title="🌸 Flower Classifier",
    description="Upload a flower image to classify it into one of the five categories: daisy, dandelion, roses, sunflowers, or tulips.",
)

if __name__ == "__main__":
    demo.launch()