import json
import os
import shutil#source_dir表示数据扩增之后的文件夹路径,此时标注的是多分类的标签
#new_dir表示转化之后得到的二分类文件夹def to2class():#json存放路径source_dir = r'1'#json保存路径new_dir = r'1'for i in os.listdir(source_dir):if i.endswith('.json'):# json.load(open(path, encoding="utf-8", errors="ignore"))data = json.load(open(os.path.join(source_dir, i), encoding="utf-8", errors="ignore"))shapes = data['shapes']for item in shapes:# if item['label'] == '2':# item['label'] ='1' #! 负极标签变成2if item['label'] == '6':item['label'] = '3'# if item['label'] == '5':# item['label'] = '3'# else:# item['label'] = '1' #! 正极标签变成1# json.dump(data, open(os.path.join(new_dir, i), 'w'))# json.dump(data, open(os.path.join(new_dir, i), 'w'))json.dump(data, open(os.path.join(new_dir, i),'w'),indent=3)print(i)if __name__=="__main__":to2class()