read .yml files in matlab

Go To StackoverFlow.com

6

I would like to read .yml files in Matlab. These files contain coordinates x and y of key points on a face image. I looked for different tools but I don't seem to find any answers.

My .yml files look like this

YAML:1.0
Image file: "00032009.jpg"
Contours count: 8
Contours:
   -
      Name: FO
      Count: 41
      Closed: 0
      Points:
         -
            x: 682.5947265625000000
            y: 743.1998901367187500
         -
            x: 685.9638061523437500
            y: 771.3800659179687500

......

and so on

Note 00032009.jpg is an image of a face x and y are coordinates of a point on a face Eg: the right corner of an eye etc

Could you please point out a way to read the file and then display the points on the face image?

Edit 1: Here is the error I get

Error: File: ReadYamlRaw.m Line: 14 Column: 11
Expression or statement is incorrect--possibly unbalanced (, {, or [.
Error in ==> ReadYaml at 38
    ry = ReadYamlRaw(filename, 0, nosuchfileaction);

What is weird is line 14 in ReadYamlRaw.m

[pth,~,~]= fileparts(mfilename('fullpath'));

 Parse error at ','(second one) and ']' usage appears to be invalid matlab syntax.

So what is the use of ~ in there and why is there an error?

Edit2: I replaced the ~ in the line above with dummy variables then I get this errors O_O

 Error using ==> ReadYamlRaw>scan at 81
Unknown data type: logical

Error in ==> ReadYamlRaw>scan_map at 138
            result.(ich) = scan(r.get(java.lang.String(ich)));

Error in ==> ReadYamlRaw>scan at 79
        result = scan_map(r);

Error in ==> ReadYamlRaw>scan_list at 116
        result{ii} = scan(i);

Error in ==> ReadYamlRaw>scan at 77
        result = scan_list(r);

Error in ==> ReadYamlRaw>scan_map at 138
            result.(ich) = scan(r.get(java.lang.String(ich)));

Error in ==> ReadYamlRaw>scan at 79
        result = scan_map(r);

Error in ==> ReadYamlRaw>load_yaml at 48
        result = scan(yaml.load(fileread([filename, fileext])));

Error in ==> ReadYamlRaw at 19
    result = load_yaml(filename, nosuchfileaction);

Error in ==> ReadYaml at 38
    ry = ReadYamlRaw(filename, 0, nosuchfileaction);

I also tried with a different yml file that looks like this

%YAML:1.0
RE-C:
   x: 919
   y: 580
LE-C:
   x: 1209
   y: 597
N-C:
   x: 1063
   y: 698
FO-B:
   x: 1045
   y: 1114
REL-O:
   x: 852
   y: 597
REL-I:
   x: 986
   y: 600
REL-T:
   x: 918
   y: 564

And I get the following errors

Java exception occurred:
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
         ^


    at org.yaml.snakeyaml.scanner.ScannerImpl.scanDirectiveName(ScannerImpl.java:1028)

    at org.yaml.snakeyaml.scanner.ScannerImpl.scanDirective(ScannerImpl.java:990)

    at org.yaml.snakeyaml.scanner.ScannerImpl.fetchDirective(ScannerImpl.java:534)

    at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:251)

    at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:179)

    at
    org.yaml.snakeyaml.parser.ParserImpl$ParseImplicitDocumentStart.produce(ParserImpl.java:198)

    at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:161)

    at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:146)

    at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:105)

    at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:121)

    at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:480)

    at org.yaml.snakeyaml.Yaml.load(Yaml.java:399)


Error in ==> ReadYamlRaw>load_yaml at 48
        result = scan(yaml.load(fileread([filename, fileext])));

Error in ==> ReadYamlRaw at 19
    result = load_yaml(filename, nosuchfileaction);

Error in ==> ReadYaml at 38
    ry = ReadYamlRaw(filename, 0, nosuchfileaction);

Maybe someone can make something out of these or you could point out another set of functions that would work? I searched but didn't find any except this one.

2012-04-04 18:39
by Michiru
that looks a lot like yaml to m - David Brown 2012-04-04 18:42
Have you tried yamlmatlab - yuk 2012-04-04 18:59
@ David Brown File is yml. I might be confused since I don't know that difference between .yml and .yaml. I've downloaded and added yamlmatlab but I don't know how to use it in my case yaml_file = 'test.yaml';

YamlStruct = ReadYaml(yaml_file); should it work if I replace yaml with yml?

Michiru 2012-04-04 19:06
@user1313569 Yes, the extension does not matter - David Brown 2012-04-04 19:15


10

