filter
import glob
import cv2
import xml.etree.ElementTree as ET
import numpy as np
# color =
def filter1(xml_root, img_root):
xml_list = glob.glob(f'{xml_root}/*')
classes = []
error_num = 0
for i, xml_name in enumerate(xml_list):
# try:
tree = ET.parse(xml_name)
root = tree.getroot()
img_name = tree.findtext('./filename').split(".jpg")[0]
img_path = img_root + '/' + img_name+".jpg"
img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), 1)
for a in root:
for b in a:
if b.tag == 'name':
if b.text == 'hat':
b.text = 'head'
length = []
for a in root:
for b in a :
if b.tag == 'name':
length.append(b.text)
if len(length) != 0:
try:
# cv2.imwrite(f"data/1/img/{img_name}.jpg", img)
name = f"data/1/img/{img_name}.jpg"
# cv2.imencode('.jpg', img)[1].tofile(name)
tree.write(f'new_xml_label/{img_name}.xml', encoding='UTF-8')
except:
print(img_root + '/' + img_name + ".jpg")
error_num+=1
continue
# except:
# error_num += 1
# continue
print(error_num)
return 0
if __name__=="__main__":
xml_roots = ['label']
img_roots = ['image']
filter1(xml_roots[0], img_roots[0])
trans
import glob
import xml.etree.ElementTree as ET
import cv2
import os
sets = ['train', 'test', 'val']
# classes = ['head', 'hat', 'person', 'lamp', 'cellphone' ]
classes = ['head','person','cellphone','lamp']
def convert(size, box): # size:(原图w,原图h) , box:(xmin,xmax,ymin,ymax)
dw = 1./size[0] # 1/w
dh = 1./size[1] # 1/h
x = (box[0] + box[1])/2.0 # 物体在图中的中心点x坐标
y = (box[2] + box[3])/2.0 # 物体在图中的中心点y坐标
w = box[1] - box[0] # 物体实际像素宽度
h = box[3] - box[2] # 物体实际像素高度
x = x*dw # 物体中心点x的坐标比(相当于 x/原图w)
w = w*dw # 物体宽度的宽度比(相当于 w/原图w)
y = y*dh # 物体中心点y的坐标比(相当于 y/原图h)
h = h*dh # 物体宽度的宽度比(相当于 h/原图h)
return x, y, w, h # 返回 相对于原图的物体中心点的x坐标比,y坐标比,宽度比,高度比,取值范围[0-1]
def convert_annotation(xml_path,i):
in_file = xml_path
# print(out_file.name)
# 解析xml文件
tree = ET.parse(in_file)
# 获得对应的键值对
root = tree.getroot()
# 获得图片的尺寸大小
img_name = root.find('filename').text.split('.jpg')[0].split('.png')[0]
# if i % 10 != 0:
# out_file = open(f'txt_label/{img_name}.txt','w')
# else :
# out_file = open(f'val_txt_label')\
out_file = open(f'labels/{img_name}.txt', 'w')
size = root.find('size')
# 获得宽
w = int(size.find('width').text)
# 获得高
h = int(size.find('height').text)
for obj in root.iter('object'):
try:
cls = obj.find('name').text
print(cls)
except:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
# 获取对应的bndbox的数组 = ['xmin','xmax','ymin','ymax']
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
bb = convert((w, h), b)
out_file.write(str(cls_id) + " " + " ".join([str(round(a,6)) for a in bb]) + '\n')
if __name__ == '__main__':
xml_path_list = glob.glob("label/*")
for i,xml_path in enumerate(xml_path_list):
convert_annotation(xml_path,i)
本文介绍了如何使用Python脚本对图像文件中的XML标注进行修改,将'hat'标签更改为'head',并处理错误图片。通过遍历XML文件,检测并替换指定类别,最后将处理后的图片和XML保存到新的目录。
1012

被折叠的 条评论
为什么被折叠?



