Muthuraja18 commited on
Commit
c36c6f8
ยท
verified ยท
1 Parent(s): 95224b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -34
app.py CHANGED
@@ -101,6 +101,7 @@ def train_model():
101
  MaxPooling2D(2,2),
102
 
103
  Flatten(),
 
104
  Dense(256, activation='relu'),
105
  Dropout(0.5),
106
 
@@ -133,8 +134,7 @@ if not os.path.exists(MODEL_PATH):
133
  else:
134
  model = tf.keras.models.load_model(MODEL_PATH)
135
 
136
- # IMPORTANT:
137
- # Ensure this matches dataset folder order exactly
138
  classes = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
139
 
140
  # -----------------------------
@@ -151,6 +151,9 @@ uploaded_file = st.file_uploader(
151
 
152
  if uploaded_file is not None:
153
  try:
 
 
 
154
  image = Image.open(uploaded_file).convert("RGB")
155
 
156
  st.image(
@@ -170,44 +173,67 @@ if uploaded_file is not None:
170
  # PREDICT
171
  # -----------------------------
172
  with st.spinner("Analyzing waste type..."):
173
- prediction = model.predict(img_array, verbose=0)
174
-
175
- probabilities = prediction[0]
 
176
 
177
- predicted_index = np.argmax(probabilities)
178
- predicted_class = classes[predicted_index]
179
- confidence = probabilities[predicted_index] * 100
180
 
181
  # -----------------------------
182
- # DISPLAY RESULTS
183
  # -----------------------------
184
- st.subheader("๐Ÿ“Š Prediction Scores")
 
 
 
185
 
186
- for i, class_name in enumerate(classes):
187
- st.write(f"{class_name.upper()}: {probabilities[i]*100:.2f}%")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
 
189
- st.success(f"Predicted Type: {predicted_class.upper()}")
190
- st.info(f"Confidence: {confidence:.2f}%")
 
191
 
192
- # -----------------------------
193
- # SUSTAINABILITY TIPS
194
- # -----------------------------
195
- tips = {
196
- 'plastic': 'Recycle plastic properly to reduce pollution.',
197
- 'paper': 'Reuse or recycle paper to save trees.',
198
- 'metal': 'Metal can be recycled efficiently.',
199
- 'glass': 'Glass is reusable and recyclable.',
200
- 'trash': 'Dispose responsibly to reduce environmental damage.',
201
- 'cardboard': 'Recycle cardboard to reduce waste.'
202
- }
203
-
204
- st.subheader("๐ŸŒฑ Sustainability Suggestion")
205
- st.write(
206
- tips.get(
207
- predicted_class,
208
- "Dispose responsibly."
 
 
209
  )
210
- )
211
 
212
  except UnidentifiedImageError:
213
  st.error(
@@ -215,10 +241,14 @@ if uploaded_file is not None:
215
  )
216
 
217
  except Exception as e:
218
- st.error(f"Error processing image: {str(e)}")
 
 
219
 
220
  # -----------------------------
221
  # FOOTER
222
  # -----------------------------
223
  st.markdown("---")
224
- st.caption("Built using TensorFlow + Streamlit for Sustainable AI")
 
 
 
101
  MaxPooling2D(2,2),
102
 
103
  Flatten(),
104
+
105
  Dense(256, activation='relu'),
106
  Dropout(0.5),
107
 
 
134
  else:
135
  model = tf.keras.models.load_model(MODEL_PATH)
136
 
137
+ # Ensure exact dataset folder order
 
138
  classes = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
139
 
140
  # -----------------------------
 
151
 
152
  if uploaded_file is not None:
153
  try:
154
+ # -----------------------------
155
+ # LOAD IMAGE
156
+ # -----------------------------
157
  image = Image.open(uploaded_file).convert("RGB")
158
 
159
  st.image(
 
173
  # PREDICT
174
  # -----------------------------
175
  with st.spinner("Analyzing waste type..."):
176
+ prediction = model.predict(
177
+ img_array,
178
+ verbose=0
179
+ )
180
 
181
+ probabilities = prediction.flatten()
 
 
182
 
183
  # -----------------------------
184
+ # VALIDATE OUTPUT
185
  # -----------------------------
186
+ if len(probabilities) != len(classes):
187
+ st.error(
188
+ f"Model output mismatch: Expected {len(classes)} classes but got {len(probabilities)}."
189
+ )
190
 
191
+ else:
192
+ predicted_index = np.argmax(probabilities)
193
+ predicted_class = classes[predicted_index]
194
+ confidence = probabilities[predicted_index] * 100
195
+
196
+ # -----------------------------
197
+ # DISPLAY SCORES
198
+ # -----------------------------
199
+ st.subheader("๐Ÿ“Š Prediction Scores")
200
+
201
+ for i, class_name in enumerate(classes):
202
+ st.write(
203
+ f"{class_name.upper()}: {probabilities[i] * 100:.2f}%"
204
+ )
205
+
206
+ # -----------------------------
207
+ # MAIN RESULT
208
+ # -----------------------------
209
+ st.success(
210
+ f"Predicted Type: {predicted_class.upper()}"
211
+ )
212
 
213
+ st.info(
214
+ f"Confidence: {confidence:.2f}%"
215
+ )
216
 
217
+ # -----------------------------
218
+ # SUSTAINABILITY TIPS
219
+ # -----------------------------
220
+ tips = {
221
+ 'plastic': 'Recycle plastic properly to reduce pollution.',
222
+ 'paper': 'Reuse or recycle paper to save trees.',
223
+ 'metal': 'Metal can be recycled efficiently.',
224
+ 'glass': 'Glass is reusable and recyclable.',
225
+ 'trash': 'Dispose responsibly to reduce environmental damage.',
226
+ 'cardboard': 'Recycle cardboard to reduce waste.'
227
+ }
228
+
229
+ st.subheader("๐ŸŒฑ Sustainability Suggestion")
230
+
231
+ st.write(
232
+ tips.get(
233
+ predicted_class,
234
+ "Dispose responsibly."
235
+ )
236
  )
 
237
 
238
  except UnidentifiedImageError:
239
  st.error(
 
241
  )
242
 
243
  except Exception as e:
244
+ st.error(
245
+ f"Error processing image: {str(e)}"
246
+ )
247
 
248
  # -----------------------------
249
  # FOOTER
250
  # -----------------------------
251
  st.markdown("---")
252
+ st.caption(
253
+ "Built using TensorFlow + Streamlit for Sustainable AI"
254
+ )