voc2labelme脚本
时间: 2023-10-13 22:26:38 浏览: 134
与深度学习相关的数据集
voc2labelme脚本是一个用于将VOC格式的数据集转换成LabelMe格式的脚本。它的作用是将VOC格式的数据集转换成LabelMe格式,以便在LabelMe工具中进行标注和数据可视化。
以下是voc2labelme脚本的代码:
```python
import os
import xml.etree.ElementTree as ET
import json
def voc2labelme(voc_path, labelme_path):
if not os.path.exists(labelme_path):
os.makedirs(labelme_path)
for root, dirs, files in os.walk(voc_path):
for file in files:
if file.endswith(".xml"):
xml_file = os.path.join(root, file)
with open(xml_file, 'r') as f:
tree = ET.parse(f)
root = tree.getroot()
filename = root.find('filename').text
size = root.find('size')
width = int(size.find('width').text)
height = int(size.find('height').text)
if not os.path.exists(os.path.join(labelme_path, filename)):
data = {}
data['version'] = "4.5.6"
data['flags'] = {}
data['shapes'] = []
data['imagePath'] = filename
data['imageData'] = None
data['imageHeight'] = height
data['imageWidth'] = width
with open(os.path.join(labelme_path, filename[:-4] + '.json'), 'w') as json_file:
json.dump(data, json_file)
for obj in root.findall('object'):
name = obj.find('name').text
bndbox = obj.find('bndbox')
xmin = int(bndbox.find('xmin').text)
ymin = int(bndbox.find('ymin').text)
xmax = int(bndbox.find('xmax').text)
ymax = int(bndbox.find('ymax').text)
shape = {'label': name, 'points': [[xmin, ymin], [xmax, ymax]], 'group_id': None,
'shape_type': 'rectangle', 'flags': {}}
with open(os.path.join(labelme_path, filename[:-4] + '.json'), 'r+') as json_file:
data = json.load(json_file)
data['shapes'].append(shape)
json_file.seek(0)
json.dump(data, json_file)
```
使用方法:
```python
voc2labelme(voc_path, labelme_path)
```
其中,`voc_path`是VOC格式数据集的路径,`labelme_path`是转换后LabelMe格式数据集的保存路径。
阅读全文