foreversheikh commited on
Commit
63d98ed
·
verified ·
1 Parent(s): 2d6450a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -80,13 +80,15 @@ import numpy as np
80
  import tensorflow as tf
81
  from tensorflow.keras.models import load_model
82
  from PIL import Image
83
- import cv2
84
  import os
85
 
86
- # Load the model
87
- model = tf.keras.models.load_model("model_n.keras")
88
 
 
 
89
 
 
90
  class_names = [
91
  'Bush Clock Vine', 'Common Lanthana', 'Datura', 'Hibiscus', 'Jatropha', 'Marigold',
92
  'Nityakalyani', 'Rose', 'Yellow_Daisy', 'adathoda', 'banana', 'champaka', 'chitrak',
@@ -95,23 +97,21 @@ class_names = [
95
  'thumba', 'touch me not', 'tridax procumbens', 'wild_potato_vine'
96
  ]
97
 
 
 
98
 
99
- on_huggingface = os.environ.get("SPACE_ID") is not None
100
-
101
-
102
- st.title(" Flower Identifier")
103
-
104
-
105
  if on_huggingface:
106
- st.warning("Real-time camera is not supported on Hugging Face Spaces. Please upload an image.")
107
  mode = "Upload Image"
108
  else:
109
  mode = st.radio("Choose input method:", ["Upload Image", "Real-Time Camera"])
110
 
 
111
  if mode == "Upload Image":
112
  st.markdown("### Upload an image of a flower")
113
  img = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
114
-
115
  if img is not None:
116
  st.image(img, caption="Uploaded Image", use_column_width=True)
117
 
@@ -126,10 +126,12 @@ if mode == "Upload Image":
126
  confidence = round(100 * np.max(prediction[0]), 2)
127
  flower_name = class_names[predicted_class]
128
 
129
- st.success(f" Predicted Flower: **{flower_name}**")
130
  st.info(f"🔍 Confidence: **{confidence}%**")
131
 
 
132
  elif mode == "Real-Time Camera":
 
133
  st.markdown("### Real-Time Flower Recognition")
134
  run = st.checkbox('Start Camera')
135
  FRAME_WINDOW = st.image([])
@@ -141,7 +143,7 @@ elif mode == "Real-Time Camera":
141
  while run:
142
  ret, frame = cap.read()
143
  if not ret:
144
- st.warning("Failed to access camera.")
145
  break
146
 
147
  img_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
@@ -153,7 +155,7 @@ elif mode == "Real-Time Camera":
153
  confidence = round(100 * np.max(predictions[0]), 2)
154
  flower_name = class_names[predicted_class]
155
 
156
- cv2.putText(frame, f"{flower_name} ({confidence}%)", (10, 30),
157
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
158
 
159
  FRAME_WINDOW.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
 
80
  import tensorflow as tf
81
  from tensorflow.keras.models import load_model
82
  from PIL import Image
 
83
  import os
84
 
85
+ # Check if running on Hugging Face Spaces
86
+ on_huggingface = os.environ.get("SPACE_ID") is not None
87
 
88
+ # Load model
89
+ model = tf.keras.models.load_model("model_n.keras")
90
 
91
+ # Class names
92
  class_names = [
93
  'Bush Clock Vine', 'Common Lanthana', 'Datura', 'Hibiscus', 'Jatropha', 'Marigold',
94
  'Nityakalyani', 'Rose', 'Yellow_Daisy', 'adathoda', 'banana', 'champaka', 'chitrak',
 
97
  'thumba', 'touch me not', 'tridax procumbens', 'wild_potato_vine'
98
  ]
99
 
100
+ # Title
101
+ st.title("🌼 Flower Identifier")
102
 
103
+ # Choose mode
 
 
 
 
 
104
  if on_huggingface:
105
+ st.warning("Real-time camera is not supported on Hugging Face. Please upload an image.")
106
  mode = "Upload Image"
107
  else:
108
  mode = st.radio("Choose input method:", ["Upload Image", "Real-Time Camera"])
109
 
110
+ # Upload image mode
111
  if mode == "Upload Image":
112
  st.markdown("### Upload an image of a flower")
113
  img = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
114
+
115
  if img is not None:
116
  st.image(img, caption="Uploaded Image", use_column_width=True)
117
 
 
126
  confidence = round(100 * np.max(prediction[0]), 2)
127
  flower_name = class_names[predicted_class]
128
 
129
+ st.success(f"🌸 Predicted Flower: **{flower_name}**")
130
  st.info(f"🔍 Confidence: **{confidence}%**")
131
 
132
+ # Real-time camera mode (local only)
133
  elif mode == "Real-Time Camera":
134
+ import cv2 # <- import only if needed
135
  st.markdown("### Real-Time Flower Recognition")
136
  run = st.checkbox('Start Camera')
137
  FRAME_WINDOW = st.image([])
 
143
  while run:
144
  ret, frame = cap.read()
145
  if not ret:
146
+ st.warning("⚠️ Failed to access camera.")
147
  break
148
 
149
  img_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 
155
  confidence = round(100 * np.max(predictions[0]), 2)
156
  flower_name = class_names[predicted_class]
157
 
158
+ cv2.putText(frame, f"{flower_name} ({confidence}%)", (10, 30),
159
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
160
 
161
  FRAME_WINDOW.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))