A comprehensive real-time traffic light detection system that uses OpenCV and HSV color segmentation to detect Red, Yellow, and Green traffic lights from webcam feeds, video files, or static images. The system features both a command-line interface and a modern web-based Streamlit interface.
- Multi-color detection: Robust HSV-based detection for Red, Yellow, and Green traffic lights
- Adaptive preprocessing: Gray-world white balance, auto gamma correction, and CLAHE enhancement
- Advanced filtering: Morphology operations, contour filtering, and brightness/saturation gates
- Smart tracking: IoU-based track management with confidence smoothing and lock-on detection
- False positive reduction: Heuristics to distinguish traffic lights from vehicle lights
- Dynamic scaling: Automatically adjusts parameters based on input resolution
- ๐ Web Interface: Modern Streamlit-based web app with real-time processing
- ๐ฅ๏ธ Desktop UI: OpenCV-based GUI for local desktop usage
- โจ๏ธ Command Line: Direct CLI access for automation and scripting
- ๐ท Webcam: Real-time detection from camera feeds
- ๐น Video Files: Process MP4, AVI, MOV, MKV, and other video formats
- ๐ผ๏ธ Images: Single image processing with JPG, PNG, BMP, TIFF support
- ๐ Sample Images: Pre-loaded test images for demonstration
Launch the modern Streamlit web interface:
./run_streamlit.shThen open your browser to http://localhost:8501 and enjoy the interactive interface!
Run the OpenCV-based desktop interface:
./run_detector.shFor direct CLI usage:
# Webcam detection
python traffic_light_detector.py --source 0 --output
# Video file processing
python traffic_light_detector.py -s "/path/to/video.mp4" -o
# Image processing
python traffic_light_detector.py -s "/path/to/image.jpg" -i -oThe provided shell scripts handle everything automatically:
# For web interface
./run_streamlit.sh
# For desktop interface
./run_detector.shIf you prefer manual installation:
# Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -U pip
pip install -r requirements.txt- opencv-python-headless (โฅ 4.8.0): Computer vision processing
- numpy (โฅ 1.20.0): Numerical computations
- streamlit (โฅ 1.28.0): Web interface framework
- Pillow (โฅ 8.0.0): Image processing support
- Input Methods: Upload images/videos, use webcam, or try sample images
- Detection Settings: Adjust confidence threshold and debug visualization
- Real-time Stats: View detection counts and performance metrics
- Debug Mode: Toggle color mask visualization for troubleshooting
- Menu Navigation: Use number keys (1-4) to select options
- Video Controls: Press
qto quit,dto toggle debug masks - Output Options: Choose whether to save processed videos/images
python traffic_light_detector.py [OPTIONS]
Options:
-s, --source SOURCE Camera index (0), video file path, or image file path
-o, --output Save processed video/image to file
-i, --image Process as image file instead of video/camera
-h, --help Show help messageTraffic-Lights-Tracking-and-Color-Detection-OpenCV/
โโโ app.py # Streamlit web application
โโโ traffic_light_detector.py # Core detection engine
โโโ simple_ui.py # OpenCV desktop interface
โโโ requirements.txt # Python dependencies
โโโ packages.txt # System packages (Linux)
โโโ run_streamlit.sh # Web app launcher script
โโโ run_detector.sh # Desktop app launcher script
โโโ sample_images/ # Test images
โ โโโ sample_red_light.jpg
โ โโโ sample_yellow_light.jpg
โ โโโ sample_green_light.jpg
โ โโโ sample_all_lights.jpg
โ โโโ sample_multiple_lights.jpg
โ โโโ sample_night_scene.jpg
โ โโโ sample_challenging_scene.jpg
โโโ venv/ # Virtual environment (created automatically)
- Preprocessing: White-balance correction, gamma adjustment, Gaussian blur
- Color Segmentation: HSV-based masking for Red, Yellow, and Green ranges
- Morphology: Closing and opening operations to clean up masks
- Contour Analysis: Size, aspect ratio, circularity, and brightness filtering
- Confidence Scoring: Geometry, color purity, and brightness-based scoring
- Tracking: IoU-based association with temporal smoothing
- Classification: Traffic light vs vehicle light heuristics
- Dynamic Scaling: Parameters adjust automatically based on input resolution
- Multi-threading: Efficient processing for real-time performance
- Memory Optimization: Minimal memory footprint for long-running sessions
- Error Handling: Robust error recovery and user feedback
No camera feed / black window
- Check camera permissions and availability
- Try different camera indices (0, 1, 2, etc.)
- Ensure no other applications are using the camera
Could not open video source
- Verify file path and format support
- Try absolute paths instead of relative paths
- Check file permissions and corruption
Low detection accuracy
- Ensure adequate lighting conditions
- Try adjusting confidence threshold
- Use debug mode to verify color mask generation
- Point camera at larger, clearer traffic signals
Performance issues
- Close other resource-intensive applications
- Reduce input resolution if possible
- Consider using image processing instead of video for testing
Enable debug visualization to see:
- Color segmentation masks
- Contour detection results
- Brightness and saturation filtering
- Final detection bounding boxes
The project includes several sample images for testing:
- ๐ด Red Light: Various red traffic light scenarios
- ๐ก Yellow Light: Yellow/amber light detection
- ๐ข Green Light: Green traffic light examples
- ๐ฆ All Lights: Multiple traffic lights in one scene
- ๐๏ธ Multiple Lights: Complex urban traffic scenarios
- ๐ Night Scene: Low-light detection challenges
- ๐ฏ Challenging Scene: Difficult detection scenarios
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
This project is open source. Please check the repository for specific licensing terms.
Built with:
- OpenCV: Computer vision processing
- Streamlit: Web interface framework
- NumPy: Numerical computations
- Pillow: Image processing
Thanks to the open-source community for tools and prior art on HSV-based traffic light detection.