Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading opencv yaml in python gives error "Input file is invalid in function 'open'"

I have following test.yaml:

Camera.ColourOrder: "RGB"
Camera.ColourDepth: "UCHAR_8"
Camera.Spectrum: "Visible_NearIR"
IMU.T_b_c1: !!opencv-matrix
   rows: 4
   cols: 4
   dt: f
   data: [0.999903, -0.0138036, -0.00208099, -0.0202141,
         0.0137985, 0.999902, -0.00243498, 0.00505961,
         0.0021144, 0.00240603, 0.999995, 0.0114047,
         0.0, 0.0, 0.0, 1.0]

I am trying to read it using python opencv cv2 module (because it contains opencv related objects like opencv_matrix embedded in yaml, which will give error if tried reading using python's inbuilt yaml module). But it is giving me error as shown below:

>>> import os
>>> 'test.yaml' in os.listdir()
True
>>> import cv2
>>> fs = cv2.FileStorage("test.yaml", cv2.FILE_STORAGE_READ)
cv2.error: OpenCV(4.8.1) /io/opencv/modules/core/src/persistence.cpp:699: error: (-5:Bad argument) Input file is invalid in function 'open'


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: <class 'cv2.FileStorage'> returned a result with an error set

I tried removing IMU.T_b_c1 and keeping just Camera.xyz values, still the same error. What I am missing here?

PS: is there any other way / module to read such yaml file with embedded opencv objects? I am ok with not using cv2 and read some other package / module.

like image 650
anir Avatar asked Nov 28 '25 18:11

anir


1 Answers

Judging by the error message and the implementation, OpenCV expects YAML data to be preceded by a YAML directive (%YAML). The directive is also shown in the documentation's sample.

Try the following (notice first line)

%YAML:1.0
Camera.ColourOrder: "RGB"
Camera.ColourDepth: "UCHAR_8"
Camera.Spectrum: "Visible_NearIR"
IMU.T_b_c1: !!opencv-matrix
   rows: 4
   cols: 4
   dt: f
   data: [0.999903, -0.0138036, -0.00208099, -0.0202141,
         0.0137985, 0.999902, -0.00243498, 0.00505961,
         0.0021144, 0.00240603, 0.999995, 0.0114047,
         0.0, 0.0, 0.0, 1.0]
like image 102
user2722968 Avatar answered Dec 01 '25 07:12

user2722968