Elasticsearch 和 Python构建面部识别系统—Elastic Stack 实战手册

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 你是否曾经尝试在图像中搜索目标? Elasticsearch 可以帮助你存储,分析和搜索图像或视频中的目标。

970X90.png

· 更多精彩内容,请下载阅读全本《Elastic Stack实战手册》

· 加入创作人行列,一起交流碰撞,参与技术圈年度盛事吧

创作人:刘晓国

你是否曾经尝试在图像中搜索目标? Elasticsearch 可以帮助你存储,分析和搜索图像或视频中的目标。

在本文中,我们将向你展示如何构建一个使用 Python 进行面部识别的系统。 了解有关如何检测和编码面部信息的更多信息-并在搜索中找到匹配项。

1.png

我们将参照代码: https://github.com/liu-xiao-guo/face_detection_elasticsearch。你可以把这个代码下载到本地的电脑:
$ pwd
/Users/liuxg/python/face_detection
$ tree -L 2
.
├── README.md
├── getVectorFromPicture.py
├── images
│   ├── shay.png
│   ├── simon.png
│   ├── steven.png
│   └── uri.png
├── images_to_be_recognized
│   └── facial-recognition-blog-elastic-founders-match.png
└── recognizeFaces.py

在上面的代码中,有如下的两个 python 文件:

  • getVectorFromPicture.py:导入在 images 目录下的图像。这些图像将被导入到 Elasticsearch 中
  • recognizeFaces.py:识别位于 images_to_be_recognized 目录下的图像文件

基础知识

面部识别

面部识别是使用面部特征来识别用户的过程,例如,为了实现身份验证机制(例如解锁智能手机)。 它根据人的面部细节捕获,分析和比较模式。 此过程可以分为三个步骤:

  • 人脸检测:识别数字图像中的人脸
  • 人脸数据编码:将人脸特征转换为数字表示
  • 脸部比对:搜寻和比较脸部特征

在示例中,我们将引导你完成每个步骤。

128 维向量

可以将面部特征转换为一组数字信息,以便进行存储和分析。

2.png

Vector data type

Elasticsearch 提供了 dense_vector 数据类型来存储浮点值的 dense vectors。 向量中的最大尺寸数不应超过 2048,这足以存储面部特征表示。

现在,让我们实现所有这些概念。

准备

要检测面部并编码信息,你需要执行以下操作:

  1. Python:在此示例中,我们将使用 Python 3
  2. Elasticsearch 集群:你可以免费使用 阿里云Elasticsearch 来启动集群。本文中,我将进行一个本地的部署 Elasticsearch 及 Kibana。
  3. 人脸识别库:一个简单的人脸识别 Python 库。
  4. Python Elasticsearch 客户端:Elasticsearch的官方Python客户端。
客户端下载: https://elasticsearch-py.readthedocs.io/en/v7.10.1/

Python教程:https://elasticstack.blog.csdn.net/article/details/111573923

Python下载::https://www.python.org/downloads/

注意,我们已经在 Ubuntu 20.04 LTS 和 Ubuntu 18.04 LTS 上测试了以下说明。 根据你的操作系统,可能需要进行一些更改。尽管下面的安装步骤是针对 Ubuntu 操作系统的,但是我们可以按照同样的步骤在 Mac OS 上进行同样的顺序进行安装(部分指令会有所不同)。

安装 Python 和 Python 库

随 Python 3 的安装一起提供了 Ubuntu 20.04 和其他版本的 Debian Linux。

如果你的系统不是这种情况,则可以点击下载并安装 Python: https://www.python.org/downloads/

要确认您的版本是最新版本,可以运行以下命令:

sudo apt update 
sudo apt upgrade

确认 Python 版本为 3.x:

python3 -V

或者:

python --version

安装 pip3 来管理 Python 库:

sudo apt install -y python3-pip

安装 face_recognition 库所需的 cmake:

pip3 install CMake

将 cmake bin 文件夹添加到 $PATH 目录中:

export PATH=$CMake_bin_folder:$PATH

在我的测试中,上述步骤可以不需要。你只要在任何一个 terminal 中打入 cmake 命令,如果能看到被执行,那么就可以不用上面的命令了。

最后,在开始编写主程序脚本之前,安装以下库:

pip3 install dlib 
pip3 install numpy 
pip3 install face_recognition  
pip3 install elasticsearch

从图像中检测和编码面部信息

使用 face_recognition 库,我们可以从图像中检测人脸,并将人脸特征转换为 128 维向量。

3.png

为此,我们创建一个叫做 getVectorFromPicture.py:

getVectorFromPicture.py

import face_recognition 
import numpy as np 
import sys
import os
from pathlib import Path
from elasticsearch import Elasticsearch
 
es = Elasticsearch([{'host':'localhost','port':9200}])
 
cwd = os.getcwd()
print("cwd: " + cwd)
 
# Get the images directory
rootdir = cwd + "/images"
print("rootdir: " + rootdir)
 
