from PIL import Image
import os
import os.path
import numpy as np
import glob
import pandas as pd
import xml.etree.ElementTree as ET
import time
start = time.time()
n =0# 指明被遍历的文件夹
rootdir =r'D:/数据集/XXXX/0bag_color/'for parent, dirnames, filenames in os.walk(rootdir):# 遍历每一张图片for filename in filenames:# print('parent is :' + parent)# print('filename is :' + filename)
currentPath = os.path.join(parent, filename)# 目前图片的地址
picture_number = filename[:-4]#图片去掉.png后的名字print("the picture name is:"+ filename)# print('the full name of the file is :' + currentPath)# 读取图片# print (img.format, img.size, img.mode)# img.show()
xml_path =r'D:/数据集/XXXX/xml/'#xml文件存放的文件夹地址
xml_file = os.listdir(xml_path)#将xml文件存放在xml_file列表里面if picture_number +'.xml'in xml_file:#如果图片有对应的xml文件则跳过passelse:print(currentPath)
n +=1
os.remove(currentPath)#没有对应的xml文件则删除
end = time.time()print("Execution Time:", end - start)print("删除的照片数为:",n)
移动未标注的图像到父目录的move文件夹下,没有move文件夹则自动创建文件夹
from PIL import Image
import os
import os.path
import numpy as np
import glob
import pandas as pd
import xml.etree.ElementTree as ET
import time
import shutil
defmymovefile(srcfile, dstfile):ifnot os.path.isfile(srcfile):print("%s not exist!"%(srcfile))else:
fpath, fname = os.path.split(dstfile)# 分离文件名和路径ifnot os.path.exists(fpath):
os.makedirs(fpath)# 创建路径# 移动文件print("move %s -> %s"%(srcfile, dstfile))
start = time.time()
n =0# 指明被遍历的文件夹,文件夹下可以有很多文件夹,这个遍历会找文件夹下的picture文件夹。图片都存放在文件夹“picture”下,xml文件存放在文件夹“xml”下
rootdir =r'D:/数据集/灰机/'for parent, dirnames, filenames in os.walk(rootdir):for dirname in dirnames:print(dirname)if dirname =="picture": 先找到名字为picture的文件夹
currentPath = os.path.join(parent, dirname)print(currentPath)ifnot os.path.isdir(os.path.join(parent,"move")):
os.mkdir(os.path.join(parent,"move"))#没有move文件夹则创建move文件夹for root, dirs, files in os.walk(currentPath):#找到picture文件夹的父目录,即currentPath后,遍历picture内的图片forfilein files:# print('parent is :' + parent)# print('filename is :' + filename)
picturePath = os.path.join(root,file)# 目前图片的地址
picture_number =file[:-4]print("the picture name is:"+file)print('the full name of the file is :'+ picturePath)
xml_path = os.path.join(parent,"xml")
xml_file = os.listdir(xml_path)if picture_number +'.xml'in xml_file:passelse:
n +=1
srcfile = os.path.join(currentPath,file)
dstfile = os.path.join(parent,"move",file)
shutil.move(srcfile, dstfile)//把图片从picture内移动到picture文件夹父文件夹下的“move”文件夹
end = time.time()print("Execution Time:", end - start)print("移动的照片数为:", n)