object_location_4class / making_json.py
KHUjongseo's picture
Upload folder using huggingface_hub
e25f555 verified
"""
로컬 이미지 디렉토리에서 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))