当前位置:   article > 正文

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

roimagelabe

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

移动未标注的图像到父目录的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)


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号