import os
import json
def only_polygons():
jsonF = []
newF = []
for i in range(7):
json_folder = r'json folder address'
new_folder = r'target folder address'
if os.path.exists(json_folder):
jsonF.append(json_folder)
newF.append(new_folder)
os.makedirs(new_folder, exist_ok=True) # 创建新文件夹,如果已存在则不报错
for l in range(len(jsonF)): # each folder
for filename in os.listdir(jsonF[l]): # each json
f = os.path.join(jsonF[l], filename)
with open(f, 'r', encoding='utf-8') as j:
data = json.load(j, strict=False)
shapes = data['shapes'] # list
# Remove rectangles from the list of shapes
shapes = [j for j in shapes if j['shape_type'] != 'rectangle']
# Update the 'shapes' key in the data dictionary
data['shapes'] = shapes
# Save the new data to the new folder
o = os.path.join(newF[l], filename)
with open(o, 'w', encoding='utf-8') as o:
json.dump(data, o, ensure_ascii=False, indent=4)
# Call the function to process the files
only_polygons()
python实现删除数据集json中的框标注
最新推荐文章于 2025-12-09 16:47:37 发布
该篇文章介绍了一个Python函数,它遍历指定的json文件夹,读取每个文件,过滤掉其中的矩形形状,只保留非矩形形状,并将处理后的数据保存到目标文件夹中。
1757

被折叠的 条评论
为什么被折叠?



