Download scripts/patch_json.py from KCBtheone/SplatAtlas: direct link, hf CLI and curl.
- Browser
- Download file 1.22 kB
-
https://huggingface.co/KCBtheone/SplatAtlas/resolve/main/scripts/patch_json.py
- Command line
-
hf download hf://KCBtheone/SplatAtlas/scripts/patch_json.py
-
curl -L -o patch_json.py https://huggingface.co/KCBtheone/SplatAtlas/resolve/main/scripts/patch_json.py
1.22 kB
| import os | |
| def patch_json_dump(target_file): | |
| if not os.path.exists(target_file): | |
| return | |
| with open(target_file, "r") as f: | |
| code = f.read() | |
| if "class NpEncoder(json.JSONEncoder):" in code: | |
| print(f"Already patched: {target_file}") | |
| return | |
| # 注入万能类型转换器 | |
| encoder_code = """import json | |
| class NpEncoder(json.JSONEncoder): | |
| def default(self, obj): | |
| if hasattr(obj, 'item'): | |
| return obj.item() | |
| return super().default(obj) | |
| """ | |
| code = code.replace("import json", encoder_code) | |
| # 替换所有的 dump 调用 | |
| code = code.replace("json.dump(stats, f, indent=4)", "json.dump(stats, f, indent=4, cls=NpEncoder)") | |
| code = code.replace("json.dump(metrics, f, indent=4)", "json.dump(metrics, f, indent=4, cls=NpEncoder)") | |
| code = code.replace("json.dump(results, f, indent=4)", "json.dump(results, f, indent=4, cls=NpEncoder)") | |
| with open(target_file, "w") as f: | |
| f.write(code) | |
| print(f"Patched JSON serialization in {target_file}") | |
| patch_json_dump("/root/autodl-tmp/SplatAtlas/scripts/compute_offline_physics.py") | |
| patch_json_dump("/root/autodl-tmp/SplatAtlas/ufd_evalkit/run_eval.py") | |