A FastAPI-based application that provides face recognition capabilities, including training face models, detecting faces in images, and real-time face recognition through a webcam.

-
Face Registration: Upload face images with names to train the recognition model
-
Face Detection: Detect and identify faces in uploaded images
-
Real-time Recognition: Detect and identify faces in real-time using a webcam
-
Model Management: View and delete trained face models
- Python 3.8+
- pip package manager
-
Clone the repository:
git clone https://github.com/yourusername/face-recognition-api.git cd face-recognition-api
-
Create and activate a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install the required dependencies:
pip install fastapi uvicorn face_recognition numpy pillow python-multipart jinja2
Note: The
face_recognition
library requiresdlib
which may need additional system dependencies. On some systems, you might need to install cmake and other build tools.
-
Start the server:
python facedetection/app.py
-
Open your browser and navigate to:
http://localhost:8000
GET /
: Main web interface for interacting with the application
POST /upload-training-face/
: Register a new facePOST /detect-face/
: Detect faces in an uploaded imageGET /known-faces/
: Get a list of all registered face namesDELETE /known-faces/{name}
: Delete a registered face by nameWebSocket /ws/detect-face-realtime/
: Real-time face detection from webcam
facedetection/
├── app.py # Main application file
├── models/ # Saved face recognition models
├── static/ # Static files (CSS, JS, uploads)
│ ├── known_faces/ # Stored face images for training
│ └── uploads/ # Temporary uploaded images
└── templates/ # HTML templates
└── index.html # Main web interface
- The application uses the
face_recognition
library, which is built on top of dlib - Face encodings are generated from images and stored in a pickle file
- Face matching uses a tolerance of 0.6 (lower values are more strict)
- Confidence scores are calculated as (1 - face_distance)
- If you encounter issues with the
face_recognition
library, ensure you have the required system dependencies for dlib - For webcam access issues, ensure your browser has permission to access your camera
- The application creates necessary directories on startup, but if you encounter permission issues, create them manually
If you're using an Apple M1/M2 MacBook or other Apple silicon device, you'll need to take some additional steps for the face_recognition library to work properly:
-
Install Homebrew if you don't have it already:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
Install CMake and other dependencies:
brew install cmake brew install openblas brew install python
-
You may also need to set some environment variables:
export OPENBLAS=$(brew --prefix openblas) export CFLAGS="-I$OPENBLAS/include" export LDFLAGS="-L$OPENBLAS/lib"
-
Install dlib separately (with specific options for Apple silicon):
pip install dlib
-
Then install the remaining dependencies:
pip install fastapi uvicorn face_recognition numpy pillow python-multipart jinja2
If you encounter any issues, you might need to install dlib from source with specific compilation options for Apple silicon. Reference the dlib documentation for more details.
This project is licensed under the MIT License - see below for details:
MIT License
Copyright (c) 2023 Face Recognition API Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
The MIT License is a permissive open-source license that allows for reuse with minimal restrictions. It permits users to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the software.
- Face recognition powered by face_recognition
- Built with FastAPI