labelme标注后的json文件去掉某个类别的标签并生成新的json文件

以去掉secondary_particle标签为例

点击查看代码
import json
import os
#去除标注图像中的一次颗粒标签
def remove_specific_labels(json_file):
    with open(json_file, 'r', encoding='utf-8', errors='ignore') as f:
        data = json.load(f)

    if "shapes" in data:
        new_shapes = []
        for shape in data["shapes"]:
            if shape["label"] not in ["secondary_particle"]:
                new_shapes.append(shape)
        data["shapes"] = new_shapes

    with open(json_file, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=4)

folder_path = r"D:\pic\zhongwei\label_pic_wfdkl\origional"  # 请替换为你的文件夹路径
for file_name in os.listdir(folder_path):
    if file_name.endswith(".json"):
        json_file = os.path.join(folder_path, file_name)
        remove_specific_labels(json_file)

posted @ 2024-05-08 10:37  阳光天气  阅读(7)  评论(0编辑  收藏  举报