for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        print(os.path.join(subdir, file))
        file_path = os.path.join(subdir, file)
 
        image = face_recognition.load_image_file(file_path)
 
        # detect the faces from the images
        face_locations = face_recognition.face_locations(image)
 
        # encode the 128-dimension face encoding for each face in the image
        face_encodings = face_recognition.face_encodings(image, face_locations)
 
        # Display the 128-dimension for each face detected
        for face_encoding in face_encodings:
            print("Face found ==>  ", face_encoding.tolist())
            print("name: " + Path(file_path).stem)
            name = Path(file_path).stem
            face_encoding = face_encoding.tolist()
 
            # format a dictionary to be indexed
            e = {
                "face_name": name,
                "face_encoding": face_encoding 
            }
 
            res = es.index(index = 'faces', doc_type ='_doc', body = e)

首先,我们需要声明的是:你需要修改上面的 Elasticsearch 的地址,如果你的 Elasticsearch 不是运行于 localhost:9200。上面的代码非常之简单。它把当前目录下的子目录 images 下的所有文件都扫描一遍,并针对每个文件进行编码。我们使用 Python client API 接口把数据导入到 Elasticsearch 中去。在我们的 images 文件夹中,有四个文件。

在导入数据之前,我们需要在 Kibana 中创建一个叫做 faces 的索引:

PUT faces
{
  "mappings": {
    "properties": {
      "face_name": {
        "type": "keyword"
      },
      "face_encoding": {
        "type": "dense_vector",
        "dims": 128
      }
    }
  }
}

让我们执行 getVectorFromPicture.py 以获取 Elastic 创始人图像的面部特征表示。

python3 getVectorFromPicture.py

4.png

现在,我们可以将面部特征表示存储到 Elasticsearch 中。

5.png

我们可以在 Elasticsearch 中看到四个文档:

GET faces/_count
{
  "count" : 4,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  }
}

我们也可以查看 faces 索引的文档:

GET faces/_search

6.png

匹配面孔

假设我们在 Elasticsearch 中索引了四个文档,其中包含 Elastic 创始人的每个面部表情。 现在,我们可以使用创始人的其他图像来匹配各个图像。

7.png

为此,我们需要创建一个叫做 recognizeFaces.py 的文件。

recognizeFaces.py

import face_recognition
import numpy as np
from elasticsearch import Elasticsearch
import sys
import os
 
from elasticsearch import Elasticsearch
 
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
 
cwd = os.getcwd()
# print("cwd: " + cwd)
 
# Get the images directory
rootdir = cwd + "/images_to_be_recognized"
# print("rootdir: {0}".format(rootdir))
 
for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        print(os.path.join(subdir, file))
        file_path = os.path.join(subdir, file)
 
        image = face_recognition.load_image_file(file_path)
 
        # detect the faces from the images
        face_locations = face_recognition.face_locations(image)
 
        # encode the 128-dimension face encoding for each face in the image
        face_encodings = face_recognition.face_encodings(image, face_locations)
 
        # Display the 128-dimension for each face detected
        i = 0
        for face_encoding in face_encodings:
            i += 1
            print("Face", i)
            response = es.search(
                index="faces",
                body={
                    "size": 1,
                    "_source": "face_name",
                    "query": {
                        "script_score": {
                            "query": {
                                "match_all": {}
                            },
                            "script": {
                                "source": "cosineSimilarity(params.query_vector, 'face_encoding')",
                                "params": {
                                    "query_vector": face_encoding.tolist()
                                }
                            }
                        }
                    }
                }
            )
 
            # print(response)
 
            for hit in response['hits']['hits']:
                # double score=float(hit['_score'])
                print("score: {}".format(hit['_score']))
                if float(hit['_score']) > 0.92:
                    print("==> This face  match with ", hit['_source']['face_name'], ",the score is", hit['_score'])
                else:
                    print("==> Unknown face")

这个文件的写法也非常简单。它从目录 images_to_be_recognized 中获取需要识别的文件,并对这个图片进行识别。我们使用 cosineSimilarity 函数来计算给定查询向量和存储在 Elasticsearch 中的文档向量之间的余弦相似度。

 # Display the 128-dimension for each face detected
        i = 0
        for face_encoding in face_encodings:
            i += 1
            print("Face", i)
            response = es.search(
                index="faces",
                body={
                    "size": 1,
                    "_source": "face_name",
                    "query": {
                        "script_score": {
                            "query": {
                                "match_all": {}
                            },
                            "script": {
                                "source": "cosineSimilarity(params.query_vector, 'face_encoding')",
                                "params": {
                                    "query_vector": face_encoding.tolist()
                                }
                            }
                        }
                    }
                }
            )

假设分数低于 0.92 被认为是未知面孔:

for hit in response['hits']['hits']:
                # double score=float(hit['_score'])
                print("score: {}".format(hit['_score']))
                if float(hit['_score']) > 0.92:
                    print("==> This face  match with ", hit['_source']['face_name'], ",the score is", hit['_score'])
                else:
                    print("==> Unknown face")

执行上面的 Python 代码:

8.png

该脚本能够检测出得分匹配度高于 0.92 的所有面孔

搜寻进阶

