Image Classification
fastai
PyTorch
English
computer-vision
waste-classification
recycling
resnet50
gradio
Instructions to use C0mpl1cated3/trash_classifier_v2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- fastai
How to use C0mpl1cated3/trash_classifier_v2 with fastai:
from huggingface_hub import from_pretrained_fastai learn = from_pretrained_fastai("C0mpl1cated3/trash_classifier_v2") - Notebooks
- Google Colab
- Kaggle
Download app.py from C0mpl1cated3/trash_classifier_v2: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/C0mpl1cated3/trash_classifier_v2/resolve/main/app.py
- Command line
-
hf download hf://C0mpl1cated3/trash_classifier_v2/app.py
-
curl -L -o app.py https://huggingface.co/C0mpl1cated3/trash_classifier_v2/resolve/main/app.py
1.45 kB
| from pathlib import Path | |
| import platform | |
| import pathlib | |
| import gradio as gr | |
| from fastai.vision.all import PILImage, load_learner | |
| if platform.system() == "Windows": | |
| pathlib.PosixPath = pathlib.WindowsPath | |
| # Load model once when app starts | |
| MODEL_PATH = Path(__file__).parent / "models" / "waste_classifier_v2.pkl" | |
| learn = load_learner(MODEL_PATH) | |
| # Waste bin mapping | |
| BIN_INFO = { | |
| "plastic": "Plastic Recycling", | |
| "glass": "Glass Recycling", | |
| "metal": "Metal Recycling", | |
| "paper": "Paper Recycling", | |
| "cardboard": "Paper/Cardboard Recycling", | |
| "organic": "Organic/Compost", | |
| "textile": "Textile Collection", | |
| "e-waste": "Electronic Waste Collection", | |
| } | |
| def predict(image): | |
| img = PILImage.create(image) | |
| pred, pred_idx, probs = learn.predict(img) | |
| confidence = float(probs[pred_idx]) | |
| bin_name = BIN_INFO[str(pred)] | |
| return ( | |
| f"## {pred.title()} ({confidence:.1%})", | |
| f"**Recommended disposal:** {bin_name}", | |
| ) | |
| demo = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="pil", label="Upload a waste image"), | |
| outputs=[ | |
| gr.Markdown(label="Prediction"), | |
| gr.Markdown(label="Disposal Recommendation"), | |
| ], | |
| title="♻️ AI Trash Sorter", | |
| description="Upload an image of waste and the AI will identify it and recommend the correct trash bin.", | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |