Manju S Vijaykumar
Manju S Vijaykumar

Reputation: 21

how to insert a 128d vector into mongodb database using pymongo in python

I'm trying to insert an 128d vector which I generated for a face in a image containing multiple faces into MongoDB collection(vectors). I'm using the famous dlib library for generating the 128d vector. When I try to insert this vector into mongodb collection I got "cannot encode object error". The error is as below.

    File "/usr/local/lib/python2.7/dist-packages/pymongo/pool.py", line 610, in _raise_connection_failure
    raise error
bson.errors.InvalidDocument: Cannot encode object: dlib.vector([-0.078586, 0.0277601, 0.02961, 0.0263595, -0.0423636, -0.0593996, -0.0353243, -0.157486, 0.169706, -0.0115421, 0.215085, 0.0998522, -0.230498, -0.0380571, -0.0662888, 0.0504411, -0.0678306, -0.0943572, -0.123836, -0.0879753, -0.0753862, 0.000870723, 0.0786572, 0.0651935, -0.0732055, -0.294396, -0.108001, -0.122248, 0.0798309, -0.0558914, -0.00326786, -0.00399151, -0.201238, -0.0997921, 0.0628334, -0.0214193, -0.0168998, -0.00545083, 0.260324, -0.0224971, -0.137103, 0.0410911, 0.0381873, 0.228159, 0.101016, 0.0886697, 0.0711474, -0.12792, 0.0942142, -0.139165, 0.0716797, 0.147697, 0.0957785, -0.00807651, 0.0464634, -0.18575, 0.00923027, 0.0976636, -0.24552, 0.145688, 0.0765331, -0.0418556, -0.0641425, 0.00440269, 0.181549, 0.134916, -0.0709987, -0.182558, 0.168222, -0.238072, 0.041242, 0.10536, -0.0684752, -0.199106, -0.233173, 0.00511742, 0.417584, 0.176161, -0.11886, 0.0600367, -0.16006, -0.0130243, 0.0705707, -0.0569518, -0.136003, 0.0180192, -0.0785295, -0.00361975, 0.212427, 0.0941055, -0.064303, 0.178207, 0.00868456, 0.0107785, 0.0646739, 0.0319019, -0.11788, -0.046726, -0.129802, 0.00561518, -0.0292626, -0.0468726, 0.132234, 0.00913511, -0.159603, 0.0933984, -0.0159525, -0.0224207, 0.00211018, 0.119351, -0.154814, -0.0764414, 0.170755, -0.303818, 0.304808, 0.111342, 0.066825, 0.12282, 0.0600208, 0.0596608, -0.0402757, -0.017425, -0.0706421, -0.102285, 0.0109511, -0.0790169, 0.18963, 0.0300883])

I did try to convert this 128d vector to list, np array but no help.

Is there a way to insert 128d vector as it is using pymongo in MongoDB since I want compare 128d vectors for similarity later on.

The part of the code where I'm trying to insert vector into mongodb is as below.

face_descriptor = facerec.compute_face_descriptor(img, shape)
        print(face_descriptor)
        result = db.vectors.insert_one({"image": face_descriptor, "paths" : f})

your help is really appreciated. Thanks.

Upvotes: 2

Views: 3585

Answers (2)

Sreeragh A R
Sreeragh A R

Reputation: 3021

I just converted it to a list to save in DB.

face_descriptor_list = list(facerec.compute_face_descriptor(img, shape))
db.vectors.insert_one({"image": face_descriptor_list, "paths" : f})

Retrieval:

After fetching it from DB, convert back to dlib vector

img_data = db.vectors.find_one({...})
face_descriptor = dlib.vector(img_data['image'])

Upvotes: 1

Wan B.
Wan B.

Reputation: 18845

The PyMongo method insert_one() does not accept arbitrary object. Please see also PyMongo Tutorial: Inserting a Document

You should convert your vector object into a document. Please see MongoDB Data Modelling Introduction as a starter guide. For example, you could design as below:

doc = { '0': [-0.078586, 0.0277601, 0.02961, 0.0263595], 
        '1': [-0.078586, 0.0277601, 0.02961, 0.0263595] }

Make sure to take into consideration on how you're going to query it later. What's the field that you could use to retrieve the data later. See also MongoDB Indexes

An alternative could also be storing the Python pickle of the object. Example:

doc = { 'queryable_value': <pickle> }

As you can see there are various ways to design the schema, play around with different designs and see what suits your application best.

Upvotes: 0

Related Questions