Xml文件处理 - xmlstarlet属性除法(用于图像注释)

时间:2018-02-19 15:39:35

标签: xml annotations dataset xmlstarlet

我有一个XML文件(如下所示)

<annotation>
<folder>Definitiva</folder>
<filename>armas (1)</filename>
<path>C:\Users\Rob\Desktop\Definitiva\armas (1).jpg</path>
<source>
  <database>Unknown</database>
</source>
<size>
  <width>240</width>
  <height>145</height>
  <depth>3</depth>
</size>
<segmented>0</segmented>
<object>
  <name>pistol</name>
  <pose>Unspecified</pose>
  <truncated>0</truncated>
  <difficult>0</difficult>
  <bndbox>
    <xmin>3</xmin>
    <ymin>1</ymin>
    <xmax>128</xmax>
    <ymax>100</ymax>
  </bndbox>
</object>

我希望从相对到绝对的边界框坐标。换句话说,我需要通过将其当前值(= 3)与图像的宽度(= 240)相比较来更新xmin值。我正在使用xmlstarlet和bash(我做了其他的,更容易的,修改并运行良好)但我编写的脚本产生错误(实际上,它完全删除了xmin属性)。你知道出了什么问题吗?

for name in *.xml; do X='/annotation/object/bndbox/xmin' 
Y='/annotation/size/width' Z=$X/$Y xmlstarlet ed --inplace -u 
'/annotation/object/bndbox/xmin' -v "$Z" "$name"; done

谢谢。

1 个答案:

答案 0 :(得分:0)

xmlstarlet 解决方案:

xmlstarlet ed -u '/annotation/object/bndbox/xmin' -x '. div /annotation/size/width' input.xml

输出(用于演示目的):

<?xml version="1.0"?>
<annotation>
  <folder>Definitiva</folder>
  <filename>armas (1)</filename>
  <path>C:\Users\Rob\Desktop\Definitiva\armas (1).jpg</path>
  <source>
    <database>Unknown</database>
  </source>
  <size>
    <width>240</width>
    <height>145</height>
    <depth>3</depth>
  </size>
  <segmented>0</segmented>
  <object>
    <name>pistol</name>
    <pose>Unspecified</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <bndbox>
      <xmin>0.0125</xmin>
      <ymin>1</ymin>
      <xmax>128</xmax>
      <ymax>100</ymax>
    </bndbox>
  </object>
</annotation>
  • div - Xpath 算术除法运算符(<operand_1> div <operand_2>