混合文件下分离xml文件和jpg文件
import osimport shutildef split(src,img,annota):imges=[]#存储所有图片的路径annotation=[]#存储所有xml的路径#第一步:遍历需要分离的文件夹for f in os.listdir(src):if f.endswith(".jpg"):imges.append(f)if f.endswith(".jpeg"):.
·
import os
import shutil
def split(src,img,annota):
imges=[] #存储所有图片的路径
annotation=[] #存储所有xml的路径
#第一步:遍历需要分离的文件夹
for f in os.listdir(src):
if f.endswith(".jpg"):
imges.append(f)
if f.endswith(".jpeg"):
imges.append(f)
if f.endswith(".png"):
imges.append(f)
if f.endswith(".xml"):
annotation.append(f)
#第二步:创建目标图片文件夹和xml文件夹
if not os.path.isdir(img): #如果目标图片文件夹不存在
os.mkdir(img)
if not os.path.isdir(annota): #如果目标xml文件夹不存在
os.mkdir(annota)
#第三步:转移到目标文件夹中
for im in imges: #遍历所有的图片,将图片文件转移到目标文件夹中
new_path=os.path.join(src,im)
print(new_path)
shutil.copy(new_path,img)
for ann in annotation: #遍历所有的xml,将xml文件转移到目标文件夹中
new_path=os.path.join(src,ann)
print(new_path)
shutil.copy(new_path,annota)
if __name__=="__main__":
point="./" #相对路径
SrcDir="train" #需要分离的文件
dicimg="images" #目标图片文件
dicann="annotations" #目标的xml文件
src=os.path.join(point,SrcDir);
imges=os.path.join(point,dicimg)
annoations=os.path.join(point,dicann)
split(src,imges,annoations)
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)