File size: 3,203 Bytes
47ff6b2
 
 
 
 
 
 
 
 
 
 
1379d8e
47ff6b2
 
 
 
 
 
 
 
 
 
 
 
1379d8e
 
 
47ff6b2
 
1379d8e
47ff6b2
1379d8e
47ff6b2
1379d8e
47ff6b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1379d8e
 
47ff6b2
1379d8e
47ff6b2
 
 
 
 
1379d8e
 
47ff6b2
 
 
 
 
 
 
 
1379d8e
47ff6b2
 
 
 
 
1379d8e
47ff6b2
1379d8e
47ff6b2
 
1379d8e
47ff6b2
 
1379d8e
 
47ff6b2
 
 
 
 
1379d8e
47ff6b2
 
 
 
 
1379d8e
d411985
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
---
title: EduSync Face Recognition
emoji: 👤
colorFrom: indigo
colorTo: purple
sdk: docker
sdk_version: "20.10.12"
python_version: "3.13"
app_file: run.py
pinned: false
---

# Face Recognition Attendance Service

Production-ready face recognition API using FastAPI, MediaPipe, and InsightFace (ArcFace ONNX).
Optimized for CPU usage and fast inference (<100ms per face).

## Architecture

- **FastAPI**: High-performance async web framework.
- **MediaPipe**: Lightweight, real-time 468-point 3D Face Mesh for precise 6-point detection & alignment.
- **InsightFace (ArcFace)**: State-of-the-art face recognition model (ResNet50 or MobileFaceNet) running on ONNX Runtime.
- **MongoDB**: For storing students and embeddings.
- **Docker**: Containerized deployment.

## Prerequisites

- Python 3.9+
- MongoDB instance (Local or Atlas)

## Setup

1. **Install Dependencies**
   ```bash
   pip install -r requirements.txt
   ```

2. **Download Model**
   The application requires the ArcFace ONNX model (`w600k_mbf.onnx` or `w600k_r50.onnx`).
   Run the setup script (if provided) or download manually:
   - Create directory `app/models/`
   - Download `w600k_mbf.onnx` (MobileFaceNet, ~3MB) or `w600k_r50.onnx` (ResNet50, ~170MB).
   - Update `.env` with the correct `MODEL_PATH`.
   
   *Tip: You can extract `w600k_mbf.onnx` from the `buffalo_s.zip` in InsightFace model zoo.*

3. **Configure Environment**
   Rename `.env.example` (if exists) or create `.env`:
   ```ini
   MONGODB_URL=mongodb://localhost:27017
   DB_NAME=sih_attendance_db
   COLLECTION_NAME=student_faces
   FACE_SIMILARITY_THRESHOLD=0.5
   MODEL_PATH=app/models/w600k_mbf.onnx
   ```

4. **Run Server**
   ```bash
   python run.py
   ```
   Or directly with Uvicorn:
   ```bash
   uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
   ```

## API Endpoints

### 1. Register Student
**POST** `/api/v1/register`
- **Form Data**:
  - `student_id`: String
  - `files`: List of images (5-10 recommended)
- **Process**: Detects faces, averages embeddings, saves to DB.

### 2. Mark Attendance
**POST** `/api/v1/mark-attendance`
- **Form Data**:
  - `file`: Single image
- **Process**: Detects face, compares with DB, returns student if confidence > threshold.

## Deployment (Render/Docker)

### Dockerfile
Use the provided `Dockerfile` to build the image.
```bash
docker build -t face-api .
docker run -p 8000:8000 face-api
```

### Render.com
1. Connect Repo.
2. Select Docker runtime.
3. Set Environment Variables (`MONGODB_URL`).
4. Ensure the model file is included in the repo or downloaded during build (e.g., via `wget` in Dockerfile).

## Performance Notes
- **Resize Strategy**: Images are resized to 320x240 for detection speed.
- **Global Model**: ONNX session is initialized once at startup.
- **Concurrency**: FastAPI handles async requests; CPU-bound inference runs in thread pool (default FastAPI behavior for sync routes) or you can offload to `run_in_threadpool`.
  *Current implementation uses standard async def but blocking calls for inference. For high load, consider `def` routes or `await run_in_threadpool`.*

Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference