import os
import json
defonly_polygons():
jsonF =[]
newF =[]for i inrange(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 inrange(len(jsonF)):# each folderfor filename in os.listdir(jsonF[l]):# each json
f = os.path.join(jsonF[l], filename)withopen(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)withopen(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()