Convert .mat annotation file into XML file

1.4k views Asked by At

I have some images, which I annotated using MATLAB's imageLabler tool. As a result I got gtruth.mat file (which contains annotations of all of the images).

Now I would like to train a simple object detector using these annotations. However, the object detector that I am using (dlib object detector), only accepts XML format.

Is it possible to convert that .mat file into an XML file, so that my all annotations remain intact and I can train my custom object detector? If so, how can I do that?

1

There are 1 answers

0
Dev-iL On

I will start by saying that this is only a partial solution to your problem, which should allow you to solve the rest of it on your own.


It appears that this question deals with converting a groundTruth object into dlib-friendly XML.

So first we need to ask ourselves, what is a groundTruth object? I have made a toy example using the Image Labeler and some images available in the MATLAB installation folder. Here's what I got:

gTruth = 
  groundTruth with properties:

          DataSource: [1×1 groundTruthDataSource]
    LabelDefinitions: [6×3 table]
           LabelData: [9×6 table]

>> gTruth.DataSource
ans = 
groundTruthDataSource for an image collection with properties

                      Source: {
                              ' ...\MATLAB\R2018b\help\symbolic\mupad_ref\adaptivemesh-d0e330.png';
                              ' ...\MATLAB\R2018b\help\symbolic\mupad_ref\adaptivemesh-d0e338.png';
                              ' ...\MATLAB\R2018b\help\symbolic\mupad_ref\adaptivemesh-d0e348.png'
                               ... and 6 more
                              }

>> gTruth.LabelDefinitions
ans =
  6×3 table
       Name         Type              Description        
    __________    _________    __________________________
    'Axis1'       Rectangle    ''                        
    'Axis2'       Rectangle    ''                        
    'Axis3'       Rectangle    ''                        
    'Equation'    Scene        'Image shows an equation.'
    'ThreeD'      Scene        'Image shows a 3D chart.' 
    'TwoD'        Scene        'Image shows a 2D chart.' 

>> gTruth.LabelData
ans =
  9×6 table
       Axis1           Axis2           Axis3        Equation    ThreeD    TwoD 
    ____________    ____________    ____________    ________    ______    _____
    [1×4 double]    [1×4 double]    [1×4 double]     false      true      false
    [1×4 double]    [1×4 double]    [1×4 double]     false      true      false
    [1×4 double]    [1×4 double]    [1×4 double]     false      true      false
    [1×4 double]    [1×4 double]    [1×4 double]     false      true      false
    []              []              []               true       false     false
    []              []              []               true       false     false
    []              []              []               true       false     false
    [1×4 double]    [1×4 double]    [1×4 double]     false      true      false
    [1×4 double]    [1×4 double]    []               false      false     true 

(The above contains 2 of the 3 available types of labels, the 3rd being Pixel Labels, which I decided to skip here.)

Now, what does a dlib-friendly XML look like? I'm not entirely sure, so I'll go with this one:

<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml-stylesheet type='text/xsl' href='image_metadata_stylesheet.xsl'?>
<dataset>
<name>imglab dataset</name>
<comment>Created by imglab tool.</comment>
<images>
  <image file='rel-path\1.jpg'>
    <box top='26' left='33' width='78' height='73'>
      <label>LabelName</label>
      <part name='1' x='67' y='68'/>
    </box>
  </image>
  ...
<images>
</dataset>

So the mapping you need appears to be:

  • For every image create an <image> node whose file attribute points to gTruth.DataSource.Source{1...n}.
  • In images that have a Rectangle-type label, create a <box> node.
    • The 4-element vector we have in gTruth.LabelData needs to be transformed into e.g. top='26' left='33' width='78' height='73'.
    • The name of the label goes to the <label> node.
  • (Guess #1) Images that have more than one label will have more than one <label> node.
  • (Guess #2) Images that have a Scene-type label, will not have a <box> node enclosing <label>.
  • No idea what the <part> node is for.

Creating a converter based on the above should be straightforward. You could use struct2xml to help you, and/or xmlwrite.