roLabelImg标注的xml文件和文件夹下的图片匹配,删除未标注的图像/移动未标注的图像

1、删除未标注的图像

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文件则跳过
            pass
        else:
            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


def mymovefile(srcfile, dstfile):
    if not os.path.isfile(srcfile):
        print("%s not exist!" % (srcfile))
    else:
        fpath, fname = os.path.split(dstfile)  # 分离文件名和路径
        if not 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)
            if not 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内的图片
                for file in 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:
                        pass
                    else:
                        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)


  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值