File size: 1,869 Bytes
81555fe
 
 
 
 
 
 
 
459e16f
81555fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import gradio as gr
import tensorflow as tf
import numpy as np
import json
from tensorflow.keras.applications.efficientnet import preprocess_input
from tensorflow.keras.preprocessing import image as keras_image

# Load Model & Class Indices
MODEL_PATH = "latest_model%252520%2525281%252529.keras"
CLASS_INDICES_PATH = "class_indices%2525252520%252525252811%2525252529 (1).json"
FLOWER_INFO_PATH = "flower_info%2525252520%25252525281%2525252529[1].json"

def load_model():
    return tf.keras.models.load_model(MODEL_PATH)

def load_class_indices():
    with open(CLASS_INDICES_PATH, "r") as f:
        return json.load(f)

def load_flower_info():
    with open(FLOWER_INFO_PATH, "r", encoding="utf-8") as f:
        return json.load(f)

model = load_model()
class_indices = load_class_indices()
flower_info = load_flower_info()
class_names = list(class_indices.keys())

def preprocess_image(pil_image):
    # Convert PIL image to numpy array and preprocess
    img_array = keras_image.img_to_array(pil_image.resize((224, 224)))
    img_array = np.expand_dims(img_array, axis=0)
    return preprocess_input(img_array)

def predict_image(pil_image):
    img_array = preprocess_image(pil_image)
    predictions = model.predict(img_array)
    predicted_class = class_names[np.argmax(predictions[0])]

    info = flower_info.get(predicted_class, "No additional information available.")

    return f"Identified as: {predicted_class}", info

def predict(pil_image):
    return predict_image(pil_image)

interface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),  # Receive image as a PIL object
    outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Flower Information")],
    title="Flower Identification App",
    description="Upload an image of a flower to identify it and get care information."
)

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