Skip to main content

Overview

Reciclaje AI uses a real-time computer vision pipeline powered by YOLOv8 to detect and classify waste materials from a camera feed. The system processes video frames continuously, identifies waste objects, and categorizes them into recyclable materials.

Detection Pipeline

The waste detection system follows a streamlined pipeline from camera input to classification output:
1

Camera Capture

The system captures live video from a webcam at 1280x720 resolution.
cap = cv2.VideoCapture(0)
cap.set(3, 1280)  # Width
cap.set(4, 720)   # Height
2

Frame Processing

Each video frame is read and passed to the YOLOv8 model for inference.
ret, frame = cap.read()
results = model(frame, stream=True, verbose=False)
The stream=True parameter enables efficient processing for real-time video.
3

Object Detection

YOLOv8 analyzes the frame and returns detected objects with:
  • Bounding box coordinates (x1, y1, x2, y2)
  • Class ID (0-4 for the 5 waste categories)
  • Confidence score (0-1 probability)
for res in results:
    boxes = res.boxes
    for box in boxes:
        x1, y1, x2, y2 = box.xyxy[0]
        cls = int(box.cls[0])
        conf = math.ceil(box.conf[0])
4

Classification & Visualization

Detected objects are classified and displayed with visual overlays:
clsName = ['Metal', 'Glass', 'Plastic', 'Carton', 'Medical']

if conf > 0:
    cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
    cv2.putText(frame, f'{clsName[cls]} {int(conf * 100)}%', 
                (x1, y1 - 20), cv2.FONT_HERSHEY_COMPLEX, 1, 
                (0, 0, 255), 2)

Real-Time Processing Loop

The system operates in a continuous loop for real-time detection:
while True:
    # Capture frame
    ret, frame = cap.read()
    
    # Run detection
    results = model(frame, stream=True, verbose=False)
    
    # Process detections
    for res in results:
        boxes = res.boxes
        for box in boxes:
            # Extract bounding box, class, and confidence
            # Draw visualization
            # Display classification
    
    # Show result
    cv2.imshow("Waste Detect", frame)
    
    # Exit on ESC key (27)
    if cv2.waitKey(5) == 27:
        break
The system uses cv2.waitKey(5) to create a 5ms delay between frames, enabling smooth video playback and allowing keyboard input for graceful shutdown.

Bounding Box Validation

To prevent rendering errors, the system validates bounding box coordinates:
# Error < 0
if x1 < 0: x1 = 0
if y1 < 0: y1 = 0
if x2 < 0: x2 = 0
if y2 < 0: y2 = 0
This ensures all coordinates are non-negative before drawing rectangles on the frame.

Model Integration

The YOLOv8 model is loaded from a pre-trained weights file:
from ultralytics import YOLO

model = YOLO('Modelos/best.pt')
The best.pt file contains the trained model weights specifically optimized for detecting the 5 waste categories in Reciclaje AI.

Key Features

Real-Time Processing

Processes live video feed with minimal latency for instant waste classification.

Multi-Object Detection

Can detect and classify multiple waste items simultaneously in a single frame.

Confidence Scoring

Each detection includes a confidence percentage indicating prediction reliability.

Visual Feedback

Draws bounding boxes and labels directly on the video feed for easy interpretation.

Performance Considerations

The stream=True parameter in YOLOv8 enables generator-based inference, which is more memory-efficient for processing video streams. It yields results as they’re produced rather than storing all frames in memory.
The system captures at 1280x720 resolution with a 5ms processing delay. For lower-spec hardware, you can reduce resolution or increase the waitKey delay to maintain smooth performance.
The code uses if conf > 0 as the threshold for displaying detections. In production, you might want to set a higher threshold (e.g., 0.5 or 0.7) to filter out low-confidence predictions.

Next Steps

Detection Classes

Learn about the 5 waste categories and their characteristics

Model Architecture

Understand the YOLOv8 architecture powering the detection

Build docs developers (and LLMs) love