Update app.py
#20
by Muthuraja18 - opened
app.py
CHANGED
|
@@ -29,6 +29,16 @@ TIPS = {
|
|
| 29 |
'cardboard': 'Recycle cardboard to reduce waste.'
|
| 30 |
}
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# -----------------------------
|
| 33 |
# PAGE SETTINGS
|
| 34 |
# -----------------------------
|
|
@@ -41,9 +51,6 @@ st.set_page_config(
|
|
| 41 |
# VALIDATE DATASET STRUCTURE
|
| 42 |
# -----------------------------
|
| 43 |
def validate_dataset():
|
| 44 |
-
"""
|
| 45 |
-
Ensure dataset folder contains 6 required class subfolders
|
| 46 |
-
"""
|
| 47 |
if not os.path.exists(DATASET_DIR):
|
| 48 |
st.error(f"β Dataset folder '{DATASET_DIR}' not found.")
|
| 49 |
st.stop()
|
|
@@ -87,7 +94,7 @@ def train_and_save_model():
|
|
| 87 |
DATASET_DIR,
|
| 88 |
target_size=IMG_SIZE,
|
| 89 |
batch_size=BATCH_SIZE,
|
| 90 |
-
classes=CLASSES,
|
| 91 |
class_mode='categorical',
|
| 92 |
subset='training',
|
| 93 |
shuffle=True
|
|
@@ -262,19 +269,31 @@ if uploaded_file is not None:
|
|
| 262 |
with st.spinner("π Analyzing waste type..."):
|
| 263 |
predicted_class, confidence, probabilities = predict_waste(image)
|
| 264 |
|
|
|
|
| 265 |
st.subheader("π Prediction Scores")
|
| 266 |
|
| 267 |
for i, class_name in enumerate(CLASSES):
|
| 268 |
st.progress(float(probabilities[i]))
|
| 269 |
st.write(f"{class_name.upper()}: {probabilities[i] * 100:.2f}%")
|
| 270 |
|
|
|
|
| 271 |
st.success(f"β
Predicted Type: {predicted_class.upper()}")
|
| 272 |
st.info(f"π― Confidence: {confidence:.2f}%")
|
| 273 |
st.write(f"π Uploaded File: {uploaded_file.name}")
|
| 274 |
|
|
|
|
| 275 |
st.subheader("π± Sustainability Suggestion")
|
| 276 |
st.write(TIPS.get(predicted_class, "Dispose responsibly."))
|
| 277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
except UnidentifiedImageError:
|
| 279 |
st.error("β Invalid image file. Upload JPG, JPEG, or PNG.")
|
| 280 |
|
|
@@ -290,12 +309,13 @@ st.write("""
|
|
| 290 |
Your dataset folder should look like:
|
| 291 |
|
| 292 |
dataset-resized/
|
| 293 |
-
|
| 294 |
-
βββ
|
| 295 |
-
βββ
|
| 296 |
-
βββ
|
| 297 |
-
βββ
|
| 298 |
-
|
|
|
|
| 299 |
""")
|
| 300 |
|
| 301 |
# -----------------------------
|
|
|
|
| 29 |
'cardboard': 'Recycle cardboard to reduce waste.'
|
| 30 |
}
|
| 31 |
|
| 32 |
+
# AI Eco Insights
|
| 33 |
+
AI_MESSAGES = {
|
| 34 |
+
'plastic': "π€ AI Insight: This appears to be plastic waste. Recycling plastic helps reduce pollution and protects oceans.",
|
| 35 |
+
'paper': "π€ AI Insight: Paper waste detected. Recycling paper saves trees and reduces landfill burden.",
|
| 36 |
+
'metal': "π€ AI Insight: Metal detected. Metal recycling conserves raw materials and energy.",
|
| 37 |
+
'glass': "π€ AI Insight: Glass waste identified. Glass is highly recyclable and reusable.",
|
| 38 |
+
'trash': "π€ AI Insight: General waste detected. Proper disposal minimizes environmental damage.",
|
| 39 |
+
'cardboard': "π€ AI Insight: Cardboard detected. Recycling cardboard supports sustainable packaging."
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
# -----------------------------
|
| 43 |
# PAGE SETTINGS
|
| 44 |
# -----------------------------
|
|
|
|
| 51 |
# VALIDATE DATASET STRUCTURE
|
| 52 |
# -----------------------------
|
| 53 |
def validate_dataset():
|
|
|
|
|
|
|
|
|
|
| 54 |
if not os.path.exists(DATASET_DIR):
|
| 55 |
st.error(f"β Dataset folder '{DATASET_DIR}' not found.")
|
| 56 |
st.stop()
|
|
|
|
| 94 |
DATASET_DIR,
|
| 95 |
target_size=IMG_SIZE,
|
| 96 |
batch_size=BATCH_SIZE,
|
| 97 |
+
classes=CLASSES,
|
| 98 |
class_mode='categorical',
|
| 99 |
subset='training',
|
| 100 |
shuffle=True
|
|
|
|
| 269 |
with st.spinner("π Analyzing waste type..."):
|
| 270 |
predicted_class, confidence, probabilities = predict_waste(image)
|
| 271 |
|
| 272 |
+
# Prediction Scores
|
| 273 |
st.subheader("π Prediction Scores")
|
| 274 |
|
| 275 |
for i, class_name in enumerate(CLASSES):
|
| 276 |
st.progress(float(probabilities[i]))
|
| 277 |
st.write(f"{class_name.upper()}: {probabilities[i] * 100:.2f}%")
|
| 278 |
|
| 279 |
+
# Main Output
|
| 280 |
st.success(f"β
Predicted Type: {predicted_class.upper()}")
|
| 281 |
st.info(f"π― Confidence: {confidence:.2f}%")
|
| 282 |
st.write(f"π Uploaded File: {uploaded_file.name}")
|
| 283 |
|
| 284 |
+
# Sustainability Tip
|
| 285 |
st.subheader("π± Sustainability Suggestion")
|
| 286 |
st.write(TIPS.get(predicted_class, "Dispose responsibly."))
|
| 287 |
|
| 288 |
+
# AI Environmental Analysis
|
| 289 |
+
st.subheader("π€ AI Environmental Analysis")
|
| 290 |
+
st.success(
|
| 291 |
+
AI_MESSAGES.get(
|
| 292 |
+
predicted_class,
|
| 293 |
+
"AI recommends responsible disposal."
|
| 294 |
+
)
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
except UnidentifiedImageError:
|
| 298 |
st.error("β Invalid image file. Upload JPG, JPEG, or PNG.")
|
| 299 |
|
|
|
|
| 309 |
Your dataset folder should look like:
|
| 310 |
|
| 311 |
dataset-resized/
|
| 312 |
+
βββ dataset-resized/
|
| 313 |
+
βββ cardboard/
|
| 314 |
+
βββ glass/
|
| 315 |
+
βββ metal/
|
| 316 |
+
βββ paper/
|
| 317 |
+
βββ plastic/
|
| 318 |
+
βββ trash/
|
| 319 |
""")
|
| 320 |
|
| 321 |
# -----------------------------
|