Skip to content

generate_face_embedding

generate_face_embedding(image_url)

Description

Detect Face on image and Generate its Embedding using deepface.

  • image_url is the url of a photo which contains a face. It can be a signed url of a cloud storage object. Then this function works well with object tables.
  • output is the output of DeepFace.represent method. It is like:
{
  embedding: [...],      # A 4096 float vector
  facial_areal: {...},   # Coordinated of detected face
  face_confidence: 1.0,  # Confidence score for face detection
}

Usage

Call or Deploy generate_face_embedding ?
Call generate_face_embedding directly

The easiest way to use bigfunctions

  • generate_face_embedding function is deployed in 39 public datasets for all of the 39 BigQuery regions.
  • It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)

Public BigFunctions Datasets

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...
Deploy generate_face_embedding in your project

Why deploy?

  • You may prefer to deploy generate_face_embedding in your own project to build and manage your own catalog of functions.
  • This is particularly useful if you want to create private functions (for example calling your internal APIs).
  • Get started by reading the framework page

Deployment

generate_face_embedding function can be deployed with:

pip install bigfunctions
bigfun get generate_face_embedding
bigfun deploy generate_face_embedding

Examples

Public test image from deepface

select bigfunctions.eu.generate_face_embedding("https://raw.githubusercontent.com/serengil/deepface/master/tests/dataset/img1.jpg")
select bigfunctions.us.generate_face_embedding("https://raw.githubusercontent.com/serengil/deepface/master/tests/dataset/img1.jpg")
select bigfunctions.europe_west1.generate_face_embedding("https://raw.githubusercontent.com/serengil/deepface/master/tests/dataset/img1.jpg")
+-------------------------------------------------------------------------+
| output                                                                  |
+-------------------------------------------------------------------------+
| {
  embedding: [...],
  facial_areal: {...},
  face_confidence: 1.0,
}
 |
+-------------------------------------------------------------------------+

Use cases

This generate_face_embedding function is useful for facial recognition tasks within BigQuery. Here's a use case:

Scenario: You have a large dataset of images stored in Google Cloud Storage, represented as an object table in BigQuery. You want to identify all images that contain a specific person.

Implementation using generate_face_embedding:

  1. Pre-calculate the embedding of the target person: Take a known image of the target person and use generate_face_embedding to calculate their facial embedding. Store this embedding (a vector of numbers) somewhere accessible, like a small BigQuery table.

  2. Process your image dataset: Query your object table and apply generate_face_embedding to the image URL column for each row. This will generate facial embeddings for all faces detected in your dataset.

  3. Compare embeddings: Use a BigQuery function (e.g., a user-defined function or a built-in function for vector similarity like cosine similarity) to compare the embeddings generated in step 2 with the target person's embedding from step 1.

  4. Filter based on similarity: Filter the results based on a similarity threshold. Images with embeddings that are highly similar to the target person's embedding likely contain the target person.

Example SQL Snippet (Illustrative):

# Assuming 'target_embeddings' table contains pre-calculated embedding
# and 'image_table' is your object table with 'image_url' column

SELECT
    image_url
FROM
    image_table
WHERE EXISTS (
    SELECT
        1
    FROM
        target_embeddings
    WHERE
        cosine_similarity(bigfunctions.us.generate_face_embedding(image_table.image_url).embedding, target_embeddings.embedding) > 0.9  -- Example threshold
);

Benefits of using generate_face_embedding within BigQuery:

  • Scalability: BigQuery's distributed processing power allows you to analyze massive image datasets efficiently.
  • Integration: Seamlessly integrates with your existing BigQuery data and workflows. No need to export data or use external tools.
  • Cost-effectiveness: BigQuery's pricing model can be advantageous for large-scale processing compared to other solutions.

Other Use Cases:

  • Face clustering: Group similar faces together to identify different individuals in a dataset.
  • Security and surveillance: Identify known individuals in security footage.
  • Image search: Search for images containing similar faces.
  • Social media analysis: Analyze profile pictures for demographic information or to identify influencers.

Need help or Found a bug?
Get help using generate_face_embedding

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about generate_face_embedding

If the function does not work as expected, please

  • report a bug so that it can be improved.
  • or open the discussion with the community on Slack.

We also provide professional suppport.


Show your ❤ by adding a ⭐ on