Overview
TrashDetect.py is a command-line interface for real-time waste detection using your webcam. It provides a lightweight, OpenCV-based detection window with bounding boxes and confidence scores.TrashDetect.py requires a connected camera (default: camera index 0) and the trained YOLOv8 model at
Modelos/best.pt.Running the CLI Detector
Run TrashDetect.py
Execute the detection script:The script will immediately start capturing video from your default webcam (camera index 0).
Detection Output
The CLI detector provides two types of output:Visual Output
The OpenCV window displays:- Bounding boxes: Red rectangles (BGR: 0, 0, 255) around detected objects
- Labels: Class name and confidence percentage above each detection
- Font: HERSHEY_COMPLEX with size 1 and thickness 2
Console Output
Real-time detection information is printed to the console:- Clase (Class): Integer ID (0=Metal, 1=Glass, 2=Plastic, 3=Carton, 4=Medical)
- Confidence: Detection confidence (0-1 scale, displayed as ceiling value)
Detected Waste Classes
The model can detect five types of waste materials:Metal
Class ID: 0Recyclable metals like aluminum cans, steel containers
Glass
Class ID: 1Glass bottles, jars, and containers
Plastic
Class ID: 2Plastic bottles, containers, and packaging
Carton
Class ID: 3Cardboard boxes, paper cartons
Medical
Class ID: 4Medical waste requiring special disposal
Code Structure
Here’s the complete TrashDetect.py implementation:Key Features
Model Inference
TrashDetect.py:23
- stream=True: Enables generator-based inference for memory efficiency
- verbose=False: Suppresses detailed logging output
Bounding Box Validation
TrashDetect.py:33-36
Confidence Thresholding
TrashDetect.py:45
Keyboard Controls
| Key | Action |
|---|---|
| ESC (27) | Exit application and release camera |
Troubleshooting
Camera not found error
Camera not found error
If you see a camera initialization error:
- Ensure your webcam is connected
- Check if another application is using the camera
- Try changing the camera index in line 10:
Model file not found
Model file not found
Verify the model path exists:If missing, ensure you’ve trained the model or downloaded the pre-trained weights.
Low detection accuracy
Low detection accuracy
Detection accuracy depends on:
- Proper lighting conditions
- Camera positioning and angle
- Distance to objects (optimal: 30-100cm)
- Object visibility and size in frame
Performance issues / Low FPS
Performance issues / Low FPS
To improve performance:
- Reduce camera resolution (see Camera Configuration)
- Ensure GPU support is enabled for YOLOv8
- Close other resource-intensive applications
Next Steps
GUI Application
Learn about the full-featured Tkinter GUI interface
Camera Configuration
Optimize camera settings for best detection results