File size: 2,349 Bytes
1568f3d
 
 
 
 
 
43c67f8
1568f3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40a6ad5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e7ce88
40a6ad5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5808594
40a6ad5
1568f3d
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
import gradio as gr
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np

# Load your trained model
model = tf.keras.models.load_model("model.keras", custom_objects={"KerasLayer": hub.KerasLayer})

# Define your waste class labels
class_names = ['batteries', 'clothes', 'e-waste', 'glass', 'light blubs',
               'metal', 'organic', 'paper', 'plastic']
IMG_SIZE = 224

# Image preprocessing function
def preprocess_image(image):
    image = tf.convert_to_tensor(image, dtype=tf.float32)
    image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE)) / 255.0
    return tf.expand_dims(image, axis=0)

# Prediction function
def predict_waste(image):
    processed = preprocess_image(image)
    preds = model.predict(processed)
    label = class_names[np.argmax(preds)]
    confidence = float(np.max(preds))
    return f"{label} ({confidence:.2f})"


# Launch Gradio Interface
import gradio as gr

def predict_waste(image):
    # Your prediction logic here
    return "Example category: Plastic"

examples = [
    ["examples/download (1).jpg"],
    ["examples/download (13) (1).jpg"],
    ["examples/download (1) (1).jpg"],
    ["examples/download (14).jpg"]
]

with gr.Blocks(title="Waste Classification Model 🌱") as demo:
    gr.Markdown("""
    # ♻️ Waste Classification with AI  
    Upload or drag an image of a waste item below. Our model will classify it into one of 9 categories such as plastic, glass, paper, or metal.

    - ✅ Powered by MobileNetV2  
    - 🌍 Helping smart cities manage waste efficiently  
    - 📷 Works with real-world waste images  
    """)

    with gr.Row():
        with gr.Column(scale=2):
            image_input = gr.Image(type="numpy", label="📤 Upload a Waste Image")
            submit_btn = gr.Button("🔍 Classify Waste")
        with gr.Column(scale=1):
            prediction_output = gr.Textbox(label="🧠 Predicted Category", interactive=False)

    submit_btn.click(fn=predict_waste, inputs=image_input, outputs=prediction_output)

    gr.Examples(
        examples=examples,
        inputs=image_input,
        label="🖼 Example Waste Images",
        examples_per_page=4
    )

    gr.Markdown("""
    ---
    🔬 *Model accuracy may vary based on image clarity and background noise. For best results, upload clear, well-lit images.*  
    """)

demo.launch()