Skip to main content

Overview

This guide demonstrates how to use the TrashSystem class to process video streams in real-time for trash classification. The system captures frames from a video source, processes them using the TrashClassificator, and displays the results with visual annotations.

Complete TrashSystem Class

The TrashSystem class provides a simple interface for video-based trash classification:
video_stream.py
import os
import sys
import cv2
import logging as log
log.basicConfig(level=log.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from trash_classificator.processor import TrashClassificator


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

    def run(self):
        while self.cap.isOpened():
            success, frame = self.cap.read()
            image, process_log = self.trash_classificator_system.frame_processing(frame)
            log.info(f"process information: {process_log}")
            if not success:
                break

            cv2.imshow("Frame", image)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        self.cap.release()
        cv2.destroyAllWindows()


if __name__ == "__main__":
    trash_system = TrashSystem(0)
    trash_system.run()

Step-by-Step Implementation

1

Import Required Libraries

Import OpenCV for video processing and the TrashClassificator for classification:
import cv2
import logging as log
from trash_classificator.processor import TrashClassificator
Make sure OpenCV is installed: pip install opencv-python
2

Initialize Video Capture

Create a VideoCapture object with your video source:
self.cap = cv2.VideoCapture(video_source)
self.cap.set(3, 1280)  # Width: 1280px
self.cap.set(4, 720)   # Height: 720px
Video Source Options:
  • 0 - Default webcam
  • 1, 2, ... - Additional cameras
  • "path/to/video.mp4" - Video file path
  • "rtsp://camera_url" - Network camera stream
3

Initialize TrashClassificator

Create an instance of the classification system:
self.trash_classificator_system = TrashClassificator()
The TrashClassificator handles all detection and classification logic internally.
4

Process Video Frames

Read frames in a loop and process each one:
while self.cap.isOpened():
    success, frame = self.cap.read()
    if not success:
        break
    
    # Process frame and get annotated image with logs
    image, process_log = self.trash_classificator_system.frame_processing(frame)
    log.info(f"process information: {process_log}")
The frame_processing() method returns:
  • image: Annotated frame with bounding boxes and labels
  • process_log: Dictionary containing detection results and metadata
5

Display Results and Handle Controls

Show the processed frame and implement keyboard controls:
cv2.imshow("Frame", image)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break
Keyboard Controls:
  • Press q to quit the application
Adjust cv2.waitKey(1) to control frame rate. Higher values slow down playback.
6

Clean Up Resources

Release the camera and close all windows:
self.cap.release()
cv2.destroyAllWindows()

Usage Examples

if __name__ == "__main__":
    # Use default webcam (device 0)
    trash_system = TrashSystem(0)
    trash_system.run()

Expected Output

When running the video stream processing, you’ll see:

Console Output

2026-03-07 10:15:23,456 - INFO - process information: {'detected_objects': 2, 'classifications': [{'class': 'plastic', 'confidence': 0.92}, {'class': 'paper', 'confidence': 0.87}], 'processing_time': 0.045}
2026-03-07 10:15:23,501 - INFO - process information: {'detected_objects': 1, 'classifications': [{'class': 'metal', 'confidence': 0.95}], 'processing_time': 0.043}

Video Window

The displayed video frame will include:
  • Bounding boxes around detected trash items
  • Class labels (plastic, paper, metal, glass, organic, other)
  • Confidence scores for each detection
  • Real-time processing with minimal latency

Performance Considerations

Processing Speed: The system processes frames at approximately 20-25 FPS on modern hardware. For real-time applications, ensure your video source matches this frame rate.
GPU Acceleration: If available, the TrashClassificator automatically uses GPU acceleration for faster inference. Check CUDA availability for optimal performance.

Troubleshooting

Camera Not Opening

if not self.cap.isOpened():
    log.error("Failed to open video source")
    # Try alternative camera indices or check connections

Low Frame Rate

  • Reduce video resolution:
    self.cap.set(3, 640)  # Lower width
    self.cap.set(4, 480)  # Lower height
    

Memory Issues

  • Process every Nth frame instead of all frames:
    frame_count = 0
    while self.cap.isOpened():
        success, frame = self.cap.read()
        frame_count += 1
        if frame_count % 3 != 0:  # Process every 3rd frame
            continue
        # Process frame...
    

Next Steps

Serial Communication

Learn how to send classification results to external devices

Custom Integration

Integrate the classifier into your own applications

Build docs developers (and LLMs) love