面部识别和搜索可以结合使用,以用于高级用例。 你可以使用 Elasticsearch 构建更复杂的查询,例如 geo_queries,query-dsl-bool-query 和 search-aggregations。

例如,以下查询将 cosineSimilarity 搜索应用于200公里半径内的特定位置:

GET /_search 
{ 
  "query": { 
    "script_score": { 
      "query": { 
    "bool": { 
      "must": { 
        "match_all": {} 
      }, 
      "filter": { 
        "geo_distance": { 
          "distance": "200km", 
          "pin.location": { 
            "lat": 40, 
            "lon": -70 
          } 
        } 
      } 
    } 
  }, 
       "script": { 
                "source": "cosineSimilarity(params.query_vector, 'face_encoding')", 
                 "params": { 
                 "query_vector":[ 
                        -0.14664565,
                       0.07806452,
                       0.03944433,
                       ...
                       ...
                       ...
                       -0.03167224,
                       -0.13942884
                    ] 
                } 
           } 
    } 
  } 
}

将 cosineSimilarity 与其他 Elasticsearch 查询结合使用,可以无限地实现更复杂的用例。

结论

面部识别可能与许多用例相关,并且你可能已经在日常生活中使用了它。 上面描述的概念可以推广到图像或视频中的任何对象检测,因此你可以将用例扩展到非常大的应用场景。

参考:

相关实践学习
利用Elasticsearch实现地理位置查询
本实验将分别介绍如何使用Elasticsearch7.10版本进行全文检索、多语言检索和地理位置查询三个Elasticsearch基础检索子场景的实现。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
20天前
|
数据库 Python
Python实践:从零开始构建你的第一个Web应用
使用Python和轻量级Web框架Flask,你可以轻松创建Web应用。先确保安装了Python,然后通过`pip install Flask`安装Flask。在`app.py`中编写基本的"Hello, World!"应用,定义路由`@app.route('/')`并运行`python app.py`启动服务器。扩展应用,可添加新路由显示当前时间,展示Flask处理动态内容的能力。开始你的Web开发之旅吧!【6月更文挑战第13天】
44 2
|
25天前
|
Java Serverless 应用服务中间件
Serverless 应用引擎操作报错合集之部署python项目时,构建过程报错,怎么解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
|
2月前
|
缓存 自然语言处理 数据库
构建高效Python Web应用:异步编程与Tornado框架
【5月更文挑战第30天】在追求高性能Web应用开发的时代,异步编程已成为提升响应速度和处理并发请求的关键手段。本文将深入探讨Python世界中的异步编程技术,特别是Tornado框架如何利用非阻塞I/O和事件循环机制来优化Web服务的性能。我们将剖析Tornado的核心组件,并通过实例演示如何构建一个高效的Web服务。
|
17天前
|
关系型数据库 MySQL 数据库
如何使用Python的Flask框架来构建一个简单的Web应用
如何使用Python的Flask框架来构建一个简单的Web应用
35 0
|
1月前
|
存储 数据管理 测试技术
构建Python构建自动化测试框架(原理与实践)
当谈到软件质量保证时,自动化测试是一个不可或缺的步骤。Python作为一种简单易学的编程语言,具有丰富的测试框架和库,使得构建自动化测试框架变得相对简单。本文将介绍如何使用Python构建自动化测试框架,包括选择合适的测试框架、编写测试用例、执行测试和生成报告等方面。
构建Python构建自动化测试框架(原理与实践)
|
13天前
|
数据采集 机器学习/深度学习 数据可视化
利用Python和Pandas库构建高效的数据分析流程
在数据驱动的时代,数据分析已成为企业决策的关键环节。本文介绍如何利用Python编程语言及其强大的数据分析库Pandas,构建一套高效且可扩展的数据分析流程。与常规的数据分析流程不同,本文不仅涵盖数据加载、清洗、转换等基础步骤,还强调数据可视化、模型探索与评估等高级分析技巧,并通过实际案例展示如何在Python中实现这些步骤,为数据分析师提供一套完整的数据分析解决方案。
|
21天前
|
Python
python执行elasticsearch异常【已解决】
python执行elasticsearch异常【已解决】
18 2
|
21天前
|
Python 存储 数据处理
【Python数据类型的奥秘】:构建程序基石,驾驭信息之海
【Python数据类型的奥秘】:构建程序基石,驾驭信息之海
|
23天前
|
消息中间件 监控 调度
构建Python中的分布式系统结合Celery与RabbitMQ
在当今的软件开发中,构建高效的分布式系统是至关重要的。Python作为一种流行的编程语言,提供了许多工具和库来帮助开发人员构建分布式系统。其中,Celery和RabbitMQ是两个强大的工具,它们结合在一起可以为你的Python应用程序提供可靠的异步任务队列和消息传递机制。
|
3天前
|
数据采集 存储 自然语言处理
Python爬虫与数据可视化:构建完整的数据采集与分析流程
Python爬虫与数据可视化:构建完整的数据采集与分析流程

热门文章

最新文章

相关产品

  • 检索分析服务 Elasticsearch版