Reputation: 11
I have a sequence of .yml
files generated by opencv
that I was trying to read into MATLAB using yamlmatlab
, but I am getting the following error:
y_data = ReadYaml(yaml_file);
Error using ReadYamlRaw>load_yaml (line 78)
while scanning a directive in "<string>", line 1, column 1: %YAML:1.0 ^ expected alphabetic or numeric character, but found :(58) in "<string>", line 1, column 6: %YAML:1.0 ^
My YAML Files look like the following:
%YAML:1.0
Vocabulary: !!opencv-matrix
rows: 100
cols: 78
dt: f
data: [ 1.00037329e-001, 8.75103176e-002, 1.09445646e-001,
1.05232671e-001, 6.78173527e-002, 9.65989158e-002,
1.62132218e-001, 1.56320035e-001, 1.12932988e-001,
1.27447948e-001, 1.88054979e-001, 1.88775390e-001,.....
And
%YAML:1.0
---
vocabulary: !!opencv-matrix
rows: 100
cols: 1
dt: f
data: [ 3.54101445e-04, 1.23916077e+02, 9.93522644e+01,
2.42377838e+02, 3.53855858e+01, 1.69853516e+02, 5.81151466e+01,
8.07454453e+01, 1.83035984e+01, 2.13557846e+02, 1.52394699e+02,
1.10933914e+02, ......
I have tried it with YAMLMatlab
but am still getting the same error. Please help how to read these file and convert them into .mat files.
Upvotes: 0
Views: 1737
Reputation: 11
You can use the parser I wrote and published recently on matlabcentral and github, cvyamlParser. It can handle the header in yaml file properly.
https://zenodo.org/record/2703498#.XNg20NMzafU
https://github.com/tmkhoyan/cvyamlParser
https://in.mathworks.com/matlabcentral/fileexchange/71508-cvyamlparser
It is a MEX-file compiled for linux and osx. You can use the src file and instructions on to compile a windows version. It will take a yaml file written by open cv and convert it to a structure with the same variable names as provided in the yaml. The variable data type is inferred at runtime, optionally you can use sorting for variables that have a numerical index like A1,A2,A4,A5 etc. Use it like so:
s = readcvYaml('../data/test_data.yaml')
s =
struct with fields:
matA0: [1000×3 double]
matA1: [1000×3 double]
matA2: [1000×3 double]
Or with sorting:
s = readcvYaml('../data/test_data.yaml','sorted')
s =
struct with fields:
matA: [1×3 struct]
Upvotes: 1
Reputation: 65460
It appears that the linked library (which appears to use SnakeYAML
under the hood) is not able to parse the YAML 1.0 YAML directive which contains a colon (:
) rather than a space in later versions of the specification.
%YAML:1.0
Became:
%YAML 1.2
It appears that the contents of the YAML file are compatible with newer YAML formats, so you could try remove the directive from the file prior to parsing (delete the first line).
As far as converting once you have the data loaded into MATLAB, you should be able to do something like:
% Read the yaml file
yaml = yaml.ReadYaml(yaml_file);
% Load in the matrix and reshape into the desired size
mat = reshape(yaml.data, yaml.cols, yaml.rows).';
% Save to .mat file
save('output.mat', 'mat')
Upvotes: 0