The translation of the material was prepared within the framework of the practical intensive "Centralized logging systems Elastic stack" .
Have you ever tried to look for objects in images? Elasticsearch can help you store, analyze, and search for objects in images or videos.
, Python. , - .
? .
- , , ( ). , , . :
: .
:
:
.
128-
.
Elasticsearch dense_vector ββ . 2048, .
.
,
:
Python: Python 3, . .
Elasticsearch: Elastic Cloud.
-
Python Elasticsearch: Python Elasticsearch.
, Ubuntu 20.04 LTS Ubuntu 18.04 LTS. .
Python Python
Ubuntu 20 Debian Linux Python 3. , Python .
, , :
sudo apt update sudo apt upgrade
, Python - 3.x:
python3 -V
pip3 Python:
sudo apt install -y python3-pip
cmake, face_recognition:
pip3 install CMake
cmake bin $PATH:
export PATH=$CMake_bin_folder:$PATH
, :
pip3 install dlib pip3 install numpy pip3 install face_recognition pip3 install elasticsearch
face_recognition, 128- .
getVectorFromPicture.py:
touch getVectorFromPicture.py
:
import face_recognition
import numpy as np
import sys
image = face_recognition.load_image_file("$PATH_TO_IMAGE")
# 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())
getVectorFromPicture.py
, Elastic. $PATH_TO_IMAGE, .
Elasticsearch.
, dense_vector
:
# Store the face 128-dimension in Elasticsearch
## Create the mapping
curl -XPUT "http://localhost:9200/faces" -H 'Content-Type: application/json' -d'
{
"mappings" : {
"properties" : {
"face_name" : {
"type" : "keyword"
},
"face_encoding" : {
"type" : "dense_vector",
"dims" : 128
}
}
}
}'
, Index API:
## Index the face feature representation
curl -XPOST "http://localhost:9200/faces/_doc" -H 'Content-Type: application/json' -d'
{
"face_name": "name",
"face_encoding": [
-0.14664565,
0.07806452,
0.03944433,
...
...
...
-0.03167224,
-0.13942884
]
}'
, Elasticsearch, Elastic. .
recognizeFaces.py:
touch recognizeFaces.py
, , Elasticsearch:
:
import face_recognition
import numpy as np
from elasticsearch import Elasticsearch
import sys
Elasticsearch:
# Connect to Elasticsearch cluster
from elasticsearch import Elasticsearch
es = Elasticsearch(
cloud_id="cluster-1:dXMa5Fx...",
http_auth=("elastic", "<password>"),
)
cosineSimilarity
, Elasticsearch.
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,93 :
for hit in response['hits']['hits']:
#double score=float(hit['_score'])
if (float(hit['_score']) > 0.93):
print("==> This face match with ", hit['_source']['face_name'], ",the score is" ,hit['_score'])
else:
print("==> Unknown face")
:
0,93.
. 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 . 14- Elastic Cloud, Elasticsearch, .