A RESTful face recognition service built with FastAPI that provides endpoints for comparing faces in images. The API supports various input methods including file uploads and URL-based image processing.
- Face Comparison: Compare faces between two images with similarity scoring
- Multiple Input Methods: Support for uploaded files and image URLs
- Facial Landmarks: Generate facial landmark overlays for visualization
- Similarity Scoring: Calculate percentage-based similarity scores
- RESTful API: Clean and well-documented REST endpoints
- Docker Support: Containerized deployment ready
- CORS Enabled: Cross-origin requests supported
- FastAPI: High-performance web framework
- face_recognition: Face detection and recognition library
- OpenCV: Image processing and computer vision
- PIL/Pillow: Image manipulation
- dlib: Machine learning algorithms for face recognition
- NumPy: Numerical computing
- Docker: Containerization
- Python 3.10+
- pip
- cmake (for dlib compilation)
-
Clone the repository
git clone https://github.com/DahalRojan/face-recognition.git cd face-recognition -
Create virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Set environment variables
# Create .env file echo "HOST=http://localhost:8000" > .env
-
Create required directories
mkdir -p temp assets/images
-
Run the application
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
-
Build the image
docker build -t face-recognition-api . -
Run the container
docker run -p 9292:9292 face-recognition-api
- Local:
http://localhost:8000 - Docker:
http://localhost:9292
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
POST /face-recognition/verify/image2imageParameters:
image1: First image file (multipart/form-data)image2: Second image file (multipart/form-data)
Example using curl:
curl -X POST "http://localhost:8000/face-recognition/verify/image2image" \
-F "image1=@path/to/first/image.jpg" \
-F "image2=@path/to/second/image.jpg"POST /face-recognition/verify/image2urlParameters:
image: Image file (multipart/form-data)url: Image URL (string)
Example using curl:
curl -X POST "http://localhost:8000/face-recognition/verify/image2url" \
-F "image=@path/to/image.jpg" \
-F "url=https://example.com/image.jpg"POST /face-recognition/verify/url2urlParameters:
url1: First image URL (string)url2: Second image URL (string)
Example using curl:
curl -X POST "http://localhost:8000/face-recognition/verify/url2url" \
-H "Content-Type: application/json" \
-d '{
"url1": "https://example.com/image1.jpg",
"url2": "https://example.com/image2.jpg"
}'GET /images/{filename}Access processed images and facial landmark overlays.
All face comparison endpoints return the following JSON structure:
{
"is_similar": true,
"similarity": 87.5,
"match_index": 1,
"nfaces": 1,
"image1": "http://localhost:8000/images/image_abc123.png",
"image1_landmark": "http://localhost:8000/images/image_def456.png",
"image2": "http://localhost:8000/images/image_ghi789.png",
"image2_landmark": "http://localhost:8000/images/image_jkl012.png"
}Response Fields:
is_similar: Boolean indicating if faces matchsimilarity: Percentage similarity score (0-100)match_index: Index of the matched facenfaces: Number of faces detected in the reference imageimage1: URL to the first processed imageimage1_landmark: URL to the first image with facial landmarksimage2: URL to the second processed imageimage2_landmark: URL to the second image with facial landmarks
HOST: Base URL for serving images (default:http://localhost:8000)
- Tolerance: Default tolerance is
0.45(configurable inai/recognition/core.py) - Supported Formats: JPEG, PNG, BMP, TIFF
face-recognition/
βββ main.py # FastAPI application entry point
βββ requirements.txt # Python dependencies
βββ Dockerfile # Docker configuration
βββ .gitignore # Git ignore patterns
βββ README.md # Project documentation
βββ ai/
βββ core/ # Core utilities
β βββ settings.py # Configuration settings
β βββ routers.py # Router initialization
β βββ views.py # Image serving endpoints
β βββ file.py # File handling utilities
β βββ image.py # Image processing utilities
β βββ utils.py # Helper functions
βββ recognition/ # Face recognition module
βββ core.py # Main recognition logic
βββ views.py # API endpoints
βββ schemas.py # Pydantic models
βββ version.py # Version information
The API returns appropriate HTTP status codes:
200: Success400: Bad request (invalid image format, missing parameters)404: No face found in image424: Failed to download image from URL500: Internal server error
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is open source and available under the MIT License.
For issues and questions:
- Create an issue on GitHub
- Check the API documentation at
/docs
- Improved API title and documentation
- Enhanced error handling and messages
- Added configuration constants
- Fixed BASE_DIR path resolution
- Added comprehensive .gitignore
- Initial release
- Basic face recognition functionality
- Docker support
- RESTful API endpoints