H
H
HexUserHex2021-10-18 20:29:26
Python
HexUserHex, 2021-10-18 20:29:26

Compare 'biometrics' of two faces with python and face_recognition module using SQL query?

Good evening,
I don’t know how clearly it turned out to reflect the essence of the issue in the title)

There is a Python code that, after recognizing a face in a photo, receives an array of data, comparing / calculating the Euclidean distance with another similar array, we will find out how similar two faces are in the photo.
I took this article as a basis:
article

The only thing I don't understand is how can I correctly save the received 'biometrics/array' into a relational database and then search only using a SQL query?

Example:

#!/usr/bin/env python3

import cv2
import face_recognition
import mysql.connector as mysql


def get_image_hash(image):
    # Открытие изображения
    img = face_recognition.load_image_file(image)

    # Blaсk Livеs Mаttеr
    #img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Получаем данные с лица
    vector = face_recognition.face_encodings(img)[0]

    vector = (str(vector),)
    
    return vector


# Open DB
conn = mysql.connect(
  host = '127.0.0.1',
  user = 'user',
  passwd = 'password'
)

cur = conn.cursor()
cur.execute("SHOW DATABASES")          

# Проверка на существование базы
db_found = False
for db in cur:
    if 'test' in db:
        db_found = True


if not db_found:
    cur.execute("CREATE DATABASE IF NOT EXISTS test;")
    conn.commit()
    
cur.execute("USE test;")


cur.execute("""CREATE TABLE IF NOT EXISTS faces(id_face BIGINT PRIMARY KEY NOT NULL AUTO_INCREMENT, face_hash TEXT)""")                            
                                         
new_image = get_image_hash('test.jpg')

# Сохранение в БД
cur.execute('''INSERT INTO faces (face_hash) VALUES(%s)''', new_image)
conn.commit()


# Загрузка фото для поиска
find_me_image = get_image_hash('findme.jpg')
#print('d: ', find_me_image[0])

# Как я здесь должен сравнивать массив полученный с только что загруженного фото с ранее сохраненными образцами в БД?
cur.execute("SELECT * FROM faces WHERE ..... ;")

cur.close()

print('find_me_image: ', str(find_me_image))
print('new_image: ', str(new_image))


If I understand correctly, at the moment I have only one way out:
extract all the faces from the database and then compare them in python using the pdist method: But what if there are millions of faces? How fast will it work? I assume that you need to have a colossal amount of RAM. ps: I'm willing to pay for a working code example!
pdist([vector1, vector2], 'euclidean')


Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Mirilaczvili, 2021-10-18
@2ord

Most recently I came across a description of one specialized DBMS.
https://milvus.io/docs/v2.0.0/overview.md
Try it. Maybe it will.

V
Vladimir Kuts, 2021-10-18
@fox_12

Here is a good solution using Elasticsearch

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question