覚え書きブログ

vod-converterのxmlファイルの改行

以下のvod-converterを用いて、kittiからvodに変換し、出力されたxmlファイルには改行が無い。webで見ると改行を自動的に付加してくるようだが、viで開くと一行になっているので、とても読みにくい。
github.com

つまり、こんな感じ。

<annotation><filename>006911.png</filename><folder>VOC2012</folder><segmented>0</segmented><size><depth>3</depth><width>1242</width><height>375</height></siz
e><source><annotation>Dummy</annotation><database>Dummy</database><image>Dummy</image></source><object><name>car</name><difficult>0</difficult><occluded>0</o
ccluded><truncated>0</truncated><pose>Unspecified</pose><bndbox><xmin>201.54</xmin><xmax>337.91</xmax><ymin>189.12</ymin><ymax>235.67</ymax></bndbox><positio
n><x>-11.23</x><y>1.89</y><z>23.91</z></position><rotation><object_angle>-0.62</object_angle><camera_y>-1.05</camera_y></rotation><dimensions><height>1.35</h
eight><width>1.6</width><length>3.86</length></dimensions></object><object><name>Van</name><difficult>0</difficult><occluded>0</occluded><truncated>0</trunca
ted><pose>Unspecified</pose><bndbox><xmin>220.01</xmin><xmax>346.11</xmax>

そこで、以下を参考に、改行するようにvoc.pyを変更した。
https://stackoverflow.com/questions/3095434/inserting-newlines-in-xml-file-generated-via-xml-etree-elementtree-in-python/33956544

変更した箇所は2箇所。まず、「#mport xml.etree.ElementTree as ET」をコメントアウトし、その代わりに「from lxml import etree as ET」でetreeモジュールを読み込む。次に、179行目の「 ET.ElementTree(xml_root).write(f"{annotations_path}/{image_id}.xml")」に以下のように「pretty_print=True」を追加した。

             ET.ElementTree(xml_root).write(f"{annotations_path}/{image_id}.xml",pretty_print=True)