PASCAL Annotation转换为VOC xml格式数据

来源:互联网 发布:java项目相对路径 编辑:程序博客网 时间:2024/06/05 12:28

本代码可以把PASCAL Annottation  Version 1.00的 .txt 数据转换为VOC格式的. xml 数据。


前提:得到 PASCAL Annotation Version 1.00 ,需要使用到其中的PASCALreadrecord.m文件。同时,我这里使用的数据集是INRIA数据集,所以只有一类目标(行人)。


代码如下:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PAS2XML.m% 作者:connie69% 博客:http://blog.csdn.net/connie69 % 邮箱:conniechen9469@gmail.com% 时间:2016/8/16%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 得到所有文件列表addpath('annotations');  %添加你存放标记文件的路径annotList = dir('annotations/*.txt');  %得到该路径下的所有txt文件annotListLen = length(annotList); % txt文件的数目for i = 1:annotListLen    % 由 PASCAL Annotaions Ver. 1.00 工具读取数据    fn = strcat('annotations/', annotList(i).name); %需要读取的文件名    record = PASreadrecord(fn); %读取数据        % 提取你所需的数据    imgname = record.imgname;    idx = regexp(imgname,'/');        imgsize = record.imgsize;    db = 'INRIA';     obj = record.objects;    object_num = length(obj);        % 建立一个xml文件,存储数据、    %这是你要存储的xml文件路径,比如我这个代码是存在当前工作目录下的‘Gen/Test/pos/文件名.xml’    path_data =  strcat('Gen/Test/pos/', annotList(i).name(1:end-4));    path_data = strcat(path_data, '.xml');         %下面建立节点,我用缩进代表层级    %建立根节点,可以看作是第一级    annotation = com.mathworks.xml.XMLUtils.createDocument('annotation');    annotationRoot = annotation.getDocumentElement;            %建立子节点,可以看作是第二级        folder = annotation.createElement('folder');        folder.appendChild(annotation.createTextNode(db));        annotationRoot.appendChild(folder);        filename = annotation.createElement('filename');        filename.appendChild(annotation.createTextNode(imgname(idx(2)+1:end)));        annotationRoot.appendChild(filename);        source = annotation.createElement('source');        annotationRoot.appendChild(source);            %建立该点的子节点,建立时用根节点建立,但是要依附到父节点而不是根节点,这里可以看作第三级            database = annotation.createElement('database');%用根节点建立            database.appendChild(annotation.createTextNode(db));            source.appendChild(database);%依附到父节点            annotaion_src = annotation.createElement('annotation');            annotaion_src.appendChild(annotation.createTextNode('PASCAL Annotaion Version 1.00'));            source.appendChild(annotaion_src);%            image = annotation.createElement('image');            image.appendChild(annotation.createTextNode('null'));            source.appendChild(image);%            flickrid_src = annotation.createElement('flickrid');            flickrid_src.appendChild(annotation.createTextNode('null'));            source.appendChild(flickrid_src);%        owner = annotation.createElement('owner');        annotationRoot.appendChild(owner);            flickrid_own = annotation.createElement('flickrid');            flickrid_own.appendChild(annotation.createTextNode('null'));            owner.appendChild(flickrid_own);%            name = annotation.createElement('name');            name.appendChild(annotation.createTextNode('null'));            owner.appendChild(name);%        size = annotation.createElement('size');        annotationRoot.appendChild(size);            width = annotation.createElement('width');            width.appendChild(annotation.createTextNode(num2str(imgsize(1))));            size.appendChild(width);%            height = annotation.createElement('height');            height.appendChild(annotation.createTextNode(num2str(imgsize(2))));            size.appendChild(height);%            depth = annotation.createElement('depth');            depth.appendChild(annotation.createTextNode(num2str(imgsize(3))));            size.appendChild(depth);%        segmented = annotation.createElement('segmented');        segmented.appendChild(annotation.createTextNode('0'));        annotationRoot.appendChild(segmented);        for j = 1:object_num %%一张图中可能有多个目标,需要循环建立多个object标注            object = annotation.createElement('object');            annotationRoot.appendChild(object);                name_obj = annotation.createElement('name');                name_obj.appendChild(annotation.createTextNode('person'));                object.appendChild(name_obj);%                pose = annotation.createElement('pose');                pose.appendChild(annotation.createTextNode(obj(j).orglabel));                object.appendChild(pose);%                truncated = annotation.createElement('truncated');                truncated.appendChild(annotation.createTextNode('0'));                object.appendChild(truncated);%                difficult = annotation.createElement('difficult');                difficult.appendChild(annotation.createTextNode('0'));                object.appendChild(difficult);%                bndbox = annotation.createElement('bndbox');                object.appendChild(bndbox);%                    xmin = annotation.createElement('xmin');                    xmin.appendChild(annotation.createTextNode(num2str(obj(j).bbox(1))));                    bndbox.appendChild(xmin);%                    ymin = annotation.createElement('ymin');                    ymin.appendChild(annotation.createTextNode(num2str(obj(j).bbox(2))));                    bndbox.appendChild(ymin);%                    xmax = annotation.createElement('xmax');                    xmax.appendChild(annotation.createTextNode(num2str(obj(j).bbox(3))));                    bndbox.appendChild(xmax);%                    ymax = annotation.createElement('ymax');                    ymax.appendChild(annotation.createTextNode(num2str(obj(j).bbox(4))));                    bndbox.appendChild(ymax);%        end    xmlwrite(path_data,annotation); %% 写入xml文件end


生成的一个例子:

<?xml version="1.0" encoding="utf-8"?><annotation>   <folder>INRIA</folder>   <filename>crop001001.png</filename>   <source>      <database>INRIA</database>      <annotation>PASCAL Annotaion Version 1.00</annotation>      <image>null</image>      <flickrid>null</flickrid>   </source>   <owner>      <flickrid>null</flickrid>      <name>null</name>   </owner>   <size>      <width>818</width>      <height>976</height>      <depth>3</depth>   </size>   <segmented>0</segmented>   <object>      <name>person</name>      <pose>UprightPerson</pose>      <truncated>0</truncated>      <difficult>0</difficult>      <bndbox>         <xmin>261</xmin>         <ymin>109</ymin>         <xmax>511</xmax>         <ymax>705</ymax>      </bndbox>   </object>   <object>      <name>person</name>      <pose>UprightPerson</pose>      <truncated>0</truncated>      <difficult>0</difficult>      <bndbox>         <xmin>31</xmin>         <ymin>326</ymin>         <xmax>209</xmax>         <ymax>712</ymax>      </bndbox>   </object>   <object>      <name>person</name>      <pose>UprightPerson</pose>      <truncated>0</truncated>      <difficult>0</difficult>      <bndbox>         <xmin>148</xmin>         <ymin>179</ymin>         <xmax>290</xmax>         <ymax>641</ymax>      </bndbox>   </object></annotation>


0 0
原创粉丝点击