python实现批量修改Pascal VOC数据集Annotation

在深度学习的数据标定过程中,针对Pascal VOC格式的XML文件,本文介绍了一种通过Python实现批量修改Annotation文件的方法,避免了手动修改大量文件的繁琐工作。文中给出了实际操作的Python代码示例,并提醒读者注意Python代码的缩进和安装必要的xml处理库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    学习DL过程中,各位同学一定会经历数据的标定工作,本篇按照Pascal VOC数据集格式,通过python实现Annotation文件的批量修改.    举个例子

<?xml version="1.0" ?><annotation>
	<folder>HK</folder>
	<filename>101.jpg</filename>
	<path>/home/li//home/li/101.jpg</path>
	<source>
		<database>Unknown</database>
	</source>
	<size>
		<width>1280</width>
		<height>720</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>Person</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>46</xmin>
			<ymin>220</ymin>
			<xmax>109</xmax>
			<ymax>402</ymax>
		</bndbox>
	</object>
	<object>
		<name>Person</name>
		<pose>Unspecified</pose>
		<trunc
### PASCAL VOC 数据集简介 PASCAL VOC(Visual Object Classes)挑战赛是一个广泛使用的图像识别和目标检测数据集集合。该系列数据集自2005年起每年发布一次,直到2012年停止更新[^2]。它包含了多种类别的对象标注信息,适用于训练和评估计算机视觉模型。 对于与道路场景相关的研究,虽然原始的PASCAL VOC数据集并未专门针对城市道路交通事故设计,但它仍然提供了丰富的车辆类别标签(如汽车、自行车、行人等),这些可以用于开发道路环境下的目标检测算法。 ### 下载 PASCAL VOC 数据集 官方发布的最新版本为 **PASCAL VOC 2012** ,可以通过以下链接下载完整的数据包: - 官方网站: http://host.robots.ox.ac.uk/pascal/VOC/voc2012/index.html#downloads[^3] 此外,在实际应用中,如果需要更贴近现代交通状况的数据,则可能还需要考虑其他补充性的开源项目或者扩展版数据集,比如 KITTI 或者 Cityscapes 等专注于自动驾驶领域内的高质量素材库。 ### 使用方法及教程推荐 #### 工具准备 为了处理此类任务通常需要用到深度学习框架支持,例如 TensorFlow 和 PyTorch 。这里给出基于这两种流行平台实现基本流程的例子: ##### Tensorflow 实现方式 TensorFlow 提供了一个预构建好的API来简化创建定制化物体探测器的过程。下面展示如何加载并解析标准 Pascal Voc xml 文件作为输入源之一。 ```python import tensorflow as tf from object_detection.utils import dataset_util def create_tf_record(output_path, label_map_dict, annotations_dir, image_dir): writer = tf.io.TFRecordWriter(output_path) examples_list = [] # 假设我们已经获取到了所有的annotation文件路径列表example_paths for idx, example in enumerate(examples_list): path = os.path.join(annotations_dir, 'Annotations', '%s.xml' % (example)) with tf.gfile.GFile(path, 'r') as fid: xml_str = fid.read() tree = ET.ElementTree(ET.fromstring(xml_str)) data = dataset_util.recursive_parse_xml_to_dict(tree.getroot())['annotation'] full_path = os.path.join(image_dir, data['filename']) with tf.gfile.GFile(full_path, 'rb') as fid: encoded_jpg = fid.read() key = hashlib.sha256(encoded_jpg).hexdigest() width = int(data['size']['width']) height = int(data['size']['height']) xmin = [] ymin = [] xmax = [] ymax = [] classes_text = [] classes = [] if 'object' in data: for obj in data['object']: difficult = bool(int(obj['difficult'])) if not difficult: xmin.append(float(obj['bndbox']['xmin']) / width) ymin.append(float(obj['bndbox']['ymin']) / height) xmax.append(float(obj['bndbox']['xmax']) / width) ymax.append(float(obj['bndbox']['ymax']) / height) class_name = obj['name'] classes_text.append(class_name.encode('utf8')) classes.append(label_map_dict[class_name]) feature_dict = { 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature( data['filename'].encode('utf8')), 'image/source_id': dataset_util.bytes_feature( data['filename'].encode('utf8')), 'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')), 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmin), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmax), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymin), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymax), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), } example = tf.train.Example(features=tf.train.Features(feature=feature_dict)) writer.write(example.SerializeToString()) writer.close() ``` 上述脚本展示了如何读取XML格式的边界框坐标以及对应的分类标签,并将其转换成适合喂给神经网络的形式即TFRecords记录文件形式存储起来以便后续高效访问。 ##### Pytorch 实现方式 PyTorch 社区同样活跃着许多贡献者分享他们关于解决特定问题的经验和技术细节。其中 torchvision 库内置了一些方便快捷的方法帮助快速搭建起自己的实例分割系统。以下是利用其提供的 transforms 功能完成图片增强操作的一个简单例子: ```python from PIL import Image import torch from torchvision.transforms.functional import to_tensor,to_pil_image class VOCDetection(torchvision.datasets.VOCSegmentation): CLASSES = [ '__background__', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor' ] def __init__(self,*args,**kwargs): super().__init__(*args,**kwargs) self.classes=self.CLASSES @staticmethod def collate_fn(batch): images=[item[0].unsqueeze(dim=0)for item in batch] targets=[{k:v.unsqueeze(dim=0)if isinstance(v,(int,float))else v.clone().detach()for k,v in t.items()}for _,t in batch ] return tuple(zip(*[(torch.cat([i]*len(targets),dim=0),targets)])) transform=torch.nn.Sequential( RandomHorizontalFlip(p=.5), Resize((600,-1)), ToTensor(), ) dataset=VOCDetection(root='./data/',year='2012',image_set="val",transforms=transform) loader=torch.utils.data.DataLoader(dataset,batch_size=4,collate_fn=dataset.collate_fn,num_workers=4,pin_memory=True) batch=next(iter(loader)) print(len(batch)) img,target=batch[:2] to_pil_image(img.squeeze()).show() ``` 此代码片段定义了一种继承自 `torchvision.datasets` 的新类 `VOCDetection`, 并重写了它的初始化函数以适应我们的需求;另外还实现了静态方法 `collate_fn()` 来适配 dataloader 批量大小不固定的情况。最后通过 DataLoader 加载数据并对每一批次执行随机水平翻转和平移缩放变换后再显示出来验证效果良好。 ###
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值