Skip to main content

Overview

Proper camera configuration is crucial for accurate waste detection. Both TrashDetect.py and main.py use identical camera settings optimized for the YOLOv8 model.

Default Camera Settings

Both applications use these standard settings:
cap = cv2.VideoCapture(0)
cap.set(3, 1280)  # Width
cap.set(4, 720)   # Height
Resolution is set to 1280x720 (720p HD) for optimal balance between detection accuracy and processing speed.

OpenCV Camera Properties

Understanding cap.set() Parameters

OpenCV uses numeric property IDs for camera configuration:
Property IDNameDefault ValueDescription
3cv2.CAP_PROP_FRAME_WIDTH1280Frame width in pixels
4cv2.CAP_PROP_FRAME_HEIGHT720Frame height in pixels
5cv2.CAP_PROP_FPS(auto)Frames per second
10cv2.CAP_PROP_BRIGHTNESS(auto)Camera brightness
11cv2.CAP_PROP_CONTRAST(auto)Camera contrast
12cv2.CAP_PROP_SATURATION(auto)Color saturation

Code Implementation

# Cap
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
The GUI uses cv2.CAP_DSHOW on Windows for DirectShow support, providing better camera compatibility and performance.

Selecting Camera Device

Camera Index

The camera index specifies which camera to use:
cap = cv2.VideoCapture(0)  # Default: first camera
Common camera indices:
  • 0: Built-in webcam or first USB camera
  • 1: Second USB camera
  • 2: Third USB camera
  • And so on…

Finding Available Cameras

Use this script to detect all available cameras:
find_cameras.py
import cv2

def list_cameras():
    """Test camera indices 0-10 to find available cameras"""
    available = []
    for i in range(10):
        cap = cv2.VideoCapture(i)
        if cap.isOpened():
            ret, frame = cap.read()
            if ret:
                available.append(i)
                print(f"Camera {i}: Available")
                print(f"  Resolution: {cap.get(3)}x{cap.get(4)}")
                print(f"  FPS: {cap.get(5)}")
            cap.release()
    return available

if __name__ == "__main__":
    print("Scanning for cameras...")
    cameras = list_cameras()
    print(f"\nFound {len(cameras)} camera(s): {cameras}")
1

Create the script

Save the code above as find_cameras.py in your project directory.
2

Run the scanner

python find_cameras.py
3

Update your code

Use the detected camera index in TrashDetect.py or main.py:
cap = cv2.VideoCapture(1)  # Use detected index

Resolution Settings

Why 1280x720?

The default resolution balances several factors:

Detection Accuracy

Higher resolution provides:
  • More pixel data for YOLO
  • Better small object detection
  • Improved edge definition

Processing Speed

720p offers:
  • Real-time performance on most hardware
  • Efficient memory usage
  • Smooth frame rates

Alternative Resolutions

Adjust based on your hardware and requirements:
For faster processing on limited hardware:
cap.set(3, 640)   # Width
cap.set(4, 480)   # Height (480p)
Pros:
  • Faster processing
  • Lower CPU/GPU usage
  • Higher frame rates
Cons:
  • Reduced detection accuracy for small objects
  • Less detail in video feed

Platform-Specific Configuration

Windows

Use DirectShow backend for better camera support:
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
cap.set(3, 1280)
cap.set(4, 720)
DirectShow (cv2.CAP_DSHOW) reduces camera initialization time and improves compatibility with USB webcams on Windows.

Linux

Use default V4L2 backend:
cap = cv2.VideoCapture(0)  # V4L2 is default on Linux
cap.set(3, 1280)
cap.set(4, 720)
Verify camera support:
v4l2-ctl --list-devices
v4l2-ctl --device=/dev/video0 --list-formats-ext

macOS

Use AVFoundation backend:
cap = cv2.VideoCapture(0, cv2.CAP_AVFOUNDATION)
cap.set(3, 1280)
cap.set(4, 720)

Advanced Camera Settings

Adjusting Brightness and Contrast

For low-light or high-glare environments:
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)

# Adjust image quality
cap.set(cv2.CAP_PROP_BRIGHTNESS, 150)  # Range: 0-255
cap.set(cv2.CAP_PROP_CONTRAST, 50)     # Range: 0-255
cap.set(cv2.CAP_PROP_SATURATION, 75)   # Range: 0-255

Setting Frame Rate

Control capture frame rate:
cap.set(cv2.CAP_PROP_FPS, 30)  # Set to 30 FPS

# Verify actual FPS
actual_fps = cap.get(cv2.CAP_PROP_FPS)
print(f"Camera FPS: {actual_fps}")
Not all cameras support manual FPS control. The camera will use its default rate if the setting is not supported.

Auto-Focus and Exposure

