| |
| """ |
| 批量渲染 bench_test_700 数据集 |
| """ |
| import os |
| import json |
| import math |
| import sys |
| import collections |
| import argparse |
| from tqdm import tqdm |
|
|
| try: |
| import visualize_blender_zones as viz |
| except ImportError: |
| print("Error: visualize_blender_zones.py not found in current directory.") |
| sys.exit(1) |
|
|
| def prepare_task_assets(task, asset_dir): |
| """Prepare assets metadata""" |
| if "layout_criteria" not in task: |
| task["layout_criteria"] = "the layout should follow the task description and adhere to common sense" |
|
|
| all_data = collections.defaultdict(list) |
| |
| for original_uid in task["assets"].keys(): |
| parts = original_uid.split('-') |
| if len(parts) > 1 and parts[-1].isdigit(): |
| uid = '-'.join(parts[:-1]) |
| else: |
| uid = original_uid |
| |
| data_path = os.path.join(asset_dir, uid, "data.json") |
| if not os.path.exists(data_path): |
| if os.path.exists(os.path.join(asset_dir, original_uid, "data.json")): |
| uid = original_uid |
| data_path = os.path.join(asset_dir, uid, "data.json") |
| else: |
| continue |
| |
| with open(data_path, "r") as f: |
| data = json.load(f) |
| data['path'] = os.path.join(asset_dir, uid, f"{uid}.glb") |
| all_data[uid].append(data) |
|
|
| category_count = collections.defaultdict(int) |
| for uid, duplicated_assets in all_data.items(): |
| if not duplicated_assets: continue |
| category_var_name = duplicated_assets[0]['annotations']['category'] |
| category_var_name = category_var_name.replace('-', "_").replace(" ", "_").replace("'", "_").replace("/", "_").replace(",", "_").lower() |
| category_count[category_var_name] += 1 |
|
|
| new_assets = {} |
| category_idx = collections.defaultdict(int) |
| |
| for uid, duplicated_assets in all_data.items(): |
| if not duplicated_assets: continue |
| category_var_name = duplicated_assets[0]['annotations']['category'] |
| category_var_name = category_var_name.replace('-', "_").replace(" ", "_").replace("'", "_").replace("/", "_").replace(",", "_").lower() |
| category_idx[category_var_name] += 1 |
| |
| for instance_idx, data in enumerate(duplicated_assets): |
| category_var_name_final = f"{category_var_name}_{chr(ord('A') + category_idx[category_var_name]-1)}" if category_count[category_var_name] > 1 else category_var_name |
| var_name = f"{category_var_name_final}_{instance_idx}" if len(duplicated_assets) > 1 else category_var_name_final |
| |
| key = f"{category_var_name_final}-{instance_idx}" |
| |
| new_assets[key] = { |
| "uid": uid, |
| "count": len(duplicated_assets), |
| "instance_var_name": var_name, |
| "asset_var_name": category_var_name_final, |
| "instance_idx": instance_idx, |
| "annotations": data["annotations"], |
| "category": data["annotations"]["category"], |
| 'description': data['annotations']['description'], |
| 'path': data['path'], |
| 'assetMetadata': { |
| "boundingBox": { |
| "x": float(data['assetMetadata']['boundingBox']['y']), |
| "y": float(data['assetMetadata']['boundingBox']['x']), |
| "z": float(data['assetMetadata']['boundingBox']['z']) |
| }, |
| } |
| } |
|
|
| task["assets"] = new_assets |
| return task |
|
|
| def process_dataset(results_dir, asset_dir, views=['topdown', 'diagonal']): |
| """Process all samples in a results directory""" |
| print(f"Processing dataset: {results_dir}") |
| |
| samples = sorted([d for d in os.listdir(results_dir) if d.startswith('sample_')]) |
| |
| for sample_name in tqdm(samples, desc="Rendering"): |
| sample_dir = os.path.join(results_dir, sample_name) |
| scene_config_path = os.path.join(sample_dir, 'scene_config.json') |
| layout_path = os.path.join(sample_dir, 'layout.json') |
| |
| if not os.path.exists(layout_path): |
| continue |
| |
| if os.path.exists(os.path.join(sample_dir, 'render_topdown.png')): |
| continue |
|
|
| try: |
| with open(scene_config_path, 'r') as f: |
| scene_config = json.load(f) |
| |
| scene_config = prepare_task_assets(scene_config, asset_dir) |
| |
| with open(layout_path, 'r') as f: |
| layout = json.load(f) |
| |
| scene_objects = [] |
| |
| for key, transform in layout.items(): |
| if key not in scene_config['assets']: |
| continue |
| |
| asset_info = scene_config['assets'][key] |
| |
| rot_rad = transform.get('rotation', [0, 0, 0]) |
| rot_deg = [math.degrees(r) for r in rot_rad] |
| |
| bbox = asset_info['assetMetadata']['boundingBox'] |
| size = [bbox['x'], bbox['y'], bbox['z']] |
| |
| obj_data = { |
| 'id': key, |
| 'model_id': asset_info['uid'], |
| 'category': asset_info['category'], |
| 'position': transform.get('position', [0, 0, 0]), |
| 'rotation': rot_deg, |
| 'size': size |
| } |
| scene_objects.append(obj_data) |
| |
| scene_for_viz = { |
| 'boundary': scene_config.get('boundary', {}).get('floor_vertices', []), |
| 'assets': scene_objects |
| } |
| |
| if not scene_for_viz['boundary'] and 'boundary' in scene_config: |
| if isinstance(scene_config['boundary'], dict): |
| scene_for_viz['boundary'] = scene_config['boundary'].get('floor_vertices', []) |
| |
| for view in views: |
| output_path = os.path.join(sample_dir, f'render_{view}.png') |
| |
| viz_view = view |
| if view == 'topdown': viz_view = 'top' |
| elif view == 'side_front': viz_view = 'front' |
| |
| if viz_view not in ['top', 'diagonal', 'front']: |
| continue |
| |
| viz.visualize_scene( |
| scene_for_viz, |
| output_path=output_path, |
| render=True, |
| asset_dir=asset_dir, |
| view=viz_view |
| ) |
| |
| except Exception as e: |
| print(f"Error processing {sample_name}: {e}") |
|
|
| def main(): |
| parser = argparse.ArgumentParser() |
| parser.add_argument('--asset_dir', required=True) |
| args = parser.parse_args() |
| |
| |
| process_dataset('results/bench_test_700', args.asset_dir, views=['topdown', 'diagonal']) |
|
|
| if __name__ == "__main__": |
| main() |
|
|