The SegmentationModel class provides trash detection and tracking functionality using a pre-trained YOLO model. It handles device management, model loading, and inference operations for real-time trash segmentation.
The model automatically selects the optimal device (GPU/CPU) and loads the pre-trained trash detection model during initialization.
Loaded YOLO segmentation model optimized for trash detection
Initialization Process:
Detects available hardware (GPU/CPU) using DeviceManager
Loads the pre-trained trash segmentation model using ModelLoader
Prepares model for inference with optimal device configuration
Example:
from trash_classificator.segmentation.main import SegmentationModel# Initialize the model (automatic device and model setup)model = SegmentationModel()print(f"Model loaded on device: {model.device}")
Model initialization may take several seconds on first load as it downloads and caches the model weights.
import cv2import numpy as npfrom trash_classificator.segmentation.main import SegmentationModel# Initialize modelmodel = SegmentationModel()# Load imageimage = cv2.imread('frame.jpg')# Run inferenceresults, trash_classes, device = model.inference(image)# Process resultsfor result in results: if result.boxes.id is not None: print(f"Detected {len(result.boxes)} trash objects") print(f"Classes: {trash_classes}") print(f"Device: {device}")
import cv2from trash_classificator.segmentation.main import SegmentationModelmodel = SegmentationModel()cap = cv2.VideoCapture('trash_video.mp4')while cap.isOpened(): ret, frame = cap.read() if not ret: break # Run inference with tracking results, trash_classes, device = model.inference(frame) # Iterate through detected objects for result in results: if result.boxes.id is not None: boxes = result.boxes.xyxy.cpu().numpy() track_ids = result.boxes.id.int().cpu().tolist() classes = result.boxes.cls.cpu().tolist() # Process each detection for box, track_id, cls in zip(boxes, track_ids, classes): x1, y1, x2, y2 = box label = trash_classes[int(cls)] print(f"Track ID {track_id}: {label} at [{x1:.0f}, {y1:.0f}, {x2:.0f}, {y2:.0f}]")cap.release()