# Disable auto-focus for stable detection
cap.set(cv2.CAP_PROP_AUTOFOCUS, 0)
cap.set(cv2.CAP_PROP_FOCUS, 50)  # Manual focus (0-255)

# Adjust exposure
cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0.25)  # Manual mode
cap.set(cv2.CAP_PROP_EXPOSURE, -5)         # Exposure value

Verification Script

Test and verify your camera configuration:
test_camera.py
import cv2

def test_camera_config():
    """Test camera settings and display current configuration"""
    cap = cv2.VideoCapture(0)
    
    # Set desired resolution
    cap.set(3, 1280)
    cap.set(4, 720)
    
    # Check actual values
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    fps = cap.get(cv2.CAP_PROP_FPS)
    
    print("Camera Configuration:")
    print(f"  Resolution: {int(width)}x{int(height)}")
    print(f"  FPS: {fps}")
    print(f"  Backend: {cap.getBackendName()}")
    
    # Test frame capture
    ret, frame = cap.read()
    if ret:
        print(f"  Frame shape: {frame.shape}")
        print("  Status: Camera working correctly!")
        
        # Display test frame
        cv2.imshow("Camera Test", frame)
        print("\nPress any key to close...")
        cv2.waitKey(0)
    else:
        print("  Status: Failed to capture frame")
    
    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    test_camera_config()

Optimal Detection Conditions

Lighting

Good Lighting

  • Natural daylight or bright indoor lighting
  • Even illumination without harsh shadows
  • No direct glare on camera lens
  • Consistent light levels

Poor Lighting

  • Very dim or dark environments
  • Harsh backlighting (object in shadow)
  • Flickering lights (fluorescent)
  • Direct sunlight causing lens flare

Camera Positioning

Recommended setup:
  • Distance: 30-100 cm from objects
  • Angle: Slightly above, looking down at ~30-45°
  • Stability: Mount camera or use stable surface
  • Field of view: Ensure entire object visible in frame

Environment

Use a clean, uncluttered background to improve detection accuracy. Busy backgrounds can cause false positives.

Troubleshooting Camera Issues

If camera doesn’t use requested resolution:
  1. Check camera capabilities:
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)

actual_w = cap.get(3)
actual_h = cap.get(4)
print(f"Requested: 1280x720")
print(f"Actual: {actual_w}x{actual_h}")
  1. Try supported resolutions: 640x480, 800x600, 1280x720, 1920x1080
  2. Update camera drivers
Linux: Check permissions
ls -l /dev/video*
sudo usermod -a -G video $USER
# Logout and login again
Windows: Install camera drivers from manufacturermacOS: Grant camera permissions in System Preferences > Security & Privacy
To improve performance:
  1. Reduce resolution:
cap.set(3, 640)
cap.set(4, 480)
  1. Close other applications using camera
  2. Ensure GPU acceleration enabled for YOLOv8:
# Check CUDA availability
from ultralytics import YOLO
import torch
print(f"CUDA available: {torch.cuda.is_available()}")
  1. Use lower quality USB cameras (less compression overhead)
Color space or aspect ratio issues:
  1. Verify color conversion:
# For display in GUI
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  1. Maintain aspect ratio when resizing:
import imutils
frame = imutils.resize(frame, width=640)  # Preserves aspect ratio
If multiple applications access the same camera:
  1. Explicitly release camera:
cap.release()
cv2.destroyAllWindows()
  1. Use different camera indices for different applications
  2. Check running processes:
# Linux
lsof /dev/video0

# Windows
# Check Task Manager for other apps using camera

Performance Benchmarking

Measure your camera and detection performance:
benchmark.py
import cv2
import time
from ultralytics import YOLO

def benchmark_detection():
    """Measure FPS and processing time"""
    cap = cv2.VideoCapture(0)
    cap.set(3, 1280)
    cap.set(4, 720)
    
    model = YOLO('Modelos/best.pt')
    
    frame_count = 0
    start_time = time.time()
    
    print("Benchmarking... (30 seconds)")
    while time.time() - start_time < 30:
        ret, frame = cap.read()
        if ret:
            results = model(frame, stream=True, verbose=False)
            for res in results:
                pass  # Process results
            frame_count += 1
    
    elapsed = time.time() - start_time
    fps = frame_count / elapsed
    
    print(f"\nResults:")
    print(f"  Frames processed: {frame_count}")
    print(f"  Time elapsed: {elapsed:.2f}s")
    print(f"  Average FPS: {fps:.2f}")
    print(f"  Frame time: {1000/fps:.2f}ms")
    
    cap.release()

if __name__ == "__main__":
    benchmark_detection()

Next Steps

CLI Detection

Start using the command-line detection tool

GUI Application

Explore the full-featured graphical interface

Build docs developers (and LLMs) love