File size: 7,878 Bytes
f568ccb
701628b
 
 
 
f568ccb
 
701628b
f568ccb
 
701628b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
---
title: AttendanceFaceRecognition
emoji: 🐠
colorFrom: green
colorTo: pink
sdk: docker
pinned: false
short_description: Face recognition attendance for One Step Greener
---

# One Step Greener – Face Recognition Attendance

A web-based **face recognition attendance system** for waste management teams. Employees and field workers (manforce) check in and out using their faceβ€”no cards or PINs. The app includes **anti-spoofing** (liveness detection) to block photos, screens, and replay attacks.

---

## What it does

- **Register** users by capturing their face (with live guidance: position, size, centering). Supports **employees** (by employee ID) and **manforce** (by Aadhaar, name, mobile).
- **Attendance** punch in/out via webcam: first scan of the day = punch in, next = punch out. One-minute cooldown between punches.
- **Dashboard** shows today’s attendance (punch-in and punch-out times) and quick links to Attendance and Register.
- **Liveness checks** during registration and recognition to reject printed photos, phone screens, and video replays (texture, motion, blink, and other cues).

---

## Features

| Feature | Description |
|--------|-------------|
| **Face registration** | Multi-frame capture with real-time feedback (face detected, centered, big enough). Optional PIN to unlock the Register page. |
| **Face recognition** | Match live face to stored embeddings (FAISS + 512-d FaceNet). Returns name, punch type (in/out), timestamp. |
| **Anti-spoofing** | Multi-layer checks: LBP texture, MoirΓ©/FFT, color, edges, specular, central-difference; plus motion and blink for sequences. |
| **User types** | **Employee**: ID + optional name. **Manforce**: Aadhaar, full name, mobile. |
| **Duplicate prevention** | Same face cannot be registered for two different people. |
| **Cooldown** | 1-minute cooldown per user between punches to avoid double taps. |
| **Today’s view** | Today’s attendance list with first punch-in and last punch-out per person. |

---

## Tech stack

- **Backend:** Flask (Python 3.10)
- **Face detection & embeddings:** MTCNN + InceptionResnetV1 (VGGFace2) via `facenet-pytorch`
- **Embedding search:** FAISS (L2 index, cosine similarity)
- **Anti-spoofing:** Custom pipeline (LBP, FFT/MoirΓ©, color, edges, specular, CDCN-style; MediaPipe for blink)
- **Database:** SQLite (`employees`, `attendance` tables)
- **Frontend:** HTML/CSS/JS, camera capture via browser

---

## Project structure

```
.
β”œβ”€β”€ app.py                 # Flask app, routes, API handlers
β”œβ”€β”€ requirements.txt       # Python dependencies
β”œβ”€β”€ Dockerfile             # Docker image for HF Spaces (port 7860)
β”œβ”€β”€ database/
β”‚   β”œβ”€β”€ db.py              # SQLite helpers (employees, attendance)
β”‚   β”œβ”€β”€ constable.db       # SQLite DB (created at runtime)
β”‚   β”œβ”€β”€ face_index.faiss   # FAISS index (created at runtime)
β”‚   └── face_meta.json     # FAISS ID β†’ employee_id mapping
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ face_engine.py     # MTCNN + InceptionResnetV1, decode/crop/embed
β”‚   β”œβ”€β”€ embeddings_store.py # FAISS wrapper, add/search
β”‚   └── anti_spoof.py      # Liveness (single frame + sequence)
β”œβ”€β”€ static/
β”‚   β”œβ”€β”€ css/style.css
β”‚   β”œβ”€β”€ js/
β”‚   β”‚   β”œβ”€β”€ camera.js      # Shared camera logic
β”‚   β”‚   β”œβ”€β”€ register.js    # Registration flow + face-check
β”‚   β”‚   └── attendance.js  # Recognition + punch
β”‚   └── images/
└── templates/
    β”œβ”€β”€ base.html
    β”œβ”€β”€ dashboard.html     # Home: Attendance + Register links
    β”œβ”€β”€ register.html      # Enroll employee / manforce
    └── attendance.html    # Punch in/out by face
```

---

## Prerequisites

