A production-ready FastAPI backend for real-time face verification and attendance tracking.
face_recognition_backend/
├── .venv/ # Virtual environment
├── employees/ # Employee photos (for training)
├── encode_faces.py # Generates face encodings
├── verify.py # Verify encodings file
├── main.py # FastAPI backend
├── encodings.pkl # Trained face encodings (generated)
├── attendance.csv # Attendance log (auto-generated)
└── requirements.txt # Dependencies
Your local environment is ready to run! Here's what's been set up:
- ✅ Virtual environment activated
- ✅ All dependencies installed
- ✅ Face encodings generated (6 employees)
- ✅ FastAPI server running
source .venv/bin/activatepython encode_faces.pyuvicorn main:app --reload --host 0.0.0.0 --port 8000Server is now running at: http://localhost:8000
curl http://localhost:8000Response:
{ "message": "Face Verification Attendance API is running." }curl http://localhost:8000/employeesResponse:
{
"employees": [
"albert-dera",
"aiony-haust",
"wellington-ferreira",
"jake-nackos",
"jurica-koletic",
"John-smith"
]
}curl -X POST http://localhost:8000/verify \
-F "file=@path/to/image.jpg"Success Response:
{
"status": "success",
"name": "John-smith",
"message": "Welcome, John-smith!"
}Error Response:
{
"status": "error",
"message": "No face detected."
}- Open Postman
- Create a new POST request to
http://localhost:8000/verify - Go to Body → form-data
- Add key:
file(type: File) - Upload one of the employee images
- Send request
Every successful face verification is logged to attendance.csv:
| Name | Time | Action |
|---|---|---|
| John-smith | 2025-10-31 14:22:15 | sign_in |
| albert-dera | 2025-10-31 14:25:30 | sign_in |
- Add their photo to
employees/folder (name it:firstname-lastname.jpg) - Re-run encoding:
python encode_faces.py
- Restart the server (it will auto-reload if using
--reloadflag)
FastAPI provides automatic interactive API docs:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- Deploy on Railway, Render, or Fly.io
- Update CORS origins in
main.pyto your frontend domain
Your React app should:
- Capture webcam photo or let user upload image
- Send POST request to
/verifyendpoint - Display success/error message
Example React fetch:
const formData = new FormData();
formData.append("file", imageFile);
const response = await fetch("http://localhost:8000/verify", {
method: "POST",
body: formData,
});
const result = await response.json();
console.log(result); // {status: "success", name: "John-smith", ...}# Check if port 8000 is in use
lsof -i :8000
# Kill process if needed
kill -9 <PID>- Ensure image has clear, front-facing face
- Check image quality and lighting
- Try adjusting
toleranceparameter inmain.py(line 78)
- Reduce tolerance:
tolerance=0.4(stricter) - Use better quality training images
- Ensure consistent lighting
Create a .env file:
ENCODINGS_FILE=encodings.pkl
ATTENDANCE_FILE=attendance.csv
TOLERANCE=0.5Built with:
- FastAPI
- face_recognition
- OpenCV
- Pandas
Ready for production! 🚀