Upload README with documentation
Browse files
README.md
CHANGED
|
@@ -1,26 +1,117 @@
|
|
| 1 |
-
--
|
| 2 |
-
tags:
|
| 3 |
-
- ml-intern
|
| 4 |
-
---
|
| 5 |
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
## Usage
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 24 |
```
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# EyeGuard 20-20-20 ποΈ
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
A robust computer-vision application that enforces the **20-20-20 rule** for eye health β with **anti-spoofing** protection against one-eye-closed tricks.
|
| 4 |
|
| 5 |
+
## The 20-20-20 Rule
|
| 6 |
+
- Every **20 minutes** of screen time
|
| 7 |
+
- Look at something **20 feet away**
|
| 8 |
+
- For at least **20 seconds**
|
| 9 |
|
| 10 |
+
## Features
|
| 11 |
|
| 12 |
+
### Core Functionality
|
| 13 |
+
- **Real-time webcam eye tracking** using MediaPipe Face Mesh (468 landmarks)
|
| 14 |
+
- **Per-eye EAR (Eye Aspect Ratio) calculation** β each eye tracked independently
|
| 15 |
+
- **20-minute timer** with visual countdown
|
| 16 |
+
- **20-second rest enforcement** β both eyes must stay closed
|
| 17 |
+
- **Blink filtering** β brief blinks (< 0.5s) don't count as rest
|
| 18 |
+
|
| 19 |
+
### Anti-Spoofing / Anti-Cheating
|
| 20 |
+
- **One-eye-closed trick = DETECTED** β requires BOTH eyes closed simultaneously for the full 20 seconds
|
| 21 |
+
- **Face disappearance detection** β if the face leaves the camera during rest, the rest attempt fails
|
| 22 |
+
- **Hysteresis thresholds** β prevents flickering between open/closed states
|
| 23 |
+
- **Temporal smoothing** β median filter over 5 frames for stable EAR readings
|
| 24 |
+
- **Geometric verification** β uses facial landmark geometry, not just single-frame classification
|
| 25 |
+
|
| 26 |
+
### State Machine
|
| 27 |
+
1. **SCREENING** β Normal work, counting down 20 minutes
|
| 28 |
+
2. **ALERT** β Time for a break! Flashing red banner asks user to close both eyes
|
| 29 |
+
3. **RESTING** β Both eyes confirmed closed, counting 20 seconds
|
| 30 |
+
4. **REST_COMPLETE** β Break finished successfully, timer resets
|
| 31 |
+
5. **SPOOFING** β One-eye trick detected! 3-second penalty before retry
|
| 32 |
+
6. **NO_FACE** β Face not visible, waiting for user
|
| 33 |
+
|
| 34 |
+
## Requirements
|
| 35 |
+
|
| 36 |
+
```bash
|
| 37 |
+
pip install opencv-python-headless mediapipe==0.10.13 numpy
|
| 38 |
+
```
|
| 39 |
|
| 40 |
## Usage
|
| 41 |
|
| 42 |
+
### Basic Usage (default webcam)
|
| 43 |
+
```bash
|
| 44 |
+
python eye_guard_2020.py
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
### Different Camera
|
| 48 |
+
```bash
|
| 49 |
+
python eye_guard_2020.py --camera 1
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
### Adjust Sensitivity
|
| 53 |
+
```bash
|
| 54 |
+
python eye_guard_2020.py --ear-open 0.18 --ear-closed 0.12
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
### Key Controls (during runtime)
|
| 58 |
+
- `q` β Quit
|
| 59 |
+
- `r` β Reset the 20-minute timer
|
| 60 |
+
- `t` β Test mode (force alert immediately)
|
| 61 |
+
|
| 62 |
+
## How It Works
|
| 63 |
+
|
| 64 |
+
### Eye Aspect Ratio (EAR)
|
| 65 |
+
```
|
| 66 |
+
EAR = (||P2-P6|| + ||P3-P5||) / (2 * ||P1-P4||)
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
**Open eye**: EAR β 0.25β0.35
|
| 70 |
+
**Closed eye**: EAR β 0.05β0.15
|
| 71 |
+
|
| 72 |
+
### Hysteresis
|
| 73 |
+
- Eye is **closed** only when EAR drops below `ear_closed` (default 0.18)
|
| 74 |
+
- Eye is **open** only when EAR rises above `ear_open` (default 0.22)
|
| 75 |
+
- This 0.04 gap provides stable state transitions
|
| 76 |
+
|
| 77 |
+
### Blink Rejection
|
| 78 |
+
A closure must last at least **15 consecutive frames** (~0.5s at 30fps) to count as "resting". Normal blinks (100β300ms) are filtered out.
|
| 79 |
+
|
| 80 |
+
### One-Eye Trick Detection
|
| 81 |
+
During RESTING, if either eye re-opens while the other stays closed:
|
| 82 |
+
1. Status immediately switches to **SPOOFING**
|
| 83 |
+
2. The rest attempt is marked as **failed**
|
| 84 |
+
3. A 3-second penalty is enforced
|
| 85 |
+
4. The user must start the 20-second rest over
|
| 86 |
+
|
| 87 |
+
## Testing
|
| 88 |
|
| 89 |
+
```bash
|
| 90 |
+
python test_eyeguard_synthetic.py
|
|
|
|
| 91 |
```
|
| 92 |
|
| 93 |
+
Tests cover:
|
| 94 |
+
1. β
Normal rest cycle (both eyes closed)
|
| 95 |
+
2. β
Spoofing detection (one eye closed)
|
| 96 |
+
3. β
Blink filtering (brief closures ignored)
|
| 97 |
+
4. β
Rest interruption (early opening)
|
| 98 |
+
5. β
No-face detection during rest
|
| 99 |
+
6. β
One-eye trick during alert phase
|
| 100 |
+
|
| 101 |
+
## Why This Approach?
|
| 102 |
+
|
| 103 |
+
Our geometric approach **cannot be fooled by closing one eye** because it computes EAR for each eye independently and enforces an **AND gate** β both must be closed. This is fundamentally more robust than deep-learning classifiers that operate on whole-face images and can be confused by partial closures.
|
| 104 |
+
|
| 105 |
+
## Parameters
|
| 106 |
+
|
| 107 |
+
| Parameter | Default | Description |
|
| 108 |
+
|-----------|---------|-------------|
|
| 109 |
+
| `ear_open` | 0.22 | EAR threshold to mark eye as open |
|
| 110 |
+
| `ear_closed` | 0.18 | EAR threshold to mark eye as closed |
|
| 111 |
+
| `SCREEN_TIME_ALERT_SECONDS` | 1200 | 20 minutes in seconds |
|
| 112 |
+
| `REST_DURATION_SECONDS` | 20 | Rest duration in seconds |
|
| 113 |
+
| `MIN_CONSECUTIVE_CLOSED_FRAMES` | 15 | Minimum frames (~0.5s) to count as rest start |
|
| 114 |
+
|
| 115 |
+
## License
|
| 116 |
+
|
| 117 |
+
MIT β free for personal and commercial use. Protect your eyes! π
|