目录
这里对json保存做了格式封装
调用代码:
python json原始保存是所有json保存一行,
这里对json保存做了格式封装
import numpy as np
class MyEncoder(json.JSONEncoder):def default(self, obj):if isinstance(obj, np.integer):return int(obj)elif isinstance(obj, np.floating):return float(obj)elif isinstance(obj, np.ndarray):return obj.tolist()else:return super(MyEncoder, self).default(obj)
调用代码:
none_count+=1if copy_mode==1:shutil.copy(img_path, save_to)json_path=save_to+os.sep+os.path.basename(img_path).replace('.jpg','.json')elif copy_mode==2:shutil.move(img_path,save_to)json_path=save_to+os.sep+os.path.basename(img_path).replace('.jpg','.json')else:json_path = img_path.replace('.jpg', '.json')json.dump(annotation, open(json_path, 'w'), indent=4, cls=MyEncoder) # indent=4 更加美观显示