It seems that versions prior to 3.1.0 do not have all FileStorage functionality implemented.
After version 3.2.0 this is no longer an issue, and all functionality we have in C++ is available in Python:
#WRITE
import numpy as np
import cv2
#writes array to .yml file
fs_write = cv2.FileStorage('new_data.yml', cv2.FILE_STORAGE_WRITE)
arr = np.random.rand(5, 5)
fs_write.write("floatdata", arr)
fs_write.release()
#READ
fs_read = cv2.FileStorage('new_data.yml', cv2.FILE_STORAGE_READ)
arr_read = fs_read.getNode('floatdata').mat()
fs_read.release()
File new_data.yml
%YAML:1.0
---
floatdata: !!opencv-matrix
rows: 5
cols: 5
dt: d
data: [ 1.3500562315570286e-01, 4.7332616739188160e-01,
7.8148724556477256e-01, 4.8515006212291845e-01,
5.3108316657166643e-03, 5.1800356136308345e-01,
5.2698594923003850e-01, 8.9589801430966309e-01,
6.9622407992566360e-01, 5.5301612450395854e-01,
8.9349016065812470e-02, 2.8443956686746197e-01,
8.1107151570238600e-01, 7.3436559904175847e-01,
9.4856255619457663e-01, 7.3314420186072704e-02,
6.5936190066337430e-01, 7.2222393075622360e-01,
1.9542185396859713e-01, 7.4787561019454296e-01,
3.1387065197283182e-01, 9.0187470903492872e-01,
2.2941454258655292e-01, 2.2337840652562313e-01,
2.6424822339902743e-01 ]
Array read from file:
In [23]: arr_read
Out[23]:
array([[ 0.13500562, 0.47332617, 0.78148725, 0.48515006, 0.00531083],
[ 0.51800356, 0.52698595, 0.89589801, 0.69622408, 0.55301612],
[ 0.08934902, 0.28443957, 0.81107152, 0.7343656 , 0.94856256],
[ 0.07331442, 0.6593619 , 0.72222393, 0.19542185, 0.74787561],
[ 0.31387065, 0.90187471, 0.22941454, 0.22337841, 0.26424822]])
To know which OpenCV version you are using in python, type in:
import cv2
cv2.__version__
you're not blind, that just does not exist.
the c++ code there heavily relies on operator-overloading for >> << [] and such. would need a lot of manual wrapping to get this translated to python.
thanks. that's too bad. I use the streams to write some values into a database (just as string) and so far I was able to communicate by that between c++ and python apps in a ROS setup. Do you know if something is planned in the future?
http://code.opencv.org/projects/openc...
might be total overkill, but sqlite is easy to use from c++ and python (also allows real binary serializatin)
That looks nice, I hope someone will do it.
@berak Still no progress in implementing filestorage write for python?
well, my 3.2 has cv2.FileStorage and cv2.FileNode
I am working with opencv 2.4.9 in python environment, and I also have those classes available. But there doesn't seem to be a way to WRITE to a file.
The operator[] outputs a type error 'cv2.FileStorage' object has no attribute '__getitem__'
And I tried to write to a file by doing :
But it is not working: TypeError: 'cv2.FileStorage' object does not support item assignment
(Dont know if it would be supposed to work anyway.. is there any other way to save data using filestorage object?)
Are these functionalities implemented in versions > 3.0.0 ?
both FileStorage andFileNode have a
write()
method. and there's amat()
methodto retrieve arraystry a :
Help just outputs the function prototypes with no additional help. FileStorage do not have write() method, could it be that I am using opencv 2.4.9?
These are the methods that are available for FileStorage in my version:
And fs.operator[] shows in the auto-complete but doesn't work (outputs syntax error)