File size: 9,721 Bytes
87593f0
 
9659bbd
1fff88a
 
87593f0
4be38ed
87593f0
45a7f47
87593f0
 
 
 
 
431c58d
87593f0
1fff88a
45a7f47
098d660
9659bbd
ce1ed1e
 
9659bbd
 
 
 
 
 
 
 
 
 
17250ae
 
 
 
 
 
 
 
 
 
098d660
ce1ed1e
098d660
afde554
ce1ed1e
afde554
 
87593f0
08fe3d7
0be254e
08fe3d7
0be254e
1fff88a
 
9659bbd
08fe3d7
0be254e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fff88a
 
 
 
45a7f47
 
 
 
1fff88a
 
 
 
 
 
17250ae
1fff88a
2d7e742
 
1fff88a
 
 
 
 
 
0be254e
1fff88a
2d7e742
 
1fff88a
 
45a7f47
 
 
 
 
 
 
 
 
 
 
 
 
 
1fff88a
2d7e742
1fff88a
2d7e742
 
1fff88a
2d7e742
 
1fff88a
2d7e742
 
1fff88a
 
 
45a7f47
1fff88a
 
45a7f47
 
 
1fff88a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45a7f47
 
1fff88a
2d7e742
1fff88a
08fe3d7
1fff88a
08fe3d7
1fff88a
08fe3d7
1fff88a
 
 
 
2d7e742
1fff88a
 
 
 
 
 
 
 
2d7e742
1fff88a
 
 
 
 
2d7e742
1fff88a
 
 
 
08fe3d7
 
9659bbd
4be38ed
87593f0
2d7e742
87593f0
9659bbd
 
 
afde554
9659bbd
87593f0
9659bbd
1fff88a
87593f0
9659bbd
87593f0
9659bbd
87593f0
 
 
2d7e742
87593f0
9659bbd
 
afde554
9659bbd
52dbe20
9659bbd
52dbe20
45a7f47
 
 
 
 
 
 
 
9659bbd
 
 
 
52dbe20
87593f0
 
9659bbd
87593f0
 
2d7e742
1fff88a
 
2d7e742
1fff88a
 
 
 
 
0be254e
 
 
 
 
 
 
1fff88a
0be254e
 
2d7e742
0be254e
 
1fff88a
 
 
87593f0
9659bbd
0be254e
9659bbd
87593f0
 
08fe3d7
87593f0
 
9659bbd
2d7e742
9659bbd
87593f0
098d660
9659bbd
098d660
 
 
9659bbd
098d660
 
 
9659bbd
 
52dbe20
17250ae
52dbe20
098d660
ce1ed1e
9659bbd
2d7e742
e544322
17250ae
9659bbd
 
 
52dbe20
17250ae
52dbe20
9659bbd
098d660
45a7f47
17250ae
 
 
 
 
 
 
 
098d660
2d7e742
098d660
 
9659bbd
 
 
 
 
 
 
 
0be254e
 
 
17250ae
 
 
 
 
 
 
9659bbd
ce1ed1e
87593f0
 
 
 
ce1ed1e
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import streamlit as st
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers, models
import numpy as np
from PIL import Image, UnidentifiedImageError
import os
from sklearn.utils.class_weight import compute_class_weight

# -----------------------------
# CONFIGURATION
# -----------------------------
MODEL_PATH = "waste_classifier.h5"
DATASET_DIR = "dataset-resized/dataset-resized"
IMG_SIZE = (128, 128)
BATCH_SIZE = 32
EPOCHS = 20  # Increased for better accuracy

# Fixed class labels
CLASSES = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']

# Sustainability tips
TIPS = {
    'plastic': 'Recycle plastic properly to reduce pollution.',
    'paper': 'Reuse or recycle paper to save trees.',
    'metal': 'Metal can be recycled efficiently.',
    'glass': 'Glass is reusable and recyclable.',
    'trash': 'Dispose responsibly to reduce environmental damage.',
    'cardboard': 'Recycle cardboard to reduce waste.'
}

