Becoming Human 2024年11月26日
Building a Local Face Search Engine — A Step by Step Guide
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了构建本地人脸搜索引擎的基本概念和步骤,并使用Python实现了简单的解决方案。文章首先阐述了人脸匹配、嵌入和相似性度量的原理,然后介绍了如何使用Insightface库进行人脸搜索的实现,包括安装库、运行脚本以及参数设置等。最后,文章指出了该方案的局限性,并预告了下一篇文章将使用向量数据库来优化搜索效率。通过本文,读者可以了解如何利用人脸嵌入技术,在本地图像中快速搜索特定人脸,并为后续的优化和扩展奠定基础。

🤔 **人脸匹配与嵌入:**文章介绍了人脸匹配的目标是找到给定查询人脸在图像池中的所有实例,并通过相似性度量来排序结果。人脸嵌入是一种将人脸特征转换为数值向量的方法,可以有效地捕捉人脸的关键特征,并忽略无关细节。

💡 **相似性度量:**利用学习到的嵌入向量,可以使用余弦相似度或欧氏距离等距离度量来衡量两张人脸的相似性。嵌入模型的质量和训练标准直接影响着嵌入向量的有效性,从而影响人脸匹配的准确性。

💻 **本地人脸搜索实现:**文章使用Insightface库实现了本地人脸搜索功能,通过Python脚本加载查询人脸、计算嵌入向量,然后与目标图像中的所有人脸嵌入向量进行比较,最后根据相似性得分筛选匹配结果。

⏱️ **性能优化展望:**文章指出了该方案的性能瓶颈,即在大规模图像搜索时效率低下,并且每次查询都需要重新计算所有嵌入向量。下一篇文章将介绍如何使用向量数据库来优化搜索效率。

🌐 **作者联系方式:**作者在文章末尾提供了自己的社交媒体账号,方便读者与其进行交流和互动。

Building a Local Face Search Engine — A Step by Step Guide

Part 1: on face embeddings and how to run face search on the fly

Sample demonstration of face recognition and search for the cast of “The Office”

In this entry (Part 1) we’ll introduce the basic concepts for face recognition and search, and implement a basic working solution purely in Python. At the end of the article you will be able to run arbitrary face search on the fly, locally on your own images.

In Part 2 we’ll scale the learning of Part 1, by using a vector database to optimize interfacing and querying.

Face matching, embeddings and similarity metrics.

The goal: find all instances of a given query face within a pool of images.
Instead of limiting the search to exact matches only, we can relax the criteria by sorting results based on similarity. The higher the similarity score, the more likely the result to be a match. We can then pick only the top N results or filter by those with a similarity score above a certain threshold.

Example of matches sorted by similarity (descending). First entry is the query face.

To sort results, we need a similarity score for each pair of faces <Q, T> (where Q is the query face and T is the target face). While a basic approach might involve a pixel-by-pixel comparison of cropped face images, a more powerful and effective method uses embeddings.

An embedding is a learned representation of some input in the form of a list of real-value numbers (a N-dimensional vector). This vector should capture the most essential features of the input, while ignoring superfluous aspect; an embedding is a distilled and compacted representation.
Machine-learning models are trained to learn such representations and can then generate embeddings for newly seen inputs. Quality and usefulness of embeddings for a use-case hinge on the quality of the embedding model, and the criteria used to train it.

In our case, we want a model that has been trained to maximize face identity matching: photos of the same person should match and have very close representations, while the more faces identities differ, the more different (or distant) the related embeddings should be. We want irrelevant details such as lighting, face orientation, face expression to be ignored.

Once we have embeddings, we can compare them using well-known distance metrics like cosine similarity or Euclidean distance. These metrics measure how “close” two vectors are in the vector space. If the vector space is well structured (i.e., the embedding model is effective), this will be equivalent to know how similar two faces are. With this we can then sort all results and select the most likely matches.

https://medium.com/media/8929d6d8077c7300dfa5acc29dba739b/href

Implement and Run Face Search

Let’s jump on the implementation of our local face search. As a requirement you will need a Python environment (version ≥3.10) and a basic understanding on the Python language.

For our use-case we will also rely on the popular Insightface library, which on top of many face-related utilities, also offers face embeddings (aka recognition) models. This library choice is just to simplify the process, as it takes care of downloading, initializing and running the necessary models. You can also go directly for the provided ONNX models, for which you’ll have to write some boilerplate/wrapper code.

First step is to install the required libraries (we advise to use a virtual environment).

pip install numpy==1.26.4 pillow==10.4.0 insightface==0.7.3

The following is the script you can use to run a face search. We commented all relevant bits. It can be run in the command-line by passing the required arguments. For example

 python run_face_search.py -q "./query.png" -t "./face_search"
https://medium.com/media/bcb2ba4a20ed239be8ffbfa61be89259/href

The query arg should point to the image containing the query face, while the target arg should point to the directory containing the images to search from. Additionally, you can control the similarity-threshold to account for a match, and the minimum resolution required for a face to be considered.

The script loads the query face, computes its embedding and then proceeds to load all images in the target directory and compute embeddings for all found faces. Cosine similarity is then used to compare each found face with the query face. A match is recorded if the similarity score is greater than the provided threshold. At the end the list of matches is printed, each with the original image path, the similarity score and the location of the face in the image (that is, the face bounding box coordinates). You can edit this script to process such output as needed.

Similarity values (and so the threshold) will be very dependent on the embeddings used and nature of the data. In our case, for example, many correct matches can be found around the 0.5 similarity value. One will always need to compromise between precision (match returned are correct; increases with higher threshold) and recall (all expected matches are returned; increases with lower threshold).

What’s Next?

And that’s it! That’s all you need to run a basic face search locally. It is quite accurate, and can be run on the fly, but it doesn’t provide optimal performances. Searching from a large set of images will be slow and, more important, all embeddings will be recomputed for every query. In the next post we will improve on this setup and scale the approach by using a vector database.

Want to Connect?
You can catch a glimpse of my latest experiments and explanations on Twitter or Threads and see my graphics results on Instagram.


Building a Local Face Search Engine — A Step by Step Guide was originally published in Becoming Human: Artificial Intelligence Magazine on Medium, where people are continuing the conversation by highlighting and responding to this story.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

人脸识别 人脸搜索 嵌入向量 Insightface Python
相关文章