Detect garbage-like objects from a webcam, images, folders, or video files using YOLO.
The default model is yolov8n.pt, which is trained on COCO. It can detect common waste-like objects such as bottles, cups, bowls, and food items. For real garbage classes such as plastic_bag, cardboard, or food_waste, train a custom model with your own labeled dataset.
- Webcam, image, folder, and video input
- Class filtering for garbage-like objects
- Recursive folder search with
--recursive - Annotated image outputs
- Custom YOLO training script and dataset config
pip install -r requirements.txtpython garbage_detector.py --source 0Press Q to quit.
python garbage_detector.py --source path\to\image.jpgAnnotated images are saved in outputs.
python garbage_detector.py --source path\to\imagesSearch inside subfolders too:
python garbage_detector.py --source path\to\images --recursivepython garbage_detector.py --source path\to\video.mp4Detect every class in the model:
python garbage_detector.py --source 0 --classes allUse only selected classes:
python garbage_detector.py --source 0 --classes bottle,cup,bowlRaise or lower confidence:
python garbage_detector.py --source 0 --confidence 0.5Create a YOLO dataset like this:
dataset/
images/
train/
val/
labels/
train/
val/
Each label file must use YOLO format:
class_id x_center y_center width height
Then train:
python train_garbage_model.py --data garbage_dataset.yaml --epochs 50After training, run detection with the best weights:
python garbage_detector.py --model runs\garbage_detector\weights\best.pt --source 0 --classes allThe included garbage_dataset.yaml uses these classes:
0 plastic_bottle
1 glass_bottle
2 can
3 paper
4 cardboard
5 plastic_bag
6 food_waste
7 other_trash
garbage_detector.py # Detect from webcam, image, folder, or video
train_garbage_model.py # Train a custom garbage detector
garbage_dataset.yaml # YOLO dataset configuration
requirements.txt # Python dependencies
Model weights, training runs, datasets, and output images are ignored by Git so the repository stays lightweight.