python实现删除数据集json中的框标注

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,linux)