| |
| """ |
| 预计算 3D-FUTURE 模型的尺寸缓存 |
| |
| 运行一次后会生成 _size_cache.json 文件,后续渲染时直接加载缓存,速度快100倍 |
| """ |
| import os |
| import sys |
| import json |
| import pickle |
| from tqdm import tqdm |
|
|
| |
| ATISS_PATH = os.path.join(os.path.dirname(__file__), 'ATISS') |
| sys.path.insert(0, ATISS_PATH) |
|
|
| def precompute_size_cache(pkl_path, output_path=None): |
| """预计算模型尺寸缓存""" |
| if output_path is None: |
| output_path = pkl_path.replace('.pkl', '_size_cache.json') |
| |
| print(f"加载数据集: {pkl_path}") |
| with open(pkl_path, 'rb') as f: |
| dataset = pickle.load(f) |
| |
| if not hasattr(dataset, 'objects'): |
| print("错误: 数据集没有 objects 属性") |
| return |
| |
| objects = dataset.objects |
| print(f"共 {len(objects)} 个模型") |
| |
| size_cache = {} |
| failed = [] |
| |
| for obj in tqdm(objects, desc="计算模型尺寸"): |
| if not hasattr(obj, 'model_uid'): |
| continue |
| |
| try: |
| |
| size = obj.size |
| size_cache[obj.model_uid] = [float(s) for s in size] |
| except Exception as e: |
| failed.append((obj.model_uid, str(e))) |
| |
| print(f"\n成功: {len(size_cache)}, 失败: {len(failed)}") |
| |
| |
| print(f"保存缓存到: {output_path}") |
| with open(output_path, 'w') as f: |
| json.dump(size_cache, f) |
| |
| if failed: |
| print(f"\n失败的模型 (前10个):") |
| for uid, err in failed[:10]: |
| print(f" {uid}: {err}") |
| |
| return size_cache |
|
|
|
|
| def main(): |
| import argparse |
| parser = argparse.ArgumentParser(description="预计算 3D-FUTURE 模型尺寸缓存") |
| parser.add_argument('--pkl', type=str, required=True, help='Pickle 数据集路径') |
| parser.add_argument('--output', type=str, default=None, help='输出缓存文件路径') |
| args = parser.parse_args() |
| |
| precompute_size_cache(args.pkl, args.output) |
|
|
|
|
| if __name__ == '__main__': |
| main() |
|
|