读取xml文件转移picture

import os.path
import shutil

import cv2
import xml.etree.ElementTree as ET
import pdb
import os
import math

def each_file(imgPath, xmlPath, new_imgPath, new_xmlPath):
    '''
    读取每个文件夹,将遇到的指定文件统统转移到指定目录中
    :param filepath: 想要获取的文件的目录
    :param new_filepath: 想要转移的指定目录
    :return: 
    '''
    l_dir = os.listdir(xmlPath)  # 读取目录下的文件或文件夹

    for one_dir in l_dir:  # 进行循环
        full_imgPath = os.path.join('%s/%s' % (imgPath, one_dir.replace('txt','jpg')))  # 构造路径
        # full_imgPath = os.path.join('%s/%s' % (imgPath, one_dir) ) # 构造路径
        # new_full_xmlPath = os.path.join('%s/%s' % (new_xmlPath, one_dir))
        if os.path.isfile(full_imgPath):  # 如果是文件类型就执行转移操作
            if one_dir.split('.')[1] == 'txt':  # 只转移txt文件,修改相应后缀就可以转移不同的文件
                # full_xmlPath = os.path.join('%s/%s' % (xmlPath, one_dir.replace('jpg','xml')))  # 构造路径
                # new_full_xmlPath = os.path.join('%s/%s' % (new_xmlPath, one_dir.replace('jpg','xml')))
                new_full_imgPath = os.path.join('%s/%s' % (new_imgPath, one_dir.replace('txt','jpg')))

                # shutil.copy(full_xmlPath, new_full_xmlPath)  # 这个是转移的语句,最关键的一句话
                # shutil.copy(full_imgPath, new_full_imgPath)  # 这个是转移的语句,最关键的一句话
                shutil.move(full_imgPath, new_full_imgPath)

        else:   # 不为文件类型就继续递归
            # each_file(full_path, new_filepath)  # 如果是文件夹类型就有可能下面还有文件,要继续递归
            os.remove(os.path.join(xmlPath, one_dir))
            pass

def create_dir_not_exist(path):
    if not os.path.exists(path):
        os.mkdir(path)

if __name__ == '__main__':

    new_img_path = '/home/liuqunwei/YOLOv5_DOTA_OBB-master/data/train'
    new_xml_path = '/home/liuqunwei/YOLOv5_DOTA_OBB-master/data/train'
    img_path = "/home/liuqunwei/YOLOv5_DOTA_OBB-master/data/images/images"
    xml_path = "/home/liuqunwei/YOLOv5_DOTA_OBB-master/data/train"


    create_dir_not_exist(new_img_path)
    create_dir_not_exist(new_xml_path)

    each_file(img_path, xml_path, new_img_path, new_xml_path)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值