File size: 5,694 Bytes
babffc8 ffa8764 babffc8 ffa8764 babffc8 ffa8764 babffc8 ffa8764 babffc8 ffa8764 babffc8 ffa8764 babffc8 ffa8764 babffc8 ffa8764 babffc8 ffa8764 babffc8 ffa8764 babffc8 ffa8764 babffc8 ffa8764 babffc8 ffa8764 babffc8 ffa8764 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | from __future__ import annotations
import argparse
import json
from pathlib import Path
import pandas as pd
VIDEO_COLUMNS = [
"sentence_name", "SENTENCE_NAME",
"video_path", "path", "file", "filename", "video",
"video_name", "VIDEO_NAME",
"sentence_id", "SENTENCE_ID",
"clip_id", "CLIP_ID",
]
TARGET_COLUMNS = [
"translation", "text", "sentence", "transcript",
"raw_text", "normalized_text",
"TRANSLATION", "TEXT", "SENTENCE", "TRANSCRIPT",
]
ID_COLUMNS = [
"id", "sentence_id", "clip_id", "video_id",
"ID", "SENTENCE_ID", "CLIP_ID", "VIDEO_ID", "SENTENCE_NAME",
]
def pick_column(df: pd.DataFrame, candidates: list[str], label: str) -> str:
lower = {c.lower(): c for c in df.columns}
for c in candidates:
if c.lower() in lower:
return lower[c.lower()]
raise ValueError(
f"Could not find {label} column.\n"
f"Tried: {candidates}\n"
f"Available columns: {list(df.columns)}"
)
def build_video_index(video_root: Path) -> dict[str, Path]:
index: dict[str, Path] = {}
for path in video_root.rglob("*.mp4"):
keys = {
path.name,
path.stem,
path.stem.replace("-rgb_front", ""),
path.name.replace("-rgb_front.mp4", ""),
}
for key in keys:
index[key] = path
return index
def find_video(value: object, video_root: Path, index: dict[str, Path]) -> Path:
raw = str(value).strip()
name = Path(raw).name
stem = Path(raw).stem
candidates = [
raw,
name,
stem,
f"{raw}.mp4",
f"{name}.mp4",
f"{raw}-rgb_front.mp4",
f"{name}-rgb_front.mp4",
raw.replace("-rgb_front", ""),
stem.replace("-rgb_front", ""),
]
for candidate in candidates:
if candidate in index:
return index[candidate]
direct_path = video_root / name
if direct_path.exists():
return direct_path
raise FileNotFoundError(f"Could not find video for '{raw}' under {video_root}")
def write_split(csv_path: Path, video_root: Path, out_path: Path, split: str) -> None:
if not csv_path.exists():
raise FileNotFoundError(f"Missing CSV: {csv_path}")
if not video_root.exists():
raise FileNotFoundError(f"Missing video directory: {video_root}")
df = pd.read_csv(csv_path, sep="\t")
print(f"{split} columns: {list(df.columns)}")
video_col = pick_column(df, VIDEO_COLUMNS, "video")
target_col = pick_column(df, TARGET_COLUMNS, "translation/text")
id_col = None
try:
id_col = pick_column(df, ID_COLUMNS, "id")
except ValueError:
pass
print(f"{split}: using video column = {video_col}")
print(f"{split}: using target column = {target_col}")
print(f"{split}: using id column = {id_col}")
video_index = build_video_index(video_root)
print(f"{split}: indexed {len(video_index)} video lookup keys")
out_path.parent.mkdir(parents=True, exist_ok=True)
written = 0
skipped_missing_video = 0
skipped_empty_target = 0
missing_examples = []
missing_report_path = out_path.with_suffix(".missing_videos.txt")
with out_path.open("w", encoding="utf-8") as f:
for i, row in df.iterrows():
target = str(row[target_col]).strip()
if not target or target.lower() == "nan":
skipped_empty_target += 1
continue
try:
video_path = find_video(row[video_col], video_root, video_index)
except FileNotFoundError:
skipped_missing_video += 1
if len(missing_examples) < 50:
missing_examples.append(str(row[video_col]))
continue
example = {
"id": str(row[id_col]).strip() if id_col else str(i),
"video_path": str(video_path),
"translation": target,
"split": split,
}
f.write(json.dumps(example, ensure_ascii=False) + "\n")
written += 1
with missing_report_path.open("w", encoding="utf-8") as f:
for item in missing_examples:
f.write(item + "\n")
total = len(df)
print(f"{split}: total metadata rows = {total}")
print(f"{split}: wrote usable rows = {written}")
print(f"{split}: skipped missing videos = {skipped_missing_video}")
print(f"{split}: skipped empty targets = {skipped_empty_target}")
print(f"{split}: missing-video examples saved to {missing_report_path}")
if total > 0:
kept_pct = 100.0 * written / total
skipped_pct = 100.0 * skipped_missing_video / total
print(f"{split}: kept {kept_pct:.2f}% of rows")
print(f"{split}: missing-video skip rate {skipped_pct:.2f}%")
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--train_metadata", required=True)
parser.add_argument("--val_metadata", required=True)
parser.add_argument("--train_video_root", required=True)
parser.add_argument("--val_video_root", required=True)
parser.add_argument("--out_train", default="data/how2sign_train.jsonl")
parser.add_argument("--out_val", default="data/how2sign_val.jsonl")
args = parser.parse_args()
write_split(
csv_path=Path(args.train_metadata),
video_root=Path(args.train_video_root),
out_path=Path(args.out_train),
split="train",
)
write_split(
csv_path=Path(args.val_metadata),
video_root=Path(args.val_video_root),
out_path=Path(args.out_val),
split="val",
)
if __name__ == "__main__":
main() |