0xZohar commited on
Commit
5ec45ba
·
verified ·
1 Parent(s): d153774

Add code/cube3d/training/dat_mapping_merge.py

Browse files
code/cube3d/training/dat_mapping_merge.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import re
4
+
5
+ # 提取文件名中起始的连续数字(核心逻辑)
6
+ def extract_starting_numbers(filename):
7
+ # 匹配字符串开头的连续数字
8
+ match = re.match(r'^\d+', filename)
9
+ if match:
10
+ return match.group() # 返回起始数字字符串(如"4592")
11
+ return filename # 无起始数字则返回原文件名(小写处理后)
12
+
13
+ # 处理单个ldr文件数据
14
+ def process_ldr_data(lines, label_mapping, label_inverse_mapping, label_counter):
15
+ all_labels = []
16
+
17
+ for line in lines:
18
+ if line.startswith('1'): # 只处理零件数据行
19
+ parts = line.split()
20
+ if len(parts) < 15:
21
+ print(f"Skipping invalid line: {line.strip()}")
22
+ continue
23
+
24
+ # 1. 统一转为小写
25
+ filename = parts[14].lower()
26
+ # 2. 提取起始连续数字作为标识
27
+ part_identifier = extract_starting_numbers(filename)
28
+ print(f"Processing file: {filename} → Identifier: {part_identifier}")
29
+
30
+ if part_identifier not in label_mapping:
31
+ label_mapping[part_identifier] = label_counter
32
+ label_inverse_mapping[label_counter] = part_identifier
33
+ label_counter += 1
34
+ all_labels.append(label_mapping[part_identifier])
35
+
36
+ return label_mapping, label_inverse_mapping, label_counter
37
+
38
+ # 处理一个文件夹中的所有ldr文件
39
+ def process_all_ldr_in_folder(folder_path):
40
+ overall_label_mapping = {}
41
+ overall_label_inverse_mapping = {}
42
+ label_counter = 0
43
+
44
+ for root, dirs, files in os.walk(folder_path):
45
+ for file in files:
46
+ if file.endswith('.ldr'):
47
+ file_path = os.path.join(root, file)
48
+ with open(file_path, 'r') as f:
49
+ lines = f.readlines()
50
+ overall_label_mapping, overall_label_inverse_mapping, label_counter = process_ldr_data(
51
+ lines, overall_label_mapping, overall_label_inverse_mapping, label_counter)
52
+
53
+ return overall_label_mapping, overall_label_inverse_mapping
54
+
55
+ # 保存mapping到文件
56
+ def save_mappings(label_mapping, label_inverse_mapping, output_dir):
57
+ os.makedirs(output_dir, exist_ok=True)
58
+
59
+ with open(os.path.join(output_dir, 'label_mapping.json'), 'w') as f:
60
+ json.dump(label_mapping, f, indent=4)
61
+
62
+ with open(os.path.join(output_dir, 'label_inverse_mapping.json'), 'w') as f:
63
+ json.dump(label_inverse_mapping, f, indent=4)
64
+
65
+
66
+ if __name__ == "__main__":
67
+ input_folder = '/public/home/wangshuo/gap/assembly/data/car_1k/subset_bottom_300/ldr_rot_expand_trans'
68
+ output_folder = '/public/home/wangshuo/gap/assembly/data/car_1k/subset_bottom_300'
69
+
70
+ label_mapping, label_inverse_mapping = process_all_ldr_in_folder(input_folder)
71
+ save_mappings(label_mapping, label_inverse_mapping, output_folder)
72
+ print(f"Label mappings have been saved to {output_folder}")