File size: 1,175 Bytes
34c44c7
 
 
a1a3309
34c44c7
8b85226
18b1ad9
 
34c44c7
e7e0f6d
591555f
8b85226
34c44c7
8b85226
 
 
e7e0f6d
8b85226
a1a3309
8b85226
 
 
e7e0f6d
 
 
 
8b85226
 
 
e7e0f6d
 
 
 
34c44c7
 
a1a3309
8b85226
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
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image

# Load model
model = tf.keras.models.load_model("model.keras")


# Classes in training order
class_names = ['batteries', 'clothes', 'e-waste', 'glass', 'light blubs', 'metal', 'organic', 'paper', 'plastic']
IMG_SIZE = (224, 224)

def preprocess(image):
    image = image.resize(IMG_SIZE)
    image_array = tf.keras.preprocessing.image.img_to_array(image)
    image_array = image_array / 255.0  # Normalize
    return np.expand_dims(image_array, axis=0)

def predict(image):
    input_tensor = preprocess(image)
    predictions = model.predict(input_tensor)[0]
    top_idx = np.argmax(predictions)
    top_class = class_names[top_idx]
    confidence = predictions[top_idx] * 100
    return f"Predicted: {top_class} ({confidence:.2f}%)"

demo = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", label="Upload an image of a recyclable item"),
    outputs=gr.Textbox(label="Prediction"),
    title="♻️ Waste Image Classifier",
    description="Classify recyclable waste images into 9 categories using a MobileNetV2-based model."
)

if __name__ == "__main__":
    demo.launch()