File size: 1,109 Bytes
34c44c7
 
 
a1a3309
34c44c7
8b85226
a1a3309
34c44c7
591555f
 
34c44c7
591555f
8b85226
34c44c7
8b85226
 
 
591555f
8b85226
a1a3309
8b85226
 
 
 
 
 
 
 
 
 
 
 
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
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.h5")

# Correct class names in training order
class_names = ['batteries', 'clothes', 'e-waste', 'glass', 'light blubs', 'metal', 'organic', 'paper', 'plastic']

# Match training input size
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 if this was done in training
    return np.expand_dims(image_array, axis=0)

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

demo = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", label="Upload an image of a recyclable item to classify it."),
    outputs=gr.Textbox(label="Output"),
    title="Waste Image Classifier"
)

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