feat: add gen code
Browse files- src/action_state/gen_task3.py +309 -3
- src/action_state/gen_task4.py +2 -2
- src/action_state/utils.py +96 -17
src/action_state/gen_task3.py
CHANGED
|
@@ -19,7 +19,7 @@
|
|
| 19 |
|
| 20 |
|
| 21 |
1.
|
| 22 |
-
The object in the initial view <image_start>[image_ref]<image_end> remains **static**. Imagine a camera rotating around this object along a continuous path. The total rotation from the start to the end is less than 180 degrees.
|
| 23 |
|
| 24 |
Below are four images captured during this rotation, labeled 1, 2, 3 and 4. They're currently shuffled.
|
| 25 |
|
|
@@ -37,7 +37,67 @@ D. [sequence_D]
|
|
| 37 |
|
| 38 |
|
| 39 |
2.
|
| 40 |
-
Given the initial view of the static object: <image_start>[image_ref]<image_end>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
Imagine a camera rotating around this object to capture a video sequence. The rotation covers an anlge of less than 180 degrees. We have extracted four frames from the sequence, labeled 1 to 4, but their order is jumbled.
|
| 43 |
|
|
@@ -52,6 +112,252 @@ A. [sequence_A]
|
|
| 52 |
B. [sequence_B]
|
| 53 |
C. [sequence_C]
|
| 54 |
D. [sequence_D]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
""
|
|
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
1.
|
| 22 |
+
The {object} in the initial view <image_start>[image_ref]<image_end> remains **static**. Imagine a camera rotating around this {object} along a continuous path. The direction of rotation is defined from a **top-down bird's-eye view**. The total rotation from the start to the end is less than 180 degrees.
|
| 23 |
|
| 24 |
Below are four images captured during this rotation, labeled 1, 2, 3 and 4. They're currently shuffled.
|
| 25 |
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
2.
|
| 40 |
+
Given the initial view of the **static** {object}: <image_start>[image_ref]<image_end>.
|
| 41 |
+
|
| 42 |
+
Imagine a camera rotating around this {object} to capture a video sequence. The rotation covers an anlge of less than 180 degrees. We have extracted four frames from the sequence, labeled 1 to 4, but their order is jumbled.
|
| 43 |
+
|
| 44 |
+
1. <image_start>[image_1]<image_end>
|
| 45 |
+
2. <image_start>[image_2]<image_end>
|
| 46 |
+
3. <image_start>[image_3]<image_end>
|
| 47 |
+
4. <image_start>[image_4]<image_end>
|
| 48 |
+
|
| 49 |
+
Which of the following four options correctly sorts these images into a coherent spatio-temporal starting after the initial view?
|
| 50 |
+
|
| 51 |
+
A. [sequence_A]
|
| 52 |
+
B. [sequence_B]
|
| 53 |
+
C. [sequence_C]
|
| 54 |
+
D. [sequence_D]
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
import os
|
| 60 |
+
import random
|
| 61 |
+
import itertools
|
| 62 |
+
import numpy as np
|
| 63 |
+
from copy import deepcopy
|
| 64 |
+
|
| 65 |
+
# 引入提供的 utils
|
| 66 |
+
from utils import (
|
| 67 |
+
CO3DDataLoader,
|
| 68 |
+
get_relative_yaw,
|
| 69 |
+
format_image_path,
|
| 70 |
+
save_jsonl_splits
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# --- 配置参数 ---
|
| 74 |
+
ROOT_PATH = "/run/determined/NAS1/public/lixinyuan/interleaved-co3d" # 请修改为实际 CO3D 数据集根目录
|
| 75 |
+
OUTPUT_DIR = "/run/determined/NAS1/public/lixinyuan/interleaved-umm/data/questions/task3"
|
| 76 |
+
CATEGORIES = ['tv'] # 示例类别,可根据需要添加更多
|
| 77 |
+
SAMPLES_PER_SEQ = 3 # 每个视频序列生成的样本数量
|
| 78 |
+
IMAGE_PREFIX = "data/raw_images" # 图片路径前缀
|
| 79 |
+
MIN_YAW_INTERVAL = 20.0 # [修改] 最小 Yaw 角度间隔 (度)
|
| 80 |
+
|
| 81 |
+
# --- 提示词模板 ---
|
| 82 |
+
# 注意:这里保留了 [image_ref] 和 [image_1] 等占位符,不直接替换为路径
|
| 83 |
+
TEMPLATES = [
|
| 84 |
+
"""The object in the initial view <image_start>[image_ref]<image_end> remains **static**. Imagine a camera rotating around this object along a continuous path. The total rotation from the start to the end is less than 180 degrees.
|
| 85 |
+
|
| 86 |
+
Below are four images captured during this rotation, labeled 1, 2, 3 and 4. They're currently shuffled.
|
| 87 |
+
|
| 88 |
+
1. <image_start>[image_1]<image_end>
|
| 89 |
+
2. <image_start>[image_2]<image_end>
|
| 90 |
+
3. <image_start>[image_3]<image_end>
|
| 91 |
+
4. <image_start>[image_4]<image_end>
|
| 92 |
+
|
| 93 |
+
Please analyze the change in perspective and determine the correct chronological order of these four images following the initial view. Select the corret sequence.
|
| 94 |
+
|
| 95 |
+
A. [sequence_A]
|
| 96 |
+
B. [sequence_B]
|
| 97 |
+
C. [sequence_C]
|
| 98 |
+
D. [sequence_D]
|
| 99 |
+
""",
|
| 100 |
+
"""Given the initial view of the static object: <image_start>[image_ref]<image_end>.
|
| 101 |
|
| 102 |
Imagine a camera rotating around this object to capture a video sequence. The rotation covers an anlge of less than 180 degrees. We have extracted four frames from the sequence, labeled 1 to 4, but their order is jumbled.
|
| 103 |
|
|
|
|
| 112 |
B. [sequence_B]
|
| 113 |
C. [sequence_C]
|
| 114 |
D. [sequence_D]
|
| 115 |
+
"""
|
| 116 |
+
]
|
| 117 |
+
|
| 118 |
+
def generate_options(correct_indices_permutation):
|
| 119 |
+
"""
|
| 120 |
+
生成 A, B, C, D 选项。
|
| 121 |
+
:param correct_indices_permutation: 正确的顺序列表,例如 [2, 1, 4, 3]
|
| 122 |
+
:return: (options_dict, correct_choice_char)
|
| 123 |
+
"""
|
| 124 |
+
all_perms = list(itertools.permutations([1, 2, 3, 4]))
|
| 125 |
+
correct_list = list(correct_indices_permutation)
|
| 126 |
+
|
| 127 |
+
distractors = [list(p) for p in all_perms if list(p) != correct_list]
|
| 128 |
+
selected_distractors = random.sample(distractors, 3)
|
| 129 |
+
|
| 130 |
+
choices_content = [correct_list] + selected_distractors
|
| 131 |
+
random.shuffle(choices_content)
|
| 132 |
+
|
| 133 |
+
options = {}
|
| 134 |
+
correct_char = None
|
| 135 |
+
chars = ['A', 'B', 'C', 'D']
|
| 136 |
+
|
| 137 |
+
for char, content in zip(chars, choices_content):
|
| 138 |
+
seq_str = "-".join(map(str, content))
|
| 139 |
+
options[f"sequence_{char}"] = seq_str
|
| 140 |
+
|
| 141 |
+
if content == correct_list:
|
| 142 |
+
correct_char = char
|
| 143 |
+
|
| 144 |
+
return options, correct_char
|
| 145 |
+
|
| 146 |
+
def sample_frames_by_yaw(loader, seq_name, frame_ids, min_yaw_interval):
|
| 147 |
+
"""
|
| 148 |
+
[核心修正] 强制单调性采样。
|
| 149 |
+
确保采样的帧在角度上是严格单调递增或递减的。
|
| 150 |
+
"""
|
| 151 |
+
n = len(frame_ids)
|
| 152 |
+
if n < 5: return None, None
|
| 153 |
+
max_attempts = 20
|
| 154 |
+
for _ in range(max_attempts):
|
| 155 |
+
# 随机选起点
|
| 156 |
+
start_idx = random.randint(0, n - 5)
|
| 157 |
+
ref_fid = frame_ids[start_idx]
|
| 158 |
+
|
| 159 |
+
selected_fids = [ref_fid]
|
| 160 |
+
selected_yaws = [0.0]
|
| 161 |
+
|
| 162 |
+
ref_info = loader.get_frame_info(seq_name, ref_fid)
|
| 163 |
+
R_ref = ref_info['R']
|
| 164 |
+
T_ref = ref_info['T']
|
| 165 |
+
|
| 166 |
+
current_idx = start_idx
|
| 167 |
+
success = True
|
| 168 |
+
|
| 169 |
+
# 记录旋转方向: 1 (正向), -1 (负向), 0 (未定)
|
| 170 |
+
rotation_direction = 0
|
| 171 |
+
|
| 172 |
+
# 寻找后续 4 帧
|
| 173 |
+
for step in range(4):
|
| 174 |
+
found_next = False
|
| 175 |
+
|
| 176 |
+
for next_idx in range(current_idx + 1, n):
|
| 177 |
+
next_fid = frame_ids[next_idx]
|
| 178 |
+
next_info = loader.get_frame_info(seq_name, next_fid)
|
| 179 |
+
|
| 180 |
+
R_next = next_info['R']
|
| 181 |
+
T_next = next_info['T']
|
| 182 |
+
|
| 183 |
+
rel_yaw = get_relative_yaw(R_ref, R_next)
|
| 184 |
+
|
| 185 |
+
# 约束 1: 总跨度 < 180
|
| 186 |
+
if abs(rel_yaw) >= 180:
|
| 187 |
+
# 超过 180 度,停止搜索当前路径
|
| 188 |
+
found_next = False
|
| 189 |
+
break
|
| 190 |
+
|
| 191 |
+
prev_yaw = selected_yaws[-1]
|
| 192 |
+
|
| 193 |
+
# [核心逻辑修改] 强制单调性
|
| 194 |
+
|
| 195 |
+
# 情况 A: 寻找第一个候选帧 (确定方向)
|
| 196 |
+
if step == 0:
|
| 197 |
+
if abs(rel_yaw) >= min_yaw_interval:
|
| 198 |
+
# 确定方向
|
| 199 |
+
rotation_direction = 1 if rel_yaw > 0 else -1
|
| 200 |
+
|
| 201 |
+
selected_fids.append(next_fid)
|
| 202 |
+
selected_yaws.append(rel_yaw)
|
| 203 |
+
current_idx = next_idx
|
| 204 |
+
found_next = True
|
| 205 |
+
break
|
| 206 |
+
|
| 207 |
+
# 情况 B: 寻找后续帧 (必须遵循方向)
|
| 208 |
+
else:
|
| 209 |
+
# 计算增量
|
| 210 |
+
diff = rel_yaw - prev_yaw
|
| 211 |
+
|
| 212 |
+
# 如果方向是正,增量必须是正且足够大
|
| 213 |
+
if rotation_direction == 1:
|
| 214 |
+
if diff >= min_yaw_interval:
|
| 215 |
+
selected_fids.append(next_fid)
|
| 216 |
+
selected_yaws.append(rel_yaw)
|
| 217 |
+
current_idx = next_idx
|
| 218 |
+
found_next = True
|
| 219 |
+
break
|
| 220 |
+
|
| 221 |
+
# 如果方向是负,增量必须是负且足够小 (绝对值足够大)
|
| 222 |
+
elif rotation_direction == -1:
|
| 223 |
+
if diff <= -min_yaw_interval:
|
| 224 |
+
selected_fids.append(next_fid)
|
| 225 |
+
selected_yaws.append(rel_yaw)
|
| 226 |
+
current_idx = next_idx
|
| 227 |
+
found_next = True
|
| 228 |
+
break
|
| 229 |
+
|
| 230 |
+
if not found_next:
|
| 231 |
+
success = False
|
| 232 |
+
break
|
| 233 |
+
|
| 234 |
+
if success and len(selected_fids) == 5:
|
| 235 |
+
return selected_fids, selected_yaws
|
| 236 |
+
return None, None
|
| 237 |
+
|
| 238 |
+
def process_category(category, all_data_list):
|
| 239 |
+
print(f"Processing category: {category}...")
|
| 240 |
+
loader = CO3DDataLoader(ROOT_PATH, category)
|
| 241 |
+
sequences = loader.get_sequences()
|
| 242 |
+
|
| 243 |
+
for seq_name in sequences:
|
| 244 |
+
frame_ids = sorted(loader.get_frames(seq_name))
|
| 245 |
+
|
| 246 |
+
attempts = 0
|
| 247 |
+
generated_count = 0
|
| 248 |
+
|
| 249 |
+
while generated_count < SAMPLES_PER_SEQ and attempts < SAMPLES_PER_SEQ * 5:
|
| 250 |
+
attempts += 1
|
| 251 |
+
|
| 252 |
+
# 1. 基于 Yaw 采样
|
| 253 |
+
selected_fids, selected_yaws = sample_frames_by_yaw(loader, seq_name, frame_ids, MIN_YAW_INTERVAL)
|
| 254 |
+
|
| 255 |
+
if selected_fids is None:
|
| 256 |
+
# 如果这个序列很难找到满足条件的帧,就跳过,避免死循环
|
| 257 |
+
break
|
| 258 |
+
|
| 259 |
+
ref_fid = selected_fids[0]
|
| 260 |
+
cand_fids = selected_fids[1:] # 4个候选帧 ID
|
| 261 |
+
|
| 262 |
+
# selected_yaws[0] 是 0 (Ref), selected_yaws[1:] 是 candidates 相对 Ref 的 Yaw
|
| 263 |
+
cand_yaws = selected_yaws[1:]
|
| 264 |
+
|
| 265 |
+
# --- 数据构建 ---
|
| 266 |
+
|
| 267 |
+
# 准备图片路径
|
| 268 |
+
ref_info = loader.get_frame_info(seq_name, ref_fid)
|
| 269 |
+
cand_infos = [loader.get_frame_info(seq_name, fid) for fid in cand_fids]
|
| 270 |
+
|
| 271 |
+
ref_path = format_image_path(ref_info['path'], ROOT_PATH, IMAGE_PREFIX)
|
| 272 |
+
cand_paths_sorted = [format_image_path(info['path'], ROOT_PATH, IMAGE_PREFIX) for info in cand_infos]
|
| 273 |
+
|
| 274 |
+
# 2. 打乱候选图片顺序
|
| 275 |
+
# 创建 (Path, Rank, Yaw) 的列表
|
| 276 |
+
# Rank 1~4 代表真实时间顺序
|
| 277 |
+
cands_data = []
|
| 278 |
+
for rank, (path, yaw) in enumerate(zip(cand_paths_sorted, cand_yaws), 1):
|
| 279 |
+
cands_data.append({
|
| 280 |
+
'path': path,
|
| 281 |
+
'rank': rank,
|
| 282 |
+
'yaw': yaw
|
| 283 |
+
})
|
| 284 |
+
|
| 285 |
+
random.shuffle(cands_data)
|
| 286 |
+
|
| 287 |
+
# 计算正确答案序列
|
| 288 |
+
# cands_data 现在的索引 0,1,2,3 对应题目中的 Label 1,2,3,4
|
| 289 |
+
correct_sequence = []
|
| 290 |
+
for r in range(1, 5):
|
| 291 |
+
for label_idx, item in enumerate(cands_data):
|
| 292 |
+
if item['rank'] == r:
|
| 293 |
+
correct_sequence.append(label_idx + 1)
|
| 294 |
+
break
|
| 295 |
+
|
| 296 |
+
# 3. 生成选项
|
| 297 |
+
options_map, correct_choice = generate_options(correct_sequence)
|
| 298 |
+
|
| 299 |
+
# 4. 填充模板
|
| 300 |
+
template = random.choice(TEMPLATES)
|
| 301 |
+
|
| 302 |
+
# 仅替换选项占位符,图片占位符保持原样 [image_ref], [image_1]...
|
| 303 |
+
text = template
|
| 304 |
+
for key, val in options_map.items():
|
| 305 |
+
text = text.replace(f"[{key}]", val)
|
| 306 |
+
|
| 307 |
+
# 5. 构建 images 字典
|
| 308 |
+
images_dict = {
|
| 309 |
+
"ref": ref_path,
|
| 310 |
+
"candidate_1": cands_data[0]['path'],
|
| 311 |
+
"candidate_2": cands_data[1]['path'],
|
| 312 |
+
"candidate_3": cands_data[2]['path'],
|
| 313 |
+
"candidate_4": cands_data[3]['path']
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
# 6. 构建最终数据项
|
| 317 |
+
data_item = {
|
| 318 |
+
"id": f"task3_{seq_name}_{ref_fid}_ordering",
|
| 319 |
+
"task": "camera_view_ordering",
|
| 320 |
+
"sequence": seq_name,
|
| 321 |
+
"category": category,
|
| 322 |
+
"question": text,
|
| 323 |
+
"images": images_dict,
|
| 324 |
+
"metadata": {
|
| 325 |
+
"ref_frame": ref_fid,
|
| 326 |
+
"candidate_frames_sorted": cand_fids,
|
| 327 |
+
"shuffled_indices": [item['rank'] for item in cands_data], # 1,2,3,4 的排列
|
| 328 |
+
"yaws_relative_to_ref": {
|
| 329 |
+
"ref": 0.0,
|
| 330 |
+
# 记录排序后的真实 Yaw,方便分析
|
| 331 |
+
"sorted_candidates": cand_yaws,
|
| 332 |
+
# 记录打乱后对应 Label 1-4 的 Yaw
|
| 333 |
+
"shuffled_candidates": [item['yaw'] for item in cands_data]
|
| 334 |
+
},
|
| 335 |
+
"min_yaw_interval": MIN_YAW_INTERVAL,
|
| 336 |
+
"correct_sequence_str": options_map[f"sequence_{correct_choice}"]
|
| 337 |
+
},
|
| 338 |
+
"gt_answer": correct_choice
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
all_data_list.append(data_item)
|
| 342 |
+
generated_count += 1
|
| 343 |
+
|
| 344 |
+
def main():
|
| 345 |
+
all_data = []
|
| 346 |
+
|
| 347 |
+
if not os.path.exists(ROOT_PATH):
|
| 348 |
+
print(f"Error: ROOT_PATH {ROOT_PATH} does not exist.")
|
| 349 |
+
return
|
| 350 |
|
| 351 |
+
for cat in CATEGORIES:
|
| 352 |
+
process_category(cat, all_data)
|
| 353 |
+
|
| 354 |
+
print(f"Total samples generated: {len(all_data)}")
|
| 355 |
+
|
| 356 |
+
if len(all_data) > 0:
|
| 357 |
+
save_jsonl_splits(all_data, OUTPUT_DIR)
|
| 358 |
+
print(f"Data saved to {OUTPUT_DIR}")
|
| 359 |
+
else:
|
| 360 |
+
print("No data generated. Check paths and categories.")
|
| 361 |
|
| 362 |
+
if __name__ == "__main__":
|
| 363 |
+
main()
|
src/action_state/gen_task4.py
CHANGED
|
@@ -13,9 +13,9 @@
|
|
| 13 |
|
| 14 |
|
| 15 |
1.
|
| 16 |
-
The object in the initial view <image_start>[image_1]<image_end> remains **static**, with a specific point marked as **A**.
|
| 17 |
|
| 18 |
-
Imagine a camera rotating around this object to a new position. The final view captured from this new angle is shown below, with four candidate points marked as **1, 2, 3 and 4**.
|
| 19 |
|
| 20 |
Based on the visual features and geometry, which of the numbered points in the final view corresponds to the **same physical location** as point **A** in the inital view?
|
| 21 |
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
1.
|
| 16 |
+
The {object} in the initial view <image_start>[image_1]<image_end> remains **static**, with a specific point marked as **A**.
|
| 17 |
|
| 18 |
+
Imagine a camera rotating around this {object} to a new position. The final view captured from this new angle is shown below, with four candidate points marked as **1, 2, 3 and 4**.
|
| 19 |
|
| 20 |
Based on the visual features and geometry, which of the numbered points in the final view corresponds to the **same physical location** as point **A** in the inital view?
|
| 21 |
|
src/action_state/utils.py
CHANGED
|
@@ -2,21 +2,79 @@ import json
|
|
| 2 |
import os
|
| 3 |
import math
|
| 4 |
import numpy as np
|
| 5 |
-
from scipy.spatial.transform import Rotation as SciRotation
|
| 6 |
-
|
| 7 |
# --- 1. 几何计算工具 ---
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
"""
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
"""
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def format_angle_direction(angle):
|
| 22 |
"""将角度数值转换为 (绝对值, 方向字符串)"""
|
|
@@ -74,27 +132,48 @@ class CO3DDataLoader:
|
|
| 74 |
def __init__(self, root_path, category):
|
| 75 |
self.root_path = root_path
|
| 76 |
self.category = category
|
| 77 |
-
self.seq_data = {} # {sequence_name: {frame_idx: {'R': ..., 'path': ...}}}
|
| 78 |
|
| 79 |
self._load_data()
|
| 80 |
|
| 81 |
def _load_data(self):
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
| 83 |
if not os.path.exists(json_path):
|
| 84 |
-
print(f"Warning: {json_path} not found.")
|
| 85 |
return
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
with open(json_path, 'r') as f:
|
| 88 |
annotations = json.load(f)
|
| 89 |
|
| 90 |
for item in annotations:
|
| 91 |
seq = item['sequence_name']
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
fid = item['frame_number']
|
| 93 |
if seq not in self.seq_data:
|
| 94 |
self.seq_data[seq] = {}
|
| 95 |
|
|
|
|
| 96 |
self.seq_data[seq][fid] = {
|
| 97 |
'R': np.array(item['viewpoint']['R']),
|
|
|
|
| 98 |
'path': item['image']['path']
|
| 99 |
}
|
| 100 |
|
|
@@ -111,7 +190,7 @@ class CO3DDataLoader:
|
|
| 111 |
return list(self.seq_data[sequence_name].keys())
|
| 112 |
|
| 113 |
def get_frame_info(self, sequence_name, frame_idx):
|
| 114 |
-
"""获取特定帧的 R 和 Path"""
|
| 115 |
return self.seq_data[sequence_name].get(frame_idx)
|
| 116 |
|
| 117 |
# --- 4. 结果保存工具 ---
|
|
|
|
| 2 |
import os
|
| 3 |
import math
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
| 5 |
# --- 1. 几何计算工具 ---
|
| 6 |
+
def get_camera_center(R, T):
|
| 7 |
+
"""计算相机中心: C = -R^T * T"""
|
| 8 |
+
return -R.T @ T
|
| 9 |
+
def get_view_direction(R):
|
| 10 |
"""
|
| 11 |
+
获取相机光轴方向。
|
| 12 |
+
PyTorch3D 约定: x_cam = x_world @ R + T
|
| 13 |
+
这意味着 R 的第 3 行 (index 2) 是相机 Z 轴在世界系下的方向。
|
| 14 |
"""
|
| 15 |
+
return R[2, :]
|
| 16 |
+
def get_relative_yaw(R_ref, R_curr):
|
| 17 |
+
"""
|
| 18 |
+
计算相对 Yaw 角度。
|
| 19 |
+
|
| 20 |
+
【重大修正 - 基于 Issue #64】
|
| 21 |
+
CO3D 的世界坐标系 Up 向量并不是 (0, 1, 0),而是倾向于 (-0.04, -0.83, -0.55)。
|
| 22 |
+
直接投影到 XZ 平面会导致巨大的误差。
|
| 23 |
+
|
| 24 |
+
本函数执行以下步骤:
|
| 25 |
+
1. 提取两帧的光轴向量。
|
| 26 |
+
2. 定义 CO3D 的平均 Up 向量。
|
| 27 |
+
3. 将光轴向量投影到垂直于 Up 向量的平面上(即真实的水平面)。
|
| 28 |
+
4. 计算投影向量之间的夹角。
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
# 1. 获取光轴
|
| 32 |
+
v_ref = get_view_direction(R_ref)
|
| 33 |
+
v_curr = get_view_direction(R_curr)
|
| 34 |
+
|
| 35 |
+
# 2. 定义 Up 向量 (来自 CO3D Issue #64 官方提供的经验值)
|
| 36 |
+
# 这是一个在 CO3D v2 中普遍适用的“地面法向量”
|
| 37 |
+
# 注意:官方代码里是 from_vec=(-0.0396, -0.8306, -0.5554)
|
| 38 |
+
# 我们将其归一化作为 Up 轴
|
| 39 |
+
up_vec = np.array([-0.0396, -0.8306, -0.5554])
|
| 40 |
+
up_vec = up_vec / np.linalg.norm(up_vec)
|
| 41 |
+
|
| 42 |
+
# 3. 投影到水平面 (Gram-Schmidt 正交化)
|
| 43 |
+
# v_flat = v - (v . up) * up
|
| 44 |
+
# 去除光轴中垂直于地面的分量
|
| 45 |
+
v_ref_flat = v_ref - np.dot(v_ref, up_vec) * up_vec
|
| 46 |
+
v_curr_flat = v_curr - np.dot(v_curr, up_vec) * up_vec
|
| 47 |
+
|
| 48 |
+
# 4. 归一化投影向量
|
| 49 |
+
norm_ref = np.linalg.norm(v_ref_flat)
|
| 50 |
+
norm_curr = np.linalg.norm(v_curr_flat)
|
| 51 |
+
|
| 52 |
+
if norm_ref < 1e-4 or norm_curr < 1e-4:
|
| 53 |
+
return 0.0
|
| 54 |
+
|
| 55 |
+
v_ref_flat = v_ref_flat / norm_ref
|
| 56 |
+
v_curr_flat = v_curr_flat / norm_curr
|
| 57 |
+
|
| 58 |
+
# 5. 计算夹角
|
| 59 |
+
# 使用点积计算角度大小: cos(theta) = a . b
|
| 60 |
+
dot_val = np.dot(v_ref_flat, v_curr_flat)
|
| 61 |
+
# 限制范围防止数值误差
|
| 62 |
+
dot_val = np.clip(dot_val, -1.0, 1.0)
|
| 63 |
+
angle_deg = np.degrees(np.arccos(dot_val))
|
| 64 |
+
|
| 65 |
+
# 6. 确定符号 (方向)
|
| 66 |
+
# 我们需要定义旋转轴 = v_ref_flat x v_curr_flat
|
| 67 |
+
# 如果旋转轴的方向与 Up 向量同向,则为正;反之为负。
|
| 68 |
+
cross_prod = np.cross(v_ref_flat, v_curr_flat)
|
| 69 |
+
direction = np.dot(cross_prod, up_vec)
|
| 70 |
+
|
| 71 |
+
if direction < 0:
|
| 72 |
+
angle_deg = -angle_deg
|
| 73 |
+
|
| 74 |
+
return angle_deg
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
|
| 79 |
def format_angle_direction(angle):
|
| 80 |
"""将角度数值转换为 (绝对值, 方向字符串)"""
|
|
|
|
| 132 |
def __init__(self, root_path, category):
|
| 133 |
self.root_path = root_path
|
| 134 |
self.category = category
|
| 135 |
+
self.seq_data = {} # {sequence_name: {frame_idx: {'R': ..., 'T': ..., 'path': ...}}}
|
| 136 |
|
| 137 |
self._load_data()
|
| 138 |
|
| 139 |
def _load_data(self):
|
| 140 |
+
# 1. 确定该类别的基础路径
|
| 141 |
+
cat_dir = os.path.join(self.root_path, 'data', 'original', self.category)
|
| 142 |
+
json_path = os.path.join(cat_dir, 'frame_annotations.json')
|
| 143 |
+
|
| 144 |
if not os.path.exists(json_path):
|
| 145 |
+
print(f"Warning: Annotation file {json_path} not found.")
|
| 146 |
return
|
| 147 |
+
|
| 148 |
+
# 2. 获取磁盘上实际存在的序列列表
|
| 149 |
+
if not os.path.exists(cat_dir):
|
| 150 |
+
print(f"Warning: Category directory {cat_dir} not found.")
|
| 151 |
+
return
|
| 152 |
+
|
| 153 |
+
valid_sequences = set()
|
| 154 |
+
for d in os.listdir(cat_dir):
|
| 155 |
+
d_path = os.path.join(cat_dir, d)
|
| 156 |
+
if os.path.isdir(d_path):
|
| 157 |
+
valid_sequences.add(d)
|
| 158 |
+
|
| 159 |
+
# 3. 加载 JSON 并过滤
|
| 160 |
with open(json_path, 'r') as f:
|
| 161 |
annotations = json.load(f)
|
| 162 |
|
| 163 |
for item in annotations:
|
| 164 |
seq = item['sequence_name']
|
| 165 |
+
|
| 166 |
+
if seq not in valid_sequences:
|
| 167 |
+
continue
|
| 168 |
+
|
| 169 |
fid = item['frame_number']
|
| 170 |
if seq not in self.seq_data:
|
| 171 |
self.seq_data[seq] = {}
|
| 172 |
|
| 173 |
+
# [修改] 同时加载 R 和 T
|
| 174 |
self.seq_data[seq][fid] = {
|
| 175 |
'R': np.array(item['viewpoint']['R']),
|
| 176 |
+
'T': np.array(item['viewpoint']['T']),
|
| 177 |
'path': item['image']['path']
|
| 178 |
}
|
| 179 |
|
|
|
|
| 190 |
return list(self.seq_data[sequence_name].keys())
|
| 191 |
|
| 192 |
def get_frame_info(self, sequence_name, frame_idx):
|
| 193 |
+
"""获取特定帧的 R, T 和 Path"""
|
| 194 |
return self.seq_data[sequence_name].get(frame_idx)
|
| 195 |
|
| 196 |
# --- 4. 结果保存工具 ---
|