dddb0513
Add first_phrase and second_phrase data
fd5df33
Raw History Blame Contribute Delete
5.92 kB
import os
import json
import glob
from pathlib import Path
from collections import defaultdict
# ================= 配置区域 =================
DATA_ROOT = "/opt/liblibai-models/user-workspace2/users/dxy/robotic/data"
REF_IMG_ROOT = "/opt/liblibai-models/user-workspace2/users/dxy/robotic/robot_ref_img"
# 修改点 1: 不再需要单一的 OUTPUT_JSONL,而是指定一个输出目录
OUTPUT_DIR = "./json_outputs"
VIDEO_EXTS = {".mp4", ".mov", ".avi", ".mkv"}
IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".webp"}
# ===========================================
def generate_strict_paired_metadata():
# 确保输出目录存在
os.makedirs(OUTPUT_DIR, exist_ok=True)
# -------------------------------------------------
# 1. 建立参考图索引 (代码保持不变)
# -------------------------------------------------
ref_images_map = {}
if os.path.exists(REF_IMG_ROOT):
print(f"Scanning reference images in {REF_IMG_ROOT}...")
for img_file in os.listdir(REF_IMG_ROOT):
name, ext = os.path.splitext(img_file)
if ext.lower() in IMAGE_EXTS:
ref_images_map[name] = os.path.abspath(os.path.join(REF_IMG_ROOT, img_file))
else:
print(f"Warning: Reference image directory '{REF_IMG_ROOT}' not found.")
# -------------------------------------------------
# 2. 建立严格的上下文视频索引 (代码保持不变)
# -------------------------------------------------
context_map = defaultdict(list)
print(f"\nScanning videos in {DATA_ROOT}...")
for root, dirs, files in os.walk(DATA_ROOT):
videos = [f for f in files if os.path.splitext(f)[1].lower() in VIDEO_EXTS]
if not videos:
continue
rel_path = os.path.relpath(root, DATA_ROOT)
path_parts = Path(rel_path).parts
if len(path_parts) < 4:
continue
scene = path_parts[0]
location = path_parts[1]
camera = path_parts[2]
# 你的逻辑:取最后一部分文件夹名为角色名
character = path_parts[-1]
for vid in videos:
motion_name = os.path.splitext(vid)[0]
full_path = os.path.join(root, vid)
unique_key = (scene, location, camera, motion_name)
context_map[unique_key].append({
"char": character,
"path": full_path,
"motion": motion_name
})
print(f"Found {len(context_map)} unique action contexts.")
# -------------------------------------------------
# 3. 生成全排列配对 (修改点:使用字典按角色分类)
# -------------------------------------------------
# key: 角色名 (例如 'unitree-g1'), value: 该角色的数据列表
results_by_char = defaultdict(list)
for key, video_list in context_map.items():
scene, loc, cam, motion = key
if len(video_list) < 2:
continue
# 这里的 target_item 就是“目标视频”
for target_item in video_list:
target_char = target_item["char"]
target_path = target_item["path"]
if target_char not in ref_images_map:
continue
ref_img_path = ref_images_map[target_char]
# 寻找 Control 视频 (其他角色做的同一个动作)
for source_item in video_list:
source_char = source_item["char"]
# 排除自己 (只用别的外观做 Control)
if source_char == target_char:
continue
control_path = source_item["path"]
# Prompt 逻辑保持不变
prompt_path = os.path.splitext(target_path)[0] + ".txt"
prompt = ""
if os.path.exists(prompt_path):
try:
with open(prompt_path, 'r', encoding='utf-8') as f:
prompt = f.read().strip()
except:
pass
if not prompt:
safe_motion = motion.replace("_", " ")
prompt = f"A robot {target_char} is {safe_motion} in {scene} at {loc}"
entry = {
"video": target_path,
"control_video": control_path,
"prompt": prompt,
"appearance_id": target_char,
"ref_image_path": ref_img_path,
}
# 【修改核心】:将数据存入对应 target_char 的列表中
results_by_char[target_char].append(entry)
# -------------------------------------------------
# 4. 分文件保存 (修改点:循环写入多个文件)
# -------------------------------------------------
print(f"\nGeneration complete! Writing files to {OUTPUT_DIR}...")
total_files = 0
for char_name, entries in results_by_char.items():
if not entries:
continue
# 构造文件名,例如: data_unitree-g1.jsonl
# 使用 replace 处理一下文件名里可能不合法的字符
safe_char_name = char_name.replace("/", "_").replace("\\", "_")
filename = f"data_{safe_char_name}.jsonl"
file_path = os.path.join(OUTPUT_DIR, filename)
with open(file_path, 'w', encoding='utf-8') as f:
for item in entries:
f.write(json.dumps(item, ensure_ascii=False) + "\n")
print(f" -> Saved {filename} ({len(entries)} samples)")
total_files += 1
print(f"\nAll done! Created {total_files} JSONL files.")
if __name__ == "__main__":
generate_strict_paired_metadata()