from __future__ import annotations import csv import json import os from dataclasses import dataclass from typing import Iterable, List, Sequence, Tuple @dataclass(frozen=True) class CaptionItem: """ 一条图文配对样本: - image_path:图片路径(绝对路径) - text:文本描述/提示词 """ image_path: str text: str def load_captions(captions_path: str, data_root: str) -> List[CaptionItem]: """ 读取 caption 文件,支持: - .jsonl:每行 {"file": "...", "text": "..."},file 可相对 data_root 或绝对路径 - .csv:两列 file,text(含表头),file 同上 """ captions_path = os.path.abspath(captions_path) data_root = os.path.abspath(data_root) if not os.path.exists(captions_path): raise FileNotFoundError(f"未找到 captions 文件:{captions_path}") items: List[CaptionItem] = [] ext = os.path.splitext(captions_path)[1].lower() def resolve_path(p: str) -> str: p = (p or "").strip() if not p: return "" if os.path.isabs(p): return p return os.path.join(data_root, p) if ext == ".jsonl": with open(captions_path, "r", encoding="utf-8") as f: for line in f: line = line.strip() if not line: continue obj = json.loads(line) img = resolve_path(str(obj.get("file", ""))) txt = str(obj.get("text", "")) if img and os.path.exists(img): items.append(CaptionItem(image_path=img, text=txt)) elif ext == ".csv": with open(captions_path, "r", encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: img = resolve_path(str(row.get("file", ""))) txt = str(row.get("text", "")) if img and os.path.exists(img): items.append(CaptionItem(image_path=img, text=txt)) else: raise ValueError("captions 文件仅支持 .jsonl 或 .csv") if len(items) == 0: raise ValueError("captions 文件未读取到有效样本(请检查 file 路径与图片是否存在)") return items