import json # 读取.py格式的配置文件并执行 def read_py_config(file_path): # 创建一个空的字典用于保存配置 config_dict = {} # 执行.py文件并将结果存入config_dict with open(file_path, 'r') as f: # 执行Python代码并保存配置 exec(f.read(), {}, config_dict) return config_dict # 将字典保存为json格式 def save_as_json(config_dict, output_file_path): with open(output_file_path, 'w') as f: json.dump(config_dict, f, indent=4) # 将字典保存为json格式 def save_as_json(config_dict, output_file_path): with open(output_file_path, 'w') as f: json.dump(config_dict, f, indent=4) # 主程序 if __name__ == "__main__": input_file = 'Massage/aucpuncture2point/configs/body_2d_keypoint/rtmo/coco/rtmo-s_8xb32-600e_coco-640x640.py' # 这里填入你要转换的.py文件路径 output_file = 'Massage/aucpuncture2point/configs/body_2d_keypoint/rtmo/coco/rtmo-s_8xb32-600e_coco-640x640.json' # 输出的json文件路径 config_dict = read_py_config(input_file) save_as_json(config_dict, output_file) print(f"配置文件已转换并保存为 {output_file}")