| import os |
| import json |
| import glob |
| import random |
|
|
| |
| |
| |
| import os |
| import json |
| import glob |
| import random |
| from collections import defaultdict |
|
|
| |
| |
| |
| DATA_ROOT = "/data/dataset/vlm_direction/direction_benchmarks/E2E_real_object" |
| OUTPUT_DIR = "./" |
|
|
| DIRECTION_CLASSES = ["up", "down", "left", "right"] |
| DIRECTION_OPTIONS = ["Up", "Down", "Left", "Right"] |
| DIR_TO_LETTER = {"up": "A", "down": "B", "left": "C", "right": "D"} |
| DIR_TO_LABEL = {"up": "Up", "down": "Down", "left": "Left", "right": "Right"} |
|
|
| random.seed(42) |
|
|
| |
| |
| |
| all_samples = [] |
| all_objects = set() |
|
|
| for cls in DIRECTION_CLASSES: |
| cls_dir = os.path.join(DATA_ROOT, cls) |
| if not os.path.isdir(cls_dir): |
| print(f"[WARN] Directory not found: {cls_dir}") |
| continue |
|
|
| videos = sorted(glob.glob(os.path.join(cls_dir, "*.mp4"))) |
| print(f"{cls}: {len(videos)} videos") |
|
|
| |
| OBJ_NAME_MAP = { |
| "sophia": "person", |
| } |
|
|
| for video_path in videos: |
| filename = os.path.basename(video_path) |
| raw_name = os.path.splitext(filename)[0] |
| obj_name = OBJ_NAME_MAP.get(raw_name, raw_name) |
| video_rel = f"{cls}/{filename}" |
|
|
| all_samples.append({ |
| "video": video_rel, |
| "category": cls, |
| "object": obj_name, |
| }) |
| all_objects.add(obj_name) |
|
|
| all_objects = sorted(list(all_objects)) |
| print(f"\nTotal: {len(all_samples)} samples, {len(all_objects)} unique objects") |
| print(f"Objects: {all_objects}") |
|
|
|
|
| |
| |
| |
| def make_direction_only(samples): |
| """조건1: 기본 - object 언급 없이 방향만""" |
| question = "In which direction is the object moving in this video?" |
| close, open_ = [], [] |
|
|
| for s in samples: |
| close.append({ |
| "video": s["video"], "category": s["category"], |
| "question": question, |
| "options": DIRECTION_OPTIONS, |
| "answer": DIR_TO_LETTER[s["category"]], |
| }) |
| open_.append({ |
| "video": s["video"], "category": s["category"], |
| "question": question, |
| "answer": DIR_TO_LABEL[s["category"]], |
| }) |
| return close, open_ |
|
|
|
|
| def make_direction_obj_in_q(samples): |
| """조건2: 질문에 object 이름 포함""" |
| close, open_ = [], [] |
|
|
| for s in samples: |
| obj = s["object"] |
| question = f"In which direction is the {obj} moving in this video?" |
| close.append({ |
| "video": s["video"], "category": s["category"], |
| "question": question, |
| "options": DIRECTION_OPTIONS, |
| "answer": DIR_TO_LETTER[s["category"]], |
| }) |
| open_.append({ |
| "video": s["video"], "category": s["category"], |
| "question": question, |
| "answer": DIR_TO_LABEL[s["category"]], |
| }) |
| return close, open_ |
|
|
|
|
| def make_direction_obj_in_a(samples): |
| """조건3: 답변에 object 이름 포함""" |
| question = "In which direction is the object moving in this video?" |
| close, open_ = [], [] |
|
|
| for s in samples: |
| obj = s["object"] |
| direction = DIR_TO_LABEL[s["category"]] |
|
|
| |
| options = [f"The {obj} is moving {d.lower()}" for d in DIRECTION_OPTIONS] |
| answer_idx = DIRECTION_CLASSES.index(s["category"]) |
| answer_letter = chr(ord("A") + answer_idx) |
|
|
| close.append({ |
| "video": s["video"], "category": s["category"], |
| "question": question, |
| "options": options, |
| "answer": answer_letter, |
| }) |
| open_.append({ |
| "video": s["video"], "category": s["category"], |
| "question": question, |
| "answer": f"The {obj} is moving {direction.lower()}", |
| }) |
| return close, open_ |
|
|
|
|
| def make_direction_obj_in_qa(samples): |
| """조건4: 질문+답변 모두 object 이름 포함""" |
| close, open_ = [], [] |
|
|
| for s in samples: |
| obj = s["object"] |
| direction = DIR_TO_LABEL[s["category"]] |
| question = f"In which direction is the {obj} moving in this video?" |
|
|
| options = [f"The {obj} is moving {d.lower()}" for d in DIRECTION_OPTIONS] |
| answer_idx = DIRECTION_CLASSES.index(s["category"]) |
| answer_letter = chr(ord("A") + answer_idx) |
|
|
| close.append({ |
| "video": s["video"], "category": s["category"], |
| "question": question, |
| "options": options, |
| "answer": answer_letter, |
| }) |
| open_.append({ |
| "video": s["video"], "category": s["category"], |
| "question": question, |
| "answer": f"The {obj} is moving {direction.lower()}", |
| }) |
| return close, open_ |
|
|
|
|
| def make_object_recognition(samples, all_objects): |
| """조건5: object 종류 맞추기""" |
| question = "What is the object moving in this video?" |
| close, open_ = [], [] |
|
|
| for s in samples: |
| obj = s["object"] |
| obj_cap = obj.capitalize() |
|
|
| |
| distractors = [o for o in all_objects if o != obj] |
| chosen = random.sample(distractors, min(3, len(distractors))) |
| options_raw = [obj] + chosen |
| random.shuffle(options_raw) |
| options = [o.capitalize() for o in options_raw] |
| answer_letter = chr(ord("A") + options_raw.index(obj)) |
|
|
| close.append({ |
| "video": s["video"], "category": s["category"], |
| "question": question, |
| "options": options, |
| "answer": answer_letter, |
| }) |
| open_.append({ |
| "video": s["video"], "category": s["category"], |
| "question": question, |
| "answer": obj_cap, |
| }) |
| return close, open_ |
|
|
|
|
| |
| |
| |
| CONDITIONS = { |
| "direction_only": lambda s: make_direction_only(s), |
| "direction_obj_in_q": lambda s: make_direction_obj_in_q(s), |
| "direction_obj_in_a": lambda s: make_direction_obj_in_a(s), |
| "direction_obj_in_qa": lambda s: make_direction_obj_in_qa(s), |
| "object_recognition": lambda s: make_object_recognition(s, all_objects), |
| } |
|
|
| for cond_name, gen_fn in CONDITIONS.items(): |
| |
| shuffled = all_samples.copy() |
| random.shuffle(shuffled) |
|
|
| close_data, open_data = gen_fn(shuffled) |
|
|
| |
| for i, d in enumerate(close_data): |
| d["id"] = i |
| for i, d in enumerate(open_data): |
| d["id"] = i |
|
|
| |
| close_path = os.path.join(OUTPUT_DIR, f"{cond_name}_close_ended.json") |
| open_path = os.path.join(OUTPUT_DIR, f"{cond_name}_open_ended.json") |
|
|
| with open(close_path, "w") as f: |
| json.dump(close_data, f, indent=2, ensure_ascii=False) |
| with open(open_path, "w") as f: |
| json.dump(open_data, f, indent=2, ensure_ascii=False) |
|
|
| print(f"\n[{cond_name}]") |
| print(f" close: {len(close_data)} → {close_path}") |
| print(f" open: {len(open_data)} → {open_path}") |
| |
| print(f" sample close: Q={close_data[0]['question']}") |
| print(f" options={close_data[0].get('options', 'N/A')}") |
| print(f" answer={close_data[0]['answer']}") |
| print(f" sample open: Q={open_data[0]['question']}") |
| print(f" answer={open_data[0]['answer']}") |
|
|
| print(f"\n=== All done! {len(CONDITIONS) * 2} JSON files saved to {OUTPUT_DIR} ===") |