Upload transform_longvideobench.py
Browse files- transform_longvideobench.py +87 -0
transform_longvideobench.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from typing import List, Dict
|
| 4 |
+
from collections import Counter, defaultdict
|
| 5 |
+
|
| 6 |
+
def LongVideoBench2TStarFormat(dataset_path: str, video_root: str, output_path: str) -> List[Dict]:
|
| 7 |
+
"""Load and transform the dataset into the required format for T*.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
dataset_path (str): Path to the input dataset JSON file.
|
| 11 |
+
video_root (str): Root directory where video files are stored.
|
| 12 |
+
output_path (str): Path to save the transformed JSON dataset.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
List[Dict]: Transformed dataset formatted for T*.
|
| 16 |
+
"""
|
| 17 |
+
with open(dataset_path, 'r', encoding='utf-8') as file:
|
| 18 |
+
lvb_dataset = json.load(file)
|
| 19 |
+
|
| 20 |
+
TStar_format_data = []
|
| 21 |
+
num2letter = ['A', 'B', 'C', 'D', 'E']
|
| 22 |
+
|
| 23 |
+
question_category_counts = Counter()
|
| 24 |
+
video_question_counts = defaultdict(int)
|
| 25 |
+
|
| 26 |
+
for idx, entry in enumerate(lvb_dataset):
|
| 27 |
+
try:
|
| 28 |
+
video_id = entry.get("video_id")
|
| 29 |
+
video_path = entry.get("video_path")
|
| 30 |
+
question = entry.get("question")
|
| 31 |
+
answer = entry.get("correct_choice", "")
|
| 32 |
+
answer = num2letter[answer]
|
| 33 |
+
question_category = entry.get("question_category", "Unknown")
|
| 34 |
+
duration_group = entry.get("duration_group", "Unknown")
|
| 35 |
+
position = entry.get("position", [])
|
| 36 |
+
options_list = entry.get("candidates", [])
|
| 37 |
+
|
| 38 |
+
# Filter out subtitle questions based on question category
|
| 39 |
+
if 'T' in question_category:
|
| 40 |
+
continue
|
| 41 |
+
# Only keep entries with duration group 3600
|
| 42 |
+
if duration_group != 3600:
|
| 43 |
+
continue
|
| 44 |
+
|
| 45 |
+
if not video_id or not question or not options_list:
|
| 46 |
+
raise ValueError(f"Missing required fields in entry {idx+1}. Skipping entry.")
|
| 47 |
+
|
| 48 |
+
options = "\n".join(f"{num2letter[i]}) {opt}" for i, opt in enumerate(options_list))
|
| 49 |
+
|
| 50 |
+
transformed_entry = {
|
| 51 |
+
"video_id": video_id,
|
| 52 |
+
"video_path": os.path.join(video_root, video_path),
|
| 53 |
+
"question": question,
|
| 54 |
+
"options": options,
|
| 55 |
+
"answer": answer,
|
| 56 |
+
"duration_group": duration_group,
|
| 57 |
+
"gt_frame_index": position,
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
TStar_format_data.append(transformed_entry)
|
| 61 |
+
|
| 62 |
+
question_category_counts[question_category] += 1
|
| 63 |
+
video_question_counts[video_id] += 1
|
| 64 |
+
|
| 65 |
+
except ValueError as e:
|
| 66 |
+
print(f"Skipping entry {idx+1}, reason: {str(e)}")
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"Error processing entry {idx+1}: {str(e)}")
|
| 69 |
+
|
| 70 |
+
print("Remaining question category counts:", dict(question_category_counts))
|
| 71 |
+
print("Number of questions per video:", len(video_question_counts))
|
| 72 |
+
|
| 73 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 74 |
+
json.dump(TStar_format_data, f, indent=4)
|
| 75 |
+
print(f"Transformed dataset saved to {output_path}")
|
| 76 |
+
|
| 77 |
+
return TStar_format_data
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
import argparse
|
| 81 |
+
parser = argparse.ArgumentParser(description="Transform LongVideoBench dataset to T* format.")
|
| 82 |
+
parser.add_argument("--dataset_path", type=str, required=True, help="Path to the dataset JSON file.")
|
| 83 |
+
parser.add_argument("--video_root", type=str, required=True, help="Root directory for video files.")
|
| 84 |
+
parser.add_argument("--output_path", type=str, required=True, help="Path to save the transformed JSON file.")
|
| 85 |
+
args = parser.parse_args()
|
| 86 |
+
|
| 87 |
+
LongVideoBench2TStarFormat(args.dataset_path, args.video_root, args.output_path)
|