Ask Your Question
0

Writing filestream with python?

asked 2015-02-17 09:56:22 -0600

updated 2015-02-17 09:56:31 -0600

Hello

Maybe I'm blind, but is there a way to write yaml-Files with Python using the filestorage functions? Normally, the docs contain both C++ and Python function calls, but python is missing here:

http://docs.opencv.org/modules/core/d...

edit retag flag offensive close merge delete

Comments

1

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.

berak gravatar imageberak ( 2015-02-17 10:00:42 -0600 )edit

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?

FooBar gravatar imageFooBar ( 2015-02-17 10:31:06 -0600 )edit

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)

berak gravatar imageberak ( 2015-02-17 10:35:52 -0600 )edit

That looks nice, I hope someone will do it.

FooBar gravatar imageFooBar ( 2015-02-17 11:02:17 -0600 )edit

@berak Still no progress in implementing filestorage write for python?

Pedro Batista gravatar imagePedro Batista ( 2017-03-06 07:10:46 -0600 )edit

well, my 3.2 has cv2.FileStorage and cv2.FileNode

berak gravatar imageberak ( 2017-03-06 07:24:02 -0600 )edit

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 :

fs = cv2.FileStorage('file.yml', cv2.FILE_STORAGE_WRITE)
fs['float_data'] = some_float_array,

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 ?

Pedro Batista gravatar imagePedro Batista ( 2017-03-06 08:34:03 -0600 )edit

both FileStorage andFileNode have a write() method. and there's a mat() methodto retrieve arrays

berak gravatar imageberak ( 2017-03-06 08:44:42 -0600 )edit

try a :

>>> help(cv2.FileStorage())
berak gravatar imageberak ( 2017-03-06 08:45:21 -0600 )edit

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:

fs.getFirstTopLevelNode  fs.open                  fs.release               fs.root                  
fs.isOpened              fs.operator[]            fs.releaseAndGetString

And fs.operator[] shows in the auto-complete but doesn't work (outputs syntax error)

Pedro Batista gravatar imagePedro Batista ( 2017-03-06 08:50:11 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-03-06 11:53:16 -0600

updated 2018-03-01 04:50:48 -0600

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__
edit flag offensive delete link more

Comments

Hi! For some reason it isn't working for me. I need to wrap FileStorage in a function. Could you provide more code example? Thanks a lot!

rory gravatar imagerory ( 2017-12-21 07:13:44 -0600 )edit

What opencv version are you using? I found out that only after 3.2.0 this was implemented in Python.

Pedro Batista gravatar imagePedro Batista ( 2017-12-21 07:30:50 -0600 )edit

Hi! I'm using 3.3.1. Actually, the problem is that I do not understand how to fill the array with my already extracted SIFT kp and des. I'm just a beginner in Python so sorry if it sounds like a trivial question.

rory gravatar imagerory ( 2017-12-21 08:24:37 -0600 )edit

If you have a problem, post a question with code :) That information is not enough to know what is wrong.

Pedro Batista gravatar imagePedro Batista ( 2017-12-21 09:30:00 -0600 )edit
1

This does not work for opencv 3.1.0.

headdab gravatar imageheaddab ( 2018-02-15 17:10:35 -0600 )edit

Is it possible to write a parent node containing children node with data in python?

xS-l gravatar imagexS-l ( 2018-03-24 18:00:37 -0600 )edit

@berak can we write a list of numpy array in yaml file :

pointsCamera.append(np.zeros(10,3), np.uint8)
pointsCamera.append(np.zeros(10,3), np.uint8)
fEchiquier = cv.FileStorage(pc.ficDonnees, cv.FILE_STORAGE_WRITE);
fEchiquier.write("Grille",pointsCamera)

Only way I found is to use a loop and save grille1, grille2, ...

LBerger gravatar imageLBerger ( 2018-12-21 03:37:22 -0600 )edit
1

@LBerger, imho it's only possible, if you make a numpy array from the list:

>>> a = np.ones((10,3),np.uint8)
>>> l = np.array([a*2, a*3, a*4])
>>> f = cv2.FileStorage("x.yml",1)
>>> f.write("list",l)
>>> f.release()

and back:

>>> f = cv2.FileStorage("x.yml",0)
>>> l = f.read("list").mat()
>>> print(l)

1st array is l[0][0], 2nd is l[0][1]

berak gravatar imageberak ( 2018-12-21 04:18:20 -0600 )edit

___ thanks I use :

pointsCamera.append(np.zeros(10,3), np.uint8)
pointsCamera.append(np.zeros(10,3), np.uint8)
p= np.asarray(pc.pointsCamera)
LBerger gravatar imageLBerger ( 2018-12-21 04:59:19 -0600 )edit
1

when matrices size are not equal it does not work :

a = np.ones((10,3),np.uint8)
b = np.ones((12,3),np.uint8)
l=np.array([a, b])
f = cv2.FileStorage("x.yml",1)
f.write("list",l)

Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: bad argument type for built-in operation

LBerger gravatar imageLBerger ( 2018-12-22 13:59:04 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-02-17 09:56:22 -0600

Seen: 6,078 times

Last updated: Mar 01 '18