k087 commited on
Commit
ba1c4f3
·
verified ·
1 Parent(s): ddd701c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ from PIL import Image
5
+ from tensorflow.keras.models import load_model
6
+
7
+ model2=load_model("/content/best_mobilenetv2_model.keras")
8
+
9
+ def classify_image(img):
10
+
11
+ img = img.convert("RGB")
12
+ img = img.resize((224, 224))
13
+
14
+ img_tensor = tf.convert_to_tensor(np.array(img), dtype=tf.float32)
15
+
16
+ img_tensor = tf.expand_dims(img_tensor, axis=0)
17
+
18
+ prediction = model2.predict(img_tensor)
19
+ predicted_class_index = np.argmax(prediction)
20
+ predicted_class_name = class_names[predicted_class_index]
21
+ confidence = prediction[0][predicted_class_index]
22
+
23
+ return f"Predicted: {predicted_class_name} (Confidence: {confidence:.2%})"
24
+
25
+
26
+ iface = gr.Interface(
27
+ fn=classify_image,
28
+ inputs=gr.Image(type="pil", label="Upload Waste Image"),
29
+ outputs=gr.Textbox(label="Prediction"),
30
+ title="♻️ Waste Classifier",
31
+ description="Upload an image of cardboard, plastic, metal, paper, trash, or glass to classify it."
32
+ )
33
+
34
+
35
+ # Launch the interface
36
+ iface.launch() # Start the Gradio interface for user interaction