# AI Eco Insights
AI_MESSAGES = {
    'plastic': "πŸ€– AI Insight: This appears to be plastic waste. Recycling plastic helps reduce pollution and protects oceans.",
    'paper': "πŸ€– AI Insight: Paper waste detected. Recycling paper saves trees and reduces landfill burden.",
    'metal': "πŸ€– AI Insight: Metal detected. Metal recycling conserves raw materials and energy.",
    'glass': "πŸ€– AI Insight: Glass waste identified. Glass is highly recyclable and reusable.",
    'trash': "πŸ€– AI Insight: General waste detected. Proper disposal minimizes environmental damage.",
    'cardboard': "πŸ€– AI Insight: Cardboard detected. Recycling cardboard supports sustainable packaging."
}

# -----------------------------
# PAGE SETTINGS
# -----------------------------
st.set_page_config(
    page_title="AI Smart Waste Classification",
    layout="centered"
)

# -----------------------------
# VALIDATE DATASET STRUCTURE
# -----------------------------
def validate_dataset():
    if not os.path.exists(DATASET_DIR):
        st.error(f"❌ Dataset folder '{DATASET_DIR}' not found.")
        st.stop()

    found_folders = sorted([
        folder for folder in os.listdir(DATASET_DIR)
        if os.path.isdir(os.path.join(DATASET_DIR, folder))
    ])

    missing = [cls for cls in CLASSES if cls not in found_folders]

    if missing:
        st.error("❌ Dataset structure is incorrect.")
        st.write("Expected folders:")
        for cls in CLASSES:
            st.write(f"- {cls}")

        st.write("Missing folders:")
        for m in missing:
            st.write(f"- {m}")

        st.stop()

    return found_folders


# -----------------------------
# TRAIN MODEL
# -----------------------------
def train_and_save_model():
    validate_dataset()

    st.info("βš™οΈ Model not found. Training a new model... This may take several minutes.")

    datagen = ImageDataGenerator(
        rescale=1./255,
        validation_split=0.2,
        rotation_range=15,
        zoom_range=0.1,
        horizontal_flip=True
    )

    train_data = datagen.flow_from_directory(
        DATASET_DIR,
        target_size=IMG_SIZE,
        batch_size=BATCH_SIZE,
        classes=CLASSES,
        class_mode='categorical',
        subset='training',
        shuffle=True
    )

    val_data = datagen.flow_from_directory(
        DATASET_DIR,
        target_size=IMG_SIZE,
        batch_size=BATCH_SIZE,
        classes=CLASSES,
        class_mode='categorical',
        subset='validation',
        shuffle=True
    )

    # -----------------------------
    # CLASS WEIGHTS FOR BALANCED TRAINING
    # -----------------------------
    class_weights = compute_class_weight(
        class_weight='balanced',
        classes=np.unique(train_data.classes),
        y=train_data.classes
    )

    class_weights = dict(enumerate(class_weights))

    # -----------------------------
    # CNN MODEL
    # -----------------------------
    model = models.Sequential([
        layers.Input(shape=(128, 128, 3)),

        layers.Conv2D(32, (3, 3), activation='relu'),
        layers.MaxPooling2D(2, 2),

        layers.Conv2D(64, (3, 3), activation='relu'),
        layers.MaxPooling2D(2, 2),

        layers.Conv2D(128, (3, 3), activation='relu'),
        layers.MaxPooling2D(2, 2),

        layers.Flatten(),

        layers.Dense(256, activation='relu'),
        layers.Dropout(0.5),

        layers.Dense(128, activation='relu'),
        layers.Dropout(0.3),

        layers.Dense(len(CLASSES), activation='softmax')
    ])

    model.compile(
        optimizer='adam',
        loss='categorical_crossentropy',
        metrics=['accuracy']
    )

    progress_bar = st.progress(0)

    for epoch in range(EPOCHS):
        model.fit(
            train_data,
            validation_data=val_data,
            epochs=1,
            verbose=1,
            class_weight=class_weights
        )

        progress_bar.progress((epoch + 1) / EPOCHS)

    model.save(MODEL_PATH)

    st.success("βœ… Model trained and saved successfully!")

    return model


# -----------------------------
# LOAD MODEL
# -----------------------------
@st.cache_resource
def load_ai_model():
    if os.path.exists(MODEL_PATH):
        try:
            model = load_model(MODEL_PATH)

            if model.output_shape[-1] != len(CLASSES):
                st.warning("⚠️ Model mismatch. Retraining...")
                return train_and_save_model()

            return model

        except Exception:
            st.warning("⚠️ Corrupted model. Retraining...")
            return train_and_save_model()

    else:
        return train_and_save_model()


