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.
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.
A numpy array is a sequence, but it is not a Sequence as it is not registered as a subclass of Sequence.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With