Skip to main content
 首页 » 编程设计

Python之blender XML 注释文件(不适用于 matlab 代码)

2024年10月01日852php

我已经使用 blender-python 创建了一个 XML 文件(注释),但问题是当我将它与 Matlab 代码一起使用来读取它时,它给了我以下错误:

Error in VOCxml2struct>parse (line 10) 
if ~isempty(parent)&&xml(ind)~='<' 

来自这个函数:

res=[]; 
 
if ~isempty(parent)&&xml(ind)~='<' 
 
i=findchar(xml,ind,'<'); 
 
res=trim(xml(ind:i-1)); 
 
ind=i; 
 
[tag,ind]=gettag(xml,i); 
 
if ~strcmp(tag,['/' parent]) 
 
    error('<%s> closed with <%s>',parent,tag); 
end 

鉴于来自 Pascal_Voc 数据集的 XML 文件使用相同的代码可以正常工作并且当您看到这两个 XML 文件看起来相同时,这有点令人困惑,我已经手动创建了所需的标签,下面是我的 blender-python 代码,希望如此,有人建议我修复它。

谢谢大家的宝贵时间。

with open("000007.xml", "w", encoding="utf-8") as file: 
  file.write ("<annotation>\n") 
  file.write (" <folder>VOC2007</folder>\n") 
  file.write (" <filename>" + '000007.jpg' + "</filename>\n") 
  file.write (" <source>\n") 
  file.write ("     <database>The VOC2007 Database</database>\n") 
  file.write ("     <annotation>PASCAL VOC2007</annotation>\n") 
  file.write ("     <image>flickr</image>\n") 
  file.write ("     <flickrid>194179466</flickrid>\n") 
  file.write (" </source>\n") 
  file.write (" <owner>\n") 
  file.write ("     <flickrid>monsieurrompu</flickrid>\n") 
  file.write ("     <name>Thom Zemanek</name>\n") 
  file.write (" </owner>\n") 
  file.write (" </size>\n") 
  file.write ("     <width>500</width>\n") 
  file.write ("     <height>333</height>\n") 
  file.write ("     <depth>3</depth>\n") 
  file.write (" </size>\n") 
  file.write (" <segmented>0</segmented>\n") 
  file.write (" <object>\n") 
  file.write ("     <name>car</name>\n") 
  file.write ("     <pose>Unspecified</pose>\n") 
  file.write ("     <truncated>1</truncated>\n") 
  file.write ("     <difficult>0</difficult>\n") 
  file.write ("     <bndbox>") 
  file.write ("\n           <xmin>" + '141' + "</xmin>") 
  file.write ("\n           <ymin>" + '50' + "</ymin>") 
  file.write ("\n           <xmax>" + '500' + "</xmax>") 
  file.write ("\n           <ymax>" + '330' + "</ymax>") 
  file.write ("\n       </bndbox>\n") 
  file.write (" </object>\n") 
  file.write ("</annotation>\n") 

请您参考如下方法:

Question ... when you see both XML files they look identical

不要相信你的眼睛使用diff程序来比较!
使用像 xmlvalidation.com 这样的 XML 验证器

不知道它是否与 Matlab 错误有关,但您的代码和由此产生的 XML 有以下错误:

# Missing Header 
<?xml version="1.0" encoding="UTF-8"?> 
 
# First has to be <size> not closing 
file.write (" </size>\n") 

考虑使用 xml Python 模块创建 XML。