File size: 8,103 Bytes
6de889a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | 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) |