Spaces:
Sleeping
Sleeping
Upload 8 files
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
- __pycache__/app.cpython-38.pyc +0 -0
- app.py +218 -0
- requirements.txt +3 -0
Examples/1.jpg
ADDED
Examples/2.jpg
ADDED
Examples/3.jpg
ADDED
Examples/4.jpg
ADDED
Examples/5.jpg
ADDED
__pycache__/app.cpython-38.pyc
ADDED
Binary file (6.4 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,218 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# from __future__ import annotations
|
2 |
+
import gradio as gr
|
3 |
+
from huggingface_hub import from_pretrained_fastai
|
4 |
+
import os
|
5 |
+
from typing import Iterable
|
6 |
+
# from gradio.themes.base import Base
|
7 |
+
# from gradio.themes.utils import colors, fonts, sizes
|
8 |
+
|
9 |
+
os.environ["HF_ENDPOINT"] = "https://huggingface.co"
|
10 |
+
|
11 |
+
materials_model = from_pretrained_fastai("pyesonekyaw/recycletree_materials")
|
12 |
+
paper_model = from_pretrained_fastai("pyesonekyaw/recycletree_paper")
|
13 |
+
plastic_model = from_pretrained_fastai("pyesonekyaw/recycletree_plastic")
|
14 |
+
metal_model = from_pretrained_fastai("pyesonekyaw/recycletree_metal")
|
15 |
+
others_model = from_pretrained_fastai("pyesonekyaw/recycletree_others")
|
16 |
+
glass_model = from_pretrained_fastai("pyesonekyaw/recycletree_glass")
|
17 |
+
|
18 |
+
examples = ["Examples/1.jpg", "Examples/2.jpg",
|
19 |
+
"Examples/3.jpg", "Examples/4.jpg", "Examples/5.jpg"]
|
20 |
+
|
21 |
+
material_names = ['Glass', 'Metal', 'Others', 'Paper', 'Plastic']
|
22 |
+
plastic_names = ['CD Disk', 'Straw', 'Plastic Bag', 'Clothes Hanger', 'Plastic Container or Bottle',
|
23 |
+
'Disposable Cutlery', 'Plastic Packaging', 'Plastic Packaging With Foil', 'Styrofoam']
|
24 |
+
paper_names = ['Beverage Carton', 'Cardboard', 'Chopsticks', 'Disposables', 'Paper Bag', 'Paper Packaging',
|
25 |
+
'Paper Product', 'Receipt', 'Paper Roll', 'Paper Sheet', 'Tissue Box', 'Tissue Paper']
|
26 |
+
glass_names = ['Ceramic', 'Glassware', 'Lightbulb']
|
27 |
+
other_names = ['Battery', 'Electronic Waste', 'Stationery']
|
28 |
+
metal_names = ['Aerosol Can', 'Aluminium Foil or Tray', 'Metal Can or Container']
|
29 |
+
|
30 |
+
material_num_name_dict = {
|
31 |
+
"metal": "Metal",
|
32 |
+
"glass": "Glass",
|
33 |
+
"paper": "Paper",
|
34 |
+
"plastic": "Plastic",
|
35 |
+
"others": "Others",
|
36 |
+
}
|
37 |
+
|
38 |
+
plastic_item_num_dict = {
|
39 |
+
"CD Disk": ["Yes", "Nil"],
|
40 |
+
"Straw": ["No, dispose as general waste","Nil"],
|
41 |
+
"Plastic Bag": ["Yes, if they are not oxo- and bio- degradable bags", "Contaminated with food waste/liquid waste/other forms of waste. Please refer to RecycleHowAh for more information"],
|
42 |
+
"Clothes Hanger": ["Yes", "Made up of more than one plastic, if unsure, just dispose as normal waste "],
|
43 |
+
"Plastic Container or Bottle": ["Yes", "When they are not emptied or not rinsed "],
|
44 |
+
"Disposable Cutlery": ["No, dispose as general waste", "Nil"],
|
45 |
+
"Plastic Packaging": ["Yes, for things like bubble wrap and egg tray but no if directly enclosing food like cling wrap", "Contaminated with food contents "],
|
46 |
+
"Plastic Packaging With Foil": ["No","Nil"],
|
47 |
+
"Styrofoam": ["No, dispose as general waste","Nil"]
|
48 |
+
}
|
49 |
+
glass_item_num_dict = {
|
50 |
+
"Ceramic": ["No, donate if can be reused", "Nil"],
|
51 |
+
"Glassware": ["Yes","If there is liquid/solid residue inside the glassware "],
|
52 |
+
"Lightbulb": ["Could be recycled at specific collection points which can be found on onemap.sg, under Lighting waste collection points", "Nil"]
|
53 |
+
}
|
54 |
+
metal_item_num_dict = {
|
55 |
+
"Aerosol Can": ["Yes","If there are any remaining contents in the can"],
|
56 |
+
"Aluminium Foil or Tray": ["Yes","If there is any residue "],
|
57 |
+
"Metal Can or Container": ["Yes","If there is any residue "]
|
58 |
+
}
|
59 |
+
others_item_num_dict = {
|
60 |
+
"Battery": ["Battery","No, rechargeable batteries can be recycled through specific collection points (e-waste collection)", "Nil"],
|
61 |
+
"Electronic Waste": ["Electronic Waste","Can be recycled through specific collection points (e-waste collection). Please refer to RecycleHowAh for more information"],
|
62 |
+
"Stationery": ["Stationery","No, donate if can be reused"]
|
63 |
+
}
|
64 |
+
paper_item_num_dict = {
|
65 |
+
"Beverage Carton": ["Yes, rinsed and flattened","Nil"],
|
66 |
+
"Cardboard": ["Yes","Remains of other materials such as tape, contaminated with other waste"],
|
67 |
+
"Chopsticks": ["No, dispose as general waste ",],
|
68 |
+
"Disposables": ["No, dispose as general waste ",],
|
69 |
+
"Paper Bag": ["Yes","Contaminated with food waste or other waste "],
|
70 |
+
"Paper Packaging": ["Yes","Made up of more than one material or contaminated with food waste"],
|
71 |
+
"Paper Product": ["Yes","Contaminated with other waste"],
|
72 |
+
"Receipt": ["Yes","Contaminated with other waste"],
|
73 |
+
"Paper Roll": ["Yes","Contaminated with other waste"],
|
74 |
+
"Paper Sheet": ["Yes","Contaminated with other waste "],
|
75 |
+
"Tissue Box": ["Yes","Plastic liners not removed or contaminated with other waste "],
|
76 |
+
"Tissue Paper": ["No, dispose as general waste","Nil"]
|
77 |
+
}
|
78 |
+
|
79 |
+
# class Seafoam(Base):
|
80 |
+
# def __init__(
|
81 |
+
# self,
|
82 |
+
# *,
|
83 |
+
# primary_hue: colors.Color | str = colors.emerald,
|
84 |
+
# secondary_hue: colors.Color | str = colors.teal,
|
85 |
+
# neutral_hue: colors.Color | str = colors.teal,
|
86 |
+
# spacing_size: sizes.Size | str = sizes.spacing_md,
|
87 |
+
# radius_size: sizes.Size | str = sizes.radius_md,
|
88 |
+
# text_size: sizes.Size | str = sizes.text_lg,
|
89 |
+
# font: fonts.Font
|
90 |
+
# | str
|
91 |
+
# | Iterable[fonts.Font | str] = (
|
92 |
+
# fonts.GoogleFont("Quicksand"),
|
93 |
+
# "ui-sans-serif",
|
94 |
+
# "sans-serif",
|
95 |
+
# ),
|
96 |
+
# font_mono: fonts.Font
|
97 |
+
# | str
|
98 |
+
# | Iterable[fonts.Font | str] = (
|
99 |
+
# fonts.GoogleFont("IBM Plex Mono"),
|
100 |
+
# "ui-monospace",
|
101 |
+
# "monospace",
|
102 |
+
# ),
|
103 |
+
# ):
|
104 |
+
# super().__init__(
|
105 |
+
# primary_hue=primary_hue,
|
106 |
+
# secondary_hue=secondary_hue,
|
107 |
+
# neutral_hue=neutral_hue,
|
108 |
+
# spacing_size=spacing_size,
|
109 |
+
# radius_size=radius_size,
|
110 |
+
# text_size=text_size,
|
111 |
+
# font=font,
|
112 |
+
# font_mono=font_mono,
|
113 |
+
# )
|
114 |
+
|
115 |
+
# super().set(
|
116 |
+
# body_background_fill="linear-gradient(45deg, *primary_200, *primary_200 10px, *primary_50 10px, *primary_50 20px)",
|
117 |
+
# body_background_fill_dark="linear-gradient(45deg, *primary_200, *primary_200 10px, *primary_50 10px, *primary_50 20px)",
|
118 |
+
# stat_background_fill_dark="linear-gradient(to right, *primary_400, *primary_200)",
|
119 |
+
# error_background_fill_dark=f"linear-gradient(to right, {colors.red.c100}, *background_fill_secondary)",
|
120 |
+
# button_primary_background_fill="linear-gradient(90deg, *primary_300, *secondary_400)",
|
121 |
+
# button_primary_background_fill_hover="linear-gradient(90deg, *primary_200, *secondary_300)",
|
122 |
+
# button_primary_background_fill_hover_dark="linear-gradient(90deg, *primary_200, *secondary_300)",
|
123 |
+
# button_primary_text_color="white",
|
124 |
+
# button_primary_background_fill_dark="linear-gradient(90deg, *primary_300, *secondary_400)",
|
125 |
+
# slider_color="*secondary_300",
|
126 |
+
# slider_color_dark="*secondary_300",
|
127 |
+
# block_title_text_weight="600",
|
128 |
+
# block_border_width="3px",
|
129 |
+
# block_shadow="*shadow_drop_lg",
|
130 |
+
# button_shadow="*shadow_drop_lg",
|
131 |
+
# button_large_padding="32px",
|
132 |
+
# )
|
133 |
+
# for k in list(self.__dict__.keys()):
|
134 |
+
# if '_dark' in k:
|
135 |
+
# setattr(self,k,None)
|
136 |
+
|
137 |
+
# seafoam = Seafoam()
|
138 |
+
|
139 |
+
def predict_image(inp):
|
140 |
+
"""
|
141 |
+
Performs inference for a given input image and returns the prediction and CAM image.
|
142 |
+
"""
|
143 |
+
try:
|
144 |
+
material_label, material_label_idx, material_probs = materials_model.predict(inp)
|
145 |
+
material_preds = {name: prob for name, prob in zip(material_names, material_probs.tolist())}
|
146 |
+
|
147 |
+
if material_label == 'paper':
|
148 |
+
specific_label, specific_label_idx, specific_probs = paper_model.predict(inp)
|
149 |
+
specific_preds = {name: prob for name, prob in zip(paper_names, specific_probs.tolist())}
|
150 |
+
specific_label = paper_names[int(specific_label_idx)]
|
151 |
+
recyclable_qn = paper_item_num_dict[specific_label][0]
|
152 |
+
recyclable_advice = paper_item_num_dict[specific_label][1]
|
153 |
+
|
154 |
+
elif material_label == 'plastic':
|
155 |
+
specific_label, specific_label_idx, specific_probs = plastic_model.predict(inp)
|
156 |
+
specific_preds = {name: prob for name, prob in zip(plastic_names, specific_probs.tolist())}
|
157 |
+
specific_label = plastic_names[int(specific_label_idx)]
|
158 |
+
recyclable_qn = plastic_item_num_dict[specific_label][0]
|
159 |
+
recyclable_advice = plastic_item_num_dict[specific_label][1]
|
160 |
+
|
161 |
+
elif material_label == 'glass':
|
162 |
+
specific_label, specific_label_idx, specific_probs = glass_model.predict(inp)
|
163 |
+
specific_preds = {name: prob for name, prob in zip(glass_names, specific_probs.tolist())}
|
164 |
+
specific_label = glass_names[int(specific_label_idx)]
|
165 |
+
recyclable_qn = glass_item_num_dict[specific_label][0]
|
166 |
+
recyclable_advice = glass_item_num_dict[specific_label][1]
|
167 |
+
|
168 |
+
elif material_label == 'metal':
|
169 |
+
specific_label, specific_label_idx, specific_probs = metal_model.predict(inp)
|
170 |
+
specific_preds = {name: prob for name, prob in zip(metal_names, specific_probs.tolist())}
|
171 |
+
specific_label = metal_names[int(specific_label_idx)]
|
172 |
+
recyclable_qn = metal_item_num_dict[specific_label][0]
|
173 |
+
recyclable_advice = metal_item_num_dict[specific_label][1]
|
174 |
+
|
175 |
+
elif material_label == 'others':
|
176 |
+
specific_label, specific_label_idx, specific_probs = others_model.predict(inp)
|
177 |
+
specific_preds = {name: prob for name, prob in zip(other_names, specific_probs.tolist())}
|
178 |
+
specific_label = other_names[int(specific_label_idx)]
|
179 |
+
recyclable_qn = others_item_num_dict[specific_label][0]
|
180 |
+
recyclable_advice = others_item_num_dict[specific_label][1]
|
181 |
+
|
182 |
+
return material_preds, specific_preds, recyclable_qn, recyclable_advice
|
183 |
+
except:
|
184 |
+
raise Exception("Invalid file format! Please only upload .jpg or .png files!")
|
185 |
+
|
186 |
+
|
187 |
+
with gr.Blocks(title="Trash Classification", css="#custom_header {min-height: 3rem} #custom_title {min-height: 3rem; text-align: center}") as demo:#, theme=seafoam) as demo:
|
188 |
+
gr.Markdown("# Recyclable Detector- Classification of trash and recyclables", elem_id="custom_title")
|
189 |
+
gr.Markdown("Gradio Inference interface", elem_id="custom_title")
|
190 |
+
|
191 |
+
with gr.Column():
|
192 |
+
with gr.Column():
|
193 |
+
with gr.Box():
|
194 |
+
gr.Markdown("## Inputs", elem_id="custom_header")
|
195 |
+
input_image = gr.Image(label="Input Image")
|
196 |
+
input_image.style(height=240)
|
197 |
+
btn = gr.Button(value="Submit")
|
198 |
+
btn.style(full_width=True)
|
199 |
+
with gr.Column():
|
200 |
+
with gr.Box():
|
201 |
+
gr.Markdown("## Outputs", elem_id="custom_header")
|
202 |
+
recycling_qn = gr.outputs.Textbox(label="Is this recyclable?")
|
203 |
+
recycling_advice = gr.outputs.Textbox(label="It is not recyclable when:")
|
204 |
+
with gr.Row():
|
205 |
+
material_probs = gr.outputs.Label(label="Material Prediction")
|
206 |
+
item_probs = gr.outputs.Label(label="Item Prediction")
|
207 |
+
|
208 |
+
gr.Examples(
|
209 |
+
examples=examples,
|
210 |
+
inputs=input_image,
|
211 |
+
fn=predict_image,
|
212 |
+
cache_examples=False,
|
213 |
+
)
|
214 |
+
|
215 |
+
btn.click(predict_image, inputs=[input_image],
|
216 |
+
outputs=[material_probs, item_probs, recycling_qn, recycling_advice])
|
217 |
+
if __name__ == "__main__":
|
218 |
+
demo.launch(show_error=True)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
huggingface_hub
|
3 |
+
fastai
|