修改labelme标注的标签名

对于有些标注标签名拼写错误的,可以用此方法

点击查看代码
# -*- coding: utf-8 -*- 
import os
import json


json_dir = ''  # JSON文件所在文件夹的路径
old_label = ''  # 要修改的旧标签名
new_label = ''  # 修改后的新标签名


# 遍历JSON文件夹中的所有JSON文件
for filename in os.listdir(json_dir):
    if filename.endswith('.json'):
        json_path = os.path.join(json_dir, filename)

        # 读取JSON文件
        with open(json_path, 'r', encoding='utf-8') as f:
            data = json.load(f)

            # 遍历每个标注对象
            if 'shapes' in data:
                shapes = data['shapes']
                for obj in shapes:
                    if obj['label'] == old_label:
                        # 修改标签名
                        obj['label'] = new_label

        # 保存修改后的JSON文件
        with open(json_path, 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=4)

print('标签名修改完成!')

posted @ 2024-11-06 17:21  阳光天气  阅读(265)  评论(0)    收藏  举报