Face Recognition Microservices With FastApi and AI Tools
This project utilizes a microservices architecture to detect and recognize faces from video streams. The workflow is divided into four stages, each handled by a separate microservice. Here’s how it works:
- First Microservice: Receives video input and preprocesses the frames.
- Second Microservice: Uses MTCNN to detect faces in each frame and forwards the detected faces along with timestamps to the third microservice.
- Third Microservice: Employs FaceNet to recognize faces against a pre-existing database. If a face is recognized, it sends the face data, the recognized person's name, and the timestamp to the fourth microservice.
- Fourth Microservice: Logs the recognized face data along with the name and timestamp.
All microservices are implemented using FastAPI and communicate with each other via POST requests.
Before running the microservices, ensure that Docker and Docker Compose are installed on your system.For Docker Instalation see link. Follow these steps to set up the project:
Clone the repository:
git clone https://github.com/rezasouri/Ai-Task.git
cd AI-Task
First Navigate to Docker directory
cd Docker
Build and run the containers:
docker-compose up --build
This command builds the images if they don’t exist and starts the containers.
If you prefer not to use Docker, you can run the APIs locally:
- Navigate to the
APIdirectory. - Run each service using Python in
TerminalwithUvicorn. For example:
uvicorn script_name:app --port PORT_NUMBER --host HOST --reloadscript_name: Replace this with the name of your Python file that contains the FastAPI app instance (e.g.,main.py).app: This should be the name of the FastAPI instance in your script.PORT_NUMBER: Specify the port number on which the server should listen (e.g.,8000).HOST: Define the host address (e.g.,127.0.0.1for local development or0.0.0.0to allow external access).--reload: This optional flag enables auto-reload on code changes, which is very useful during development.
Once the services are running, you can send a video stream to the first microservice's endpoint to begin the face detection and recognition process. Use the following endpoint to post video data:
curl -X POST http://localhost:8000/docs#/default/read_video_upload__post \
-F "file=@/path/to/your/video.file" \
-H "Content-Type: multipart/form-data"
Or you can go to
http:\\localhost:8000/docs
and send post request (select video and send it)
You can use this command for see logs data
docker logs [container id]
Results are look like below:
- @rezasouri - Idea & Initial work
For those who prefer to run face detection locally without using the API, you can utilize the scripts provided in the Model directory. Here's how to use them:
- Navigate to the
Modeldirectory. - Run the
encoding.pyscript to create embedding data from your images. This step preprocesses your images and creates a dataset of face embeddings that can be used for recognition.
- Use the
face_detection_images.pyscript to recognize faces within static images. This script will analyze each image, detect faces, and attempt to recognize them based on the embeddings created previously.
- For real-time face recognition, run the
face_detection_video.pyscript. This script is capable of recognizing faces in video streams, including live feeds from a webcam. It processes video frames in real time, detects and recognizes faces, and displays the results.
These tools provide a flexible way to implement face recognition technology directly on your local system, either for development or testing purposes. Feel free to explore and modify the scripts according to your needs!


