用labelme軟件對圖像進行實例分割或語義分割標注后會得到json文件,如果想要刪除某個特定標簽,可以使用如下代碼,完整代碼下載地址:代碼地址
import json
import os# 要處理的json文件夾路徑
folder_path = 'H:/json'
# 需要刪除的標簽列表,填寫自己的標簽
labels_to_remove = ['0', '1', '2', '3']# 遍歷文件夾中的所有文件
for filename in os.listdir(folder_path):if filename.endswith('.json'):file_path = os.path.join(folder_path, filename)# 讀取JSON文件with open(file_path, 'r') as file:data = json.load(file)# 遍歷并刪除特定標簽的標注data['shapes'] = [annotation for annotation in data['shapes'] if annotation['label'] not in labels_to_remove]# 保存修改后的JSON文件with open(file_path, 'w') as file:json.dump(data, file, indent=4)