File size: 4,655 Bytes
2d6450a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae378d9
 
 
 
c5b5003
 
 
 
 
ae378d9
63d98ed
2d6450a
ae378d9
c5b5003
 
 
 
 
 
 
 
ae378d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# import streamlit as st
# import numpy as np
# import tensorflow as tf
# from tensorflow.keras.models import load_model
# from PIL import Image
# import cv2

# # Load the model
# model = tf.keras.models.load_model("model_n.keras")

# # Define class names
# class_names = [
#     'Bush Clock Vine', 'Common Lanthana', 'Datura', 'Hibiscus', 'Jatropha', 'Marigold',
#     'Nityakalyani', 'Rose', 'Yellow_Daisy', 'adathoda', 'banana', 'champaka', 'chitrak',
#     'crown flower', "four o'clock flower", 'honeysuckle', 'indian mallow', 'malabar melastome',
#     'nagapoovu', 'pinwheel flower', 'shankupushpam', 'spider lily', 'sunflower', 'thechi',
#     'thumba', 'touch me not', 'tridax procumbens', 'wild_potato_vine'
# ]

# # Title
# st.title("Flower Identifier")

# # Choose mode
# mode = st.radio("Choose input method:", ["Upload Image", "Real-Time Camera"])

# if mode == "Upload Image":
#     st.markdown("### Upload an image of a flower")
#     img = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
    
#     if img is not None:
#         st.image(img, caption="Uploaded Image", use_column_width=True)

#         image = Image.open(img).convert("RGB")
#         image = tf.keras.preprocessing.image.img_to_array(image)
#         image = tf.cast(image, tf.float32)
#         image = tf.expand_dims(image, 0)

#         if st.button("Identify Flower"):
#             prediction = model.predict(image)
#             predicted_class = np.argmax(prediction[0])
#             confidence = round(100 * np.max(prediction[0]), 2)
#             flower_name = class_names[predicted_class]

#             st.success(f"Predicted Flower: **{flower_name}**")
#             st.info(f"Confidence: **{confidence}%**")

# elif mode == "Real-Time Camera":
#     st.markdown("### Real-Time Flower Recognition")
#     run = st.checkbox('Start Camera')
#     FRAME_WINDOW = st.image([])

#     cap = None
#     if run:
#         cap = cv2.VideoCapture(0)
#         while run:
#             ret, frame = cap.read()
#             if not ret:
#                 st.warning("Failed to access camera.")
#                 break

#             img_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
#             img_array = tf.keras.preprocessing.image.img_to_array(img_rgb)
#             img_array = tf.expand_dims(tf.cast(img_array, tf.float32), 0)

#             predictions = model.predict(img_array)
#             predicted_class = np.argmax(predictions[0])
#             confidence = round(100 * np.max(predictions[0]), 2)
#             flower_name = class_names[predicted_class]

#             # Annotate frame
#             cv2.putText(frame, f"{flower_name} ({confidence}%)", (10, 30), 
#                         cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)

#             FRAME_WINDOW.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

#         if cap:
#             cap.release()



import gradio as gr
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
from PIL import Image

# Load the model
model = tf.keras.models.load_model("model_n.keras")

# Define class names
class_names = [
    'Bush Clock Vine', 'Common Lanthana', 'Datura', 'Hibiscus', 'Jatropha', 'Marigold',
    'Nityakalyani', 'Rose', 'Yellow_Daisy', 'adathoda', 'banana', 'champaka', 'chitrak',
    'crown flower', "four o'clock flower", 'honeysuckle', 'indian mallow', 'malabar melastome',
    'nagapoovu', 'pinwheel flower', 'shankupushpam', 'spider lily', 'sunflower', 'thechi',
    'thumba', 'touch me not', 'tridax procumbens', 'wild_potato_vine'
]

# Prediction function
def predict_flower(img):
    image = img.convert("RGB")
    image = tf.keras.preprocessing.image.img_to_array(image)
    image = tf.cast(image, tf.float32)
    image = tf.expand_dims(image, 0)

    prediction = model.predict(image)
    predicted_class = np.argmax(prediction[0])
    confidence = round(100 * np.max(prediction[0]), 2)
    flower_name = class_names[predicted_class]

    return f"🌼 Predicted Flower: {flower_name} ({confidence}%)"

# Gradio interface
title = "🌸 Flower Identifier using Deep Learning"
description = "Upload an image or use your camera to identify a flower from 28 known classes."

iface = gr.Interface(
    fn=predict_flower,
    inputs=[
        gr.Image(type="pil", label="Upload or Capture Flower Image", source="upload", tool="editor")
    ],
    outputs="text",
    title=title,
    description=description,
    live=False,
    examples=None,
)

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