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.
csv.writer() takes a file object, not a filename; with open('file.csv', 'wb') as outf: writer = csv.writer(outf)..csv file suffix in the pickle dump. also, shouldn't you be opening it in binary mode (i.e., 'wb')?pickle.dump() doesn't need a file opened in binary mode.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.
pickle.np.savezcan save several arrays to a ZIP archive.np.savetxtwrites a CSV like text file. It just arranges its input into a 2d array, and then writes it (with a format string), one row per file line. Nothing fancy.