DEV Community

Cover image for How to create an Instagram face finder app?
Art Quant
Art Quant

Posted on

How to create an Instagram face finder app?

We won’t focus on collecting a dataset of face images, when dealing with facial data, it’s important to consider privacy laws and ethical implications. Ensure that you comply with privacy laws and user consent for face recognition. A freely available dataset of 100,000 celebrities, would be a great choice for learning.

How the process typically works

Face Embeddings
First, you need to convert each face into a vector representation, often called an embedding. This is done using deep learning models specifically designed for facial recognition or facial feature extraction. These models output a vector (a list of numbers) for each face, where similar faces have similar vectors.

Storing in a Vector Database
Next, these vectors are stored in a vector database like SingleStore. This type of database is optimized for handling vector data and can efficiently perform operations like nearest neighbor searches, which are crucial for finding similar faces.

SQL Queries for Searching
With the vectors stored in a database, you can then perform search queries to find similar faces. In some vector databases, you can use SQL-like syntax to query the data, making it more accessible and easier to integrate with existing systems. You write a query where you input a face vector, and the database returns the most similar face vectors from your dataset.

Scalability: Vector databases are designed to handle large datasets efficiently.
Speed: Searching for similar vectors is much faster compared to traditional methods.
Flexibility: SQL-like querying allows for easy integration and more complex queries.

Accuracy: The accuracy of your face search service will largely depend on the quality of the face embedding model.
Privacy and Ethical Concerns: When dealing with facial data, it’s important to consider privacy laws and ethical implications.
Data Preparation: The face images need to be pre-processed and normalized before generating embeddings for consistency.

Updates and Maintenance
Keep in mind that maintaining a database and ensuring the face embedding model stays current (as better models are developed) are ongoing tasks.

Best face embedding models

When looking for the best face embedding models, you want to consider factors like accuracy, speed, robustness against variations (like lighting, pose, age, etc.), and the size of the model (which affects deployment feasibility). Here are some of the notable models and frameworks:

  1. Facenet: Developed by Google, Facenet has been a popular choice for face recognition tasks. It uses a deep convolutional network, trained to optimize the embedding itself, rather than intermediate features.
  2. DeepFace: Developed by Facebook, DeepFace is another highly influential model in this field. It’s known for its robustness and high accuracy.
  3. VGGFace and VGGFace2: Developed by the Visual Graphics Group at Oxford, these models are built on the famous VGG-16 architecture and are known for their high performance in face recognition.
  4. ArcFace: This model is known for its angular margin loss function, which significantly enhances the discriminative power of the embeddings.
  5. Dlib’s Face Recognition Model: Based on a ResNet architecture, this model is widely used in the community for its balance of accuracy and resource efficiency.
  6. OpenFace: An open-source tool, OpenFace offers good performance and is easier to deploy in practical applications.
  7. InsightFace: This is a recent and popular deep learning toolkit for face analysis, known for its state-of-the-art performance in face detection and recognition.

When selecting a face embedding model, it’s important to consider the specific requirements of your application. For instance, if you’re working on a mobile application, you might prioritize models that are more lightweight and efficient. On the other hand, for a security-focused application, the accuracy and robustness of the model would be the top priority.

Face finder app

Face finder app


Creating a face recognition application using Gradio is a simpler approach, especially for prototyping and demonstration purposes. These frameworks allow for rapid development of interactive web applications with Python. I’ll provide an example using Gradio, as it’s well-suited for tasks involving image processing and machine learning.

Setting Up the Environment

Install necessary Python libraries.
You’ll need gradio, face_recognition, and numpy. Install them via pip:

pip install gradio face_recognition numpy

Building the Gradio App

Create a Python script (e.g.,
This script will set up the Gradio interface and handle the face recognition logic.

import gradio as gr
import face_recognition
import numpy as np
import sqlite3
import json
import os

# Path to the SQLite database file
db_file = 'faces.db'

# Function to get a new SQLite connection
def get_new_connection():
    conn = sqlite3.connect(db_file)
    cursor = conn.cursor()
    cursor.execute('''CREATE TABLE IF NOT EXISTS faces (id INTEGER PRIMARY KEY AUTOINCREMENT, embedding TEXT)''')
    return conn

def upload_face(input_image):
    conn = get_new_connection()
    cursor = conn.cursor()

    embeddings = face_recognition.face_encodings(input_image)
    if not embeddings:
        return "No face detected in the uploaded image."

    # Store the first face embedding in the database
    embedding = embeddings[0].tolist()
    cursor.execute("INSERT INTO faces (embedding) VALUES (?)", (json.dumps(embedding),))
    return "Face uploaded successfully!"

def search_faces(input_image):
    conn = get_new_connection()
    cursor = conn.cursor()

    embeddings = face_recognition.face_encodings(input_image)
    if not embeddings:
        return "No face detected in the search image."

    input_embedding = embeddings[0]

    cursor.execute("SELECT id, embedding FROM faces")
    rows = cursor.fetchall()
    if not rows:
        return "No faces in database to compare with."

    min_distance = float('inf')
    closest_id = None
    for row in rows:
        db_embedding = json.loads(row[1])
        distance = np.linalg.norm(np.array(db_embedding) - input_embedding)
        if distance < min_distance:
            min_distance = distance
            closest_id = row[0]

    if closest_id is not None:
        result = f"Closest match is ID {closest_id} with distance {min_distance:.2f}"
        explanation = "Note: A smaller distance indicates a closer match. "
        explanation += "Distance is a measure of similarity between the face embeddings."
        return result + "\n" + explanation
        return "No similar faces found."

with gr.Blocks() as app:
    gr.Markdown("Upload a face to add to the database")
    with gr.Row():
        input_image1 = gr.Image()
        submit_button1 = gr.Button("Upload")
    output1 = gr.Textbox(label="Upload Status"), inputs=input_image1, outputs=output1)

    gr.Markdown("Search for a similar face in the database")
    with gr.Row():
        input_image2 = gr.Image()
        submit_button2 = gr.Button("Search")
    output2 = gr.Textbox(label="Search Results"), inputs=input_image2, outputs=output2)

if __name__ == "__main__":
    # Ensure the database file exists
    if not os.path.exists(db_file):
        open(db_file, 'a').close()

Enter fullscreen mode Exit fullscreen mode

Run the Gradio app

Execute the script to start the application:


This will start a local server, and you can interact with your app through the web interface.

How App Works

  • Users can upload a multiple photos (one at a time) through the first function. The app computes its face embedding and stores it in the database.
  • Users can then search for similar faces using the second function. The app computes the embedding of the search image and finds the closest match from the stored embeddings. A distance of 0 for a full match.
  • The interface now supports two different operations: uploading and searching.


  • This implementation is for demonstration purposes. In a real-world application, you would need to handle cases where deal with multiple faces in a photo, and ensure robust error handling.
  • As before, consider the privacy and ethical implications of handling and storing facial data.

Top comments (0)