This project demonstrates facial recognition and similarity search using face-api.js. It extracts face embeddings from images, stores them in a PostgreSQL database, and performs similarity searches using L2 distance. The application includes both a backend API and a frontend demo.
The system uses face-api.js to extract 128-dimensional face embeddings from images. These embeddings are stored in a PostgreSQL database with the pgvector extension, allowing for efficient similarity searches. The L2 distance between face embeddings is used to determine similarity between faces.
demovideo.mp4
- Node.js (version 21 or higher recommended)
- PostgreSQL database
- npm (Node Package Manager)
- Clone the repository:
git clone https://github.com/yourusername/face-api-test.git
cd face-api-test
- Install dependencies:
npm install
- Set up environment variables:
Create a
.env
file in the root directory and add the following variables:
DATABASE_URL=postgresql://username:password@localhost:5432/your_database_name
PORT=3000
- Start the PostgreSQL database using Docker Compose:
docker-compose up -d
This will start a PostgreSQL container with the pgvector extension installed.
- Generate Prisma client:
npx prisma generate
- Set up the database: Connect to the PostgreSQL database and create the following tables:
CREATE EXTENSION vector;
CREATE TABLE users (id uuid PRIMARY KEY, embedding vector(128));
CREATE TABLE user_images (id uuid PRIMARY KEY, image text);
To start the server, run:
npm start
The server will start on the port specified in your .env file (default is 3000).
The project includes a frontend that provides a user interface to interact with the facial recognition and similarity search features. Access the frontend demo by navigating to http://localhost:3000 in your web browser.
The application provides several API endpoints for face recognition and similarity search. Here's how to use them:
Add a new face to the database.
Request body:
{
"base64Image": "base64_encoded_image_data"
}
Response:
{
"user_id": "generated_uuid",
"message": "User created!"
}
Compare two faces for similarity.
{
"base64referenceImage": "base64_encoded_reference_image",
"base64queryImage": "base64_encoded_query_image"
}
Response:
{
"similarity": 85.5
}
Search for similar faces in the database.
Request body:
{
"threshold": 0.5,
"limit": 10,
"base64queryImage": "base64_encoded_query_image"
}
Response:
{
"similars": [
{
"id": "user_uuid",
"distance": 0.3
},
// ... more similar faces
]
}
Retrieve a specific image by ID.
Note: Replace :id in the URL with the actual image ID (usually the similar.id from the search results). For all endpoints that require base64 image data, ensure that you're sending the full base64 string, including the data URI prefix (e.g., "data:image/jpeg;base64,").
npm start: Start the application
npm run fixTensorFlow: Fix TensorFlow dependencies (if needed)
The project includes an import-images.js script that allows you to import a dataset of celebrity face images for testing purposes. To use this script:
- Download the "Celebrity Face Image Dataset" from Kaggle: https://www.kaggle.com/datasets/vishesh1412/celebrity-face-image-dataset
- Extract the downloaded dataset to a directory named celebrity_faces in the project root.
- Run the import script:
node import-images.js
#or
npm run import
This will process the images and add them to your database for testing.
@fastify/helmet: Security headers for Fastify
@fastify/static: Static file serving for Fastify
@prisma/client: Prisma ORM client
@tensorflow/tfjs-node: TensorFlow.js for Node.js
canvas: Canvas implementation for Node.js
dotenv: Environment variable management
face-api.js: Face detection and recognition library
fastify: Web framework
uuid: UUID generation
The project includes a docker-compose.yml file to easily set up the PostgreSQL database with the pgvector extension. To start the database, run:
docker-compose up -d
This will create a PostgreSQL container with the following configuration:
Username: face-api-test
Password: 614D05
Database: face-api
Port: 5432 (mapped to host)
This project makes use of the face-api.js library, which provides the core functionality for face detection and recognition.
- Repository: https://github.com/justadudewhohacks/face-api.js
- Description: JavaScript face recognition API for the browser and nodejs implemented on top of tensorflow.js core (tensorflow/tfjs-core)
- Author: Vincent Mühler (justadudewhohacks)
- License: MIT
This project is licensed under the MIT License. See the LICENSE file for details.