Labelme JSON 转换为 VIA 格式数据的 Python 脚本
以下是一个将 Labelme 的 JSON 数据转换为 VIA 格式数据的 Python 脚本示例:
import json
def labelme_to_via(json_file, via_file):
with open(json_file, 'r') as f:
data = json.load(f)
via_data = {
"_via_settings": {
"ui": {
"annotation_editor_height": 25,
"annotation_editor_fontsize": 0.8,
"leftsidebar_width": 18,
"image": {
"region_label": "__via_region_id__",
"region_color": "__via_default_region_color__"
},
"resizable": true
},
"core": {
"buffer_size": 18,
"filepath": {}
}
},
"_via_img_metadata": {},
"_via_attributes": {},
"_via_data_format_version": "2.0.10"
}
for i, img in enumerate(data['images']):
img_filename = img['file_name']
img_regions = []
for ann in data['annotations']:
if ann['image_id'] == img['id']:
region = {
"shape_attributes": {
"name": "polygon",
"all_points_x": ann['segmentation'][0][::2],
"all_points_y": ann['segmentation'][0][1::2]
},
"region_attributes": {}
}
img_regions.append(region)
via_data["_via_img_metadata"][str(i)] = {
"filename": img_filename,
"regions": img_regions,
"file_attributes": {}
}
with open(via_file, 'w') as f:
json.dump(via_data, f, indent=4)
# 示例用法
labelme_to_via('labelme_data.json', 'via_data.json')
注意:上述代码仅适用于 Labelme JSON 数据的一种特定结构,如果你的 JSON 数据结构不同,请根据实际情况进行修改。
原文地址: https://www.cveoy.top/t/topic/lRvU 著作权归作者所有。请勿转载和采集!