對於有些標註標籤名拼寫錯誤的,可以用此方法
點選檢視程式碼
# -*- coding: utf-8 -*-
import os
import json
json_dir = '' # JSON檔案所在資料夾的路徑
old_label = '' # 要修改的舊標籤名
new_label = '' # 修改後的新標籤名
# 遍歷JSON資料夾中的所有JSON檔案
for filename in os.listdir(json_dir):
if filename.endswith('.json'):
json_path = os.path.join(json_dir, filename)
# 讀取JSON檔案
with open(json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
# 遍歷每個標註物件
if 'shapes' in data:
shapes = data['shapes']
for obj in shapes:
if obj['label'] == old_label:
# 修改標籤名
obj['label'] = new_label
# 儲存修改後的JSON檔案
with open(json_path, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
print('標籤名修改完成!')