ZoneMaestro_code / eval /respace /tools /complement_bottom.py
kkkkiiii's picture
Add files using upload-large-folder tool
6de889a verified
Raw
History Blame Contribute Delete
8.1 kB
import os
import json
from pathlib import Path
def integrate_bounds_fields(scenes_dir: str, grouped_layouts_dir: str, backup: bool = True):
"""
将scenes目录下的bounds_top和bounds_bottom字段整合到grouped_layouts目录下的同名文件中
Args:
scenes_dir: dataset-ssr3dfront/scenes目录路径
grouped_layouts_dir: grouped_layouts目录路径
backup: 是否在修改前创建备份
"""
scenes_path = Path(scenes_dir)
grouped_path = Path(grouped_layouts_dir)
if not scenes_path.exists():
print(f"Scenes目录不存在: {scenes_path}")
return
if not grouped_path.exists():
print(f"Grouped layouts目录不存在: {grouped_path}")
return
# 创建备份目录(如果需要)
if backup:
backup_dir = grouped_path.parent / "grouped_layouts_backup"
backup_dir.mkdir(exist_ok=True)
print(f"备份目录: {backup_dir}")
# 统计信息
total_files = 0
processed_files = 0
missing_scenes = 0
errors = 0
print("开始处理文件...")
# 遍历grouped_layouts目录中的所有JSON文件
for grouped_file in grouped_path.glob("*.json"):
total_files += 1
# 查找对应的scene文件
scene_file = scenes_path / grouped_file.name
if not scene_file.exists():
missing_scenes += 1
print(f"警告: 未找到对应的scene文件: {scene_file.name}")
continue
try:
# 读取grouped layout文件
with open(grouped_file, 'r', encoding='utf-8') as f:
grouped_data = json.load(f)
# 读取scene文件
with open(scene_file, 'r', encoding='utf-8') as f:
scene_data = json.load(f)
# 检查scene文件是否包含所需字段
if 'bounds_top' not in scene_data or 'bounds_bottom' not in scene_data:
print(f"警告: Scene文件缺少bounds字段: {scene_file.name}")
continue
# 创建备份(如果需要)
if backup:
backup_file = backup_dir / grouped_file.name
with open(backup_file, 'w', encoding='utf-8') as f:
json.dump(grouped_data, f, ensure_ascii=False, indent=2)
# 添加bounds字段到grouped data
grouped_data['bounds_top'] = scene_data['bounds_top']
grouped_data['bounds_bottom'] = scene_data['bounds_bottom']
# 写回grouped layout文件
with open(grouped_file, 'w', encoding='utf-8') as f:
json.dump(grouped_data, f, ensure_ascii=False, indent=2)
processed_files += 1
if processed_files % 100 == 0:
print(f"已处理: {processed_files}/{total_files} 文件")
except Exception as e:
errors += 1
print(f"处理文件时出错 {grouped_file.name}: {e}")
# 输出统计信息
print("\n=== 处理完成 ===")
print(f"总文件数: {total_files}")
print(f"成功处理: {processed_files}")
print(f"缺少对应scene文件: {missing_scenes}")
print(f"处理错误: {errors}")
if backup:
print(f"原始文件已备份到: {backup_dir}")
def verify_integration(grouped_layouts_dir: str, sample_size: int = 10):
"""
验证整合结果,检查部分文件是否正确添加了bounds字段
Args:
grouped_layouts_dir: grouped_layouts目录路径
sample_size: 检查的样本文件数量
"""
grouped_path = Path(grouped_layouts_dir)
json_files = list(grouped_path.glob("*.json"))
if not json_files:
print("未找到JSON文件")
return
# 随机选择样本文件进行检查
import random
sample_files = random.sample(json_files, min(sample_size, len(json_files)))
print(f"\n=== 验证结果 (检查 {len(sample_files)} 个样本文件) ===")
success_count = 0
for file_path in sample_files:
try:
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
has_bounds_top = 'bounds_top' in data
has_bounds_bottom = 'bounds_bottom' in data
if has_bounds_top and has_bounds_bottom:
success_count += 1
print(f"✓ {file_path.name}: 包含bounds字段")
else:
missing_fields = []
if not has_bounds_top:
missing_fields.append('bounds_top')
if not has_bounds_bottom:
missing_fields.append('bounds_bottom')
print(f"✗ {file_path.name}: 缺少字段 {missing_fields}")
except Exception as e:
print(f"✗ {file_path.name}: 读取错误 - {e}")
print(f"\n验证成功率: {success_count}/{len(sample_files)} ({success_count/len(sample_files)*100:.1f}%)")
def show_file_structure_comparison(scenes_dir: str, grouped_layouts_dir: str, filename: str):
"""
显示指定文件在两个目录中的结构对比
Args:
scenes_dir: scenes目录路径
grouped_layouts_dir: grouped_layouts目录路径
filename: 要对比的文件名
"""
scenes_file = Path(scenes_dir) / filename
grouped_file = Path(grouped_layouts_dir) / filename
print(f"\n=== 文件结构对比: {filename} ===")
# 显示scene文件结构
if scenes_file.exists():
try:
with open(scenes_file, 'r', encoding='utf-8') as f:
scene_data = json.load(f)
print(f"\nScene文件字段: {list(scene_data.keys())}")
if 'bounds_top' in scene_data:
print(f"bounds_top: {scene_data['bounds_top'][:2]}...") # 显示前2个元素
if 'bounds_bottom' in scene_data:
print(f"bounds_bottom: {scene_data['bounds_bottom'][:2]}...")
except Exception as e:
print(f"读取scene文件错误: {e}")
else:
print("Scene文件不存在")
# 显示grouped layout文件结构
if grouped_file.exists():
try:
with open(grouped_file, 'r', encoding='utf-8') as f:
grouped_data = json.load(f)
print(f"\nGrouped layout文件字段: {list(grouped_data.keys())}")
if 'bounds_top' in grouped_data:
print("✓ 包含bounds_top字段")
else:
print("✗ 缺少bounds_top字段")
if 'bounds_bottom' in grouped_data:
print("✓ 包含bounds_bottom字段")
else:
print("✗ 缺少bounds_bottom字段")
except Exception as e:
print(f"读取grouped layout文件错误: {e}")
else:
print("Grouped layout文件不存在")
if __name__ == "__main__":
# 设置目录路径
SCENES_DIR = "/home/v-meiszhang/amlt-project/respace/dataset-ssr3dfront/scenes"
GROUPED_LAYOUTS_DIR = "/home/v-meiszhang/amlt-project/respace/grouped_layouts_v2"
# 示例文件名进行结构对比
EXAMPLE_FILE = "0a8d471a-2587-458a-9214-586e003e9cf9-c944563d-1e2a-4ed0-9457-5a8f43c9f17c.json"
print("步骤1: 显示文件结构对比")
show_file_structure_comparison(SCENES_DIR, GROUPED_LAYOUTS_DIR, EXAMPLE_FILE)
print("\n" + "="*50)
print("步骤2: 开始整合bounds字段")
# 执行整合
integrate_bounds_fields(SCENES_DIR, GROUPED_LAYOUTS_DIR, backup=False)
print("\n" + "="*50)
print("步骤3: 验证整合结果")
# 验证结果
verify_integration(GROUPED_LAYOUTS_DIR, sample_size=5)
print("\n" + "="*50)
print("步骤4: 显示整合后的文件结构")
# 再次显示结构对比以确认更改
show_file_structure_comparison(SCENES_DIR, GROUPED_LAYOUTS_DIR, EXAMPLE_FILE)