栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python代码批量修改Labelme标注的json文件:删除同一类的所有标签

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python代码批量修改Labelme标注的json文件:删除同一类的所有标签

代码参考:https://blog.csdn.net/Sharonnn_/article/details/124365542

创建自己的数据集时,不打算分割名为"thread"的对象,因此要删除thread对象的标签。但是在labelme软件中,右侧的label list类别是不能改动的,也就是说在labelme中删除标签得一张一张的删除,很浪费时间。换一种思路:对.json文件中的"shapes"中"label"为要删除标注对应的shape进行删除,那么就能够通过操作.json文件,完成批量删除标签,处理速度很快。

原文中一张图片中同一类物体有多个时,效果不好,不能把标签全部删除。本文把for循环改成了倒序循环再删除标签,可以解决同类多物体删除问题。

# !/usr/bin/env python
# -*- encoding: utf-8 -*-

import os
import json

# 这里写你自己的存放照片和json文件的路径
json_dir = 'D:DeepLearningExercisesmatlab_filesimage1234json_files/'
json_files = os.listdir(json_dir)

# 这里写你要删除的标签名
delete_name = "thread"

for json_file in json_files:
    json_file_ext = os.path.splitext(json_file)

    if json_file_ext[1] == '.json':
        # 判断是否为json文件
        jsonfile = json_dir + '\' + json_file

        with open(jsonfile, 'r', encoding='utf-8') as jf:
            info = json.load(jf)

            # for i, label in enumerate(info['shapes']):
            for i in range(len(info['shapes'])-1,0,-1):
                if info['shapes'][i]['label'] == delete_name:
                    # 找到位置进行删除
                    del info['shapes'][i]
            # 使用新字典替换修改后的字典
            json_dict = info

        # 将替换后的内容写入原文件
        with open(jsonfile, 'w') as new_jf:
            json.dump(json_dict, new_jf)

print('delete label over!')

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/870271.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号