File size: 2,329 Bytes
c388140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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