It's YAML file indeed (as @DavidBrown mentioned in his comment, the extension does not matter). But it has some problems. Don't know if it's due to wrong YAML format or MATLAB implementation.

I've installed YAMLMATLAB and played a little with your file.

YamlStruct = ReadYaml(yaml_file);

YAMLMATLAB returns error if the files is feed as is. It works only if I comment the first line and remove spaces from field names. So the beginning of the file looks like this:

#YAML:1.0
Imagefile: 00032009.jpg
Contourscount: 8
...skipped the rest...

Then I get the correct structure. For example you can access the 1st point's x coordinate as

YamlStruct.Contours{1}.Points{1}.x

ans =

  682.5947

UPDATE

Space in filed names is actually a known problem in YAMLMATLAB. See the report and possible solution here.

UPDATE 2

According to comment from @Jirka_cigler (YAMLMATLAB developers group):

In the program release 0.4.3 we added support for whitespaces in field names, so the problem should not appear again.

Great!

I've also removed previous developers comment on the problem in the first UPDATE since it's not true any more.

2012-04-05 13:50
by yuk
Thank you for your reply. I get this error Error: File: ReadYamlRaw.m Line: 14 Column: 11 Expression or statement is incorrect--possibly unbalanced (, {, or [.Error in ==> ReadYaml at 38 ry = ReadYamlRaw(filename, 0, nosuchfileaction); I have put 0032009.yml into yaml_file variable, removed the spaces and commented first line as you said then read it but this error occurs. The folder and subfolders are added to the MATLAB path. Then am I missing something? Please help - Michiru 2012-04-05 19:15
I might add that i work with r2007b release because that is what my teacher told me to install not because I wanted to. Perhaps that is what causes the error - Michiru 2012-04-05 19:26
The error is strange. Probably the yamlmatlab were corrupted somehow. Try to redownload and install the latest version. Can you upload your yml-file somewhere? Some public sharing site may be? I'll have a look - yuk 2012-04-05 20:12
Sorry if the file was mine I would have given it to you but because it is not mine I can't distribute it. The error that appears is in ReadYaml function at [pth,~,~] says something like matlab can't parse the ',' and ']'. Can't remember exactly I'm not at my computer now - Michiru 2012-04-06 06:18
Show the full error message, when you can - yuk 2012-04-06 14:25
I edited my question. I also tried to test some files with my teacher on 2007 release but it seems there is something that matlab doesn't like. Feels like a dead end to me - Michiru 2012-04-09 18:09
I found this link ~ notation to denote missing inputs in function declarations, and missing outputs in calls to functions I didn't know know that and it seems to be a feature of 2009 release. So is it a dead end (for me re-installing Matlab is a dead end :D - Michiru 2012-04-09 18:54
I search for ~ symbol in the package and in context of this new feature it appears only in 3 statements. One is in selftest_yamlmatlab.m, line 42 (you probably don't need this file), then in ReadYamlRaw.m line 14 and WriteYaml line 7. In those 2 files you can just leave pth = ... or substitute ~ symbol with some dummy variable, like [pth,dummy,dummy] = .... I guarantee it will work. If it won't then the problem is not related to ~ - yuk 2012-04-09 19:53
Yes I've already tried that in line 14 ReadYamlRaw.m . Still doesn't work. Maybe it's the structure of the file but that's a blind guess - Michiru 2012-04-09 20:13
Did you still get the same error? Check that you saved the corrected ReadYamlRaw file to the proper directory returned by which ReadYamlRaw - yuk 2012-04-09 20:17
I edited the question with the errors I got.I saved ReadYamlRaw to the directory returned by which ReadYamlRaw . I don't know what to do anymore : - Michiru 2012-04-10 18:56
You should comment the 1st line with #, not % as in MATLAB - yuk 2012-04-10 19:44
Yeah I know that was already in the file I didn't add that. But now I've downloded version 0.1.1 from the old releases and it works only with the second file though and if I comment out %YAML 1.0. Also you can see that there are fields like REL-I: and it only works if i remove the - from every field. Oh well better something than nothing and thank you so much for your help. I'm marking you answer as correct since it could be of help for other people too and it help me also - Michiru 2012-04-11 20:12
I belong to the group of developers, so I'd like to react..

In the program release 0.4.3 we added support for whitespaces in field names, so the problem should not appear again - Jirka cigler 2012-10-05 08:18

@Jirkacigler: Thanks! I've updated the answer - yuk 2012-10-05 16:17


0

ZozaniDB Database Toolbox comes with a Matlab-native YAML implementation that handles the .yml file here. To parse a string or a file, use:

>> yaml_parse ( str )
>> yaml_read ( filename )
2015-03-14 07:34
by ahmetsacan
Ads