File size: 3,801 Bytes
e25f555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
로컬 이미지 디렉토리에서 lmms-eval용 JSON 데이터셋 2종 생성
- close_ended.json : 4지선다 (A/B/C/D)
- open_ended.json  : 자유 응답 (Up/Down/Left/Right)
"""

import os
import json
import glob

# ============================================================
# 설정
# ============================================================
DATA_ROOT = "/data/dataset/vlm_direction/object_location_4_class_subset_1of5/val"
OUTPUT_DIR = "./"

# 폴더명 → 영어 레이블 매핑
LABEL_MAP = {
    "up":    "Up",
    "down":  "Down",
    "left":  "Left",
    "right": "Right",
}

# Close-ended 선택지 (고정 순서)
OPTIONS = ["Up", "Down", "Left", "Right"]
OPTION_LETTERS = ["A", "B", "C", "D"]

# ============================================================
# 데이터 수집
# ============================================================
close_ended_data = []
open_ended_data = []

idx = 0
for folder_name, eng_label in LABEL_MAP.items():
    folder_path = os.path.join(DATA_ROOT, folder_name)
    if not os.path.isdir(folder_path):
        print(f"[WARNING] 폴더 없음: {folder_path}")
        continue

    image_files = sorted(
        glob.glob(os.path.join(folder_path, "*.png"))
        + glob.glob(os.path.join(folder_path, "*.jpg"))
        + glob.glob(os.path.join(folder_path, "*.jpeg"))
    )
    print(f"[INFO] {folder_name}: {len(image_files)} images found")

    for img_path in image_files:
        # abs_path = os.path.abspath(img_path)
        filename = os.path.basename(img_path)

        # 정답 옵션 letter (A/B/C/D)
        answer_idx = OPTIONS.index(eng_label)
        answer_letter = OPTION_LETTERS[answer_idx]

        # --------------------------------------------------
        # 1) Close-ended (4지선다)
        # --------------------------------------------------
        close_ended_data.append({
            "id": idx,
            "image": filename,
            "question": "From the viewer's perspective, where is the object located in the image?",
            "options": OPTIONS,              # ["Up", "Down", "Left", "Right"]
            "answer": answer_letter,         # "A" / "B" / "C" / "D"
            "answer_text": eng_label,        # "Up" / "Down" / "Left" / "Right"
            "category": folder_name,
        })

        # --------------------------------------------------
        # 2) Open-ended (자유 응답)
        # --------------------------------------------------
        open_ended_data.append({
            "id": idx,
            "image": filename,
            "question": "From the viewer's perspective, where is the object located in the image? Answer with one of: Up, Down, Left, Right.",
            "answer": eng_label,             # "Up" / "Down" / "Left" / "Right"
            "category": folder_name,
        })

        idx += 1

# ============================================================
# JSON 저장
# ============================================================
os.makedirs(OUTPUT_DIR, exist_ok=True)

close_path = os.path.join(OUTPUT_DIR, "close_ended.json")
open_path  = os.path.join(OUTPUT_DIR, "open_ended.json")

with open(close_path, "w", encoding="utf-8") as f:
    json.dump(close_ended_data, f, ensure_ascii=False, indent=2)

with open(open_path, "w", encoding="utf-8") as f:
    json.dump(open_ended_data, f, ensure_ascii=False, indent=2)

print(f"\n{'='*60}")
print(f"Total {idx} samples processed")
print(f"Close-ended → {close_path} ({len(close_ended_data)} samples)")
print(f"Open-ended  → {open_path} ({len(open_ended_data)} samples)")
print(f"{'='*60}")

# 샘플 출력
print("\n[Sample] Close-ended:")
print(json.dumps(close_ended_data[0], ensure_ascii=False, indent=2))
print("\n[Sample] Open-ended:")
print(json.dumps(open_ended_data[0], ensure_ascii=False, indent=2))