把faster-rcnn检测出来的结果保存成txt,再转成xml

来源:互联网 发布:乐乎lofter 编辑:程序博客网 时间:2024/05/20 05:26

利用faster-rcnn检测图片,先把结果保存成txt,就像下面这样

利用下面这段代码就可以做到,把这段代码保存成XX.py,再运行。代码里需要改的地方都注释了,不知道怎么上传源码的,将就着用

[python] view plain copy
  1. #!/usr/bin/env python  
  2. # -*- coding: UTF-8 -*-   
  3. # --------------------------------------------------------  
  4. # Faster R-CNN  
  5. # Copyright (c) 2015 Microsoft  
  6. # Licensed under The MIT License [see LICENSE for details]  
  7. # Written by Ross Girshick  
  8. # --------------------------------------------------------  
  9.   
  10. """ 
  11. Demo script showing detections in sample images. 
  12.  
  13. See README.md for installation instructions before running. 
  14. """  
  15.   
  16. import _init_paths  
  17. from fast_rcnn.config import cfg  
  18. from fast_rcnn.test import im_detect  
  19. from fast_rcnn.nms_wrapper import nms  
  20. from utils.timer import Timer  
  21. import matplotlib.pyplot as plt  
  22. import numpy as np  
  23. import scipy.io as sio  
  24. import caffe, os, sys, cv2  
  25. import argparse  
  26.   
  27. CLASSES = ('__background__',  
  28.            'aeroplane''bicycle''bird''boat',  
  29.            'bottle''bus''car''cat''chair',  
  30.            'cow''diningtable''dog''horse',  
  31.            'motorbike''person''pottedplant',  
  32.            'sheep''sofa''train''tvmonitor')                                     #改成你的类别  
  33.   
  34. NETS = {'vgg16': ('VGG16',  
  35.                   'VGG16_faster_rcnn_final.caffemodel'),  
  36.         'zf': ('ZF',  
  37.                   'ZF_faster_rcnn_final.caffemodel')}  
  38.   
  39.   
  40. def vis_detections(image_name, class_name, dets, thresh=0.5):  
  41.     """Draw detected bounding boxes."""  
  42.     inds = np.where(dets[:, -1] >= thresh)[0]  
  43.     if len(inds) == 0:  
  44.         return  
  45.   
  46.     for i in inds:  
  47.         bbox = dets[i, :4]  
  48.         score = dets[i, -1]  
  49.     if(class_name == '__background__'):  
  50.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')                                                             #最终的txt保存在这个路径下,下面的都改  
  51.         fw.write(str(image_name)+' '+class_name+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')  
[python] view plain copy
  1.             #                    图片名                 标签名                          四个坐标  
  2.         fw.close()  
  3.     elif(class_name == 'aeroplane'):  
  4.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  5.         fw.write(str(image_name)+' '+class_name+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')  
  6.         fw.close()  
  7.         elif(class_name == 'bicycle'):      
  8.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  9.         fw.write(str(image_name)+' '+'n02835271'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')  #双人自行车  
  10.         fw.close()  
  11.         elif(class_name == 'bird'):        
  12.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  13.         fw.write(str(image_name)+' '+'n01833805'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')   #蜂鸟  
  14.         fw.close()  
  15.         elif(class_name == 'boat'):  
  16.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  17.         fw.write(str(image_name)+' '+'n04273569'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')    #快艇  
  18.         fw.close()  
  19.         elif(class_name == 'bottle'):  
  20.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  21.         fw.write(str(image_name)+' '+'n04557648'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')    #水瓶  
  22.         fw.close()  
  23.         elif(class_name == 'bus'):  
  24.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  25.         fw.write(str(image_name)+' '+'n03769881'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')    #小巴  
  26.         fw.close()  
  27.         elif(class_name == 'car'):  
  28.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  29.         fw.write(str(image_name)+' '+'n04461696'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')   #拖车  
  30.         fw.close()  
  31.         elif(class_name == 'cat'):  
  32.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  33.         fw.write(str(image_name)+' '+'n02123045'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')   #大花猫  
  34.         fw.close()  
  35.         elif(class_name == 'chair'):  
  36.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  37.         fw.write(str(image_name)+' '+'n02791124'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')    #理发椅  
  38.         fw.close()  
  39.         elif(class_name == 'cow'):  
  40.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  41.         fw.write(str(image_name)+' '+class_name+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')  
  42.         fw.close()  
  43.         elif(class_name == 'diningtable'):  
  44.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  45.         fw.write(str(image_name)+' '+class_name+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')  
  46.         fw.close()  
  47.         elif(class_name == 'dog'):  
  48.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  49.         fw.write(str(image_name)+' '+'n0211673'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')     #非洲猎犬,土狼狗,普猎犬,红腹锦鸡森林狼  
  50.         fw.close()  
  51.         elif(class_name == 'horse'):  
  52.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  53.         fw.write(str(image_name)+' '+'n12768682'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')  
  54.         fw.close()  
  55.         elif(class_name == 'motorbike'):  
  56.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  57.         fw.write(str(image_name)+' '+class_name+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')  
  58.         fw.close()  
  59.         elif(class_name == 'person'):  
  60.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  61.         fw.write(str(image_name)+' '+class_name+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')  
  62.         fw.close()  
  63.         elif(class_name == 'pottedplant'):  
  64.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  65.         fw.write(str(image_name)+' '+class_name+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')  
  66.         fw.close()  
  67.         elif(class_name == 'sheep'):  
  68.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  69.         fw.write(str(image_name)+' '+'n02415577'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')   #大角羊  
  70.         fw.close()  
  71.         elif(class_name == 'sofa'):  
  72.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  73.         fw.write(str(image_name)+' '+class_name+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')  
  74.         fw.close()  
  75.         elif(class_name == 'train'):  
  76.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  77.         fw.write(str(image_name)+' '+'n02917067'+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')  #子弹头列车  
  78.         fw.close()  
  79.         elif(class_name == 'tvmonitor'):  
  80.             fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  81.         fw.write(str(image_name)+' '+class_name+' '+str(int(bbox[0]))+' '+str(int(bbox[1]))+' '+str(int(bbox[2]))+' '+str(int(bbox[3]))+'\n')  
  82.         fw.close()  
  83.   
  84.   
  85. def demo(net, image_name):  
  86.     """Detect object classes in an image using pre-computed object proposals."""  
  87.   
  88.     # Load the demo image  
  89.     #im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)  
  90.     im_file = os.path.join('/','media','zc','A','Imagenet2012','img_train','n01440764',image_name)                  #改成你图片的位置  
  91.     im = cv2.imread(im_file)  
  92.   
  93.     # Detect all object classes and regress object bounds  
  94.     timer = Timer()  
  95.     timer.tic()  
  96.     scores, boxes = im_detect(net, im)  
  97.     timer.toc()  
  98.     print ('Detection took {:.3f}s for '  
  99.            '{:d} object proposals').format(timer.total_time, boxes.shape[0])  
  100.   
  101.     # Visualize detections for each class  
  102.     CONF_THRESH = 0.8  
  103.     NMS_THRESH = 0.3  
  104.      
  105.     #fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  106.     #fw.write(str(image_name)+'\t')  
  107.     #fw.close()  
  108.      
  109.     for cls_ind, cls in enumerate(CLASSES[1:]):  
  110.         cls_ind += 1 # because we skipped background  
  111.         cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]  
  112.         cls_scores = scores[:, cls_ind]  
  113.         dets = np.hstack((cls_boxes,  
  114.                           cls_scores[:, np.newaxis])).astype(np.float32)  
  115.         keep = nms(dets, NMS_THRESH)  
  116.         dets = dets[keep, :]  
  117.         vis_detections(image_name, cls, dets, thresh=CONF_THRESH)  
  118.     #fw = open('/media/zc/A/Imagenet2012/img_train/n01440764/result.txt','a')  
  119.     #fw.write('\n')  
  120.     #fw.close()  
  121.   
  122. def parse_args():  
  123.     """Parse input arguments."""  
  124.     parser = argparse.ArgumentParser(description='Faster R-CNN demo')  
  125.     parser.add_argument('--gpu', dest='gpu_id', help='GPU device id to use [0]',  
  126.                         default=0, type=int)  
  127.     parser.add_argument('--cpu', dest='cpu_mode',  
  128.                         help='Use CPU mode (overrides --gpu)',  
  129.                         action='store_true')  
  130.     parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16]',  
  131.                         choices=NETS.keys(), default='vgg16')  
  132.   
  133.     args = parser.parse_args()  
  134.   
  135.     return args  
  136.   
  137. if __name__ == '__main__':  
  138.     cfg.TEST.HAS_RPN = True  # Use RPN for proposals  
  139.   
  140.     args = parse_args()  
  141.   
  142.     prototxt = os.path.join(cfg.MODELS_DIR, NETS[args.demo_net][0],  
  143.                             'faster_rcnn_alt_opt''faster_rcnn_test.pt')  
  144.     caffemodel = os.path.join(cfg.DATA_DIR, 'faster_rcnn_models',  
  145.                               NETS[args.demo_net][1])  
  146.   
  147.     if not os.path.isfile(caffemodel):  
  148.         raise IOError(('{:s} not found.\nDid you run ./data/script/'  
  149.                        'fetch_faster_rcnn_models.sh?').format(caffemodel))  
  150.   
  151.     if args.cpu_mode:  
  152.         caffe.set_mode_cpu()  
  153.     else:  
  154.         caffe.set_mode_gpu()  
  155.         caffe.set_device(args.gpu_id)  
  156.         cfg.GPU_ID = args.gpu_id  
  157.     net = caffe.Net(prototxt, caffemodel, caffe.TEST)  
  158.   
  159.     print '\n\nLoaded network {:s}'.format(caffemodel)  
  160.   
  161.     # Warmup on a dummy image  
  162.     im = 128 * np.ones((3005003), dtype=np.uint8)  
  163.     for i in xrange(2):  
  164.         _, _= im_detect(net, im)  
  165.   
  166.    #im_names = ['000456.jpg', '000542.jpg', '001150.jpg',  
  167.     #            '001763.jpg', '004545.jpg']  
  168.       
  169.     fr = open('/media/zc/A/Imagenet2012/img_train/n01440764/temp.txt','r')          #这个txt里面保存的是图片的名字,一行一个  
  170.     for im_name in fr:  
  171.         im_name = im_name.strip('\n')  
  172.         print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'  
  173.         print 'Demo for data/demo/{}'.format(im_name)  
[python] view plain copy
  1.     demo(net, im_name)  
  2.   
  3. plt.show()  
  4. fr.close  

再用一个matlab代码,就可以把txt转化成xml,感谢小咸鱼的分享,如果你的图片是jpg,只要修改四个变量就能用,十分方便,如果是JPEG,下面还要修改两个地方,我注释了

[plain] view plain copy
  1. %%  
  2. %该代码可以做voc2007数据集中的xml文件,  
  3. %txt文件每行格式为:000002.jpg dog 44 28 132 121  
  4. %即每行由图片名、目标类型、包围框坐标组成,空格隔开  
  5. %如果一张图片有多个目标,则格式如下:(比如两个目标)  
  6. %000002.jpg dog 44 28 132 121  
  7. %000002.jpg car 50 27 140 110  
  8. %包围框坐标为左上角和右下角  
  9. %作者:小咸鱼_  
  10. %CSDN:http://blog.csdn.net/sinat_30071459  
  11. %%  
  12. clc;  
  13. clear;  
  14. %注意修改下面四个变量  
  15. imgpath='img\';%图像存放文件夹  
  16. txtpath='img\output.txt';%txt文件  
  17. xmlpath_new='Annotations/';%修改后的xml保存文件夹  
  18. foldername='VOC2007';%xml的folder字段名  
  19.   
  20.   
  21. fidin=fopen(txtpath,'r');  
  22. lastname='begin';  
  23.   
  24. while ~feof(fidin)  
  25.      tline=fgetl(fidin);  
  26.      str = regexp(tline, ' ','split');  
  27.      filepath=[imgpath,str{1}];  
  28.      img=imread(filepath);  
  29.      [h,w,d]=size(img);  
  30.       imshow(img);  
  31.       rectangle('Position',[str2double(str{3}),str2double(str{4}),str2double(str{5})-str2double(str{3}),str2double(str{6})-str2double(str{4})],'LineWidth',4,'EdgeColor','r');  
  32.       pause(0.1);  
  33.         
  34.         if strcmp(str{1},lastname)%如果文件名相等,只需增加object  
  35.            object_node=Createnode.createElement('object');  
  36.            Root.appendChild(object_node);  
  37.            node=Createnode.createElement('name');  
  38.            node.appendChild(Createnode.createTextNode(sprintf('%s',str{2})));  
  39.            object_node.appendChild(node);  
  40.             
  41.            node=Createnode.createElement('pose');  
  42.            node.appendChild(Createnode.createTextNode(sprintf('%s','Unspecified')));  
  43.            object_node.appendChild(node);  
  44.             
  45.            node=Createnode.createElement('truncated');  
  46.            node.appendChild(Createnode.createTextNode(sprintf('%s','0')));  
  47.            object_node.appendChild(node);  
  48.   
  49.            node=Createnode.createElement('difficult');  
  50.            node.appendChild(Createnode.createTextNode(sprintf('%s','0')));  
  51.            object_node.appendChild(node);  
  52.             
  53.            bndbox_node=Createnode.createElement('bndbox');  
  54.            object_node.appendChild(bndbox_node);  
  55.   
  56.            node=Createnode.createElement('xmin');  
  57.            node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{3}))));  
  58.            bndbox_node.appendChild(node);  
  59.   
  60.            node=Createnode.createElement('ymin');  
  61.            node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{4}))));  
  62.            bndbox_node.appendChild(node);  
  63.   
  64.            node=Createnode.createElement('xmax');  
  65.            node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{5}))));  
  66.            bndbox_node.appendChild(node);  
  67.   
  68.            node=Createnode.createElement('ymax');  
  69.            node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{6}))));  
  70.            bndbox_node.appendChild(node);  
  71.         else %如果文件名不等,则需要新建xml  
  72.            copyfile(filepath, 'JPEGImages');  
  73.             %先保存上一次的xml  
  74.            if exist('Createnode','var')  
  75.               tempname=lastname;  
  76.               tempname=strrep(tempname,'.jpg','.xml');                                                                          %你的图片是JPEG,这里就要把jpg改成JPEG   
  77.               xmlwrite(tempname,Createnode);     
  78.            end  
  79.               
  80.               
  81.             Createnode=com.mathworks.xml.XMLUtils.createDocument('annotation');  
  82.             Root=Createnode.getDocumentElement;%根节点  
  83.             node=Createnode.createElement('folder');  
  84.             node.appendChild(Createnode.createTextNode(sprintf('%s',foldername)));  
  85.             Root.appendChild(node);  
  86.             node=Createnode.createElement('filename');  
  87.             node.appendChild(Createnode.createTextNode(sprintf('%s',str{1})));  
  88.             Root.appendChild(node);  
  89.             source_node=Createnode.createElement('source');  
  90.             Root.appendChild(source_node);  
  91.             node=Createnode.createElement('database');  
  92.             node.appendChild(Createnode.createTextNode(sprintf('My Database')));  
  93.             source_node.appendChild(node);  
  94.             node=Createnode.createElement('annotation');  
  95.             node.appendChild(Createnode.createTextNode(sprintf('VOC2007')));  
  96.             source_node.appendChild(node);  
  97.   
  98.            node=Createnode.createElement('image');  
  99.            node.appendChild(Createnode.createTextNode(sprintf('flickr')));  
  100.            source_node.appendChild(node);  
  101.   
  102.            node=Createnode.createElement('flickrid');  
  103.            node.appendChild(Createnode.createTextNode(sprintf('NULL')));  
  104.            source_node.appendChild(node);  
  105.            owner_node=Createnode.createElement('owner');  
  106.            Root.appendChild(owner_node);  
  107.            node=Createnode.createElement('flickrid');  
  108.            node.appendChild(Createnode.createTextNode(sprintf('NULL')));  
  109.            owner_node.appendChild(node);  
  110.   
  111.            node=Createnode.createElement('name');  
  112.            node.appendChild(Createnode.createTextNode(sprintf('xiaoxianyu')));  
  113.            owner_node.appendChild(node);  
  114.            size_node=Createnode.createElement('size');  
  115.            Root.appendChild(size_node);  
  116.   
  117.           node=Createnode.createElement('width');  
  118.           node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(w))));  
  119.           size_node.appendChild(node);  
  120.   
  121.           node=Createnode.createElement('height');  
  122.           node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(h))));  
  123.           size_node.appendChild(node);  
  124.   
  125.          node=Createnode.createElement('depth');  
  126.          node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(d))));  
  127.          size_node.appendChild(node);  
  128.            
  129.           node=Createnode.createElement('segmented');  
  130.           node.appendChild(Createnode.createTextNode(sprintf('%s','0')));  
  131.           Root.appendChild(node);  
  132.           object_node=Createnode.createElement('object');  
  133.           Root.appendChild(object_node);  
  134.           node=Createnode.createElement('name');  
  135.           node.appendChild(Createnode.createTextNode(sprintf('%s',str{2})));  
  136.           object_node.appendChild(node);  
  137.             
  138.           node=Createnode.createElement('pose');  
  139.           node.appendChild(Createnode.createTextNode(sprintf('%s','Unspecified')));  
  140.           object_node.appendChild(node);  
  141.             
  142.           node=Createnode.createElement('truncated');  
  143.           node.appendChild(Createnode.createTextNode(sprintf('%s','0')));  
  144.           object_node.appendChild(node);  
  145.   
  146.           node=Createnode.createElement('difficult');  
  147.           node.appendChild(Createnode.createTextNode(sprintf('%s','0')));  
  148.           object_node.appendChild(node);  
  149.             
  150.           bndbox_node=Createnode.createElement('bndbox');  
  151.           object_node.appendChild(bndbox_node);  
  152.   
  153.          node=Createnode.createElement('xmin');  
  154.          node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{3}))));  
  155.          bndbox_node.appendChild(node);  
  156.   
  157.          node=Createnode.createElement('ymin');  
  158.          node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{4}))));  
  159.          bndbox_node.appendChild(node);  
  160.   
  161.         node=Createnode.createElement('xmax');  
  162.         node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{5}))));  
  163.         bndbox_node.appendChild(node);  
  164.   
  165.         node=Createnode.createElement('ymax');  
  166.         node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{6}))));  
  167.         bndbox_node.appendChild(node);  
  168.          
  169.        lastname=str{1};  
  170.         end  
  171.         %处理最后一行  
  172.         if feof(fidin)  
  173.             tempname=lastname;  
  174.             tempname=strrep(tempname,'.jpg','.xml');                                                         %你的图片是JPEG,这里就要把jpg改成JPEG   
  175.             xmlwrite(tempname,Createnode);  
  176.         end  
  177. end  
  178. fclose(fidin);  
  179.   
  180. file=dir(pwd);  
  181. for i=1:length(file)  
  182.    if length(file(i).name)>=4 && strcmp(file(i).name(end-3:end),'.xml')  
  183.     fold=fopen(file(i).name,'r');  
  184.     fnew=fopen([xmlpath_new file(i).name],'w');  
  185.     line=1;  
  186.     while ~feof(fold)  
  187.         tline=fgetl(fold);  
  188.         if line==1  
  189.            line=2;  
  190.            continue;  
  191.         end  
  192.         expression = '   ';  
  193.         replace=char(9);  
  194.         newStr=regexprep(tline,expression,replace);  
  195.         fprintf(fnew,'%s\n',newStr);  
  196.     end  
  197.     fprintf('已处理%s\n',file(i).name);  
  198.     fclose(fold);  
  199.     fclose(fnew);  
  200.     delete(file(i).name);  
  201.    end  
  202. end