Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,49 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
-
|
4 |
-
import
|
5 |
-
|
6 |
-
# Load
|
7 |
-
model_name = "prithivMLmods/Recycling-Net-11"
|
8 |
-
processor = AutoImageProcessor.from_pretrained(model_name)
|
9 |
-
model = SiglipForImageClassification.from_pretrained(model_name)
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoImageProcessor, SiglipForImageClassification
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Load model and processor from HuggingFace
|
7 |
+
model_name = "prithivMLmods/Recycling-Net-11"
|
8 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
9 |
+
model = SiglipForImageClassification.from_pretrained(model_name)
|
10 |
+
|
11 |
+
# Define recyclable and non-recyclable categories
|
12 |
+
recyclable_labels = [
|
13 |
+
"cardboard", "glass", "metal", "paper", "plastic", "can", "carton"
|
14 |
+
]
|
15 |
+
non_recyclable_labels = [
|
16 |
+
"food waste", "trash", "garbage", "organic"
|
17 |
+
]
|
18 |
+
|
19 |
+
# Get model class label mapping
|
20 |
+
id2label = model.config.id2label
|
21 |
+
|
22 |
+
def classify_frame(frame):
|
23 |
+
if frame is None:
|
24 |
+
return "No frame detected"
|
25 |
+
|
26 |
+
img = Image.fromarray(frame)
|
27 |
+
inputs = processor(images=img, return_tensors="pt")
|
28 |
+
|
29 |
+
with torch.no_grad():
|
30 |
+
logits = model(**inputs).logits
|
31 |
+
probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
|
32 |
+
|
33 |
+
pred_idx = max(range(len(probs)), key=lambda i: probs[i])
|
34 |
+
pred_label = id2label[pred_idx].lower()
|
35 |
+
|
36 |
+
if any(word in pred_label for word in recyclable_labels):
|
37 |
+
return f"♻️ Recyclable ({probs[pred_idx]*100:.1f}%)"
|
38 |
+
else:
|
39 |
+
return f"🗑️ Non-Recyclable ({probs[pred_idx]*100:.1f}%)"
|
40 |
+
|
41 |
+
# Gradio Interface
|
42 |
+
gr.Interface(
|
43 |
+
fn=classify_frame,
|
44 |
+
inputs=gr.Image(source="webcam", streaming=True, label="Live Waste Feed"),
|
45 |
+
outputs=gr.Text(label="Prediction"),
|
46 |
+
live=True,
|
47 |
+
title="Live Waste Classification",
|
48 |
+
description="Classifies live webcam input into Recyclable or Non-Recyclable using 11-class model."
|
49 |
+
).launch()
|