Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store numpy.ndarray on DynamoDB?

Tags:

I have this numpy.ndarray generated by @ageitgey's facial_recognition Python library when I call the face_encodings function. I need to save this data to Amazon's DynamoDB; but I'm not sure how.

The numpy.ndarray that I get when I run the face_encodings function, is a representation of a person's face, from a given image. I can use this data to compare to another image, and check if the person (represented as an encoding) is present or not in that image.

I thought that I could save the numpy.ndarray as a binary (using numpy.ndarray.tobytes, but I'm not sure how to transform that binary (when I retrieve the data back from DynamoDB) back to numpy.ndarray.

My code to compare should be something like this:

unknown_encoding = face_recognition.face_encodings(unknown_picture)[0]
# database_encoding_array should come from DynamoDB
results = face_recognition.compare_faces(database_encoding_array, unknown_encoding, tolerance=0.595)
# results is an array of booleans

In summary, I don't know what's the best way to save a numpy.ndarray to DynamoDB, and how to query it at a later time.

like image 689
cesarvargas Avatar asked Feb 14 '19 17:02

cesarvargas


People also ask

Is Ndarray same as NumPy array?

NumPy is used to work with arrays. The array object in NumPy is called ndarray . We can create a NumPy ndarray object by using the array() function.

Is NumPy Ndarray a sequence?

A numpy array is a sequence, but it is not a Sequence as it is not registered as a subclass of Sequence.

Is Ndarray a matrix?

The numpy ndarray class is used to represent both matrices and vectors. To construct a matrix in numpy we list the rows of the matrix in a list and pass that list to the numpy array constructor. The first slice selects all rows in A, while the second slice selects just the middle entry in each row.


1 Answers

You can try converting results to a string of bytes using ndarray.tostring. This should be straightforward to work with for Dynamo.

arr = np.array([1, 2])

encoded = arr.tostring()
encoded
# b'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00'

You can then restore the array using np.frombuffer.

np.array_equal(arr, np.frombuffer(encoded, dtype=int))
# True
like image 87
cs95 Avatar answered Sep 17 '22 22:09

cs95