此脚本适用于标注出来的部分xml文件错误,错误内容是width 或hight为0。(好像是因为png格式图片有问题)。
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
import sys
class Logger(object):
def __init__(self, filename="log.txt"):
self.terminal = sys.stdout
self.log = open(filename, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
#path = os.path.abspath(os.path.dirname(__file__))
#type = sys.getfilesystemencoding()
#sys.stdout = Logger()
count = 0
xmllist = os.listdir("/home/easen/wyc/data/tmp/xmls/")
for file in xmllist:
if os.path.splitext(file)[1] == ".xml":
in_file = open("/home/easen/wyc/data/tmp/xmls/"+file)
tree=ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
if w == 0 :
print(file)
in_file.close()
os.remove("/home/easen/wyc/data/tmp/xmls/"+file)
os.remove("/home/easen/wyc/data/tmp/imgs/"+os.path.splitext(file)[0]+".jpg")
count +=1
print(count)