driver-paddy / app.py
Testimony Adekoya
WIP: Quick demo for project
e65b3b4
raw
history blame
6.33 kB
# main.py
import os
import sys
from gradio_interface import GradioWebRTCInterface
from dotenv import load_dotenv
load_dotenv()
def check_opencv_installation():
"""Check if OpenCV is properly installed with required cascades"""
try:
import cv2
# Check for required cascade files
cascade_files = [
'haarcascade_frontalface_default.xml',
'haarcascade_eye.xml',
'haarcascade_smile.xml'
]
missing_cascades = []
for cascade in cascade_files:
cascade_path = cv2.data.haarcascades + cascade
if not os.path.exists(cascade_path):
missing_cascades.append(cascade)
if missing_cascades:
print(f"❌ Missing OpenCV cascade files: {missing_cascades}")
print("πŸ’‘ Please reinstall OpenCV: pip install opencv-python")
return False
print("βœ… OpenCV and required cascade files found!")
return True
except ImportError:
print("❌ OpenCV not found. Please install: pip install opencv-python")
return False
def check_optional_dependencies():
"""Check for optional dependencies and provide info"""
optional_deps = {
'mediapipe': 'Enhanced facial landmark detection',
'google.generativeai': 'AI-powered voice alerts',
'scipy': 'Advanced mathematical computations'
}
available = []
missing = []
for dep, description in optional_deps.items():
try:
__import__(dep)
available.append(f"βœ… {dep} - {description}")
except ImportError:
missing.append(f"βšͺ {dep} - {description}")
if available:
print("πŸ“¦ Available optional features:")
for item in available:
print(f" {item}")
if missing:
print("πŸ“¦ Optional features (install for enhanced functionality):")
for item in missing:
print(f" {item}")
def main():
"""Main entry point"""
print("πŸš— Starting AI Driver Drowsiness Detection System...")
print("πŸ”§ Using OpenCV-based detection (no external model downloads required)")
if not check_opencv_installation():
sys.exit(1)
check_optional_dependencies()
print("\nπŸš€ All core requirements satisfied!")
# Create and launch interface
try:
interface_manager = GradioWebRTCInterface()
demo = interface_manager.create_interface()
print("🌐 Launching Gradio interface...")
print("πŸ“± The interface will be available in your browser")
print("πŸ”— A public link will be generated for sharing")
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=True,
show_error=True,
enable_queue=True,
max_threads=10,
favicon_path=None
)
except Exception as e:
print(f"❌ Error launching interface: {e}")
print("πŸ’‘ Try running: pip install --upgrade gradio")
sys.exit(1)
if __name__ == "__main__":
main()
# requirements.txt
"""
opencv-python>=4.5.0
gradio>=4.0.0
numpy>=1.21.0
scipy>=1.7.0
google-generativeai>=0.3.0
mediapipe>=0.10.0 # Optional for enhanced detection
"""
# README.md
"""
# πŸš— AI Driver Drowsiness Detection System
A real-time drowsiness detection system using computer vision and AI-powered alerts.
## ✨ Features
- **No External Downloads**: Uses OpenCV's built-in face detection models
- **Real-time Processing**: WebRTC streaming for low latency
- **Multi-modal Detection**: Eyes, mouth, and head pose analysis
- **AI Voice Alerts**: Contextual messages powered by Gemini AI
- **Adaptive System**: Graceful fallback without external dependencies
- **Easy Setup**: Simple pip install, no model downloads required
## πŸš€ Quick Start
1. **Install dependencies:**
```bash
pip install opencv-python gradio numpy scipy google-generativeai
# Optional for enhanced detection:
pip install mediapipe
```
2. **Run the system:**
```bash
python main.py
```
3. **Open browser** and navigate to the provided URL
4. **Optional**: Enter Gemini API key for AI-powered voice alerts
## πŸ”§ How It Works
### Detection Methods
- **Primary**: MediaPipe Face Mesh (if available) for precise landmarks
- **Fallback**: OpenCV Haar Cascades for basic face/eye/mouth detection
- **Hybrid Approach**: Automatically selects best available method
### Drowsiness Indicators
- **Eye Aspect Ratio (EAR)**: Detects eye closure patterns
- **Mouth Aspect Ratio (MAR)**: Identifies yawning behavior
- **Head Pose**: Tracks head nodding and position
### Alert System
- **AI-Generated**: Contextual messages via Gemini
- **Audio Alerts**: Attention-grabbing beep patterns
- **Visual Feedback**: Real-time overlay on video stream
- **Smart Cooldown**: Prevents alert spam
## βš™οΈ Configuration
### Detection Thresholds
- **EAR Threshold**: 0.20 (adjustable for sensitivity)
- **MAR Threshold**: 0.8 (calibrated for yawn detection)
- **Head Nod**: 20Β° deviation threshold
- **Alert Cooldown**: 8 seconds between alerts
### Performance Optimization
- **Stream Rate**: 10 FPS processing (configurable)
- **Queue Management**: Prevents frame backlog
- **Multi-threading**: Separate processing pipeline
- **Graceful Degradation**: Maintains functionality with limited resources
## πŸ›‘οΈ Safety Notice
**This system is for demonstration and research purposes only.**
- Not a substitute for responsible driving practices
- Always pull over safely if feeling drowsy
- Use as supplementary tool alongside other safety measures
- Ensure proper camera setup and lighting
## πŸ“‹ System Requirements
- **Python**: 3.7+
- **Camera**: Webcam or built-in camera
- **OS**: Windows, macOS, Linux
- **RAM**: 4GB+ recommended
- **CPU**: Multi-core recommended for real-time processing
## πŸ” Troubleshooting
- **No face detected**: Check lighting and camera position
- **Poor detection**: Ensure face is clearly visible and well-lit
- **High CPU usage**: Reduce stream rate or video resolution
- **Audio issues**: Check browser permissions and audio settings
## πŸ“ License
MIT License - See LICENSE file for details
"""