-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelasticsearch-facerecognition.py
More file actions
81 lines (59 loc) · 1.95 KB
/
Copy pathelasticsearch-facerecognition.py
File metadata and controls
81 lines (59 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
## FIXME
# docker run -d --name es1 --net host -e 'discovery.type=single-node' elasticsearch:5-alpine
# docker run -d --name es1 -p 9200:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:8.15.0
# #
# python -m pip install elasticsearch face-recognition Pillow
# Connect to Elasticsearch
from elasticsearch import Elasticsearch
es = Elasticsearch('http://localhost:9200')
print('Elasticsearch info', es.info())
# Create index with vector field
index_body = {
'settings': {'number_of_shards': 1},
'mappings': {
'properties': {
'name': {'type': 'keyword'},
'embedding': {
'type': 'dense_vector',
'dims': 128,
'index': True,
'similarity': 'cosine'
}
}
}
}
es.indices.create(index='faces', body=index_body, ignore=400)
# Detect Face and Save Embedding
from PIL import Image
import face_recognition
# Load image
image2detect = Image.open('elasticsearch-detect.jpg')
# Detect faces
face_locations = face_recognition.face_locations(image2detect)
face_encodings = face_recognition.face_encodings(image2detect, face_locations)
# Save each face in Elasticsearch
for i, encoding in enumerate(face_encodings):
doc = {
'name': f'person_{i}',
'embedding': encoding.tolist()
}
es.index(index='faces', document=doc)
# Capture Face from Camera and Search
image2recognize = Image.open('elasticsearch-recognize.jpg')
# Detect faces
face_locations = face_recognition.face_locations(image2recognize)
face_encodings = face_recognition.face_encodings(image2recognize, face_locations)
for encoding in face_encodings:
query_vector = encoding.tolist()
# Search in Elasticsearch
search_body = {
'knn': {
'field': 'embedding',
'query_vector': query_vector,
'k': 1,
'num_candidates': 5
}
}
results = es.search(index='faces', body=search_body)
for hit in results['hits']['hits']:
print('Match:', hit['_source']['name'], 'Score:', hit['_score'])