简体   繁体   中英

How to store numpy.ndarray on DynamoDB?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM