将sunrgbd转为voc格式(三)

txt to xml

clc;

clear;
%ע���޸������ĸ�����
%imgpath='img/';%ͼ�����ļ���
txtpath='img/output2.txt';%txt�ļ�
xmlpath_new='Annotations/';%�޸ĺ��xml�����ļ���
foldername='VOC2007';%xml��folder�ֶ���


fidin=fopen(txtpath,'r');
lastname='begin';

while ~feof(fidin)
     tline=fgetl(fidin);
     if strfind(tline,'    ')
         tline=strrep(tline,'    ',' ')   
     end
     if strfind(tline,'   ')
         tline=strrep(tline,'   ',' ')   
     end
     if strfind(tline,'  ')
         tline=strrep(tline,'  ',' ')   
     end
     if strfind(tline,'     ')
         tline=strrep(tline,'     ',' ')   
     end
     if strfind(tline,'      ')
         tline=strrep(tline,'      ',' ')   
     end
     if strfind(tline,'      ')
         tline=strrep(tline,'      ',' ')   
     end
     str = regexp(tline, ' ','split');
     filepath=str{1};
     img=imread(filepath);
     [h,w,d]=size(img);
      imshow(img);
     rectangle('Position',[str2double(str{3}),str2double(str{4}),str2double(str{5})-str2double(str{3}),str2double(str{6})-str2double(str{4})],'LineWidth',4,'EdgeColor','r');
     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{2})));
           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(str{3}))));
           bndbox_node.appendChild(node);

           node=Createnode.createElement('ymin');
           node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{4}))));
           bndbox_node.appendChild(node);

           node=Createnode.createElement('xmax');
           node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{5}))));
           bndbox_node.appendChild(node);

           node=Createnode.createElement('ymax');
           node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{6}))));
           bndbox_node.appendChild(node);
        else %����ļ���ȣ�����Ҫ�½�xml
           copyfile(filepath, 'JPEGImages');
            %�ȱ�����һ�ε�xml
           if exist('Createnode','var')
              tempname=lastname;
              tempname=strrep(tempname,'.jpg','.xml');
              xmlwrite(tempname,Createnode);   
           end
            
            
            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',str{1})));
            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('xiaoxianyu')));
           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);
          object_node=Createnode.createElement('object');
          Root.appendChild(object_node);
          node=Createnode.createElement('name');
          node.appendChild(Createnode.createTextNode(sprintf('%s',str{2})));
          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(str{3}))));
         bndbox_node.appendChild(node);

         node=Createnode.createElement('ymin');
         node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{4}))));
         bndbox_node.appendChild(node);

        node=Createnode.createElement('xmax');
        node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{5}))));
        bndbox_node.appendChild(node);

        node=Createnode.createElement('ymax');
        node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{6}))));
        bndbox_node.appendChild(node);
       
       lastname=str{1};
        end
        %�������һ��
        if feof(fidin)
            tempname=lastname;
            tempname=strrep(tempname,'.jpg','.xml');
            xmlwrite(tempname,Createnode);
        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;
           continue;
        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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
将darknet格式数据集转为VOC格式数据集需要以下步骤和注意事项: 1. 首先,确保你已经下载了darknet格式的数据集,并且了解其文件结构。darknet格式的数据集通常包含一个文件夹用于存放图像,以及一个txt文件用于存放图像的标注信息,如目标的类别、边界框坐标等。 2. 创建一个新的文件夹,用于存放VOC格式的数据集。新的文件夹中通常包含个子文件夹:Annotations(用于存放xml格式的标注文件),JPEGImages(用于存放图像文件),以及ImageSets(用于存放训练、验证和测试的图像文件列表)。 3. 遍历darknet数据集中的每一张图像。将图像文件复制到VOC数据集的JPEGImages文件夹中。 4. 对于每张图像,根据对应的txt文件中的标注信息,创建一个相应的xml文件。这个xml文件应该包含图像的尺寸信息、目标的类别、边界框坐标等。 5. 将创建的xml标注文件保存到VOC数据集的Annotations文件夹中。 6. 在ImageSets文件夹中创建一个文本文件,用于记录训练、验证和测试的图像文件的文件名(不带文件扩展名)。 7. 根据你的需求,可以选择将图像文件随机划分为训练、验证和测试集,或者按照一定比例划分。 8. 最后,将处理后的图像文件名列表保存到对应的训练、验证和测试的txt文件中,并存放在ImageSets/Main/文件夹中。 需要注意的是,darknet和VOC格式的数据集之间的差异在于标注文件的格式VOC使用xml格式保存目标的类别、边界框坐标等信息,而darknet使用txt文本文件保存。因此,在转换格式时,需要根据VOC的xml格式创建对应的标注文件,并将相关信息从darknet的txt文件中提取出来保存到xml文件中。同时,还需要将图像文件和标注文件保存到相应的文件夹中,并生成对应的训练、验证和测试集的文件列表。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值