File size: 6,114 Bytes
b8acd28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f5425b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20a0f19
 
 
 
 
f884788
20a0f19
 
e6439c1
 
 
 
20a0f19
b8acd28
 
 
 
 
20a0f19
 
 
 
 
 
 
 
 
 
 
 
 
 
f884788
e6439c1
f884788
20a0f19
 
 
f884788
b8acd28
 
 
 
e6439c1
b8acd28
84f94fd
b8acd28
 
20a0f19
f884788
 
20a0f19
9f5425b
20a0f19
 
e6439c1
20a0f19
 
9f5425b
20a0f19
 
e6439c1
9f5425b
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# 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 = "model.keras"
# CLASS_INDICES_PATH = "class_indices.json"
# FLOWER_INFO_PATH = "flower_info.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()
# 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 = "model.keras"
# CLASS_INDICES_PATH = "class_indices.json"
# FLOWER_INFO_PATH = "flower_info.json"

# def load_model():
#     try:
#         return tf.keras.models.load_model(MODEL_PATH)
#     except Exception as e:
#         print(f"Error loading model: {e}")
#         return None

# 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):
#     try:
#         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
#     except Exception as e:
#         return "Error in prediction", str(e)

# 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.",
#     theme="compact"  # Optional: Use a compact theme to reduce UI clutter
# )

# if __name__ == "__main__":
#     interface.launch(share=True, api=True)  # Add api=True if you need API access

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 = "model.keras"
CLASS_INDICES_PATH = "class_indices.json"
FLOWER_INFO_PATH = "flower_info.json"

def load_model():
    try:
        return tf.keras.models.load_model(MODEL_PATH)
    except Exception as e:
        print(f"Error loading model: {e}")
        return None

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):
    try:
        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
    except Exception as e:
        return "Error in prediction", str(e)

def predict(pil_image):
    return predict_image(pil_image)

# Launch the Gradio interface
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(share=True)  # No api=True here