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.
Create a new Python file or notebook and import the main classifier:
import cv2import numpy as npfrom trash_classificator.processor import TrashClassificator
2
Initialize the System
Create an instance of the TrashClassificator:
# Initialize the trash classification systemclassificator = TrashClassificator()print("System initialized successfully!")
You’ll see output indicating which device is being used:
Model is using device: NVIDIA GeForce RTX 3060System initialized successfully!
3
Process a Single Image
Test with a single image first:
# Load a test imageimage = cv2.imread("path/to/your/trash/image.jpg")# Process the imageresult_image, status = classificator.frame_processing(image)# Display the resultcv2.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 osimport sysimport cv2import logging as loglog.basicConfig(level=log.INFO, format="%(asctime)s - %(levelname)s - %(message)s")from trash_classificator.processor import TrashClassificatorclass 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()
2024-03-07 10:15:23,456 - INFO - Model is using device: CUDA2024-03-07 10:15:24,789 - INFO - process information: Trash detected2024-03-07 10:15:24,823 - INFO - process information: Trash detected2024-03-07 10:15:24,856 - INFO - process information: Trash detected
import globclassificator = 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}")
Saving Detection Results
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()
Integration with Robotics
Coordinate with VEX arm controller:
from examples.serial_com import CommunicationManagercomm = CommunicationManager(port='COM7')comm.connect()# Send scan commandcomm.send_message('scan_service', {})# Process classifications and control robot# See Robotics Integration guide for details