I'm getting a tuple of numpy arrays as (keypoint, descriptor)
when I run the compute
function to extract them from an image.
Is there a way to pack this tuple together so that I can save them to a file, or write them into a CSV as a row?
I'm getting a tuple of numpy arrays as (keypoint, descriptor)
when I run the compute
function to extract them from an image.
Is there a way to pack this tuple together so that I can save them to a file, or write them into a CSV as a row?
There are a few ways you can do this:
You can use Python csv
module, specifically writer
objects:
import csv
writer = csv.writer(open("file.csv","w"))
for row in array:
writer.writerow(str(row))
According to this question, there might be some formatting problems with this.
As mentioned in the comments, you can use numpy.savetxt()
. This is probably the best way:
numpy.savetxt("file.csv",array,delimiter=",")
You can also use the pickle
module:
import pickle
pickle.dump(array,open("file","w"))
As mentioned in the docs I linked above, pickle.load()
should not be used to load untrusted data.
Rather than using pickle
, I would recommend using cPickle
:
import cPickle as pickle
pickle.dump(array, open('file.p', 'wb'))
Note that all of your calls remain the same as when you are using pickle
-- only the import
line changes.
cPickle
is much faster than pickle
and with large arrays can save you a lot of time.
That said, when you're dealing with numpy
arrays, using numpy
's save functions -- e.g., np.savetxt
-- is your best bet.