File size: 1,080 Bytes
d21c4b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
import gradio as gr
import tensorflow as tf
import numpy as np
import json

# Load the trained model
model = tf.keras.models.load_model("model/plant_identifier_efficientnetb0.keras")

# Load class indices
with open("model/class_indices.json", "r") as f:
    class_indices = json.load(f)

# Reverse the class_indices to map predicted index -> label
index_to_class = {v: k for k, v in class_indices.items()}

def predict(image):
    image = image.resize((224, 224))
    img_array = np.array(image) / 255.0
    img_array = img_array[np.newaxis, ...]

    # Predict
    prediction = model.predict(img_array)
    predicted_index = int(np.argmax(prediction))
    confidence = float(np.max(prediction))
    label = index_to_class[predicted_index]

    return f"This looks like a {label} ({confidence:.2%} confidence)."

# Gradio interface
demo = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="Plant Identifier",
    description="Upload an image of a plant, and this AI will tell you what type it is.",
    theme="default",
)

demo.launch()