Skip to main content

Overview

This guide will help you run your first trash classification in under 5 minutes. You’ll process a video stream and see real-time trash detection and classification in action.
Make sure you’ve completed the Installation steps before proceeding.

Run Your First Classification

1

Import the Classifier

Create a new Python file or notebook and import the main classifier:
import cv2
import numpy as np
from trash_classificator.processor import TrashClassificator
2

Initialize the System

Create an instance of the TrashClassificator:
# Initialize the trash classification system
classificator = TrashClassificator()
print("System initialized successfully!")
You’ll see output indicating which device is being used:
Model is using device: NVIDIA GeForce RTX 3060
System initialized successfully!
3

Process a Single Image

Test with a single image first:
# Load a test image
image = cv2.imread("path/to/your/trash/image.jpg")

# Process the image
result_image, status = classificator.frame_processing(image)

# Display the result
cv2.imshow("Trash Classification", result_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

print(f"Status: {status}")
4

Run Real-time Video Classification

For real-time processing, use the TrashSystem class from the examples:
video_classification.py
import os
import sys
import cv2
import logging as log

log.basicConfig(level=log.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

from trash_classificator.processor import TrashClassificator

class TrashSystem:
    def __init__(self, video_source):
        self.cap = cv2.VideoCapture(video_source)
        self.cap.set(3, 1280)  # Width
        self.cap.set(4, 720)   # Height
        self.trash_classificator_system = TrashClassificator()

    def run(self):
        while self.cap.isOpened():
            success, frame = self.cap.read()
            if not success:
                break
            
            # Process frame
            image, process_log = self.trash_classificator_system.frame_processing(frame)
            log.info(f"process information: {process_log}")
            
            # Display result
            cv2.imshow("Trash Classification", image)
            
            # Press 'q' to quit
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        
        self.cap.release()
        cv2.destroyAllWindows()

if __name__ == "__main__":
    # Use 0 for webcam, or provide video file path
    trash_system = TrashSystem(0)
    trash_system.run()

Understanding the Output

The classification system provides rich visual feedback:

Color-Coded Masks

  • Blue: Cardboard/Paper (BGR: 255, 0, 0)
  • Yellow: Metal (BGR: 255, 255, 0)
  • Gray: Plastic (BGR: 200, 200, 200)

Bounding Boxes

Each detected object has a labeled box with:
  • Class name
  • Confidence score
  • Unique tracking ID

Tracking Lines

Trajectory paths show object movement across frames with color-coded lines per tracked item

Expected Results

Console Output

2024-03-07 10:15:23,456 - INFO - Model is using device: CUDA
2024-03-07 10:15:24,789 - INFO - process information: Trash detected
2024-03-07 10:15:24,823 - INFO - process information: Trash detected
2024-03-07 10:15:24,856 - INFO - process information: Trash detected

Visual Output

The processed video will display:
  1. Segmentation Masks: Semi-transparent colored overlays on detected trash
  2. Bounding Boxes: Rectangles around each object with labels
  3. Track Lines: Movement trajectories for tracked objects
  4. Class Labels: Text showing the waste category and confidence

Video Source Options

You can classify trash from multiple sources:
Use your computer’s webcam (index 0):
trash_system = TrashSystem(0)
If you have multiple cameras, try indices 1, 2, etc.

Quick Configuration

Adjust Video Resolution

Modify the resolution for better performance:
self.cap.set(3, 640)   # Lower width for faster processing
self.cap.set(4, 480)   # Lower height

Change Confidence Threshold

The default confidence threshold is 0.55. To modify it, edit the inference call in segmentation/main.py:
results = self.trash_segmentation_model.track(
    image, 
    conf=0.65,  # Increase for fewer, more confident detections
    verbose=False, 
    persist=True, 
    imgsz=640,
    stream=True
)

Common Use Cases

Process individual images for testing:
import glob

classificator = TrashClassificator()

for image_path in glob.glob("test_images/*.jpg"):
    image = cv2.imread(image_path)
    result, status = classificator.frame_processing(image)
    cv2.imwrite(f"results/{os.path.basename(image_path)}", result)
    print(f"{image_path}: {status}")
Save processed video with annotations:
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (1280, 720))

while cap.isOpened():
    success, frame = cap.read()
    if not success:
        break
    
    result, status = classificator.frame_processing(frame)
    out.write(result)

out.release()
Coordinate with VEX arm controller:
from examples.serial_com import CommunicationManager

comm = CommunicationManager(port='COM7')
comm.connect()

# Send scan command
comm.send_message('scan_service', {})

# Process classifications and control robot
# See Robotics Integration guide for details

Troubleshooting

If you encounter performance issues, try these optimizations:
  • Reduce video resolution (640x480 instead of 1280x720)
  • Lower the confidence threshold to reduce processing overhead
  • Ensure GPU acceleration is working (check device output)
  • Close other GPU-intensive applications

No Trash Detected

If the system shows “No trash detected”:
  1. Check Lighting: Ensure adequate lighting in the scene
  2. Object Distance: Objects should be clearly visible and not too far
  3. Lower Confidence: Try reducing conf parameter to 0.4 or 0.45
  4. Camera Quality: Use a better camera or improve focus

Low Frame Rate

For slow processing:
  1. Use GPU: Ensure CUDA or MPS is available and enabled
  2. Reduce Resolution: Lower image size to 640x480
  3. Adjust imgsz: Set imgsz=416 for faster (less accurate) inference
  4. Skip Frames: Process every 2nd or 3rd frame

Next Steps

Core Concepts

Understand how the system works

Training Guide

Train models on custom datasets

API Reference

Explore the complete API

Examples

View more usage examples

Performance Benchmarks

Expected performance on different hardware:
HardwareResolutionFPSNotes
RTX 30601280x72045-60Recommended for real-time
RTX 20601280x72030-40Good performance
Apple M11280x72025-35MPS acceleration
CPU Only640x4805-10Testing only
Start with a lower resolution (640x480) and scale up once you verify everything is working correctly.

Build docs developers (and LLMs) love