前言:使用labelimg进行标注的时候,由于都是用的是默认的名称,有时候类的名字会出现拼写错误,比如我想要写的是“cow” 结果打上去的是“cwo”, 一出错就错一片,这很常见,所以参考了:https://www.jianshu.com/p/cf12bef0872c 的代码,修改了冗余的代码,并添加了新的模块以后,将代码分享给大家。
<annotation>
<folder>part3folder>
<filename>0015-1150.jpgfilename>
<path>I:\part3\0015-1150.jpgpath>
<source>
<database>Unknowndatabase>
source>
<size>
<width>1280width>
<height>720height>
<depth>3depth>
size>
<segmented>0segmented>
<object>
<name>cowname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>459xmin>
<ymin>88ymin>
<xmax>567xmax>
<ymax>163ymax>
bndbox>
object>
<object>
<name>cowname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>543xmin>
<ymin>92ymin>
<xmax>630xmax>
<ymax>163ymax>
bndbox>
object>
<object>
<name>cowname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>659xmin>
<ymin>156ymin>
<xmax>773xmax>
<ymax>263ymax>
bndbox>
object>
<object>
<name>cowname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>497xmin>
<ymin>171ymin>
<xmax>677xmax>
<ymax>315ymax>
bndbox>
object>
<object>
<name>cowname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>584xmin>
<ymin>303ymin>
<xmax>715xmax>
<ymax>395ymax>
bndbox>
object>
<object>
<name>cowname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>537xmin>
<ymin>389ymin>
<xmax>814xmax>
<ymax>497ymax>
bndbox>
object>
<object>
<name>cowname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>750xmin>
<ymin>303ymin>
<xmax>918xmax>
<ymax>434ymax>
bndbox>
object>
<object>
<name>cowname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>770xmin>
<ymin>223ymin>
<xmax>958xmax>
<ymax>285ymax>
bndbox>
object>
<object>
<name>cowname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>974xmin>
<ymin>477ymin>
<xmax>1152xmax>
<ymax>636ymax>
bndbox>
object>
<object>
<name>cowname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>66xmin>
<ymin>454ymin>
<xmax>308xmax>
<ymax>696ymax>
bndbox>
object>
<object>
<name>cowname>
<pose>Unspecifiedpose>
<truncated>1truncated>
<difficult>0difficult>
<bndbox>
<xmin>347xmin>
<ymin>548ymin>
<xmax>550xmax>
<ymax>720ymax>
bndbox>
object>
<object>
<name>cowname>
<pose>Unspecifiedpose>
<truncated>0truncated>
<difficult>0difficult>
<bndbox>
<xmin>996xmin>
<ymin>154ymin>
<xmax>1128xmax>
<ymax>245ymax>
bndbox>
object>
annotation>
def changeName(xml_fold, origin_name, new_name):
'''
xml_fold: xml存放文件夹
origin_name: 原始名字,比如弄错的名字,原先要cow,不小心打成cwo
new_name: 需要改成的正确的名字,在上个例子中就是cow
'''
files = os.listdir(xml_fold)
cnt = 0
for xmlFile in files:
file_path = os.path.join(xml_fold, xmlFile)
dom = parse(file_path)
root = dom.getroot()
for obj in root.iter('object'):#获取object节点中的name子节点
tmp_name = obj.find('name').text
if tmp_name == origin_name: # 修改
obj.find('name').text = new_name
print("change %s to %s." % (origin_name, new_name))
cnt += 1
dom.write(file_path, xml_declaration=True)#保存到指定文件
print("有%d个文件被成功修改。" % cnt)
适用于只有一个类,并且进行改名的时候。
def changeAll(xml_fold,new_name):
'''
xml_fold: xml存放文件夹
new_name: 需要改成的正确的名字,在上个例子中就是cow
'''
files = os.listdir(xml_fold)
cnt = 0
for xmlFile in files:
file_path = os.path.join(xml_fold, xmlFile)
dom = parse(file_path)
root = dom.getroot()
for obj in root.iter('object'):#获取object节点中的name子节点
tmp_name = obj.find('name').text
obj.find('name').text = new_name
print("change %s to %s." % (tmp_name, new_name))
cnt += 1
dom.write(file_path, xml_declaration=True)#保存到指定文件
print("有%d个文件被成功修改。" % cnt)
def countAll(xml_fold):
'''
xml_fold: xml存放文件夹
'''
files = os.listdir(xml_fold)
dict={}
for xmlFile in files:
file_path = os.path.join(xml_fold, xmlFile)
dom = parse(file_path)
root = dom.getroot()
for obj in root.iter('object'):#获取object节点中的name子节点
tmp_name = obj.find('name').text
if tmp_name not in dict:
dict[tmp_name] = 0
else:
dict[tmp_name] += 1
dom.write(file_path, xml_declaration=True)#保存到指定文件
print("统计结果如下:")
print("-"*10)
for key,value in dict.items():
print("类别为%s的目标个数为%d." % (key, value))
print("-"*10)
import os
import os.path
from xml.etree.ElementTree import parse, Element
def changeName(xml_fold, origin_name, new_name):
'''
xml_fold: xml存放文件夹
origin_name: 原始名字,比如弄错的名字,原先要cow,不小心打成cwo
new_name: 需要改成的正确的名字,在上个例子中就是cow
'''
files = os.listdir(xml_fold)
cnt = 0
for xmlFile in files:
file_path = os.path.join(xml_fold, xmlFile)
dom = parse(file_path)
root = dom.getroot()
for obj in root.iter('object'):#获取object节点中的name子节点
tmp_name = obj.find('name').text
if tmp_name == origin_name: # 修改
obj.find('name').text = new_name
print("change %s to %s." % (origin_name, new_name))
cnt += 1
dom.write(file_path, xml_declaration=True)#保存到指定文件
print("有%d个文件被成功修改。" % cnt)
def changeAll(xml_fold,new_name):
'''
xml_fold: xml存放文件夹
new_name: 需要改成的正确的名字,在上个例子中就是cow
'''
files = os.listdir(xml_fold)
cnt = 0
for xmlFile in files:
file_path = os.path.join(xml_fold, xmlFile)
dom = parse(file_path)
root = dom.getroot()
for obj in root.iter('object'):#获取object节点中的name子节点
tmp_name = obj.find('name').text
obj.find('name').text = new_name
print("change %s to %s." % (tmp_name, new_name))
cnt += 1
dom.write(file_path, xml_declaration=True)#保存到指定文件
print("有%d个文件被成功修改。" % cnt)
def countAll(xml_fold):
'''
xml_fold: xml存放文件夹
'''
files = os.listdir(xml_fold)
dict={}
for xmlFile in files:
file_path = os.path.join(xml_fold, xmlFile)
dom = parse(file_path)
root = dom.getroot()
for obj in root.iter('object'):#获取object节点中的name子节点
tmp_name = obj.find('name').text
if tmp_name not in dict:
dict[tmp_name] = 0
else:
dict[tmp_name] += 1
dom.write(file_path, xml_declaration=True)#保存到指定文件
print("统计结果如下:")
print("-"*10)
for key,value in dict.items():
print("类别为%s的目标个数为%d." % (key, value))
print("-"*10)
if __name__ == '__main__':
path = r"I:\dongpeijiePickup\assignment\part2_xml" #xml文件所在的目录
# changeName(path, "cattle", "cow")
# changeAll(path, "cattle")
countAll(path)