Spaces:
Runtime error
Runtime error
pyesonekyaw
commited on
Commit
·
bd99105
1
Parent(s):
5a3cab2
initial commit
Browse files- Examples/1.jpg +0 -0
- Examples/2.jpg +0 -0
- Examples/3.jpg +0 -0
- Examples/4.jpg +0 -0
- Examples/5.jpg +0 -0
- README.md +4 -4
- app.py +152 -0
- requirements.txt +2 -0
Examples/1.jpg
ADDED
Examples/2.jpg
ADDED
Examples/3.jpg
ADDED
Examples/4.jpg
ADDED
Examples/5.jpg
ADDED
README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
---
|
2 |
-
title: RecycleTree
|
3 |
-
emoji:
|
4 |
colorFrom: green
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: openrail
|
|
|
1 |
---
|
2 |
+
title: RecycleTree - Trash/Recyclable Classification
|
3 |
+
emoji: ♻️
|
4 |
colorFrom: green
|
5 |
+
colorTo: green
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.9
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: openrail
|
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import from_pretrained_fastai
|
3 |
+
import os
|
4 |
+
os.environ["HF_ENDPOINT"] = "https://huggingface.co"
|
5 |
+
|
6 |
+
materials_model = from_pretrained_fastai("pyesonekyaw/recycletree_materials")
|
7 |
+
paper_model = from_pretrained_fastai("pyesonekyaw/recycletree_paper")
|
8 |
+
plastic_model = from_pretrained_fastai("pyesonekyaw/recycletree_plastic")
|
9 |
+
metal_model = from_pretrained_fastai("pyesonekyaw/recycletree_metal")
|
10 |
+
others_model = from_pretrained_fastai("pyesonekyaw/recycletree_others")
|
11 |
+
glass_model = from_pretrained_fastai("pyesonekyaw/recycletree_glass")
|
12 |
+
|
13 |
+
examples = ["Examples/1.jpg", "Examples/2.jpg",
|
14 |
+
"Examples/3.jpg", "Examples/4.jpg", "Examples/5.jpg"]
|
15 |
+
|
16 |
+
material_names = ['Glass', 'Metal', 'Others', 'Paper', 'Plastic']
|
17 |
+
plastic_names = ['CD Disk', 'Straw', 'Plastic Bag', 'Clothes Hanger', 'Plastic Container or Bottle',
|
18 |
+
'Disposable Cutlery', 'Plastic Packaging', 'Plastic Packaging With Foil', 'Styrofoam']
|
19 |
+
paper_names = ['Beverage Carton', 'Cardboard', 'Chopsticks', 'Disposables', 'Paper Bag', 'Paper Packaging',
|
20 |
+
'Paper Product', 'Receipt', 'Paper Roll', 'Paper Sheet', 'Tissue Box', 'Tissue Paper']
|
21 |
+
glass_names = ['Ceramic', 'Glassware', 'Lightbulb']
|
22 |
+
other_names = ['Battery', 'Electronic Waste', 'Stationery']
|
23 |
+
metal_names = ['Aerosol Can', 'Aluminium Foil or Tray', 'Metal Can or Container']
|
24 |
+
|
25 |
+
material_num_name_dict = {
|
26 |
+
"metal": "Metal",
|
27 |
+
"glass": "Glass",
|
28 |
+
"paper": "Paper",
|
29 |
+
"plastic": "Plastic",
|
30 |
+
"others": "Others",
|
31 |
+
}
|
32 |
+
|
33 |
+
plastic_item_num_dict = {
|
34 |
+
"CD Disk": ["Yes", "Nil"],
|
35 |
+
"Straw": ["No, dispose as general waste","Nil"],
|
36 |
+
"Plastic Bag": ["Yes, if they are not oxo- and bio- degradable bags", "Contaminated with food waste/liquid waste/other forms of waste "],
|
37 |
+
"Clothes Hanger": ["Yes", "Made up of more than one plastic, if unsure, just dispose as normal waste "],
|
38 |
+
"Plastic Container or Bottle": ["Yes", "When they are not emptied or not rinsed "],
|
39 |
+
"Disposable Cutlery": ["No, dispose as general waste", "Nil"],
|
40 |
+
"Plastic Packaging": ["Yes, for things like bubble wrap and egg tray but no if directly enclosing food like cling wrap", "Contaminated with food contents "],
|
41 |
+
"Plastic Packaging With Foil": ["No","Nil"],
|
42 |
+
"Styrofoam": ["No, dispose as general waste","Nil"]
|
43 |
+
}
|
44 |
+
glass_item_num_dict = {
|
45 |
+
"Ceramic": ["No, donate if can be reused", "Nil"],
|
46 |
+
"Glassware": ["Yes","If there is liquid/solid residue inside the glassware "],
|
47 |
+
"Lightbulb": ["Could be recycled at specific collection points which can be found on onemap.sg, under Lighting waste collection points", "Nil"]
|
48 |
+
}
|
49 |
+
metal_item_num_dict = {
|
50 |
+
"Aerosol Can": ["Yes","If there are any remaining contents in the can"],
|
51 |
+
"Aluminium Foil or Tray": ["Yes","If there is any residue "],
|
52 |
+
"Metal Can or Container": ["Yes","If there is any residue "]
|
53 |
+
}
|
54 |
+
others_item_num_dict = {
|
55 |
+
"battery": ["Battery","No, rechargeable batteries can be recycled through specific collection points (e-waste collection)", "Nil"],
|
56 |
+
"electronic_waste": ["Electronic Waste","Can be recycled through specific collection points (e-waste collection)"],
|
57 |
+
"stationery": ["Stationery","No, donate if can be reused"]
|
58 |
+
}
|
59 |
+
paper_item_num_dict = {
|
60 |
+
"Beverage Carton": ["Yes, rinsed and flattened","Nil"],
|
61 |
+
"Cardboard": ["Yes","Remains of other materials such as tape, contaminated with other waste"],
|
62 |
+
"Chopsticks": ["No, dispose as general waste ",],
|
63 |
+
"Disposables": ["No, dispose as general waste ",],
|
64 |
+
"Paper Bag": ["Yes","Contaminated with food waste or other waste "],
|
65 |
+
"Paper Packaging": ["Yes","Made up of more than one material or contaminated with food waste"],
|
66 |
+
"Paper Product": ["Yes","Contaminated with other waste"],
|
67 |
+
"Receipt": ["Yes","Contaminated with other waste"],
|
68 |
+
"Paper Roll": ["Yes","Contaminated with other waste"],
|
69 |
+
"Paper Sheet": ["Yes","Contaminated with other waste "],
|
70 |
+
"Tissue Box": ["Yes","Plastic liners not removed or contaminated with other waste "],
|
71 |
+
"Tissue Paper": ["No, dispose as general waste","Nil"]
|
72 |
+
}
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
def predict_image(inp):
|
77 |
+
"""
|
78 |
+
Performs inference for a given input image and returns the prediction and CAM image.
|
79 |
+
"""
|
80 |
+
material_label, material_label_idx, material_probs = materials_model.predict(inp)
|
81 |
+
material_preds = {name: prob for name, prob in zip(material_names, material_probs.tolist())}
|
82 |
+
|
83 |
+
if material_label == 'paper':
|
84 |
+
specific_label, specific_label_idx, specific_probs = paper_model.predict(inp)
|
85 |
+
specific_preds = {name: prob for name, prob in zip(paper_names, specific_probs.tolist())}
|
86 |
+
specific_label = paper_names[int(specific_label_idx)]
|
87 |
+
recyclable_qn = paper_item_num_dict[specific_label][0]
|
88 |
+
recyclable_advice = paper_item_num_dict[specific_label][1]
|
89 |
+
|
90 |
+
elif material_label == 'plastic':
|
91 |
+
specific_label, specific_label_idx, specific_probs = plastic_model.predict(inp)
|
92 |
+
specific_preds = {name: prob for name, prob in zip(plastic_names, specific_probs.tolist())}
|
93 |
+
specific_label = plastic_names[int(specific_label_idx)]
|
94 |
+
recyclable_qn = plastic_item_num_dict[specific_label][0]
|
95 |
+
recyclable_advice = plastic_item_num_dict[specific_label][1]
|
96 |
+
|
97 |
+
elif material_label == 'glass':
|
98 |
+
specific_label, specific_label_idx, specific_probs = glass_model.predict(inp)
|
99 |
+
specific_preds = {name: prob for name, prob in zip(glass_names, specific_probs.tolist())}
|
100 |
+
specific_label = glass_names[int(specific_label_idx)]
|
101 |
+
recyclable_qn = glass_item_num_dict[specific_label][0]
|
102 |
+
recyclable_advice = glass_item_num_dict[specific_label][1]
|
103 |
+
|
104 |
+
elif material_label == 'metal':
|
105 |
+
specific_label, specific_label_idx, specific_probs = metal_model.predict(inp)
|
106 |
+
specific_preds = {name: prob for name, prob in zip(metal_names, specific_probs.tolist())}
|
107 |
+
specific_label = metal_names[int(specific_label_idx)]
|
108 |
+
recyclable_qn = metal_item_num_dict[specific_label][0]
|
109 |
+
recyclable_advice = metal_item_num_dict[specific_label][1]
|
110 |
+
|
111 |
+
elif material_label == 'others':
|
112 |
+
specific_label, specific_label_idx, specific_probs = others_model.predict(inp)
|
113 |
+
specific_preds = {name: prob for name, prob in zip(other_names, specific_probs.tolist())}
|
114 |
+
specific_label = other_names[int(specific_label_idx)]
|
115 |
+
recyclable_qn = others_item_num_dict[specific_label][0]
|
116 |
+
recyclable_advice = others_item_num_dict[specific_label][1]
|
117 |
+
|
118 |
+
return material_preds, specific_preds, recyclable_qn, recyclable_advice
|
119 |
+
|
120 |
+
|
121 |
+
with gr.Blocks(title="Trash Classification", css="#custom_header {min-height: 3rem} #custom_title {min-height: 3rem; text-align: center}") as demo:
|
122 |
+
gr.Markdown("# Trash Classification", elem_id="custom_title")
|
123 |
+
gr.Markdown("Gradio Inference interface for classification of trash and recyclables. To use it, simply upload your image, or click one of the examples to load them", elem_id="custom_title")
|
124 |
+
|
125 |
+
with gr.Column():
|
126 |
+
with gr.Column():
|
127 |
+
with gr.Box():
|
128 |
+
gr.Markdown("## Inputs", elem_id="custom_header")
|
129 |
+
input_image = gr.Image(label="Input Image")
|
130 |
+
input_image.style(height=240)
|
131 |
+
btn = gr.Button(value="Submit")
|
132 |
+
btn.style(full_width=True)
|
133 |
+
with gr.Column():
|
134 |
+
with gr.Box():
|
135 |
+
gr.Markdown("## Outputs", elem_id="custom_header")
|
136 |
+
recycling_qn = gr.outputs.Textbox(label="Is this recyclable?")
|
137 |
+
recycling_advice = gr.outputs.Textbox(label="It is not recyclable when:")
|
138 |
+
with gr.Row():
|
139 |
+
material_probs = gr.outputs.Label(label="Material Prediction")
|
140 |
+
item_probs = gr.outputs.Label(label="Item Prediction")
|
141 |
+
|
142 |
+
gr.Examples(
|
143 |
+
examples=examples,
|
144 |
+
inputs=input_image,
|
145 |
+
fn=predict_image,
|
146 |
+
cache_examples=False,
|
147 |
+
)
|
148 |
+
|
149 |
+
btn.click(predict_image, inputs=[input_image],
|
150 |
+
outputs=[material_probs, item_probs, recycling_qn, recycling_advice])
|
151 |
+
if __name__ == "__main__":
|
152 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
huggingface_hub
|