我有以下测试.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]

我正在try 使用python OpenCV cv2模块来读取它(因为它包含与OpenCV相关的对象,比如嵌入在YAML中的opencv_matrix,如果try 使用Python内置的YAML模块来读取,它将会给出错误).但它给我的错误如下所示:

>>> 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

我try 删除IMU.T_b_c1个值,只保留Camera.xyz个值,仍然是相同的错误.我在这里错过了什么?

PS:有没有其他方法/模块来读取这种嵌入了OpenCV对象的YAML文件?我可以不使用CV2,并阅读一些其他的包/模块.

推荐答案

从错误消息和the implementation判断,OpenCV期望YAML数据前面有一条YAML指令(%YAML).该指令还显示为in the documentation's sample.

try 以下操作(注意第一行)

%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]

Python相关问答推荐

Python中两个矩阵的自定义Hadamard风格产物

如果我已经使用了time,如何要求Python在12秒后执行另一个操作.sleep

如何销毁框架并使其在tkinter中看起来像以前的样子?

在for循环中仅执行一次此操作

跟踪我已从数组中 Select 的样本的最有效方法

在Pandas 日历中插入一行

配置Sweetviz以分析对象类型列,而无需转换

线性模型PanelOLS和statmodels OLS之间的区别

运行Python脚本时,用作命令行参数的SON文本

在Python Attrs包中,如何在field_Transformer函数中添加字段?

如何使用LangChain和AzureOpenAI在Python中解决AttribeHelp和BadPressMessage错误?

如何在Django基于类的视图中有效地使用UTE和RST HTIP方法?

Python脚本使用蓝牙运行在Windows 11与raspberry pi4

索引到 torch 张量,沿轴具有可变长度索引

需要帮助重新调整python fill_between与数据点

Pandas Data Wrangling/Dataframe Assignment

以逻辑方式获取自己的pyproject.toml依赖项

将标签移动到matplotlib饼图中楔形块的开始处

计算空值

ModuleNotFoundError:Python中没有名为google的模块''