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)
读取xml文件转移picture
最新推荐文章于 2024-06-20 17:22:39 发布