Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -102,108 +102,3 @@ def main():
|
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
main()
|
| 105 |
-
|
| 106 |
-
# requirements.txt
|
| 107 |
-
"""
|
| 108 |
-
opencv-python>=4.5.0
|
| 109 |
-
gradio>=4.0.0
|
| 110 |
-
numpy>=1.21.0
|
| 111 |
-
scipy>=1.7.0
|
| 112 |
-
google-generativeai>=0.3.0
|
| 113 |
-
mediapipe>=0.10.0 # Optional for enhanced detection
|
| 114 |
-
"""
|
| 115 |
-
|
| 116 |
-
# README.md
|
| 117 |
-
"""
|
| 118 |
-
# π AI Driver Drowsiness Detection System
|
| 119 |
-
|
| 120 |
-
A real-time drowsiness detection system using computer vision and AI-powered alerts.
|
| 121 |
-
|
| 122 |
-
## β¨ Features
|
| 123 |
-
|
| 124 |
-
- **No External Downloads**: Uses OpenCV's built-in face detection models
|
| 125 |
-
- **Real-time Processing**: WebRTC streaming for low latency
|
| 126 |
-
- **Multi-modal Detection**: Eyes, mouth, and head pose analysis
|
| 127 |
-
- **AI Voice Alerts**: Contextual messages powered by Gemini AI
|
| 128 |
-
- **Adaptive System**: Graceful fallback without external dependencies
|
| 129 |
-
- **Easy Setup**: Simple pip install, no model downloads required
|
| 130 |
-
|
| 131 |
-
## π Quick Start
|
| 132 |
-
|
| 133 |
-
1. **Install dependencies:**
|
| 134 |
-
```bash
|
| 135 |
-
pip install opencv-python gradio numpy scipy google-generativeai
|
| 136 |
-
|
| 137 |
-
# Optional for enhanced detection:
|
| 138 |
-
pip install mediapipe
|
| 139 |
-
```
|
| 140 |
-
|
| 141 |
-
2. **Run the system:**
|
| 142 |
-
```bash
|
| 143 |
-
python main.py
|
| 144 |
-
```
|
| 145 |
-
|
| 146 |
-
3. **Open browser** and navigate to the provided URL
|
| 147 |
-
|
| 148 |
-
4. **Optional**: Enter Gemini API key for AI-powered voice alerts
|
| 149 |
-
|
| 150 |
-
## π§ How It Works
|
| 151 |
-
|
| 152 |
-
### Detection Methods
|
| 153 |
-
- **Primary**: MediaPipe Face Mesh (if available) for precise landmarks
|
| 154 |
-
- **Fallback**: OpenCV Haar Cascades for basic face/eye/mouth detection
|
| 155 |
-
- **Hybrid Approach**: Automatically selects best available method
|
| 156 |
-
|
| 157 |
-
### Drowsiness Indicators
|
| 158 |
-
- **Eye Aspect Ratio (EAR)**: Detects eye closure patterns
|
| 159 |
-
- **Mouth Aspect Ratio (MAR)**: Identifies yawning behavior
|
| 160 |
-
- **Head Pose**: Tracks head nodding and position
|
| 161 |
-
|
| 162 |
-
### Alert System
|
| 163 |
-
- **AI-Generated**: Contextual messages via Gemini
|
| 164 |
-
- **Audio Alerts**: Attention-grabbing beep patterns
|
| 165 |
-
- **Visual Feedback**: Real-time overlay on video stream
|
| 166 |
-
- **Smart Cooldown**: Prevents alert spam
|
| 167 |
-
|
| 168 |
-
## βοΈ Configuration
|
| 169 |
-
|
| 170 |
-
### Detection Thresholds
|
| 171 |
-
- **EAR Threshold**: 0.20 (adjustable for sensitivity)
|
| 172 |
-
- **MAR Threshold**: 0.8 (calibrated for yawn detection)
|
| 173 |
-
- **Head Nod**: 20Β° deviation threshold
|
| 174 |
-
- **Alert Cooldown**: 8 seconds between alerts
|
| 175 |
-
|
| 176 |
-
### Performance Optimization
|
| 177 |
-
- **Stream Rate**: 10 FPS processing (configurable)
|
| 178 |
-
- **Queue Management**: Prevents frame backlog
|
| 179 |
-
- **Multi-threading**: Separate processing pipeline
|
| 180 |
-
- **Graceful Degradation**: Maintains functionality with limited resources
|
| 181 |
-
|
| 182 |
-
## π‘οΈ Safety Notice
|
| 183 |
-
|
| 184 |
-
**This system is for demonstration and research purposes only.**
|
| 185 |
-
|
| 186 |
-
- Not a substitute for responsible driving practices
|
| 187 |
-
- Always pull over safely if feeling drowsy
|
| 188 |
-
- Use as supplementary tool alongside other safety measures
|
| 189 |
-
- Ensure proper camera setup and lighting
|
| 190 |
-
|
| 191 |
-
## π System Requirements
|
| 192 |
-
|
| 193 |
-
- **Python**: 3.7+
|
| 194 |
-
- **Camera**: Webcam or built-in camera
|
| 195 |
-
- **OS**: Windows, macOS, Linux
|
| 196 |
-
- **RAM**: 4GB+ recommended
|
| 197 |
-
- **CPU**: Multi-core recommended for real-time processing
|
| 198 |
-
|
| 199 |
-
## π Troubleshooting
|
| 200 |
-
|
| 201 |
-
- **No face detected**: Check lighting and camera position
|
| 202 |
-
- **Poor detection**: Ensure face is clearly visible and well-lit
|
| 203 |
-
- **High CPU usage**: Reduce stream rate or video resolution
|
| 204 |
-
- **Audio issues**: Check browser permissions and audio settings
|
| 205 |
-
|
| 206 |
-
## π License
|
| 207 |
-
|
| 208 |
-
MIT License - See LICENSE file for details
|
| 209 |
-
"""
|
|
|
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|