Spaces:
Paused
Paused
Add code/cube3d/training/missing_obj_checker.py
Browse files
code/cube3d/training/missing_obj_checker.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import shutil
|
| 4 |
+
|
| 5 |
+
# 未找到对应OBJ的前缀列表
|
| 6 |
+
missing_prefixes = [
|
| 7 |
+
"6014a", "5654", "98138pb010", "3069bpb0348", "2431pb038",
|
| 8 |
+
"98138pb058", "MAYDAY0329", "85984pb289", "3069bpx36",
|
| 9 |
+
"44301a", "3069bpb001", "3069bpb0399", "98138pb033",
|
| 10 |
+
"3039pb041", "3069BPB0648", "MMAYDAY25TH", "4304"
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
# 配置路径
|
| 14 |
+
ldr_directory = "/public/home/wangshuo/gap/assembly/data/car_1k/subset_self/ldr_l30_rotrans_expand_wom" # LDR源目录
|
| 15 |
+
target_directory = "/public/home/wangshuo/gap/assembly/cubedit/outputs/missingobj_ldr_files" # 目标文件夹
|
| 16 |
+
|
| 17 |
+
# 创建目标目录(如果不存在)
|
| 18 |
+
os.makedirs(target_directory, exist_ok=True)
|
| 19 |
+
|
| 20 |
+
# 将缺失前缀转为小写(统一基准)
|
| 21 |
+
missing_prefixes_lower = [prefix.lower() for prefix in missing_prefixes]
|
| 22 |
+
|
| 23 |
+
# 结果字典:{原始前缀: [使用该前缀的LDR文件列表]}
|
| 24 |
+
usage_results = {prefix: [] for prefix in missing_prefixes}
|
| 25 |
+
# 记录每个前缀要复制的LDR文件(每个前缀最多一个)
|
| 26 |
+
prefix_to_ldr = {}
|
| 27 |
+
|
| 28 |
+
# 遍历LDR文件
|
| 29 |
+
for filename in os.listdir(ldr_directory):
|
| 30 |
+
if filename.lower().endswith('.ldr') and len(prefix_to_ldr) < len(missing_prefixes): # 所有前缀都找到对应文件后可提前退出
|
| 31 |
+
filepath = os.path.join(ldr_directory, filename)
|
| 32 |
+
try:
|
| 33 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
| 34 |
+
content_lower = f.read().lower()
|
| 35 |
+
|
| 36 |
+
# 检查每个小写前缀是否在小写内容中出现
|
| 37 |
+
for i, prefix_lower in enumerate(missing_prefixes_lower):
|
| 38 |
+
original_prefix = missing_prefixes[i]
|
| 39 |
+
# 如果该前缀还没有找到对应的LDR文件,则进行匹配
|
| 40 |
+
if original_prefix not in prefix_to_ldr:
|
| 41 |
+
pattern = r'(?<!\w)' + re.escape(prefix_lower) + r'(?!\w)'
|
| 42 |
+
if re.search(pattern, content_lower):
|
| 43 |
+
usage_results[original_prefix].append(filename)
|
| 44 |
+
# 记录第一个找到的LDR文件
|
| 45 |
+
prefix_to_ldr[original_prefix] = filepath
|
| 46 |
+
|
| 47 |
+
except UnicodeDecodeError:
|
| 48 |
+
print(f"警告:文件 {filename} 编码异常,已跳过")
|
| 49 |
+
continue
|
| 50 |
+
|
| 51 |
+
# 复制每个前缀对应的LDR文件(每个前缀只复制一个)
|
| 52 |
+
copied_files = []
|
| 53 |
+
for prefix, src_path in prefix_to_ldr.items():
|
| 54 |
+
filename = os.path.basename(src_path)
|
| 55 |
+
dest_path = os.path.join(target_directory, filename)
|
| 56 |
+
|
| 57 |
+
# 避免重复复制
|
| 58 |
+
if not os.path.exists(dest_path):
|
| 59 |
+
shutil.copy2(src_path, dest_path) # 保留文件元数据
|
| 60 |
+
copied_files.append(f"{prefix} -> {filename}")
|
| 61 |
+
print(f"已复制: {prefix} -> {filename}")
|
| 62 |
+
|
| 63 |
+
# 打印结果
|
| 64 |
+
print("\n===== 未找到OBJ前缀的LDR文件使用情况 =====")
|
| 65 |
+
for prefix, files in usage_results.items():
|
| 66 |
+
if files:
|
| 67 |
+
print(f"\n前缀 '{prefix}' 被以下LDR文件使用:")
|
| 68 |
+
for file in files:
|
| 69 |
+
print(f" - {file}")
|
| 70 |
+
else:
|
| 71 |
+
print(f"\n前缀 '{prefix}' 未被任何LDR文件使用")
|
| 72 |
+
|
| 73 |
+
# 结果汇总
|
| 74 |
+
used_prefixes = [prefix for prefix, files in usage_results.items() if files]
|
| 75 |
+
unused_prefixes = [prefix for prefix, files in usage_results.items() if not files]
|
| 76 |
+
|
| 77 |
+
print("\n===== 结果汇总 =====")
|
| 78 |
+
print(f"被使用的缺失前缀 ({len(used_prefixes)}个):")
|
| 79 |
+
print(", ".join(used_prefixes))
|
| 80 |
+
|
| 81 |
+
print(f"\n未被使用的缺失前缀 ({len(unused_prefixes)}个):")
|
| 82 |
+
print(", ".join(unused_prefixes))
|
| 83 |
+
|
| 84 |
+
print(f"\n已为 {len(copied_files)} 个前缀各复制了一个相关LDR文件到目标目录: {target_directory}")
|
| 85 |
+
if copied_files:
|
| 86 |
+
print("复制详情:")
|
| 87 |
+
for item in copied_files:
|
| 88 |
+
print(f" {item}")
|