# !/usr/bin/env python
# -*- encoding: utf-8 -*-
import os
import json
# 这里写你自己的存放照片和json文件的路径
json_dir = 'F:\\data\\ABD\\liver\\2\\paddle_seg_format\\jsons'
json_files = os.listdir(json_dir)
def d_label():
# 这里写你要删除的标签名
delete_name = "nozzle"
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']):
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!')
def d_label2():
# 删除label 对应的 point <=2的节点 检测框的label 删除
# 这里写你要删除的标签名
delete_name = "nozzle"
a=set()
b= {'cat':0, 'play':0, 'sky':0, 'water':0}
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']):
points = info['shapes'][i]['points']
points_list = [tuple(point) for point in points]
a.add(info['shapes'][i]['label'])
b[info['shapes'][i]['label']]=b[info['shapes'][i]['label']]+1
if len(points_list) <=3:
print(info['shapes'][i]['label'], len(points_list))
del info['shapes'][i] #直接删除这个字典标签
# 找到位置进行删除
# 使用新字典替换修改后的字典
json_dict = info
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# 将替换后的内容写入原文件
with open(jsonfile, 'w',encoding="utf-8") as new_jf:
json.dump(json_dict, new_jf,ensure_ascii = False,indent=1)
print('delete label over!')
print(a)
for k,v in b.items():
print(k,v)
json.dump(json_dict, new_jf,ensure_ascii = False,indent=1)
避免中文乱码和没有换行