Deepakbusa commited on
Commit
9f7c069
·
verified ·
1 Parent(s): 059bdc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -43
app.py CHANGED
@@ -1,43 +1,49 @@
1
- import gradio as gr
2
- from transformers import AutoImageProcessor, SiglipForImageClassification
3
- from PIL import Image
4
- import torch
5
-
6
- # Load processor and model
7
- model_name = "prithivMLmods/Recycling-Net-11"
8
- processor = AutoImageProcessor.from_pretrained(model_name)
9
- model = SiglipForImageClassification.from_pretrained(model_name)
10
- id2label = model.config.id2label
11
-
12
- # Label groups
13
- recyclable_labels = ["cardboard", "glass", "metal", "paper", "plastic", "can", "carton"]
14
- non_recyclable_labels = ["food waste", "trash", "garbage", "organic"]
15
-
16
- # Inference function
17
- def classify_image(image):
18
- img = image.convert("RGB") if not isinstance(image, Image.Image) else image
19
- inputs = processor(images=img, return_tensors="pt")
20
-
21
- with torch.no_grad():
22
- logits = model(**inputs).logits
23
- probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
24
-
25
- pred_idx = max(range(len(probs)), key=lambda i: probs[i])
26
- pred_label = id2label[pred_idx].lower()
27
-
28
- category = "Recyclable" if any(label in pred_label for label in recyclable_labels) else "Non-Recyclable"
29
- confidence = probs[pred_idx] * 100
30
-
31
- return f"{category} ({pred_label}) with {confidence:.2f}% confidence"
32
-
33
- # Gradio interface using Camera
34
- interface = gr.Interface(
35
- fn=classify_image,
36
- inputs=gr.Camera(label="Take a photo of the waste"),
37
- outputs="text",
38
- live=False,
39
- title="♻️ Live Waste Classifier",
40
- description="Capture waste via webcam to classify as recyclable or non-recyclable using a HuggingFace model."
41
- )
42
-
43
- interface.launch()
 
 
 
 
 
 
 
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()