OpenCV Python API的文件存储 [英] FileStorage for OpenCV Python API

查看:344
本文介绍了OpenCV Python API的文件存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用 FileStorage 类存储矩阵 XML / YAML ,使用OpenCV C ++ API。

I'm currently using FileStorage class for storing matrices XML/YAML using OpenCV C++ API.

但是,我必须编写一个Python脚本来读取这些 XML / YAML 文件。

However, I have to write a Python Script that reads those XML/YAML files.

对于可以读取由 OpenCV C ++ API 生成的 XML / YAML 文件的现有OpenCV Python API

I'm looking for existing OpenCV Python API that can read the XML/YAML files generated by OpenCV C++ API

推荐答案

您可以使用 PyYAML 解析YAML文件。

You can use PyYAML to parse the YAML file.

由于PyYAML不理解OpenCV数据类型,因此您需要为每个要加载的OpenCV数据类型指定一个构造函数。例如:

Since PyYAML doesn't understand OpenCV data types, you need to specify a constructor for each OpenCV data type that you are trying to load. For example:

import yaml
def opencv_matrix(loader, node):
    mapping = loader.construct_mapping(node, deep=True)
    mat = np.array(mapping["data"])
    mat.resize(mapping["rows"], mapping["cols"])
    return mat
yaml.add_constructor(u"tag:yaml.org,2002:opencv-matrix", opencv_matrix)


b $ b

一旦你这样做,加载yaml文件很简单:

Once you've done that, loading the yaml file is simple:

with open(file_name) as fin:
    result = yaml.load(fin.read())

一个字典,其中的键是你在YAML中保存的任何名称。

Result will be a dict, where the keys are the names of whatever you saved in the YAML.

这篇关于OpenCV Python API的文件存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