labelme生成的json数据集转换voc2007 xml格式
时间: 2025-06-26 15:14:45 AIGC 浏览: 43
### 将LabelMe JSON格式数据集转换为VOC2007 XML格式
要实现从LabelMe生成的JSON数据集到VOC2007 XML格式的转换,可以利用Python编写自定义脚本来完成这一任务。以下是详细的说明和代码示例。
#### 转换逻辑概述
1. **解析JSON文件**:读取LabelMe生成的JSON文件并提取其中的关键信息,例如图像路径、尺寸、对象类别及其边界框坐标。
2. **创建XML结构**:按照VOC2007标准构建对应的XML文档,包含必要的字段(如`folder`, `filename`, `size`, `object`等)。
3. **保存XML文件**:将生成的XML内容写入目标文件夹中的对应位置。
---
#### Python代码示例
以下是一个完整的Python脚本,用于执行上述转换:
```python
import os
import json
from xml.dom.minidom import Document
def convert_labelme_to_voc(json_file, output_dir, labels_txt):
"""
Convert a single LabelMe JSON file to VOC2007 XML format.
Args:
json_file (str): Path to the input LabelMe JSON file.
output_dir (str): Directory where the resulting XML files will be saved.
labels_txt (str): File containing class names used in annotations.
"""
with open(labels_txt, 'r') as f:
classes = [line.strip() for line in f]
with open(json_file) as f:
data = json.load(f)
img_filename = os.path.basename(data['imagePath'])
height, width = data['imageHeight'], data['imageWidth']
doc = Document()
annotation = doc.createElement('annotation')
doc.appendChild(annotation)
folder = doc.createElement('folder')
folder.appendChild(doc.createTextNode('VOC2007'))
annotation.appendChild(folder)
filename = doc.createElement('filename')
filename.appendChild(doc.createTextNode(img_filename))
annotation.appendChild(filename)
size = doc.createElement('size')
annotation.appendChild(size)
w = doc.createElement('width')
w.appendChild(doc.createTextNode(str(width)))
size.appendChild(w)
h = doc.createElement('height')
h.appendChild(doc.createTextNode(str(height)))
size.appendChild(h)
d = doc.createElement('depth')
d.appendChild(doc.createTextNode('3')) # Assuming RGB images
size.appendChild(d)
objects = data.get('shapes', [])
for obj in objects:
label = obj['label']
if label not in classes:
continue # Skip unrecognized labels
points = obj['points']
xmin, ymin = min(points[0][0], points[1][0]), min(points[0][1], points[1][1])
xmax, ymax = max(points[0][0], points[1][0]), max(points[0][1], points[1][1])
object_elem = doc.createElement('object')
annotation.appendChild(object_elem)
name = doc.createElement('name')
name.appendChild(doc.createTextNode(label))
object_elem.appendChild(name)
bndbox = doc.createElement('bndbox')
object_elem.appendChild(bndbox)
x_min = doc.createElement('xmin')
x_min.appendChild(doc.createTextNode(str(int(xmin))))
bndbox.appendChild(x_min)
y_min = doc.createElement('ymin')
y_min.appendChild(doc.createTextNode(str(int(ymin))))
bndbox.appendChild(y_min)
x_max = doc.createElement('xmax')
x_max.appendChild(doc.createTextNode(str(int(xmax))))
bndbox.appendChild(x_max)
y_max = doc.createElement('ymax')
y_max.appendChild(doc.createTextNode(str(int(ymax))))
bndbox.appendChild(y_max)
xml_filepath = os.path.join(output_dir, os.path.splitext(os.path.basename(json_file))[0] + '.xml')
with open(xml_filepath, 'w') as f:
f.write(doc.toprettyxml(indent=' '))
# Example usage of function
convert_labelme_to_voc(
json_file="data/sample.json",
output_dir="output/voc_xmls/",
labels_txt="data/labels.txt"
)
```
此脚本实现了单个JSON文件向VOC XML格式的转换[^1]。如果需要批量处理多个JSON文件,则可以通过遍历指定目录来调用该函数多次。
---
#### 批量转换方法
为了支持批量操作,可扩展如下功能:
```python
def batch_convert_labelme_to_voc(input_dir, output_dir, labels_txt):
"""
Batch conversion of all LabelMe JSON files within an input directory.
Args:
input_dir (str): Directory containing LabelMe JSON files.
output_dir (str): Output directory for generated VOC XML files.
labels_txt (str): File listing valid class names.
"""
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for root, _, files in os.walk(input_dir):
for fname in files:
if fname.endswith('.json'):
json_path = os.path.join(root, fname)
try:
convert_labelme_to_voc(json_path, output_dir, labels_txt)
except Exception as e:
print(f"Error processing {fname}: {e}")
batch_convert_labelme_to_voc(
input_dir="data/json_files/",
output_dir="output/voc_xmls/",
labels_txt="data/labels.txt"
)
```
通过以上两部分代码组合即可高效完成整个流程[^2]。
---
#### 注意事项
- 确保输入的JSON文件遵循LabelMe的标准格式。
- 提供的标签列表应覆盖所有可能的对象类别,并按顺序排列以便后续使用。
- 如果存在多边形标注而非矩形框,请先将其转化为最小包围盒再进行转换[^4]。
---
阅读全文
相关推荐

















