Skip to content

Face recognition with ONNX Runtime, YOLOX, InsightFace, PostgreSQL, pgvector, and FastAPI.

Notifications You must be signed in to change notification settings

ruhyadi/vision-fr

Repository files navigation

Face Recognition

Introduction

Face Recognition is a computer vision task of identifying and verifying a person from a digital image or video frame. Face recognition systems can be used for various applications such as security, surveillance, access control, and more.

This repository contains an End-to-End Face Recognition API implemented using ONNX Runtime, PostgreSQL (pgvector), and FastAPI. We also combining YOLOx for face detection and InsigtFace for face recognition in ONNX format. With pgvector (a vector similarity search extension for PostgreSQL), we can store and search for face embeddings efficiently. Lastly, we use FastAPI to expose the face recognition API.

Docker Image

We provide a production-ready Docker image for convenience. You can pull the image from Docker Hub and run it directly. The details are as follows:

Version Docker Image Description
v1.0.0 ruhyadi/vision-fr:v1.0.0 Contains YOLOx_s model for face detection and MobileFaceNet model for face recognition.

Supported Models

The following models are supported in this repository:

Face Detection

Model Name Description Input Shape Output Shape Link
YOLOx s face YOLOx small model for face detection. Trained on WIDER FACE dataset. ONNX NMS supported. (-1, 3, 640, 640) (num_dets, boxes, classes, scores) yoloxs_face.onnx

Face Recognition

Model Name Description Input Shape Output Shape Link
MobileFaceNet MobileFaceNet model for face recognition. Trained on WebFace600K. Official model from InsightFace. (-1, 3, 112, 112) (-1, 512) w600k_mbf.onnx

Getting Started

Prerequisites

We recommend using Docker either for development or production. You can install Docker by following the instructions here.

Next, you can clone this repository and navigate to the project directory:

git clone https://github.com/ruhyadi/vision-fr
cd vision-fr

Production

To run the Face Recognition API in production, you can use the provided Docker image. You can pull the image from Docker Hub and run it directly. The details are as follows:

docker pull ruhyadi/vision-fr:latest

We provide a docker-compose.onnx.prod.yaml file to run the API with PostgreSQL. You can run the following command to start the services:

docker compose -f docker-compose.onnx.prod.yaml up -d

The swagger documentation will be available at http://localhost:7030.

Development

We encourage you to use devcontainer for development. Please make sure you have installed Visual Studio Code and Remote - Containers extension.

We provide a devcontainer/onnx/devcontainer.json file to build the ONNX development container.

Before you start devcontainer, you need to start the PostgreSQL service. You can run the following command to start the PostgreSQL service:

docker compose -f docker-compose.devel.db.yaml up -d

Next, you can press Ctrl+Shift+P to open the command palette. Then, type and select Remote-Containers: Reopen in Container. The development container will be built and you can start developing.

API Endpoints

The details of endpoints can be found in the Swagger documentation. Here the brief description of the endpoints:

Method Endpoint Description
GET /api/v1/engine/face Get list of all faces data in the database.
POST /api/v1/engine/face/register Register a new face to the database.
POST /api/v1/engine/face/recognize Compare a face with the database.
DELETE /api/v1/engine/face/{id} Delete a face from the database.

Acknowledgements

  • ONNX Runtime: ONNX Runtime is a performance-focused scoring engine for Open Neural Network Exchange (ONNX) models.
  • PostgreSQL: PostgreSQL is a powerful, open-source object-relational database system.
  • pgvector: pgvector is a vector similarity search extension for PostgreSQL.
  • FastAPI: FastAPI is a modern, fast (high-performance), web framework for building APIs with Python.
  • YOLOx: YOLOX is a high-performance anchor-free YOLO with ONNX NMS supported.
  • InsigtFace: InsightFace is an open-source 2D and 3D face analysis toolkit.