pythonlabelImgxml格式的数据集解析及可视化1、XML 格式分析
需要关注的⼀般只有:
filename :图⽚名称
size:width,heights 图⽚尺⼨
object:图⽚中标注的⽬标,可能含有多个⽬标,这个xml就有2个标注⽬标
----- name:标注⽬标 类别标签 labels
----- bndbox :标注⽬标框 xmin ,ymin ,xmax ,ymax (左上⾓,右下⾓坐标)
2、可视化:把 xml 中的⽬标框在原图上绘制出来,并显⽰标签
原作者代码
ElementTree as ET # 读取xml。
import os
from PIL import Image,ImageDraw,ImageFont
def parse_rec(filename):
tree = ET.parse(filename) # 解析读取xml函数
objects = []
img_dir =[]
for xml_name in tree.findall('filename'):
img_path = os.path.join(pic_path, )
img_dir.append(img_path)
for obj in tree.findall('object'):
obj_struct = {}
obj_struct['name'] = obj.find('name').text
obj_struct['pose'] = obj.find('pose').text
obj_struct['truncated'] = int(obj.find('truncated').text)
obj_struct['difficult'] = int(obj.find('difficult').text)
bbox = obj.find('bndbox')
obj_struct['bbox'] = [int(bbox.find('xmin').text),
int(bbox.find('ymin').text),
int(bbox.find('xmax').text),
int(bbox.find('ymax').text)]
objects.append(obj_struct)
return objects,img_dir
# 可视化
def visualise_gt(objects,img_dir):
for id,img_path in enumerate(img_dir):
img = Image.open(img_path)
draw = ImageDraw.Draw(img)
for a in objects:
xmin =int(a['bbox'][0])
ymin =int(a['bbox'][1])
xmax =int(a['bbox'][2])
ymax =int(a['bbox'][3])
label = a['name']
<((xmin-10,ymin-15), label, fill = (0,255,0),font=font) # 利⽤ImageDraw的内置函数,在图⽚上写⼊⽂字 img.show()
fontPath = "C:\Windows\Fonts\f" # 字体路径
root = 'F:/dataset/AQM'
ann_path = os.path.join(root, 'Annotations') # xml⽂件所在路径
pic_path = os.path.join(root, 'JPEGImages') # 样本图⽚路径
font = uetype(fontPath, 16)
for filename in os.listdir(ann_path):
xml_path = os.path.join(ann_path,filename)
object,img_dir = parse_rec(xml_path)
visualise_gt(object,img_dir )
根据我的任务编写的代码
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#功能:根据原图和XML⽂件中的框,截取图⽚
import sys
import os
import cv2 as cv
from tqdm import tqdm
try:
try:
cElementTree as ET
except ImportError:
ElementInclude as ET
root = "E:\\ProjectAll\\OCLE\\DatasetOriginal\\casing_cap\\casing_cap_missing"
ann_path = os.path.join(root,'Annotations') #xml⽂件路径
pic_path = os.path.join(root,'JPEGImages') #样本图⽚路径
save_path_root = "E:\\ProjectAll\\OCLE\\DatasetExtraction\\casing_cap" #图⽚保存路径process_name = 'casing_cap' #定义要处理的部件,以调⽤不同的处理函数
def Crop_image(pic_name, img_path, label, x0, y0, x1, y1):
img = cv.imread(img_path)
img_crop = img[y0:y1, x0:x1] #第⼀个是y⽅向取值,第⼆个是x⽅向取值
save_path = os.path.join(save_path_root, label)
img_new_name = (pic_name[0:7]+'_'+str(x0)+'.jpg')
save_name = os.path.join(save_path, img_new_name)
print(save_name)
cv.imwrite(save_name, img_crop)
def split_pin_find(tree): #开⼝销处理函数
for xml_name in tree.findall('filename'):
pic_name =
img_path = os.path.join(pic_path, pic_name)
for obj in tree.findall('object'): #遍历所有object
label = obj.find('name').text
bbox = obj.find('bndbox')
if label == 'split_pin_loose':
x0 = int(bbox.find('xmin').text)
y0 = int(bbox.find('ymin').text)
x1 = int(bbox.find('xmax').text)
y1 = int(bbox.find('ymax').text)
Crop_image(pic_name, img_path, label, x0, y0, x1, y1)
elif label == 'split_pin_missing':
x0 = int(bbox.find('xmin').text)
y0 = int(bbox.find('ymin').text)
x1 = int(bbox.find('xmax').text)
y1 = int(bbox.find('ymax').text)
python处理xml文件Crop_image(pic_name, img_path, label, x0, y0, x1, y1)
elif label == 'split_pin_normal':
x0 = int(bbox.find('xmin').text)
y0 = int(bbox.find('ymin').text)
x1 = int(bbox.find('xmax').text)
y1 = int(bbox.find('ymax').text)
Crop_image(pic_name, img_path, label, x0, y0, x1, y1)
else:
x0 = int(bbox.find('xmin').text)
y0 = int(bbox.find('ymin').text)
x1 = int(bbox.find('xmax').text)
y1 = int(bbox.find('ymax').text)
label = 'split_pin_otherside'
Crop_image(pic_name, img_path, label, x0, y0, x1, y1)
def casing_cap_find(tree): #管帽处理函数
for xml_name in tree.findall('filename'):
pic_name =
img_path = os.path.join(pic_path, pic_name)
for obj in tree.findall('object'): #遍历所有object
label = obj.find('name').text
bbox = obj.find('bndbox')
if label == 'casing_cap_loose':
x0 = int(bbox.find('xmin').text)
y0 = int(bbox.find('ymin').text)
x1 = int(bbox.find('xmax').text)
y1 = int(bbox.find('ymax').text)
Crop_image(pic_name, img_path, label, x0, y0, x1, y1)
Crop_image(pic_name, img_path, label, x0, y0, x1, y1)
elif label == 'casing_cap_missing':
x0 = int(bbox.find('xmin').text)
y0 = int(bbox.find('ymin').text)
x1 = int(bbox.find('xmax').text)
y1 = int(bbox.find('ymax').text)
Crop_image(pic_name, img_path, label, x0, y0, x1, y1)
else:
x0 = int(bbox.find('xmin').text)
y0 = int(bbox.find('ymin').text)
x1 = int(bbox.find('xmax').text)
y1 = int(bbox.find('ymax').text)
label = 'casing_cap_normal'
Crop_image(pic_name, img_path, label, x0, y0, x1, y1)
def parse_rec(filename):
tree = ET.parse(filename)
if process_name == 'split_pin':
split_pin_find(tree)
elif process_name == 'casing_cap':
casing_cap_find(tree)
def main():
for filename in tqdm(os.listdir(ann_path)):
xml_path = os.path.join(ann_path, filename)
parse_rec(xml_path)
if __name__ == '__main__':
main()
3、对 xml 及进⾏操作的重要函数
XML是⼀种固有的分层数据格式,最⾃然的表⽰⽅法是使⽤树,其内元素称作⼦节点
通过 parse() 解析xml⽂本,返回根元素 tree。(⼀级节点Annotation)
通过对 tree 进⾏findall操作,可到到带有指定标签的节点(⼆级节点eg:filename,object)。
Element对象有以下常⽤属性:
1、.tag: 标签
2、.text: 去除标签,获得标签中的内容。
3、.attrib: 获取标签中的属性和属性值。
4、.findall() : 只到带有标签的 所有节点
5、.append() : 增加新节点
6、.set():增加或者修改属性
7、.remove():删除节点
保存xml⽂件: ElementTree.write()
,另⼀种xml的解析⽅式
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论