RecycleTree / app.py
Beansbeansbeansbeans's picture
Update app.py
d1ea6ed verified
from __future__ import annotations
import gradio as gr
from huggingface_hub import from_pretrained_fastai
import os
os.environ["HF_ENDPOINT"] = "https://huggingface.co"
materials_model = from_pretrained_fastai("pyesonekyaw/recycletree_materials")
paper_model = from_pretrained_fastai("pyesonekyaw/recycletree_paper")
plastic_model = from_pretrained_fastai("pyesonekyaw/recycletree_plastic")
metal_model = from_pretrained_fastai("pyesonekyaw/recycletree_metal")
others_model = from_pretrained_fastai("pyesonekyaw/recycletree_others")
glass_model = from_pretrained_fastai("pyesonekyaw/recycletree_glass")
examples = ["Examples/1.jpg", "Examples/2.jpg",
"Examples/3.jpg", "Examples/4.jpg", "Examples/5.jpg"]
material_names = ['Glass', 'Metal', 'Others', 'Paper', 'Plastic']
plastic_names = ['CD Disk', 'Straw', 'Plastic Bag', 'Clothes Hanger', 'Plastic Container or Bottle',
'Disposable Cutlery', 'Plastic Packaging', 'Plastic Packaging With Foil', 'Styrofoam']
paper_names = ['Beverage Carton', 'Cardboard', 'Chopsticks', 'Disposables', 'Paper Bag', 'Paper Packaging',
'Paper Product', 'Receipt', 'Paper Roll', 'Paper Sheet', 'Tissue Box', 'Tissue Paper']
glass_names = ['Ceramic', 'Glassware', 'Lightbulb']
other_names = ['Battery', 'Electronic Waste', 'Stationery']
metal_names = ['Aerosol Can', 'Aluminium Foil or Tray', 'Metal Can or Container']
material_num_name_dict = {
"metal": "Metal",
"glass": "Glass",
"paper": "Paper",
"plastic": "Plastic",
"others": "Others",
}
plastic_item_num_dict = {
"CD Disk": ["CD Disk", "No, find a local e-waste collection near you (refer to the info page!!!)"],
"Straw": ["Straw", "No, dispose as general waste"],
"Plastic Bag": ["Plastic Bag", "Yes, plastic bags can be recycled at most big grocery stores (refer to info page!!) unless it's contaminated with food waste/liquid waste/other forms of waste."],
"Clothes Hanger": ["Clothes Hanger", "Yes, but if it's made up of more than one plastic just dispose as normal waste "],
"Plastic Container or Bottle": ["Plastic Container or Bottle", "Yes. Empty it, rinse it if needed, and keep the cap on."],
"Disposable Cutlery": ["Disposable Cutlery", "No, dispose as general waste", "Nil"],
"Plastic Packaging": ["Plastic Packaging", "Yes, some packaging can be recycled at collection centers (refer to info page!!!) but no if directly enclosing food like cling wrap or contaminated with food contents "],
"Plastic Packaging With Foil": ["Plastic Packaging With Foil", "No, dispose as general waste"],
"Styrofoam": ["Styrofoam", "No, dispose as general waste or drop it at a collection center (refer to info page)"]
}
glass_item_num_dict = {
"Ceramic": ["Ceramic", "No, donate if can be reused"],
"Glassware": ["Glassware", "Yes. Unless there is liquid/solid residue inside the glassware. Clean and recycle if possible."],
"Lightbulb": ["Lightbulb", "No, find a local e-waste collection near you (refer to the info page!!!)"]
}
metal_item_num_dict = {
"Aerosol Can": ["Aerosol Can", "Yes, only if there is nothing left in the can"],
"Aluminium Foil or Tray": ["Aluminium Foil or Tray", "Yes. Unless there is any residue. Clean and recycle if possible."],
"Metal Can or Container": ["Metal Can or Container", "Yes. Unless there is any residue. Clean and recycle if possible."]
}
others_item_num_dict = {
"Battery": ["Battery", "No, find a local e-waste collection near you (refer to the info page!!!)"],
"Electronic Waste": ["Electronic Waste", "No, find a local e-waste collection near you (refer to the info page!!!)"],
"Stationery": ["Stationery", "No, donate if can be reused"]
}
paper_item_num_dict = {
"Beverage Carton": ["Beverage Carton", "Yes, rinsed and flattened"],
"Cardboard": ["Cardboard", "Yes, remove other materials such as tape and flatten!! Don't recycle if contaminated with other waste like food"],
"Chopsticks": ["Chopsticks", "No, dispose as general waste"],
"Disposables": ["Disposables", "No, dispose as general waste"],
"Paper Bag": ["Paper Bag", "Yes. Unless contaminated with food waste or other waste"],
"Paper Packaging": ["Paper Packaging", "Yes, remove other materials such as tape and flatten!! Don't recycle if contaminated with other waste like food"],
"Paper Product": ["Paper Product", "Yes, remove other materials such as tape and flatten!! Don't recycle if contaminated with other waste like food"],
"Receipt": ["Receipt", "No, dispose as general waste"],
"Paper Roll": ["Paper Roll", "Yes. Unless contaminated with food waste or other waste"],
"Paper Sheet": ["Paper Sheet", "Yes. Unless contaminated with food waste or other waste"],
"Tissue Box": ["Tissue Box", "Yes, remove plastic liners. Don't recycle if contaminated with other waste "],
"Tissue Paper": ["Tissue Paper", "No, dispose as general waste"]
}
def predict_image(inp):
"""
Performs inference for a given input image and returns the prediction and CAM image.
"""
try:
material_label, material_label_idx, material_probs = materials_model.predict(inp)
material_preds = {name: prob for name, prob in zip(material_names, material_probs.tolist())}
if material_label == 'paper':
specific_label, specific_label_idx, specific_probs = paper_model.predict(inp)
specific_preds = {name: prob for name, prob in zip(paper_names, specific_probs.tolist())}
specific_label = paper_names[int(specific_label_idx)]
recyclable_qn = paper_item_num_dict[specific_label][0]
recyclable_advice = paper_item_num_dict[specific_label][1]
elif material_label == 'plastic':
specific_label, specific_label_idx, specific_probs = plastic_model.predict(inp)
specific_preds = {name: prob for name, prob in zip(plastic_names, specific_probs.tolist())}
specific_label = plastic_names[int(specific_label_idx)]
recyclable_qn = plastic_item_num_dict[specific_label][0]
recyclable_advice = plastic_item_num_dict[specific_label][1]
elif material_label == 'glass':
specific_label, specific_label_idx, specific_probs = glass_model.predict(inp)
specific_preds = {name: prob for name, prob in zip(glass_names, specific_probs.tolist())}
specific_label = glass_names[int(specific_label_idx)]
recyclable_qn = glass_item_num_dict[specific_label][0]
recyclable_advice = glass_item_num_dict[specific_label][1]
elif material_label == 'metal':
specific_label, specific_label_idx, specific_probs = metal_model.predict(inp)
specific_preds = {name: prob for name, prob in zip(metal_names, specific_probs.tolist())}
specific_label = metal_names[int(specific_label_idx)]
recyclable_qn = metal_item_num_dict[specific_label][0]
recyclable_advice = metal_item_num_dict[specific_label][1]
elif material_label == 'others':
specific_label, specific_label_idx, specific_probs = others_model.predict(inp)
specific_preds = {name: prob for name, prob in zip(other_names, specific_probs.tolist())}
specific_label = other_names[int(specific_label_idx)]
recyclable_qn = others_item_num_dict[specific_label][0]
recyclable_advice = others_item_num_dict[specific_label][1]
return material_preds, specific_preds, recyclable_qn, recyclable_advice
except:
raise Exception("Invalid file format! Please only upload .jpg or .png files!")
with gr.Blocks(title="Trash Classification") as demo:
gr.Markdown("# Check whether your trash is recyclable or not!", elem_id="custom_title")
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. Images uploaded are never saved or indexed. No data is saved. This space is free to use.", elem_id="desc1")
gr.Markdown("Also feel free to go through the examples shown!", elem_id="desc2")
inp = gr.Image(type="filepath", label="Upload Your Image Here", elem_id="image_input")
material_probs = gr.Label(label="Material Classification Probabilities", elem_id="material_probs")
specific_probs = gr.Label(label="Specific Classification Probabilities", elem_id="specific_probs")
recyclable_qn = gr.Textbox(label="Item Recyclability", elem_id="recyclable_qn")
recyclable_advice = gr.Textbox(label="Recycling Advice", elem_id="recyclable_advice")
btn = gr.Button("Classify", elem_id="classify_btn")
btn.click(fn=predict_image, inputs=inp, outputs=[material_probs, specific_probs, recyclable_qn, recyclable_advice])
examples = gr.Examples(examples=examples, inputs=inp, outputs=[material_probs, specific_probs, recyclable_qn, recyclable_advice])
demo.launch()