| import argparse |
| import json |
| from pathlib import Path |
|
|
|
|
| DETECTION_INSTRUCTION = ( |
| "Determine whether the video contains an anomalous event. " |
| "Output 1 if an anomaly is present." |
| ) |
| TIMESTAMP_INSTRUCTION = ( |
| "\n" |
| " Locate the position of the anomalous segment in the video. " |
| "Report the start and end timestamps in mmss format rather than seconds, " |
| "and use the following output format:\n" |
| " [xxxx, xxxx]\n" |
| " " |
| ) |
|
|
| EXPECTED_TEST_TASKS = { |
| "Classification", |
| "Cause", |
| "Result", |
| "Timestamp", |
| "Description", |
| "Detection", |
| } |
|
|
|
|
| def load_json(path: Path): |
| with path.open("r", encoding="utf-8") as f: |
| return json.load(f) |
|
|
|
|
| def dump_json(path: Path, data) -> None: |
| with path.open("w", encoding="utf-8") as f: |
| json.dump(data, f, ensure_ascii=False, indent=2) |
| f.write("\n") |
|
|
|
|
| def build_instruction_map(gt_rows): |
| task_to_instruction = {} |
| for row in gt_rows: |
| task = row["task"] |
| instruction = TIMESTAMP_INSTRUCTION if task == "Timestamp" else row["instruction"] |
| existing = task_to_instruction.get(task) |
| if existing is None: |
| task_to_instruction[task] = instruction |
| elif existing != instruction: |
| raise ValueError(f"Task {task!r} has multiple instructions in gt") |
| return task_to_instruction |
|
|
|
|
| def fill_test_rows(test_rows, task_to_instruction): |
| filled = [] |
| for row in test_rows: |
| task = row["task"] |
| if task == "Detection": |
| instruction = DETECTION_INSTRUCTION |
| else: |
| if task not in task_to_instruction: |
| raise KeyError(f"Missing instruction mapping for task {task!r}") |
| instruction = task_to_instruction[task] |
|
|
| filled.append( |
| { |
| "instruction": instruction, |
| "visual_input": row["visual_input"], |
| "output": row["output"], |
| "task": row["task"], |
| "ID": row["ID"], |
| } |
| ) |
| return filled |
|
|
|
|
| def resolve_data_dir(repo_root: Path) -> Path: |
| raw_dir = repo_root / "raw" |
| return raw_dir if raw_dir.exists() else repo_root |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="Fill missing instructions in CUVA test annotations.") |
| parser.add_argument("--repo-root", type=Path, default=Path(__file__).resolve().parent.parent) |
| args = parser.parse_args() |
|
|
| repo_root = args.repo_root.resolve() |
| data_dir = resolve_data_dir(repo_root) |
|
|
| gt_path = data_dir / "cuva_gt_cleaned.json" |
| test_path = data_dir / "cuva_test.json" |
| output_path = data_dir / "cuva_test_filled.json" |
|
|
| gt_rows = load_json(gt_path) |
| test_rows = load_json(test_path) |
|
|
| task_to_instruction = build_instruction_map(gt_rows) |
| test_tasks = {row["task"] for row in test_rows} |
| unknown_tasks = test_tasks - EXPECTED_TEST_TASKS |
| if unknown_tasks: |
| raise ValueError(f"Unexpected tasks in test set: {sorted(unknown_tasks)}") |
|
|
| filled_rows = fill_test_rows(test_rows, task_to_instruction) |
| dump_json(output_path, filled_rows) |
|
|
| print(f"Wrote {len(filled_rows)} rows to {output_path}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|