model = load_ai_model()

# -----------------------------
# PREPROCESS IMAGE
# -----------------------------
def preprocess_image(image):
    image = image.convert("RGB")
    image = image.resize(IMG_SIZE)

    img_array = np.array(image, dtype=np.float32) / 255.0

    if img_array.shape != (128, 128, 3):
        raise ValueError("Image preprocessing failed.")

    img_array = np.expand_dims(img_array, axis=0)

    return img_array


# -----------------------------
# PREDICT
# -----------------------------
def predict_waste(image):
    processed_img = preprocess_image(image)

    prediction = model.predict(processed_img, verbose=0)

    probabilities = prediction[0]

    trash_index = CLASSES.index("trash")

    # Trash threshold boost
    if probabilities[trash_index] > 0.40:
        predicted_index = trash_index
    else:
        predicted_index = np.argmax(probabilities)

    predicted_class = CLASSES[predicted_index]
    confidence = probabilities[predicted_index] * 100

    return predicted_class, confidence, probabilities


# -----------------------------
# UI HEADER
# -----------------------------
st.title("♻️ AI Smart Waste Classification")
st.write("Upload an image to classify waste and support sustainable recycling.")

# -----------------------------
# SIDEBAR
# -----------------------------
with st.sidebar:
    st.header("πŸ“‚ Dataset Status")

    if os.path.exists(DATASET_DIR):
        folders = sorted([
            folder for folder in os.listdir(DATASET_DIR)
            if os.path.isdir(os.path.join(DATASET_DIR, folder))
        ])

        if folders:
            st.success("Dataset Found")

            for folder in folders:
                st.write(f"βœ”οΈ {folder}")

        else:
            st.error("No class folders found")

    else:
        st.error("Dataset Missing")

# -----------------------------
# FILE UPLOADER
# -----------------------------
uploaded_file = st.file_uploader(
    "Upload Waste Image",
    type=["jpg", "jpeg", "png"]
)

# -----------------------------
# ANALYSIS
# -----------------------------
if uploaded_file is not None:
    try:
        image = Image.open(uploaded_file)

        st.image(
            image,
            caption=f"Uploaded Image: {uploaded_file.name}",
            use_container_width=True
        )

        with st.spinner("πŸ” Analyzing waste type..."):
            predicted_class, confidence, probabilities = predict_waste(image)

        # Prediction Scores
        st.subheader("πŸ“Š Prediction Scores")

        for i, class_name in enumerate(CLASSES):
            st.progress(float(probabilities[i]))
            st.write(f"{class_name.upper()}: {probabilities[i] * 100:.2f}%")

        # Main Output
        st.success(f"βœ… Predicted Type: {predicted_class.upper()}")
        st.info(f"🎯 Confidence: {confidence:.2f}%")
        st.write(f"πŸ“ Uploaded File: {uploaded_file.name}")

        # Sustainability Tip
        st.subheader("🌱 Sustainability Suggestion")
        st.write(TIPS.get(predicted_class, "Dispose responsibly."))

        # AI Analysis
        st.subheader("πŸ€– AI Environmental Analysis")
        st.success(
            AI_MESSAGES.get(
                predicted_class,
                "AI recommends responsible disposal."
            )
        )

    except UnidentifiedImageError:
        st.error("❌ Invalid image file. Upload JPG, JPEG, or PNG.")

    except Exception as e:
        st.error(f"❌ Error processing image: {str(e)}")

# -----------------------------
# SAMPLE GUIDE
# -----------------------------
st.markdown("---")
st.subheader("πŸ–ΌοΈ Recommended Test Images")
st.write("""
Your dataset folder should look like:

dataset-resized/
└── dataset-resized/
    β”œβ”€β”€ cardboard/
    β”œβ”€β”€ glass/
    β”œβ”€β”€ metal/
    β”œβ”€β”€ paper/
    β”œβ”€β”€ plastic/
    └── trash/
""")

# -----------------------------
# FOOTER
# -----------------------------
st.markdown("---")
st.caption("Built using TensorFlow + Streamlit for Sustainable AI")