Caesarrr's picture
Add files using upload-large-folder tool
60fde3b verified
import json
import os
import math
import numpy as np
import torch
from pytorch3d.transforms import so3_exp_map
def get_camera_center(R, T):
"""计算相机中心: C = -R^T * T"""
return -R.T @ T
def get_view_direction(R):
"""
获取相机光轴方向。
PyTorch3D 约定: x_cam = x_world @ R + T
这意味着 R 的第 3 行 (index 2) 是相机 Z 轴在世界系下的方向。
"""
return R[2, :]
def get_camera_up(R):
"""
获取相机的 Up 向量。
在 PyTorch3D/CO3D 约定中:
R 的第 1 行 (index 1) 是相机坐标系的 Y 轴在世界系的方向。
通常相机坐标系 Y 轴向下 (Screen Down),所以 Up 向量是 -R[1, :]。
"""
return -R[1, :]
def align_scene_to_standard_up(seq_data, to_vec=(0.0, 1.0, 0.0),
from_vec=(-0.0396, -0.8306, -0.5554)):
"""
将CO3D场景旋转到标准坐标系,使得地面法向量指向Y轴正方向。
基于CO3D GitHub Issue #64的官方方法。
Args:
seq_data: 序列数据 {frame_id: {'R': ..., 'T': ..., 'path': ...}}
to_vec: 目标up向量(默认Y轴正方向)
from_vec: CO3D场景中的地面法向量(来自官方issue)
Returns:
aligned_seq_data: 对齐后的序列数据
R_align: 对齐旋转矩阵
mean_center: 场景中心
"""
# 1. 计算所有相机中心
centers = []
for fid, info in seq_data.items():
C = get_camera_center(info['R'], info['T'])
centers.append(C)
mean_center = np.mean(centers, axis=0)
# 2. 计算对齐旋转(从CO3D地面法向量到标准Y轴)
to_vec_tensor = torch.FloatTensor(to_vec)
from_vec_tensor = torch.FloatTensor(from_vec)
# 旋转轴角表示
rot_axis_angle = torch.cross(to_vec_tensor, from_vec_tensor)
R_align = so3_exp_map(rot_axis_angle[None])[0].numpy()
# 3. 应用对齐变换到所有相机
aligned_seq_data = {}
for fid, info in seq_data.items():
R_orig = info['R']
T_orig = info['T']
# 变换公式(参考issue #64)
# 相机外参的变换:R_new = R_align^T @ R_orig
R_new = R_align.T @ R_orig
# T_new = (R_align^T @ T_orig) - R_align^T @ mean_center + mean_center
# 简化:先中心化,旋转,再去中心化
T_new = R_align.T @ T_orig
aligned_seq_data[fid] = {
'R': R_new,
'T': T_new,
'path': info['path']
}
return aligned_seq_data, R_align, mean_center
def get_sequence_geometry(seq_data, align_to_standard=True):
"""
获取序列的几何信息。
Args:
seq_data: 序列数据
align_to_standard: 是否对齐到标准坐标系(Y轴向上)
Returns:
mean_center: 场景中心
basis: 基底矩阵
aligned_seq_data: 对齐后的序列数据(如果align_to_standard=True)
"""
if align_to_standard:
# 使用CO3D官方的地面法向量进行对齐
aligned_seq_data, R_align, mean_center = align_scene_to_standard_up(seq_data)
# 对齐后的坐标系就是标准的PyTorch3D坐标系
# Y轴向上(地面法向量),XZ平面是地面
world_up = np.array([0.0, 1.0, 0.0])
u = np.array([1.0, 0.0, 0.0])
v = np.array([0.0, 0.0, 1.0])
basis = np.stack([u, v, world_up], axis=0)
return mean_center, basis, aligned_seq_data
else:
# 不对齐,使用CO3D原始的地面法向量
centers = []
for fid, info in seq_data.items():
C = get_camera_center(info['R'], info['T'])
centers.append(C)
mean_center = np.mean(centers, axis=0)
# CO3D的地面法向量(来自官方issue #64)
co3d_ground_normal = np.array([-0.0396, -0.8306, -0.5554])
co3d_ground_normal = co3d_ground_normal / np.linalg.norm(co3d_ground_normal)
# 构建与地面法向量正交的两个水平方向
# 选择一个任意向量
if abs(co3d_ground_normal[0]) < 0.9:
arbitrary = np.array([1, 0, 0])
else:
arbitrary = np.array([0, 1, 0])
# Gram-Schmidt正交化
u = arbitrary - np.dot(arbitrary, co3d_ground_normal) * co3d_ground_normal
u = u / np.linalg.norm(u)
v = np.cross(co3d_ground_normal, u)
v = v / np.linalg.norm(v)
basis = np.stack([u, v, co3d_ground_normal], axis=0)
return mean_center, basis, seq_data
def get_relative_yaw(R_ref, T_ref, R_curr, T_curr, mean_center, basis):
"""
计算两帧之间在地面平面上的相对角度(azimuth)。
Args:
R_ref, T_ref: 参考帧的旋转和平移
R_curr, T_curr: 当前帧的旋转和平移
mean_center: 场景中心
basis: 基底矩阵 [u, v, normal],其中normal是地面法向量
Returns:
angle_deg: 角度(度),正值表示逆时针,负值表示顺时针
"""
C_ref = get_camera_center(R_ref, T_ref)
C_curr = get_camera_center(R_curr, T_curr)
# 转换到局部坐标系
p_ref_local = (C_ref - mean_center) @ basis.T
p_curr_local = (C_curr - mean_center) @ basis.T
# 只使用前两个分量(地面平面上的投影)
u_ref, v_ref = p_ref_local[0], p_ref_local[1]
u_curr, v_curr = p_curr_local[0], p_curr_local[1]
# 计算方位角
angle_ref = np.arctan2(v_ref, u_ref)
angle_curr = np.arctan2(v_curr, u_curr)
# 计算差值
diff_rad = angle_curr - angle_ref
# 归一化到 (-pi, pi]
diff_rad = (diff_rad + np.pi) % (2 * np.pi) - np.pi
angle_deg = np.degrees(diff_rad)
return angle_deg
def format_angle_direction(angle):
"""将角度数值转换为 (绝对值, 方向字符串)"""
angle = (angle + 180) % 360 - 180
direction = "anticlockwise" if angle > 0 else "clockwise"
return int(round(abs(angle))), direction
def get_angle_diff(angle_a, angle_b):
"""计算两个角度在圆周上的最小差值"""
diff = abs(angle_a - angle_b)
return min(diff, 360 - diff)
# --- 2. 路径处理工具 ---
def format_image_path(raw_path, root_path, image_prefix="data/"):
"""
统一处理图片路径:
1. 去除绝对路径中的 root_path
2. 加上统一的 image_prefix
"""
if root_path.endswith('/'):
root_path = root_path[:-1]
if raw_path.startswith(root_path):
rel_path = raw_path[len(root_path):]
if rel_path.startswith('/'):
rel_path = rel_path[1:]
else:
rel_path = raw_path
if not image_prefix:
return rel_path
if not image_prefix.endswith('/'):
image_prefix += '/'
if rel_path.startswith(image_prefix):
return rel_path
return os.path.join(image_prefix, rel_path)
# --- 3. 数据加载类 (核心) ---
class CO3DDataLoader:
"""
负责加载指定 Category 的 annotation,并提供方便的接口获取帧数据。
"""
def __init__(self, root_path, category):
self.root_path = root_path
self.category = category
self.seq_data = {}
self._load_data()
def _load_data(self):
cat_dir = os.path.join(self.root_path, 'data', 'original', self.category)
json_path = os.path.join(cat_dir, 'frame_annotations.json')
if not os.path.exists(json_path):
print(f"Warning: Annotation file {json_path} not found.")
return
if not os.path.exists(cat_dir):
print(f"Warning: Category directory {cat_dir} not found.")
return
valid_sequences = set()
for d in os.listdir(cat_dir):
d_path = os.path.join(cat_dir, d)
if os.path.isdir(d_path):
valid_sequences.add(d)
with open(json_path, 'r') as f:
annotations = json.load(f)
for item in annotations:
seq = item['sequence_name']
if seq not in valid_sequences:
continue
fid = item['frame_number']
if seq not in self.seq_data:
self.seq_data[seq] = {}
self.seq_data[seq][fid] = {
'R': np.array(item['viewpoint']['R']),
'T': np.array(item['viewpoint']['T']),
'path': item['image']['path']
}
def get_sequences(self):
"""返回排好序的序列列表"""
seqs = list(self.seq_data.keys())
seqs.sort()
return seqs
def get_frames(self, sequence_name):
"""返回该序列下所有帧的索引列表"""
if sequence_name not in self.seq_data:
return []
return list(self.seq_data[sequence_name].keys())
def get_frame_info(self, sequence_name, frame_idx):
"""获取特定帧的 R, T 和 Path"""
return self.seq_data[sequence_name].get(frame_idx)
# --- 4. 结果保存工具 ---
def save_jsonl_splits(all_data, output_dir, ratios=(0.8, 0.1, 0.1), max_items=10000, seed=42):
"""
自动打乱、切分 Train/Val/Test 并保存为分片的 JSONL
"""
import random
random.seed(seed)
random.shuffle(all_data)
n_total = len(all_data)
r_train, r_val, r_test = ratios
total_r = sum(ratios)
r_train, r_val, r_test = r_train/total_r, r_val/total_r, r_test/total_r
n_train = int(n_total * r_train)
n_val = int(n_total * r_val)
splits = {
"train": all_data[:n_train],
"val": all_data[n_train : n_train + n_val],
"test": all_data[n_train + n_val :]
}
print(f"Split result: Train({len(splits['train'])}), Val({len(splits['val'])}), Test({len(splits['test'])})")
for split_name, data_list in splits.items():
if not data_list: continue
save_path = os.path.join(output_dir, split_name)
os.makedirs(save_path, exist_ok=True)
num_files = math.ceil(len(data_list) / max_items)
for i in range(num_files):
chunk = data_list[i*max_items : (i+1)*max_items]
fname = f"{split_name}_{i+1}.jsonl"
with open(os.path.join(save_path, fname), 'w') as f:
for item in chunk:
f.write(json.dumps(item) + '\n')
def decompose_angle(target_angle, allowed_steps=[15, 30, 45]):
"""
使用贪心算法将目标角度拆解为允许的步长组合。
优先使用大步长,总和最接近 target_angle。
"""
allowed_steps = sorted(allowed_steps, reverse=True)
steps_taken = []
current_sum = 0
# 简单的贪心策略:只要加上步长不超过目标太多,就加
# 这里设定一个容忍度,允许稍微超过一点点,或者稍微少一点点
# 比如目标 27,步长 [15, 10]。
# 1. 选 15 -> rem 12
# 2. 选 10 -> rem 2
# 3. 剩余 2,太小,忽略。
# 结果 [15, 10] -> 总和 25
remaining = target_angle
while remaining > 0:
best_step = None
min_diff = float('inf')
# 尝试找一个步长,使得剩余角度变小
for step in allowed_steps:
# 如果这个步长比剩余的大太多(比如剩余5度,步长30度),就不合适
# 但如果剩余 13度,步长15度,这是合适的
if step <= remaining + 5: # +5 是容忍度,允许轻微过冲
best_step = step
break # 因为已经排过序,找到的第一个就是最大的合适步长
if best_step:
steps_taken.append(best_step)
remaining -= best_step
current_sum += best_step
else:
# 如果找不到合适的步长了(剩余角度比最小步长还小很多),就停止
break
# 如果列表为空(比如角度极小),至少给一个最小步长
if not steps_taken:
steps_taken.append(allowed_steps[-1])
return steps_taken
def estimate_ground_normal_pca(R_list, T_list):
"""
使用 PCA/SVD 拟合相机轨迹所在的平面,并利用相机 Up 向量消除方向歧义。
Args:
R_list: 旋转矩阵列表
T_list: 平移向量列表
Returns:
normal: 矫正后的地面法向量 (指向"天空")
mean_center: 轨迹的几何中心
"""
# 1. 计算所有相机中心
centers = []
up_vectors = []
for R, T in zip(R_list, T_list):
# 计算相机中心 C = -R^T * T
C = -np.dot(R.T, T)
centers.append(C)
# 获取相机 Up 向量 (在世界坐标系下)
# PyTorch3D/Co3D 约定: R[1, :] 是相机 Y 轴 (Screen Down)
# 所以 Up 向量是 -R[1, :]
up_vec = -R[1, :]
up_vectors.append(up_vec)
centers = np.array(centers)
up_vectors = np.array(up_vectors)
# 2. SVD 拟合平面
mean_center = np.mean(centers, axis=0)
centered_points = centers - mean_center
# 对点集进行奇异值分解
# vh 的最后一行对应最小奇异值,即平面的法向量
try:
u, s, vh = np.linalg.svd(centered_points)
normal = vh[2, :]
except np.linalg.LinAlgError:
# 如果 SVD 失败(例如点太少或共线),回退到默认 Y 轴
return np.array([0.0, 1.0, 0.0]), mean_center
# 3. 消除方向歧义 (Ambiguity Resolution)
# 计算法向量与所有相机 Up 向量的平均点积
# 如果大多数相机是正着拿的,它们的 Up 向量应该和地面法向量夹角小于 90 度
avg_up = np.mean(up_vectors, axis=0)
if np.dot(normal, avg_up) > 0:
normal = -normal
return normal, mean_center
def get_sequence_geometry_pca(seq_data):
"""
基于 PCA 拟合的动态对齐方法。
替代原有的 get_sequence_geometry。
"""
# 提取所有 R 和 T
R_list = [info['R'] for info in seq_data.values()]
T_list = [info['T'] for info in seq_data.values()]
# 1. 计算 PCA 法向量和中心
pca_normal, mean_center = estimate_ground_normal_pca(R_list, T_list)
# 2. 构建新的基底 (Basis)
# 我们需要构建 [u, v, n],其中 n = pca_normal
# u, v 是平面上的正交向量
# 归一化法向量
n = pca_normal / np.linalg.norm(pca_normal)
# 选取一个辅助向量来生成 u (避免与 n 平行)
if abs(n[0]) < 0.9:
arbitrary = np.array([1, 0, 0])
else:
arbitrary = np.array([0, 1, 0])
# Gram-Schmidt 正交化
u = arbitrary - np.dot(arbitrary, n) * n
u = u / np.linalg.norm(u)
v = np.cross(n, u)
v = v / np.linalg.norm(v)
# Basis: rows are u, v, n
basis = np.stack([u, v, n], axis=0)
return mean_center, basis, n