|
|
import gradio as gr |
|
|
from transformers import pipeline |
|
|
from PIL import Image |
|
|
|
|
|
|
|
|
classifier = pipeline( |
|
|
"image-classification", |
|
|
model="watersplash/waste-classification" |
|
|
) |
|
|
|
|
|
|
|
|
waste_instructions = { |
|
|
"plastic": "โป๏ธ Place in plastic recycling bin. Avoid single-use plastics where possible.", |
|
|
"glass": "๐พ Clean and recycle in glass bin. Broken glass should be wrapped safely before disposal.", |
|
|
"paper": "๐ Recycle clean paper. Avoid recycling wet or oily paper.", |
|
|
"metal": "๐ฅซ Recycle cans after rinsing. Large scrap metal should go to a recycling center.", |
|
|
"cardboard": "๐ฆ Flatten boxes before recycling.", |
|
|
"organic": "๐ฑ Compost food waste and garden scraps.", |
|
|
"trash": "๐๏ธ Not recyclable. Dispose properly." |
|
|
} |
|
|
|
|
|
fun_facts = { |
|
|
"plastic": "It can take up to 500 years for plastic to decompose!", |
|
|
"glass": "Glass is 100% recyclable and can be recycled endlessly without loss of quality.", |
|
|
"paper": "Recycling 1 ton of paper saves 17 trees and 7,000 gallons of water.", |
|
|
"metal": "Aluminum cans can be recycled and back on shelves in as little as 60 days.", |
|
|
"cardboard": "Cardboard can be recycled up to 5โ7 times before the fibers weaken.", |
|
|
"organic": "Composting reduces landfill waste and returns nutrients to the soil.", |
|
|
"trash": "Some waste cannot be recycled โ reduce and reuse where possible." |
|
|
} |
|
|
|
|
|
|
|
|
def identify_waste(image): |
|
|
|
|
|
image = image.resize((224, 224)) |
|
|
|
|
|
|
|
|
results = classifier(image) |
|
|
label = results[0]["label"].lower() |
|
|
|
|
|
if label in waste_instructions: |
|
|
return f"๐๏ธ Detected Waste Type: {label.capitalize()}\n\nโ
{waste_instructions[label]}\n\n๐ก Fun Fact: {fun_facts[label]}" |
|
|
else: |
|
|
return f"๐๏ธ Detected Waste Type: {label}\n\nโ ๏ธ No recycling info available. Please dispose responsibly." |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("# โป๏ธ Smart Waste Identifier") |
|
|
gr.Markdown("Upload an image to identify the type of waste and get recycling instructions.") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
image_input = gr.Image(type="pil", label="Upload Waste Image") |
|
|
btn = gr.Button("Identify Waste") |
|
|
with gr.Column(): |
|
|
output_text = gr.Textbox(label="Result", lines=6) |
|
|
|
|
|
btn.click(fn=identify_waste, inputs=image_input, outputs=output_text) |
|
|
|
|
|
gr.Markdown("---") |
|
|
gr.Markdown("### โน๏ธ This app classifies input images into categories and provides recycling tips or fun facts.") |
|
|
|
|
|
|
|
|
demo.launch() |