- **Python 3.10** (or 3.8+)
- **Camera** for registration and attendance (browser will request access)
- **Optional:** GPU for faster face models (CUDA); runs on CPU otherwise

---

## Installation

### 1. Clone and enter the project

```bash
git clone <your-repo-url>
cd hf-space
```

### 2. Create a virtual environment (recommended)

```bash
python3 -m venv venv
source venv/bin/activate   # Linux/macOS
# or: venv\Scripts\activate  # Windows
```

### 3. Install dependencies

```bash
pip install -r requirements.txt
```

On Linux, OpenCV and other libs may need system packages:

```bash
# Debian/Ubuntu
sudo apt-get update
sudo apt-get install -y libgl1-mesa-glx libglib2.0-0 libsm6 libxext6 libxrender-dev
```

---

## Configuration

| Variable | Description | Default |
|----------|-------------|--------|
| `PORT` | HTTP port | `5000` (local) / `7860` (Docker/HF Spaces) |
| `SECRET_KEY` | Flask secret key | `constable-secret-2025` |
| `REGISTER_PIN` | PIN to unlock Register page | `3620` |
| `FLASK_DEBUG` | Set to `1` for debug mode | `0` |

Example:

```bash
export REGISTER_PIN=1234
export PORT=5000
```

---

## Running the app

### Local (development)

```bash
python app.py
```

Then open **http://localhost:5000** (or the port you set). You should see the dashboard with **Attendance** and **Register**.

### Docker (e.g. Hugging Face Spaces)

The Dockerfile is set up for **Hugging Face Spaces** (port **7860**):

```bash
docker build -t attendance-face .
docker run -p 7860:7860 attendance-face
```

Open **http://localhost:7860**.

---

## Usage instructions

### Dashboard (`/` or `/dashboard`)

- **Attendance** – Open the attendance page to punch in/out with your face.
- **Register** – Open the registration page (optionally enter a PIN if set).

### Register (`/register`)

1. Optionally enter the **Register PIN** (default `3620`) to unlock the form.
2. Choose **Employee** or **Manforce**:
   - **Employee:** Enter Employee ID (and optional name). Submit with face capture.
   - **Manforce:** Enter Aadhaar, full name, and mobile. Submit with face capture.
3. Allow camera access. Position your face in the oval; wait until the indicator shows **Ready** (face detected, centered, big enough).
4. Capture multiple frames when prompted. The app runs **liveness checks** (e.g. motion, blink); do not use a photo or screen.
5. On success, the person is stored in the DB and their face embeddings are added to the FAISS index. You can then use **Attendance** to punch in/out.

### Attendance (`/attendance`)

1. Open the Attendance page and allow camera access.
2. Look at the camera. The app will:
   - Detect your face and run **liveness** (single frame or sequence).
   - Match your face to the stored embeddings.
   - If matched: **first punch of the day** = punch **in**, **next** = punch **out** (with a 1-minute cooldown between punches).
3. You’ll see your name, punch type (in/out), and time. Today’s attendance is available from the dashboard.

### API (for integration)

| Endpoint | Method | Purpose |
|----------|--------|--------|
| `/api/face-check` | POST | Check if a frame has a valid face (centered, big enough). Body: `{ "frame": "<base64DataUrl>" }`. |
| `/api/register` | POST | Register employee or manforce. Body: `user_type`, `frames`, and either `employee_id` or `aadhaar`+`name`+`mobile`. |
| `/api/recognize` | POST | Recognize face and punch in/out. Body: `{ "frame": "..." }` or `{ "frames": ["...", ...] }`. |
| `/api/verify-pin` | POST | Verify Register PIN. Body: `{ "pin": "3620" }`. |
| `/api/employees` | GET | List all employees. |
| `/api/attendance/today` | GET | Today’s attendance records. |
| `/api/health` | GET | Health check + total indexed faces. |

---

## Notes

- **First run:** The app creates `database/constable.db`, `face_index.faiss`, and `face_meta.json` on first use. No manual DB setup required.
- **Hugging Face Spaces:** Use the Dockerfile and set the Space to use **Docker** and port **7860**.
- **Security:** Set `SECRET_KEY` and `REGISTER_PIN` in production; avoid default PIN in production.

---

## License

See repository license (if any).