Ask Your Question
1

How do I load an OpenCV generated yaml file in python?

asked 2014-04-03 16:46:05 -0600

Lucas Walter gravatar image

It appears that OpenCV is writing yaml 1.0 but PyYaml only wants to read yaml 1.1 (http://pyyaml.org/#Whatimplementationshouldyouuse and http://stackoverflow.com/questions/12058510/xml-or-yml-parsing-in-opencv-with-python). Do I just need a script to reformat the yaml so it doesn't generate errors like expected alphabetic or numeric character, but found ':' on the first line %YAML:1.0, or is there a better way to go about loading the files?

I've tried cv2.cv.load(filename) but it generates and error about not finding xml tags (opencv 2.4.6 and whatever python-opencv bindings are provided in Ubuntu 13.10).

edit retag flag offensive close merge delete

Comments

1

Indeed YAML 1.0 is not supported by current YAML libraries (which require YAML 1.1 --- at the start of documents). I think opencv should switch over to YAML 1.1, I'm filing a bug report to that effect now.

B. Bogart gravatar imageB. Bogart ( 2015-12-18 11:29:54 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
0

answered 2016-02-19 16:53:53 -0600

Lucas Walter gravatar image

updated 2016-02-19 16:54:46 -0600

I ended up following this: http://stackoverflow.com/questions/28..., though I only needed to skip the first line, the rest of my yamls are not opencv data types, just strings and numbers and dicts and lists.

edit flag offensive delete link more
1

answered 2017-12-19 22:03:29 -0600

stiv-yakovenko gravatar image

To load camera calibration matrix like this:

%YAML:1.0
---
image_width: 4000
image_height: 3000
camera_matrix: !!opencv-matrix
   rows: 3
   cols: 3
   dt: d
   data: [ 3.1943912478853654e+03, 0., 1.9850941722590378e+03, 0.,
       3.2021356095317910e+03, 1.5509955246019449e+03, 0., 0., 1. ]
distortion_coefficients: !!opencv-matrix
   rows: 1
   cols: 5
   dt: d
   data: [ 1.3952810090687282e-01, -3.8313647492178071e-01,
       5.0555840762660396e-03, 2.3753464602670597e-03,
       3.3952514744179502e-01 ]

I use code like this:

import cv2
fs = cv2.FileStorage("./calib_asus_chess/cam_calib_asus.yml", cv2.FILE_STORAGE_READ)
fn = fs.getNode("camera_matrix")
print(fn.mat())

And it gives me:

[[  3.19439125e+03   0.00000000e+00   1.98509417e+03]
 [  0.00000000e+00   3.20213561e+03   1.55099552e+03]
 [  0.00000000e+00   0.00000000e+00   1.00000000e+00]]
edit flag offensive delete link more

Comments

@stiv-yakovenko this post is really old opencv 2.4.6 today it is opencv 3.4 (soon) and I hope that the answer is in doc for 3.4.0

LBerger gravatar imageLBerger ( 2017-12-20 02:13:52 -0600 )edit

See also.

To parse the classical node, it is something like this:

image_width = fs.getNode("image_width").real()
Eduardo gravatar imageEduardo ( 2017-12-20 04:52:18 -0600 )edit

Came here via Google--this is nowadays the correct answer and should be accepted!

speedymcs gravatar imagespeedymcs ( 2019-06-21 03:48:56 -0600 )edit
0

answered 2014-11-12 06:32:24 -0600

It's probably too late for you to use my answer, but for future reference... For my that works:

yaml_data = numpy.asarray(cv2.cv.Load("my_file.yaml"))

my_file.yaml is generated by some OpenCV application written in C++, and contains a cv::Mat 2D matrix.

edit flag offensive delete link more

Comments

It's more than just an array, it is a bunch of key value pairs. cv2.cv.Load on my yamls produce The node does not represent a user object (unknown type?) errors.

Lucas Walter gravatar imageLucas Walter ( 2016-02-19 16:42:16 -0600 )edit

It has disappeared since version 3.0...

paolo.rota gravatar imagepaolo.rota ( 2016-09-20 07:58:10 -0600 )edit

Question Tools

Stats

Asked: 2014-04-03 16:46:05 -0600

Seen: 16,331 times

Last updated: Dec 19 '17