davisking / dlib

A toolkit for making real world machine learning and data analysis applications in C++
http://dlib.net
Boost Software License 1.0
13.56k stars 3.38k forks source link

How to properly store and retrieve face vector representation data from SQLite database? #2682

Closed rajhlinux closed 2 years ago

rajhlinux commented 2 years ago

Alright so I was able to store each face vector representation into SQLite database using:

st.bind(1, face_descriptors[i]);
st.exec();

However it seems like this will store the specific vector data as blob into SQLlite database and when I observe the data in the database it shows as gibberish characters. Alright, this is understandable and it is blob data after all... but blob data shows gibberish characters?

When I try to retrieve the blob face representation vector data using:

statement st2(db, "select * from vector_id");
st2.exec();

while(st2.move_next())
{
    string vector_db;

    st2.get_column(0, vector_db);

    cout << vector_db << endl;
}

It get the same gibberish data that was stored into the database.

In essence, a solution I can do is to convert each face_descriptors[i] vectors into strings, store it to database, but I have no idea how I can convert vector strings back into face_descriptors data type what dlib understands.

I took a look at the matrix.h file and it looks way too complicated for me to parse through.

What are some ways which I can store the vector data into database and retrieve it so that dlib can understand it for doing the face matching/clustering?

Thanks.

arrufat commented 2 years ago

I am not sure what you're trying to do, but maybe you could serialize them to b64 strings?

Check out http://dlib.net/file_to_code_ex.cpp.html.

rajhlinux commented 2 years ago

Thanks for the reply, how can I convert them back into face_descriptors datatype when retrieving the vector data from database?

arrufat commented 2 years ago

Did you read that example? It has all the information about going back and forth.

rajhlinux commented 2 years ago

Yes I have, but how do I add it into the face_descriptors vector?

I tried doing something like face_descriptors[i] = db_vector[i] and get segmentation fault.

arrufat commented 2 years ago

I don't know what type face descriptors are. Also, are you planning on reading the data from another non-dlib program? Why don't you just serialize to disk?

rajhlinux commented 2 years ago

I guess I'll just serialize to disk and do the clustering algo myself.

But I was really curious how to feed it back into dlibs code so that I do not need to make the clustering algo myself.

Here is the sample code for face recoginition on how face_descriptors is used: http://dlib.net/dnn_face_recognition_ex.cpp.html

std::vector<matrix<float,0,1>> face_descriptors = net(faces);

Planning to use everything within dlib program.

arrufat commented 2 years ago

I still can't see what prevents you from using dlib's clustering algorithm :/

rajhlinux commented 2 years ago

So I ended up reading how dlib's matrix algo works. Thank goodness Mr. King, has kindly gave examples how his matrix algo works. Had he not provided such explanation how his matrix code works, I guess many of us would be doomed for not knowing how and why the algo works.

Anyways I was able to easily and properly store and load face representation vectors to SQLite database using dlib's sqlite implementation simply using the dlib's matrix.

There is no need to do the deserialize/serialize since this adds extra overhead and computation when Mr. King already created a sqlite library which does this for us. I also did not want to simply store the data to "disk", I wanted to store it to a database directly using the SQLite API, like any professional would.

Anyone who wants to store face representation vectors to SQLite database, I recommend reading all of dlib's sqlite's header files and .cpp examples, read and understand how things are working. This will help to understand how SQLite is used and implemented.

I am using the SQLite's great blob data type implementation which Mr. King has also integrated into dlib's sqlite.

The face vectors are stored into the SQLite database as a blob. I have done a google on this and it seems many people are having the same issue to store vectors/array data into the SQLite and many people says that SQLite can not store vectors/arrays into the SQLite database. These conjectures are falsely wrong since the SQLite blob data type is specifically used for this case and can also store any arbitrary data like pictures, music, binary and etc.

I then recommend to read the dlib's dnn_face_recognition_ex.cpp sample code. It's really easy to understand how things are working and very organized.

Then read the matrix_ex.cpp and matrix_expressions_ex.cpp examples. This will help to understand how the face vectors are stored and create a matrix variable.

Thanks.