VOC2007XML格式文件生成(针对EAD2019的图片和txt文档数据格式)

版权声明:本文为博主原创文章,转载请注明作者和出处。https://blog.csdn.net/xq920831/article/details/88868099

最近关注了MICCAI会议相关的竞赛板块Grand-Challenges ,其中一个关于肠镜目标检测的项目:EAD2019

官方给的数据有三批,前两批是训练数据,第三批是测试数据

数据为图片和标记文本一一对应,其中文本数据格式每行类似:0  0.20459  0.67055  0.01927  0.02873

数据意义如下图:

所以为了后面做目标检测的深度学习,就需要根据图片和文本生成XML文件

依据文本信息的意义,选用MATLAB生成,代码如下:

%%
clc;
clear all;
close all;
%注意修改下面四个变量
imgpath='C:/Users/Administrator/Desktop/EAD2019/ead2019_trainingData-I/EAD2019_challengeData_phase-I';%图像存放文件夹
txtpath='C:/Users/Administrator/Desktop/EAD2019/ead2019_trainingData-I/EAD2019_challengeBBoxAnnotations_phase-I';%txt文件
xmlpath_new='C:/Users/Administrator/Desktop/EAD2019/ead2019_trainingData-I/EAD2019_traindata_xml/';%修改后的xml保存文件夹
foldername='EAD2019';%xml的folder字段名
imglist=dir(imgpath);
nimgs=length(imglist)-2;

for imgind=1:nimgs
    lastname='begin';
    imgname=imglist(imgind+2).name
    imgfullname=[imgpath,'/',imgname];
    fprintf('[%d/%d]:%s\n',imgind,nimgs,imgfullname);
     img=imread(imgfullname);
     [h,w,d]=size(img);
     imshow(img);
      textfullname=[txtpath,'/',imgname(1:end-4),'.txt'];
      fidin=fopen(textfullname,'r');
            Createnode=com.mathworks.xml.XMLUtils.createDocument('annotation');
            Root=Createnode.getDocumentElement;%根节点
            node=Createnode.createElement('folder');
            node.appendChild(Createnode.createTextNode(sprintf('%s',foldername)));
            Root.appendChild(node);
            node=Createnode.createElement('filename');
            node.appendChild(Createnode.createTextNode(sprintf('%s',imgname)));
            Root.appendChild(node);
            source_node=Createnode.createElement('source');
            Root.appendChild(source_node);
            node=Createnode.createElement('database');
            node.appendChild(Createnode.createTextNode(sprintf('My Database')));
            source_node.appendChild(node);
            node=Createnode.createElement('annotation');
            node.appendChild(Createnode.createTextNode(sprintf('VOC2007')));
            source_node.appendChild(node);

           node=Createnode.createElement('image');
           node.appendChild(Createnode.createTextNode(sprintf('flickr')));
           source_node.appendChild(node);

           node=Createnode.createElement('flickrid');
           node.appendChild(Createnode.createTextNode(sprintf('NULL')));
           source_node.appendChild(node);
           owner_node=Createnode.createElement('owner');
           Root.appendChild(owner_node);
           node=Createnode.createElement('flickrid');
           node.appendChild(Createnode.createTextNode(sprintf('NULL')));
           owner_node.appendChild(node);

           node=Createnode.createElement('name');
           node.appendChild(Createnode.createTextNode(sprintf('gesture')));
           owner_node.appendChild(node);
           size_node=Createnode.createElement('size');
           Root.appendChild(size_node);

          node=Createnode.createElement('width');
          node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(w))));
          size_node.appendChild(node);

          node=Createnode.createElement('height');
          node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(h))));
          size_node.appendChild(node);

         node=Createnode.createElement('depth');
         node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(d))));
         size_node.appendChild(node);
         
          node=Createnode.createElement('segmented');
          node.appendChild(Createnode.createTextNode(sprintf('%s','0')));
          Root.appendChild(node);
      while ~feof(fidin)
       tline=fgetl(fidin);
       if tline(1)=='%' 
            tline=fgetl(fidin);   %%判断是否有%,如果有读取下一行
       end
       str = regexp(tline, ' ','split');
%        rectangle('Position',[str2double(str{5}),str2double(str{6}),str2double(str{7})-str2double(str{5}),str2double(str{8})-str2double(str{6})],'LineWidth',4,'EdgeColor','r'); %位置是从1开始
       x1 = str2double(str{2})-str2double(str{4})/2;
       x2 = x1+str2double(str{4});
       y1 = str2double(str{3})-str2double(str{5})/2;
       y2 = y1+str2double(str{5});
       x1 = x1*w; x2 = x2*w;
       y1 = y1*h; y2 = y2*h;

       rectangle('Position',[x1,y1,x2-x1,y2-y1],'LineWidth',4,'EdgeColor','r'); %位置是从1开始
       pause(0.1)
     
%         if strcmp(str{1},lastname)%如果类名相等,只需增加object
           object_node=Createnode.createElement('object');
           Root.appendChild(object_node);
           node=Createnode.createElement('name');
           node.appendChild(Createnode.createTextNode(sprintf('%s',str{1})));
           object_node.appendChild(node);
          
           node=Createnode.createElement('pose');
           node.appendChild(Createnode.createTextNode(sprintf('%s','Unspecified')));
           object_node.appendChild(node);
          
           node=Createnode.createElement('truncated');
           node.appendChild(Createnode.createTextNode(sprintf('%s','0')));
           object_node.appendChild(node);

           node=Createnode.createElement('difficult');
           node.appendChild(Createnode.createTextNode(sprintf('%s','0')));
           object_node.appendChild(node);
          
           bndbox_node=Createnode.createElement('bndbox');
           object_node.appendChild(bndbox_node);

           node=Createnode.createElement('xmin');
           node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(x1))));
           bndbox_node.appendChild(node);

           node=Createnode.createElement('ymin');
           node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(y1))));
           bndbox_node.appendChild(node);

           node=Createnode.createElement('xmax');
%           node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str2double(str{4})+str2double(str{6})))));
           node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(x2))));
           bndbox_node.appendChild(node);

           node=Createnode.createElement('ymax');
%           node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str2double(str{5})+str2double(str{7})))));
           node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(y2))));
           bndbox_node.appendChild(node);
        %处理最后一行
        if feof(fidin)
            tempname=imgname;
            tempname=strrep(tempname,'.jpg','.xml');
            xmlwrite(tempname,Createnode);
            %copyfile(imgfullname, 'JPEGImages');
        end
      end
fclose(fidin);

file=dir(pwd);
for i=1:length(file)
   if length(file(i).name)>=4 && strcmp(file(i).name(end-3:end),'.xml')
    fold=fopen(file(i).name,'r');
    fnew=fopen([xmlpath_new file(i).name],'w');
    line=1;
    while ~feof(fold)
        tline=fgetl(fold);
        if line==1
            line=2;
            tline=fgetl(fold);
        end
        expression = '   ';
        replace=char(9);
        newStr=regexprep(tline,expression,replace);
        fprintf(fnew,'%s\n',newStr);
    end
    fprintf('已处理%s\n',file(i).name);
    fclose(fold);
    fclose(fnew);
	delete(file(i).name);
   end
end
end

欢迎指正!

猜你喜欢

转载自blog.csdn.net/xq920831/article/details/88868099
今日推荐