批量提取指定类别的xml和对应图片

'''
the function of the code
批量提取指定类别的xml和对应图片
author@bjtu_huangyuxiang
'''
from __future__ import division
import os
import xml.dom.minidom
import shutil

classes = ['messenger_wire']
dirnames = ['xml','images']


def extraction_box(ImgPath,AnnoPath,OutPath):
#判断输出路径是否存在,不存在的话就新建
    output_subdir = os.path.join(OutPath)
    if not os.path.exists(output_subdir):
        os.makedirs(output_subdir)
    outpath1 = os.path.join(output_subdir,dirnames[0]) + '/'
    if not os.path.exists(outpath1):
        os.makedirs(outpath1)
    outpath2 = os.path.join(output_subdir,dirnames[1]) + '/'
    if not os.path.exists(outpath2):
        os.makedirs(outpath2)
        
    i = 0
    imagelist = os.listdir(ImgPath)
    for image in imagelist:
        image_pre, ext = os.path.splitext(image)#分割文件名和后缀
        imgfile = ImgPath + image_pre + '.jpg'
        if not os.path.exists(AnnoPath + image_pre + '.xml' ):
            continue
        xmlfile = AnnoPath + image_pre + '.xml'
        DomTree = xml.dom.minidom.parse(xmlfile)
        root = DomTree.documentElement #获得xml文档对象
        objectlist = root.getElementsByTagName('object') #获得xml文档目标名称
        for objects in objectlist:
            namelist = objects.getElementsByTagName('name')
            objectname = namelist[0].childNodes[0].data
            #print(objectname)
            if objectname == classes[0]:
                shutil.copy(xmlfile,outpath1)
                shutil.copy(imgfile,outpath2)
                print("提取%s,%s"%(imgfile,xmlfile))
                i = i+1
    print("%s files have been selected!"%i)

if __name__ == "__main__":
    ImgPath = r'D:\desktop\S3_data哈/'
    AnnoPath = r'D:\desktop\xml存储/'
    OutPath = r'D:\desktop\tiqu2/'
    extraction_box(ImgPath, AnnoPath, OutPath)
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值