赞
踩
需要关注的一般只有:
filename :图片名称
size:width,heights 图片尺寸
object:图片中标注的目标,可能含有多个目标,这个xml就有2个标注目标
----- name:标注目标 类别标签 labels
----- bndbox :标注目标框 xmin ,ymin ,xmax ,ymax (左上角,右下角坐标)
- import xml.etree.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, xml_name.text)
- 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']
- draw.rectangle((xmin,ymin,xmax,ymax), fill=None, outline=(0,255,0),width=2)
- draw.text((xmin-10,ymin-15), label, fill = (0,255,0),font=font) # 利用ImageDraw的内置函数,在图片上写入文字
- img.show()
-
-
-
- fontPath = "C:\Windows\Fonts\Consolas\consola.ttf" # 字体路径
- root = 'F:/dataset/AQM'
- ann_path = os.path.join(root, 'Annotations') # xml文件所在路径
- pic_path = os.path.join(root, 'JPEGImages') # 样本图片路径
- font = ImageFont.truetype(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:
- import xml.etree.cElementTree as ET
- except ImportError:
- import xml.etree.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 = xml_name.text
- 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)
- 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 = xml_name.text
- 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)
- 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()
官方手册
xml.etree.ElementTree 资料
Elements and Element Trees
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.dom.minidom,另一种xml的解析方式
参考:python xml 格式的数据集标注文件解析(修改、保存、删除),可视化:https://blog.csdn.net/qq_36758461/article/details/103947168?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。