0xZohar commited on
Commit
fa0e257
·
verified ·
1 Parent(s): 764fa39

Add code/cube3d/training/rotate_bacthLdr.py

Browse files
code/cube3d/training/rotate_bacthLdr.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ import pandas as pd
4
+ from math import cos, sin, radians
5
+ # 从原有文件导入所需函数
6
+ from rotateLdr import (checkEncoding, readTextFile,
7
+ apply_rotation, apply_rotation_matrix,
8
+ getModulePart, process_ldr)
9
+
10
+ def create_rotation_matrix(y_rotation_code):
11
+ """
12
+ 根据旋转编码创建Y轴旋转矩阵
13
+ 0: 不旋转, 1: 绕Y轴90度, 2: 绕Y轴180度, 3: 绕Y轴270度
14
+ """
15
+ rotation_angles = {0: 0, 1: -90, 2: -180, 3: -270}
16
+ y_angle = rotation_angles.get(y_rotation_code, 0)
17
+ y_rad = radians(y_angle)
18
+
19
+ # 绕Y轴旋转矩阵
20
+ rot_mat = [
21
+ [cos(y_rad), 0, sin(y_rad)],
22
+ [0, 1, 0],
23
+ [-sin(y_rad), 0, cos(y_rad)]
24
+ ]
25
+ return rot_mat, y_angle
26
+
27
+ def extract_prefix(filename):
28
+ """从文件名中提取前缀(去除扩展名和可能的_车底)"""
29
+ # 先去除文件扩展名
30
+ prefix = os.path.splitext(str(filename))[0]
31
+ # 去除可能存在的_车底后缀
32
+ if prefix.endswith("_车底"):
33
+ prefix = prefix[:-3] # "_车底"是3个字符,截取前面部分
34
+ return prefix
35
+
36
+ def batch_process_ldr(input_dir, output_dir, excel_path):
37
+ """
38
+ 批量处理LDR文件,通过前缀匹配Excel中的记录
39
+ """
40
+ # 创建输出目录
41
+ os.makedirs(output_dir, exist_ok=True)
42
+
43
+ # 读取Excel文件
44
+ try:
45
+ df = pd.read_excel(excel_path, engine='openpyxl')
46
+ # 确保旋转编码列是整数类型
47
+ rotation_code_col = df.columns[1] # 第二列是旋转编码
48
+ df[rotation_code_col] = df[rotation_code_col].astype(int, errors='ignore')
49
+
50
+ # 提取文件名前缀并添加到DataFrame
51
+ df['prefix'] = df['Name'].apply(extract_prefix)
52
+ print(f"成功读取Excel文件,共 {len(df)} 条记录")
53
+ except Exception as e:
54
+ print(f"读取Excel文件出错: {e}")
55
+ return
56
+
57
+ # 获取所有LDR文件
58
+ ldr_files = [f for f in os.listdir(input_dir) if f.lower().endswith('.ldr')]
59
+ print(f"在 {input_dir} 中找到 {len(ldr_files)} 个LDR文件")
60
+
61
+ # 处理每个LDR文件
62
+ processed_count = 0
63
+ for ldr_file in ldr_files:
64
+ # 提取LDR文件的前缀和扩展名
65
+ file_base, file_ext = os.path.splitext(ldr_file)
66
+ # 构建带_rot后缀的输出文件名 (例如 car_29.ldr → car_29_rot.ldr)
67
+ output_filename = f"{file_base}_rot{file_ext}"
68
+ # 提取LDR文件的前缀
69
+ ldr_prefix = extract_prefix(ldr_file)
70
+
71
+ # 查找匹配的前缀
72
+ matched_rows = df[df['prefix'] == ldr_prefix]
73
+ if matched_rows.empty:
74
+ print(f"警告: 在Excel中未找到与 {ldr_file} (前缀: {ldr_prefix}) 匹配的记录,跳过")
75
+ continue
76
+
77
+ # 获取旋转编码
78
+ try:
79
+ rotation_code = int(matched_rows.iloc[0][rotation_code_col])
80
+ if rotation_code not in [0, 1, 2, 3]:
81
+ print(f"警告: {ldr_file} 的旋转编码 {rotation_code} 无效,使用默认值0")
82
+ rotation_code = 0
83
+ except (ValueError, IndexError, KeyError) as e:
84
+ print(f"警告: {ldr_file} 的旋转编码获取失败: {e},使用默认值0")
85
+ rotation_code = 0
86
+
87
+ # 创建旋转矩阵
88
+ rot_mat, y_angle = create_rotation_matrix(rotation_code)
89
+ print(f"处理文件: {ldr_file}, 旋转编码: {rotation_code} (绕Y轴旋转 {y_angle} 度)")
90
+
91
+ # 处理文件
92
+ input_path = os.path.join(input_dir, ldr_file)
93
+ output_path = os.path.join(output_dir, output_filename)
94
+
95
+ if process_ldr(input_path, output_path, rot_mat):
96
+ processed_count += 1
97
+
98
+ print(f"批量处理完成,成功处理 {processed_count}/{len(ldr_files)} 个文件")
99
+
100
+
101
+ if __name__ == "__main__":
102
+ INPUT_DIR = "/public/home/wangshuo/gap/assembly/data/car_1k/subset_bottom_300/ldr_bottom_gather_test/" # 包含多个LDR文件的目录
103
+ OUTPUT_DIR = "/public/home/wangshuo/gap/assembly/data/car_1k/subset_bottom_300/ldr_rot_test/" # 输出目录
104
+ EXCEL_PATH = "/public/home/wangshuo/gap/assembly/data/car_1k/subset_bottom_300/Pose_test.xlsx" # 包含旋转角度的Excel文件
105
+ batch_process_ldr(INPUT_DIR, OUTPUT_DIR, EXCEL_PATH)
106
+