ds-ocr 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ds_ocr/__init__.py +12 -0
- ds_ocr/__main__.py +4 -0
- ds_ocr/cli.py +42 -0
- ds_ocr/config.py +49 -0
- ds_ocr/ocr.py +955 -0
- ds_ocr/sniff.py +25 -0
- ds_ocr-0.1.0.dist-info/METADATA +76 -0
- ds_ocr-0.1.0.dist-info/RECORD +11 -0
- ds_ocr-0.1.0.dist-info/WHEEL +4 -0
- ds_ocr-0.1.0.dist-info/entry_points.txt +2 -0
- ds_ocr-0.1.0.dist-info/licenses/LICENSE +21 -0
ds_ocr/__init__.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""用 DeepSeek 视觉模型把本地图片和 PDF 识别成 Markdown。"""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
4
|
+
|
|
5
|
+
from ds_ocr.ocr import OCR, PdfPageLimitError
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
__version__ = version("ds-ocr")
|
|
9
|
+
except PackageNotFoundError:
|
|
10
|
+
__version__ = "0.1.0"
|
|
11
|
+
|
|
12
|
+
__all__ = ["OCR", "PdfPageLimitError", "__version__"]
|
ds_ocr/__main__.py
ADDED
ds_ocr/cli.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""命令行:把图片或 PDF 识别成 Markdown。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from ds_ocr.ocr import OCR, PdfPageLimitError
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def main(argv: list[str] | None = None) -> None:
|
|
13
|
+
parser = argparse.ArgumentParser(
|
|
14
|
+
prog="ds-ocr",
|
|
15
|
+
description="用 DeepSeek 识别本地图片或 PDF,输出 Markdown。",
|
|
16
|
+
)
|
|
17
|
+
parser.add_argument("paths", nargs="+", help="图片或 PDF 路径,按顺序识别")
|
|
18
|
+
parser.add_argument("--api-key", required=True, help="调用时传入的 API 密钥")
|
|
19
|
+
parser.add_argument("--base-url", required=True, help="调用时传入的接口地址")
|
|
20
|
+
parser.add_argument("--model", required=True, help="调用时传入的模型名")
|
|
21
|
+
parser.add_argument(
|
|
22
|
+
"-o",
|
|
23
|
+
"--output",
|
|
24
|
+
help="写入该文件;省略则打印到标准输出",
|
|
25
|
+
)
|
|
26
|
+
args = parser.parse_args(argv)
|
|
27
|
+
try:
|
|
28
|
+
ocr = OCR(api_key=args.api_key, base_url=args.base_url, model=args.model)
|
|
29
|
+
text = ocr.recognize(args.paths)
|
|
30
|
+
except PdfPageLimitError as exc:
|
|
31
|
+
print(exc, file=sys.stderr)
|
|
32
|
+
raise SystemExit(2) from exc
|
|
33
|
+
if args.output:
|
|
34
|
+
Path(args.output).write_text(text, encoding="utf-8")
|
|
35
|
+
return
|
|
36
|
+
sys.stdout.write(text)
|
|
37
|
+
if text and not text.endswith("\n"):
|
|
38
|
+
sys.stdout.write("\n")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
if __name__ == "__main__":
|
|
42
|
+
main()
|
ds_ocr/config.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""运行时可调参数,均来自环境变量。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
_TRUE = {"1", "true", "yes", "on"}
|
|
8
|
+
_FALSE = {"0", "false", "no", "off"}
|
|
9
|
+
|
|
10
|
+
_DEFAULT_TIMEOUT_S = 180.0
|
|
11
|
+
_DEFAULT_PDF_OCR_MAX_PAGES = 50
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def llm_enabled() -> bool:
|
|
15
|
+
"""是否调用 DeepSeek。未设置或无法识别时为开启。"""
|
|
16
|
+
raw = os.getenv("LLM_ENABLED", "").strip().lower()
|
|
17
|
+
if not raw or raw in _TRUE:
|
|
18
|
+
return True
|
|
19
|
+
if raw in _FALSE:
|
|
20
|
+
return False
|
|
21
|
+
return True
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def llm_timeout_s() -> float:
|
|
25
|
+
"""单次 API 请求超时(秒)。"""
|
|
26
|
+
raw = os.getenv("LLM_TIMEOUT_S", "").strip()
|
|
27
|
+
if not raw:
|
|
28
|
+
return _DEFAULT_TIMEOUT_S
|
|
29
|
+
try:
|
|
30
|
+
value = float(raw)
|
|
31
|
+
except ValueError:
|
|
32
|
+
return _DEFAULT_TIMEOUT_S
|
|
33
|
+
if value <= 0:
|
|
34
|
+
return _DEFAULT_TIMEOUT_S
|
|
35
|
+
return value
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def pdf_ocr_max_pages() -> int:
|
|
39
|
+
"""含待识别页的 PDF 允许的最大页数。"""
|
|
40
|
+
raw = os.getenv("PDF_OCR_MAX_PAGES", "").strip()
|
|
41
|
+
if not raw:
|
|
42
|
+
return _DEFAULT_PDF_OCR_MAX_PAGES
|
|
43
|
+
try:
|
|
44
|
+
value = int(raw)
|
|
45
|
+
except ValueError:
|
|
46
|
+
return _DEFAULT_PDF_OCR_MAX_PAGES
|
|
47
|
+
if value < 1:
|
|
48
|
+
return _DEFAULT_PDF_OCR_MAX_PAGES
|
|
49
|
+
return value
|
ds_ocr/ocr.py
ADDED
|
@@ -0,0 +1,955 @@
|
|
|
1
|
+
"""识别本地图片或 PDF,返回 Markdown。
|
|
2
|
+
|
|
3
|
+
图片以 image_url 内联送入,并指定 detail=original,避免 file_id 忽略细节级别。
|
|
4
|
+
入模前总像素仍会被缩到约 1300×1300,因此超过该像素量的图先切开,每张原图的
|
|
5
|
+
切片放进同一次请求。多张图、PDF 的多页扫描并行识别。体积放不进请求体时才
|
|
6
|
+
退回 Files API。
|
|
7
|
+
|
|
8
|
+
PDF 先用 pdf-inspector 逐页判断:文字页直接采用它的 Markdown;非文字页按
|
|
9
|
+
3 倍(216 DPI)渲染、轻度锐化后再切开。各段结果按原页序拼接。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import base64
|
|
15
|
+
import math
|
|
16
|
+
import re
|
|
17
|
+
import tempfile
|
|
18
|
+
import time
|
|
19
|
+
from collections.abc import Sequence
|
|
20
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
from loguru import logger
|
|
25
|
+
from openai import APIStatusError, OpenAI, RateLimitError
|
|
26
|
+
|
|
27
|
+
from ds_ocr.config import llm_enabled, llm_timeout_s, pdf_ocr_max_pages
|
|
28
|
+
from ds_ocr.sniff import sniff_suffix
|
|
29
|
+
|
|
30
|
+
# Files API:单文件 64 MiB;对话里单边最长 8192 像素。
|
|
31
|
+
_MAX_BYTES = 64 * 1024 * 1024
|
|
32
|
+
_API_MAX_SIDE = 8192
|
|
33
|
+
# 入模前更大的图会缩到总像素约等于 1300×1300。file_id 忽略 detail,上传前切开。
|
|
34
|
+
_MODEL_EDGE = 1300
|
|
35
|
+
_MODEL_PIXELS = _MODEL_EDGE * _MODEL_EDGE
|
|
36
|
+
# 相邻切片重叠,避免文字落在切缝上。
|
|
37
|
+
_TILE_OVERLAP = 48
|
|
38
|
+
# 切开时单块至少留这么高,避免切成过碎的横条。
|
|
39
|
+
_MIN_TILE_SIDE = 256
|
|
40
|
+
# 72pt 的整数倍,渲染不插值。3 倍即 216 DPI。
|
|
41
|
+
_RENDER_SCALE = 3.0
|
|
42
|
+
# 多张图、多页扫描同时识别的上限。
|
|
43
|
+
_RECOGNIZE_WORKERS = 4
|
|
44
|
+
# 内联 base64 的保守上限:单张 32 MiB、整包 48 MiB,这里留出编码膨胀。
|
|
45
|
+
_INLINE_IMAGE_BYTES = 20 * 1024 * 1024
|
|
46
|
+
_INLINE_BODY_BYTES = 40 * 1024 * 1024
|
|
47
|
+
_DETAIL = "original"
|
|
48
|
+
# 上传后最短可设 1 小时;识别完会主动删除,这里只作删除失败时的兜底。
|
|
49
|
+
_FILE_TTL_S = 3600
|
|
50
|
+
_MAX_OUTPUT_TOKENS = 65536
|
|
51
|
+
|
|
52
|
+
_RATE_LIMIT_MAX_RETRIES = 5
|
|
53
|
+
_RATE_LIMIT_BASE_WAIT_S = 60
|
|
54
|
+
# 15 张及以上时单边上限降为 4096,因此每批最多 14 张。
|
|
55
|
+
_IMAGES_PER_REQUEST = 14
|
|
56
|
+
|
|
57
|
+
_NATIVE_EXTS = frozenset({".jpg", ".png", ".gif", ".webp"})
|
|
58
|
+
_IMAGE_EXTS = _NATIVE_EXTS | frozenset({".bmp", ".tif", ".tiff"})
|
|
59
|
+
|
|
60
|
+
_FENCE_RE = re.compile(
|
|
61
|
+
r"^```(?:markdown|md)?\s*\n(.*)\n```\s*$",
|
|
62
|
+
re.DOTALL | re.IGNORECASE,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
_PROMPT = """\
|
|
66
|
+
识别图片中的全部文字,按阅读顺序输出 Markdown。
|
|
67
|
+
表格用 Markdown 表格。不要描述图片,不要补充图中没有的内容。
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
_SLICE_PROMPT = """\
|
|
71
|
+
下面这些图片是同一页文档按从左到右、从上到下切开的连续切片,相邻切片有少量重叠。
|
|
72
|
+
按阅读顺序识别全部文字,重叠处只保留一次,输出 Markdown。
|
|
73
|
+
表格用 Markdown 表格。不要描述图片,不要补充图中没有的内容。
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class PdfPageLimitError(RuntimeError):
|
|
78
|
+
"""PDF 含待识别页且总页数超过上限。调用方应记入 pdf_ocr_skips,不要当识别失败重试。"""
|
|
79
|
+
|
|
80
|
+
def __init__(self, page_count: int, *, limit: int, pref: str = "") -> None:
|
|
81
|
+
self.page_count = page_count
|
|
82
|
+
self.limit = limit
|
|
83
|
+
super().__init__(
|
|
84
|
+
f"{pref}PDF 共 {page_count} 页,超过 {limit} 页,未调用 DeepSeek"
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _prefix(config_id: int | str | None) -> str:
|
|
89
|
+
return f"id-{config_id}: " if config_id is not None else "id-deepseek: "
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _normalize_ext(ext: str) -> str:
|
|
93
|
+
ext = (ext or "").lower()
|
|
94
|
+
if ext == ".jpeg":
|
|
95
|
+
return ".jpg"
|
|
96
|
+
if ext == ".tiff":
|
|
97
|
+
return ".tif"
|
|
98
|
+
return ext
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _detect_ext(src: Path) -> str:
|
|
102
|
+
with src.open("rb") as handle:
|
|
103
|
+
head = handle.read(64)
|
|
104
|
+
sniffed = sniff_suffix(head)
|
|
105
|
+
if sniffed in {".pdf", *_IMAGE_EXTS, ".tiff"}:
|
|
106
|
+
return _normalize_ext(sniffed)
|
|
107
|
+
return _normalize_ext(src.suffix)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _is_rate_limit_error(exc: BaseException) -> bool:
|
|
111
|
+
if isinstance(exc, RateLimitError):
|
|
112
|
+
return True
|
|
113
|
+
if isinstance(exc, APIStatusError) and getattr(exc, "status_code", None) == 429:
|
|
114
|
+
return True
|
|
115
|
+
msg = str(exc).lower()
|
|
116
|
+
return "429" in msg or "rate limit" in msg or "too many requests" in msg
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _call_with_retry(action):
|
|
120
|
+
"""429 时指数退避。中间重试不打日志,只在最后一次失败时由调用方记录。"""
|
|
121
|
+
last: BaseException | None = None
|
|
122
|
+
for attempt in range(_RATE_LIMIT_MAX_RETRIES + 1):
|
|
123
|
+
try:
|
|
124
|
+
return action()
|
|
125
|
+
except Exception as exc:
|
|
126
|
+
last = exc
|
|
127
|
+
if _is_rate_limit_error(exc) and attempt < _RATE_LIMIT_MAX_RETRIES:
|
|
128
|
+
time.sleep(_RATE_LIMIT_BASE_WAIT_S * (2**attempt))
|
|
129
|
+
continue
|
|
130
|
+
raise
|
|
131
|
+
assert last is not None
|
|
132
|
+
raise last
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _required(value: str, name: str) -> str:
|
|
136
|
+
text = (value or "").strip()
|
|
137
|
+
if not text:
|
|
138
|
+
raise ValueError(f"缺少 {name}")
|
|
139
|
+
return text
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class OCR:
|
|
143
|
+
"""配置一次密钥、接口地址和模型,之后重复调用 ``recognize``。"""
|
|
144
|
+
|
|
145
|
+
def __init__(self, *, api_key: str, base_url: str, model: str) -> None:
|
|
146
|
+
self.model = _required(model, "model")
|
|
147
|
+
self._client = OpenAI(
|
|
148
|
+
api_key=_required(api_key, "api_key"),
|
|
149
|
+
base_url=_required(base_url, "base_url"),
|
|
150
|
+
timeout=llm_timeout_s(),
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
def recognize(
|
|
154
|
+
self,
|
|
155
|
+
path: str | Path | Sequence[str | Path],
|
|
156
|
+
*,
|
|
157
|
+
config_id: int | str | None = None,
|
|
158
|
+
flags: dict[str, bool] | None = None,
|
|
159
|
+
) -> str:
|
|
160
|
+
"""识别本地图片或 PDF,返回 Markdown 正文。
|
|
161
|
+
|
|
162
|
+
密钥、接口地址和模型使用创建实例时的配置,这里只传文件。
|
|
163
|
+
``path`` 可以是单个路径,也可以按顺序传入多个图片 / PDF。
|
|
164
|
+
总像素超过约 1300×1300 的图片会先切开,同一张图的切片一次送出。
|
|
165
|
+
多张图和 PDF 的多页扫描并行识别,结果仍按传入顺序拼接。
|
|
166
|
+
PDF:文字页用 pdf-inspector;非文字页按 3 倍渲染后同样切开。
|
|
167
|
+
整份 PDF 超过 ``PDF_OCR_MAX_PAGES``(默认 50,可用环境变量覆盖)且仍有非文字页时抛
|
|
168
|
+
``PdfPageLimitError``,不上传。
|
|
169
|
+
``LLM_ENABLED=false`` 时不请求模型:图片返回空字符串,PDF 只保留文字页。
|
|
170
|
+
|
|
171
|
+
传入 ``flags`` 时写入 ``flags["from_ocr"]``:本轮是否实际调用了视觉识别。
|
|
172
|
+
本方法只返回 Markdown 正文,不添加额外前缀。
|
|
173
|
+
"""
|
|
174
|
+
pref = _prefix(config_id)
|
|
175
|
+
sources = _coerce_paths(path)
|
|
176
|
+
if not sources:
|
|
177
|
+
raise ValueError(f"{pref}未传入文件")
|
|
178
|
+
for src in sources:
|
|
179
|
+
if not src.is_file():
|
|
180
|
+
raise FileNotFoundError(f"{pref}文件不存在: {src}")
|
|
181
|
+
|
|
182
|
+
parts: list[str] = []
|
|
183
|
+
pending: list[Path] = []
|
|
184
|
+
used_ocr = False
|
|
185
|
+
|
|
186
|
+
def flush_images() -> None:
|
|
187
|
+
nonlocal used_ocr
|
|
188
|
+
if not pending:
|
|
189
|
+
return
|
|
190
|
+
batch = list(pending)
|
|
191
|
+
pending.clear()
|
|
192
|
+
if llm_enabled():
|
|
193
|
+
used_ocr = True
|
|
194
|
+
text = _recognize_image_files(batch, pref, self._client, self.model)
|
|
195
|
+
if text:
|
|
196
|
+
parts.append(text)
|
|
197
|
+
|
|
198
|
+
for src in sources:
|
|
199
|
+
ext = _detect_ext(src)
|
|
200
|
+
if ext == ".pdf":
|
|
201
|
+
flush_images()
|
|
202
|
+
try:
|
|
203
|
+
text, pdf_ocr = _recognize_pdf(src, pref, self._client, self.model)
|
|
204
|
+
except PdfPageLimitError:
|
|
205
|
+
raise
|
|
206
|
+
except Exception as exc:
|
|
207
|
+
logger.error(f"{pref}PDF 解析失败 {src.name}: {exc}")
|
|
208
|
+
raise
|
|
209
|
+
if pdf_ocr:
|
|
210
|
+
used_ocr = True
|
|
211
|
+
if text:
|
|
212
|
+
parts.append(text)
|
|
213
|
+
continue
|
|
214
|
+
if ext not in _IMAGE_EXTS:
|
|
215
|
+
raise ValueError(
|
|
216
|
+
f"{pref}仅支持图片(png/jpg/gif/webp/bmp/tif)或 PDF: {src.name}"
|
|
217
|
+
)
|
|
218
|
+
pending.append(src)
|
|
219
|
+
flush_images()
|
|
220
|
+
if flags is not None:
|
|
221
|
+
flags["from_ocr"] = used_ocr
|
|
222
|
+
return _join_texts(parts)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _clip_name(name: str) -> str:
|
|
226
|
+
raw = name.encode("utf-8")
|
|
227
|
+
if len(raw) <= 512:
|
|
228
|
+
return name
|
|
229
|
+
stem = Path(name).stem
|
|
230
|
+
suffix = Path(name).suffix
|
|
231
|
+
budget = 512 - len(suffix.encode("utf-8"))
|
|
232
|
+
clipped = stem.encode("utf-8")[: max(budget, 1)].decode("utf-8", errors="ignore")
|
|
233
|
+
return f"{clipped}{suffix}" or "file.jpg"
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def _to_rgb(image: Any) -> Any:
|
|
237
|
+
from PIL import Image
|
|
238
|
+
|
|
239
|
+
if image.mode == "RGB":
|
|
240
|
+
return image
|
|
241
|
+
if image.mode in ("RGBA", "LA", "P"):
|
|
242
|
+
rgba = image.convert("RGBA")
|
|
243
|
+
background = Image.new("RGB", rgba.size, (255, 255, 255))
|
|
244
|
+
background.paste(rgba, mask=rgba.getchannel("A"))
|
|
245
|
+
return background
|
|
246
|
+
return image.convert("RGB")
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _save_jpeg(image: Any, dest: Path, *, pref: str) -> None:
|
|
250
|
+
from PIL import Image
|
|
251
|
+
|
|
252
|
+
current = _to_rgb(image)
|
|
253
|
+
quality = 90
|
|
254
|
+
while True:
|
|
255
|
+
current.save(
|
|
256
|
+
dest,
|
|
257
|
+
format="JPEG",
|
|
258
|
+
quality=quality,
|
|
259
|
+
optimize=True,
|
|
260
|
+
subsampling=0,
|
|
261
|
+
)
|
|
262
|
+
if dest.stat().st_size <= _MAX_BYTES:
|
|
263
|
+
return
|
|
264
|
+
if quality > 60:
|
|
265
|
+
quality -= 15
|
|
266
|
+
continue
|
|
267
|
+
width, height = current.size
|
|
268
|
+
if max(width, height) <= 800:
|
|
269
|
+
raise RuntimeError(f"{pref}图片压缩后仍超过 64 MiB: {dest.name}")
|
|
270
|
+
current = current.resize(
|
|
271
|
+
(max(width // 2, 1), max(height // 2, 1)),
|
|
272
|
+
Image.Resampling.LANCZOS,
|
|
273
|
+
)
|
|
274
|
+
quality = 85
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def _read_size(src: Path) -> tuple[int, int] | None:
|
|
278
|
+
try:
|
|
279
|
+
from PIL import Image
|
|
280
|
+
|
|
281
|
+
with Image.open(src) as image:
|
|
282
|
+
width, height = image.size
|
|
283
|
+
except Exception:
|
|
284
|
+
return None
|
|
285
|
+
if width <= 0 or height <= 0:
|
|
286
|
+
return None
|
|
287
|
+
return width, height
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def _fits_model(src: Path) -> bool:
|
|
291
|
+
"""原生格式、体积与边长合法,且总像素不超过入模上限时可以直接送去识别。"""
|
|
292
|
+
if _detect_ext(src) not in _NATIVE_EXTS:
|
|
293
|
+
return False
|
|
294
|
+
if src.stat().st_size > _MAX_BYTES:
|
|
295
|
+
return False
|
|
296
|
+
size = _read_size(src)
|
|
297
|
+
if size is None:
|
|
298
|
+
return False
|
|
299
|
+
width, height = size
|
|
300
|
+
return (
|
|
301
|
+
width <= _API_MAX_SIDE
|
|
302
|
+
and height <= _API_MAX_SIDE
|
|
303
|
+
and width * height <= _MODEL_PIXELS
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _axis_spans(total: int, tile: int, overlap: int) -> list[tuple[int, int]]:
|
|
308
|
+
"""沿一条边覆盖 ``total``。每段长度不超过 ``tile``,末段对齐到边缘。"""
|
|
309
|
+
if total <= tile:
|
|
310
|
+
return [(0, total)]
|
|
311
|
+
stride = max(1, tile - overlap)
|
|
312
|
+
starts = [0]
|
|
313
|
+
pos = 0
|
|
314
|
+
while pos + tile < total:
|
|
315
|
+
nxt = pos + stride
|
|
316
|
+
if nxt + tile >= total:
|
|
317
|
+
nxt = total - tile
|
|
318
|
+
if nxt <= pos:
|
|
319
|
+
nxt = pos + 1
|
|
320
|
+
starts.append(nxt)
|
|
321
|
+
if nxt + tile >= total:
|
|
322
|
+
break
|
|
323
|
+
pos = nxt
|
|
324
|
+
return [(start, min(total, start + tile)) for start in starts]
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def _tile_boxes(width: int, height: int) -> list[tuple[int, int, int, int]]:
|
|
328
|
+
"""把图片分成若干块,每块总像素不超过入模上限,阅读顺序为从左到右、从上到下。"""
|
|
329
|
+
if width <= 0 or height <= 0:
|
|
330
|
+
raise ValueError("图片尺寸无效")
|
|
331
|
+
if (
|
|
332
|
+
width * height <= _MODEL_PIXELS
|
|
333
|
+
and width <= _API_MAX_SIDE
|
|
334
|
+
and height <= _API_MAX_SIDE
|
|
335
|
+
):
|
|
336
|
+
return [(0, 0, width, height)]
|
|
337
|
+
|
|
338
|
+
cols = 1
|
|
339
|
+
tile_w = min(width, _API_MAX_SIDE)
|
|
340
|
+
while cols <= width:
|
|
341
|
+
nominal_w = math.ceil(width / cols)
|
|
342
|
+
extra_w = _TILE_OVERLAP if nominal_w < width else 0
|
|
343
|
+
candidate = min(width, nominal_w + extra_w, _API_MAX_SIDE)
|
|
344
|
+
height_cap = _MODEL_PIXELS // max(candidate, 1)
|
|
345
|
+
if height_cap >= min(_MIN_TILE_SIDE, height):
|
|
346
|
+
tile_w = candidate
|
|
347
|
+
break
|
|
348
|
+
cols += 1
|
|
349
|
+
else:
|
|
350
|
+
tile_w = min(width, _API_MAX_SIDE)
|
|
351
|
+
|
|
352
|
+
budget_h = max(1, _MODEL_PIXELS // max(tile_w, 1))
|
|
353
|
+
tile_h = min(height, _API_MAX_SIDE, budget_h)
|
|
354
|
+
if tile_w * tile_h > _MODEL_PIXELS:
|
|
355
|
+
tile_h = max(1, _MODEL_PIXELS // max(tile_w, 1))
|
|
356
|
+
|
|
357
|
+
x_overlap = _TILE_OVERLAP if tile_w < width else 0
|
|
358
|
+
y_overlap = _TILE_OVERLAP if tile_h < height else 0
|
|
359
|
+
return [
|
|
360
|
+
(left, top, right, bottom)
|
|
361
|
+
for top, bottom in _axis_spans(height, tile_h, y_overlap)
|
|
362
|
+
for left, right in _axis_spans(width, tile_w, x_overlap)
|
|
363
|
+
]
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
def _page_scale(width_pt: float, height_pt: float) -> float:
|
|
367
|
+
long_pt = max(float(width_pt), float(height_pt), 1.0)
|
|
368
|
+
return min(_RENDER_SCALE, _API_MAX_SIDE / long_pt)
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def _sharpen_text(image: Any) -> Any:
|
|
372
|
+
"""轻度锐化扫描页,让笔画边缘更清楚。"""
|
|
373
|
+
from PIL import ImageFilter
|
|
374
|
+
|
|
375
|
+
return image.filter(ImageFilter.UnsharpMask(radius=1.0, percent=60, threshold=2))
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
def _render_pdf(
|
|
379
|
+
src: Path,
|
|
380
|
+
dest_dir: Path,
|
|
381
|
+
pref: str,
|
|
382
|
+
pages: list[int] | None = None,
|
|
383
|
+
) -> list[list[Path]]:
|
|
384
|
+
"""把指定页渲染成 JPEG 切片。``pages`` 为 0 起始页码,缺省时渲染全部页。"""
|
|
385
|
+
import pypdfium2 as pdfium
|
|
386
|
+
|
|
387
|
+
pdf = pdfium.PdfDocument(str(src))
|
|
388
|
+
page_slices: list[list[Path]] = []
|
|
389
|
+
try:
|
|
390
|
+
page_count = len(pdf)
|
|
391
|
+
if page_count <= 0:
|
|
392
|
+
raise RuntimeError(f"{pref}PDF 没有页面: {src.name}")
|
|
393
|
+
selected = list(range(page_count)) if pages is None else list(pages)
|
|
394
|
+
limit = pdf_ocr_max_pages()
|
|
395
|
+
if pages is None and page_count > limit:
|
|
396
|
+
raise PdfPageLimitError(page_count, limit=limit, pref=pref)
|
|
397
|
+
for index in selected:
|
|
398
|
+
if index < 0 or index >= page_count:
|
|
399
|
+
raise RuntimeError(f"{pref}PDF 页码越界: {index + 1}/{page_count}")
|
|
400
|
+
page = pdf[index]
|
|
401
|
+
try:
|
|
402
|
+
width, height = page.get_size()
|
|
403
|
+
bitmap = page.render(scale=_page_scale(width, height))
|
|
404
|
+
try:
|
|
405
|
+
# to_pil 可能与位图共用内存,先拷贝再关闭位图。
|
|
406
|
+
image = _sharpen_text(bitmap.to_pil().copy())
|
|
407
|
+
finally:
|
|
408
|
+
bitmap.close()
|
|
409
|
+
finally:
|
|
410
|
+
page.close()
|
|
411
|
+
page_slices.append(
|
|
412
|
+
_slice_loaded(
|
|
413
|
+
image,
|
|
414
|
+
dest_dir,
|
|
415
|
+
pref,
|
|
416
|
+
stem=f"page-{index + 1:03d}",
|
|
417
|
+
)
|
|
418
|
+
)
|
|
419
|
+
finally:
|
|
420
|
+
pdf.close()
|
|
421
|
+
return page_slices
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def _pdf_page_count(src: Path) -> int:
|
|
425
|
+
import pypdfium2 as pdfium
|
|
426
|
+
|
|
427
|
+
pdf = pdfium.PdfDocument(str(src))
|
|
428
|
+
try:
|
|
429
|
+
return len(pdf)
|
|
430
|
+
finally:
|
|
431
|
+
pdf.close()
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def _split_pdf_pages(src: Path) -> tuple[int, list[str | None]]:
|
|
435
|
+
"""按原页序区分文字页。
|
|
436
|
+
|
|
437
|
+
返回 ``(page_count, slots)``。``slots[i]`` 为该页 Markdown;
|
|
438
|
+
``None`` 表示这一页不是 text_based,需要交给 DeepSeek。
|
|
439
|
+
pdf-inspector 没有单独的逐页类型字段,``needs_ocr`` 为假即视为文字页。
|
|
440
|
+
"""
|
|
441
|
+
import pdf_inspector
|
|
442
|
+
|
|
443
|
+
page_count = _pdf_page_count(src)
|
|
444
|
+
if page_count <= 0:
|
|
445
|
+
return 0, []
|
|
446
|
+
|
|
447
|
+
extracted = pdf_inspector.extract_pages_markdown(str(src))
|
|
448
|
+
ocr_pages = {
|
|
449
|
+
int(number) for number in (getattr(extracted, "pages_needing_ocr", None) or [])
|
|
450
|
+
}
|
|
451
|
+
slots: list[str | None] = [None] * page_count
|
|
452
|
+
seen: set[int] = set()
|
|
453
|
+
for fallback, page in enumerate(getattr(extracted, "pages", None) or []):
|
|
454
|
+
raw_index = getattr(page, "page", None)
|
|
455
|
+
index = fallback if raw_index is None else int(raw_index)
|
|
456
|
+
if index < 0 or index >= page_count or index in seen:
|
|
457
|
+
continue
|
|
458
|
+
seen.add(index)
|
|
459
|
+
needs_ocr = getattr(page, "needs_ocr", None)
|
|
460
|
+
if needs_ocr is None:
|
|
461
|
+
needs_ocr = (index + 1) in ocr_pages
|
|
462
|
+
text = (getattr(page, "markdown", None) or "").strip()
|
|
463
|
+
slots[index] = None if needs_ocr else text
|
|
464
|
+
return page_count, slots
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
def _fmt_pages(indexes: list[int]) -> str:
|
|
468
|
+
if not indexes:
|
|
469
|
+
return "无"
|
|
470
|
+
return ",".join(str(index + 1) for index in indexes)
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
def _join_pages(slots: list[str | None]) -> str:
|
|
474
|
+
parts = [text.strip() for text in slots if text and text.strip()]
|
|
475
|
+
return "\n\n".join(parts)
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
def _slice_loaded(image: Any, dest_dir: Path, pref: str, *, stem: str) -> list[Path]:
|
|
479
|
+
"""按入模像素上限切开并写成 JPEG。未超过上限时只写一张。"""
|
|
480
|
+
width, height = image.size
|
|
481
|
+
boxes = _tile_boxes(width, height)
|
|
482
|
+
if len(boxes) > 1:
|
|
483
|
+
logger.info(
|
|
484
|
+
f"{pref}{stem} 为 {width}×{height},"
|
|
485
|
+
f"超过入模约 {_MODEL_EDGE}×{_MODEL_EDGE} 像素,切成 {len(boxes)} 块"
|
|
486
|
+
)
|
|
487
|
+
paths: list[Path] = []
|
|
488
|
+
for index, box in enumerate(boxes, start=1):
|
|
489
|
+
name = f"{stem}.jpg" if len(boxes) == 1 else f"{stem}-{index:03d}.jpg"
|
|
490
|
+
out = dest_dir / name
|
|
491
|
+
_save_jpeg(image.crop(box), out, pref=pref)
|
|
492
|
+
paths.append(out)
|
|
493
|
+
return paths
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
def _convert_and_slice(
|
|
497
|
+
src: Path,
|
|
498
|
+
dest_dir: Path,
|
|
499
|
+
pref: str,
|
|
500
|
+
*,
|
|
501
|
+
stem: str = "image",
|
|
502
|
+
) -> list[Path]:
|
|
503
|
+
from PIL import Image, UnidentifiedImageError
|
|
504
|
+
|
|
505
|
+
try:
|
|
506
|
+
with Image.open(src) as image:
|
|
507
|
+
image.load()
|
|
508
|
+
loaded = image.copy()
|
|
509
|
+
except Image.DecompressionBombError as exc:
|
|
510
|
+
raise RuntimeError(f"{pref}图片尺寸过大: {src.name}") from exc
|
|
511
|
+
except UnidentifiedImageError as exc:
|
|
512
|
+
raise RuntimeError(f"{pref}无法读取图片: {src.name}") from exc
|
|
513
|
+
return _slice_loaded(loaded, dest_dir, pref, stem=stem)
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
def _prepare_one_image(src: Path, dest_dir: Path, pref: str, index: int) -> list[Path]:
|
|
517
|
+
ext = _detect_ext(src)
|
|
518
|
+
if ext not in _IMAGE_EXTS:
|
|
519
|
+
raise ValueError(
|
|
520
|
+
f"{pref}仅支持图片(png/jpg/gif/webp/bmp/tif)或 PDF: {src.name}"
|
|
521
|
+
)
|
|
522
|
+
if _fits_model(src):
|
|
523
|
+
return [src]
|
|
524
|
+
return _convert_and_slice(src, dest_dir, pref, stem=f"image-{index:03d}")
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
def _inputs_label(sources: list[Path]) -> str:
|
|
528
|
+
if len(sources) == 1:
|
|
529
|
+
return sources[0].name
|
|
530
|
+
shown = "、".join(src.name for src in sources[:3])
|
|
531
|
+
if len(sources) > 3:
|
|
532
|
+
return f"{shown} 等 {len(sources)} 个文件"
|
|
533
|
+
return shown
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
def _coerce_paths(path: str | Path | Sequence[str | Path]) -> list[Path]:
|
|
537
|
+
if isinstance(path, (str, Path)):
|
|
538
|
+
return [Path(path)]
|
|
539
|
+
return [Path(item) for item in path]
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
def _join_texts(parts: list[str]) -> str:
|
|
543
|
+
return "\n\n".join(part.strip() for part in parts if part and part.strip())
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
def _mime(path: Path) -> str:
|
|
547
|
+
return {
|
|
548
|
+
".jpg": "image/jpeg",
|
|
549
|
+
".png": "image/png",
|
|
550
|
+
".gif": "image/gif",
|
|
551
|
+
".webp": "image/webp",
|
|
552
|
+
}.get(_detect_ext(path), "image/jpeg")
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
def _is_blank(image: Any) -> bool:
|
|
556
|
+
"""近乎全白或全黑的图没有可识别文字。"""
|
|
557
|
+
from PIL import Image
|
|
558
|
+
|
|
559
|
+
small = image.convert("L").resize((32, 32), Image.Resampling.BOX)
|
|
560
|
+
hist = small.histogram()
|
|
561
|
+
total = 32 * 32
|
|
562
|
+
white = sum(hist[248:])
|
|
563
|
+
black = sum(hist[:8])
|
|
564
|
+
return white / total >= 0.992 or black / total >= 0.992
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
def _is_blank_file(path: Path) -> bool:
|
|
568
|
+
from PIL import Image
|
|
569
|
+
|
|
570
|
+
try:
|
|
571
|
+
with Image.open(path) as image:
|
|
572
|
+
image.load()
|
|
573
|
+
return _is_blank(image)
|
|
574
|
+
except Exception:
|
|
575
|
+
return False
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
def _drop_blanks(images: list[Path], pref: str) -> list[Path]:
|
|
579
|
+
kept: list[Path] = []
|
|
580
|
+
for path in images:
|
|
581
|
+
if _is_blank_file(path):
|
|
582
|
+
logger.info(f"{pref}跳过近乎空白的图片 {path.name}")
|
|
583
|
+
continue
|
|
584
|
+
kept.append(path)
|
|
585
|
+
return kept
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
def _can_inline(images: list[Path]) -> bool:
|
|
589
|
+
total = 0
|
|
590
|
+
for path in images:
|
|
591
|
+
size = path.stat().st_size
|
|
592
|
+
if size > _INLINE_IMAGE_BYTES:
|
|
593
|
+
return False
|
|
594
|
+
total += size + 1024
|
|
595
|
+
return total * 4 // 3 <= _INLINE_BODY_BYTES
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
def _data_url(path: Path) -> str:
|
|
599
|
+
payload = base64.b64encode(path.read_bytes()).decode("ascii")
|
|
600
|
+
return f"data:{_mime(path)};base64,{payload}"
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
def _upload(client: OpenAI, path: Path, pref: str) -> str:
|
|
604
|
+
filename = _clip_name(path.name)
|
|
605
|
+
|
|
606
|
+
def _create() -> str:
|
|
607
|
+
with path.open("rb") as handle:
|
|
608
|
+
uploaded = client.files.create(
|
|
609
|
+
file=(filename, handle),
|
|
610
|
+
purpose="user_data",
|
|
611
|
+
expires_after={"anchor": "created_at", "seconds": _FILE_TTL_S},
|
|
612
|
+
)
|
|
613
|
+
file_id = getattr(uploaded, "id", "") or ""
|
|
614
|
+
if not file_id:
|
|
615
|
+
raise RuntimeError(f"{pref}上传文件未返回 file_id: {path.name}")
|
|
616
|
+
return file_id
|
|
617
|
+
|
|
618
|
+
return _call_with_retry(_create)
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
def _delete_uploaded(client: OpenAI, file_ids: list[str], pref: str) -> None:
|
|
622
|
+
if not file_ids:
|
|
623
|
+
return
|
|
624
|
+
|
|
625
|
+
def _delete(file_id: str) -> None:
|
|
626
|
+
try:
|
|
627
|
+
client.files.delete(file_id)
|
|
628
|
+
except Exception as exc:
|
|
629
|
+
logger.warning(f"{pref}删除已上传文件失败 {file_id}: {exc}")
|
|
630
|
+
|
|
631
|
+
if len(file_ids) == 1:
|
|
632
|
+
_delete(file_ids[0])
|
|
633
|
+
return
|
|
634
|
+
with ThreadPoolExecutor(max_workers=min(_RECOGNIZE_WORKERS, len(file_ids))) as pool:
|
|
635
|
+
list(pool.map(_delete, file_ids))
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
def _message_text(message: Any) -> str:
|
|
639
|
+
content = getattr(message, "content", None)
|
|
640
|
+
if isinstance(content, str):
|
|
641
|
+
return content
|
|
642
|
+
if isinstance(content, list):
|
|
643
|
+
parts: list[str] = []
|
|
644
|
+
for block in content:
|
|
645
|
+
if isinstance(block, str):
|
|
646
|
+
parts.append(block)
|
|
647
|
+
elif isinstance(block, dict):
|
|
648
|
+
parts.append(str(block.get("text") or ""))
|
|
649
|
+
else:
|
|
650
|
+
parts.append(str(getattr(block, "text", "") or ""))
|
|
651
|
+
return "\n".join(parts)
|
|
652
|
+
return ""
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
def _strip_fence(text: str) -> str:
|
|
656
|
+
stripped = text.strip()
|
|
657
|
+
matched = _FENCE_RE.match(stripped)
|
|
658
|
+
if matched:
|
|
659
|
+
return matched.group(1).strip()
|
|
660
|
+
return stripped
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
def _content_blocks(
|
|
664
|
+
images: list[Path],
|
|
665
|
+
*,
|
|
666
|
+
prompt: str,
|
|
667
|
+
label_order: bool,
|
|
668
|
+
file_ids: list[str] | None = None,
|
|
669
|
+
) -> list[dict[str, Any]]:
|
|
670
|
+
blocks: list[dict[str, Any]] = [{"type": "text", "text": prompt}]
|
|
671
|
+
total = len(images)
|
|
672
|
+
for index, image in enumerate(images):
|
|
673
|
+
if label_order and total > 1:
|
|
674
|
+
blocks.append({"type": "text", "text": f"切片 {index + 1}/{total}"})
|
|
675
|
+
if file_ids is None:
|
|
676
|
+
blocks.append(
|
|
677
|
+
{
|
|
678
|
+
"type": "image_url",
|
|
679
|
+
"image_url": {"url": _data_url(image), "detail": _DETAIL},
|
|
680
|
+
}
|
|
681
|
+
)
|
|
682
|
+
else:
|
|
683
|
+
blocks.append({"type": "file", "file_id": file_ids[index]})
|
|
684
|
+
return blocks
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
def _create_completion(
|
|
688
|
+
client: OpenAI,
|
|
689
|
+
model: str,
|
|
690
|
+
blocks: list[dict[str, Any]],
|
|
691
|
+
pref: str,
|
|
692
|
+
) -> str:
|
|
693
|
+
def _create() -> str:
|
|
694
|
+
response = client.chat.completions.create(
|
|
695
|
+
model=model,
|
|
696
|
+
messages=[{"role": "user", "content": blocks}],
|
|
697
|
+
max_tokens=_MAX_OUTPUT_TOKENS,
|
|
698
|
+
extra_body={"thinking": {"type": "disabled"}},
|
|
699
|
+
)
|
|
700
|
+
choice = response.choices[0] if response.choices else None
|
|
701
|
+
if choice is None:
|
|
702
|
+
raise RuntimeError(f"{pref}DeepSeek 未返回识别结果")
|
|
703
|
+
return _strip_fence(_message_text(choice.message))
|
|
704
|
+
|
|
705
|
+
return _call_with_retry(_create)
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
def _upload_all(client: OpenAI, images: list[Path], pref: str) -> list[str]:
|
|
709
|
+
if len(images) == 1:
|
|
710
|
+
return [_upload(client, images[0], pref)]
|
|
711
|
+
with ThreadPoolExecutor(max_workers=min(_RECOGNIZE_WORKERS, len(images))) as pool:
|
|
712
|
+
return list(pool.map(lambda path: _upload(client, path, pref), images))
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
def _recognize_batch(
|
|
716
|
+
client: OpenAI,
|
|
717
|
+
model: str,
|
|
718
|
+
images: list[Path],
|
|
719
|
+
pref: str,
|
|
720
|
+
*,
|
|
721
|
+
prompt: str,
|
|
722
|
+
label_order: bool = False,
|
|
723
|
+
) -> str:
|
|
724
|
+
"""识别一批已经切好的图片。能内联就一次请求送出,否则并行上传后再识别。"""
|
|
725
|
+
if not images:
|
|
726
|
+
raise RuntimeError(f"{pref}没有可识别的图片")
|
|
727
|
+
if len(images) > _IMAGES_PER_REQUEST:
|
|
728
|
+
raise RuntimeError(
|
|
729
|
+
f"{pref}单次请求 {len(images)} 张,超过 {_IMAGES_PER_REQUEST} 张"
|
|
730
|
+
)
|
|
731
|
+
if _can_inline(images):
|
|
732
|
+
logger.info(f"{pref}DeepSeek 内联识别 {len(images)} 张")
|
|
733
|
+
blocks = _content_blocks(images, prompt=prompt, label_order=label_order)
|
|
734
|
+
return _create_completion(client, model, blocks, pref)
|
|
735
|
+
|
|
736
|
+
logger.info(f"{pref}图片过大,改为上传后识别 {len(images)} 张")
|
|
737
|
+
file_ids: list[str] = []
|
|
738
|
+
try:
|
|
739
|
+
file_ids = _upload_all(client, images, pref)
|
|
740
|
+
blocks = _content_blocks(
|
|
741
|
+
images,
|
|
742
|
+
prompt=prompt,
|
|
743
|
+
label_order=label_order,
|
|
744
|
+
file_ids=file_ids,
|
|
745
|
+
)
|
|
746
|
+
return _create_completion(client, model, blocks, pref)
|
|
747
|
+
finally:
|
|
748
|
+
_delete_uploaded(client, file_ids, pref)
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
def _recognize_prepared(
|
|
752
|
+
client: OpenAI,
|
|
753
|
+
model: str,
|
|
754
|
+
images: list[Path],
|
|
755
|
+
pref: str,
|
|
756
|
+
*,
|
|
757
|
+
prompt: str,
|
|
758
|
+
label_order: bool = False,
|
|
759
|
+
) -> str:
|
|
760
|
+
"""按每批最多 ``_IMAGES_PER_REQUEST`` 张识别,再按批次顺序拼接。"""
|
|
761
|
+
images = _drop_blanks(images, pref)
|
|
762
|
+
if not images:
|
|
763
|
+
logger.info(f"{pref}图片近乎空白,跳过识别")
|
|
764
|
+
return ""
|
|
765
|
+
batches = [
|
|
766
|
+
images[start : start + _IMAGES_PER_REQUEST]
|
|
767
|
+
for start in range(0, len(images), _IMAGES_PER_REQUEST)
|
|
768
|
+
]
|
|
769
|
+
if len(batches) > 1:
|
|
770
|
+
logger.info(
|
|
771
|
+
f"{pref}DeepSeek 识别 {len(images)} 张,"
|
|
772
|
+
f"分 {len(batches)} 批,每批最多 {_IMAGES_PER_REQUEST} 张"
|
|
773
|
+
)
|
|
774
|
+
parts: list[str] = []
|
|
775
|
+
for batch_no, batch in enumerate(batches, start=1):
|
|
776
|
+
if len(batches) > 1:
|
|
777
|
+
logger.info(
|
|
778
|
+
f"{pref}DeepSeek 第 {batch_no}/{len(batches)} 批,{len(batch)} 张"
|
|
779
|
+
)
|
|
780
|
+
text = _recognize_batch(
|
|
781
|
+
client,
|
|
782
|
+
model,
|
|
783
|
+
batch,
|
|
784
|
+
pref,
|
|
785
|
+
prompt=prompt,
|
|
786
|
+
label_order=label_order,
|
|
787
|
+
)
|
|
788
|
+
if not (text or "").strip():
|
|
789
|
+
logger.warning(
|
|
790
|
+
f"{pref}第 {batch_no}/{len(batches)} 批 DeepSeek 未返回可识别内容"
|
|
791
|
+
)
|
|
792
|
+
parts.append(text)
|
|
793
|
+
return _join_texts(parts)
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
def _run_jobs(
|
|
797
|
+
client: OpenAI,
|
|
798
|
+
model: str,
|
|
799
|
+
jobs: list[tuple[list[Path], str, bool]],
|
|
800
|
+
pref: str,
|
|
801
|
+
) -> str:
|
|
802
|
+
"""按提交顺序取回结果。多于一组时并行识别。"""
|
|
803
|
+
if not jobs:
|
|
804
|
+
return ""
|
|
805
|
+
|
|
806
|
+
def run(job: tuple[list[Path], str, bool]) -> str:
|
|
807
|
+
images, prompt, label_order = job
|
|
808
|
+
return _recognize_prepared(
|
|
809
|
+
client,
|
|
810
|
+
model,
|
|
811
|
+
images,
|
|
812
|
+
pref,
|
|
813
|
+
prompt=prompt,
|
|
814
|
+
label_order=label_order,
|
|
815
|
+
)
|
|
816
|
+
|
|
817
|
+
if len(jobs) == 1:
|
|
818
|
+
return run(jobs[0])
|
|
819
|
+
|
|
820
|
+
workers = min(_RECOGNIZE_WORKERS, len(jobs))
|
|
821
|
+
logger.info(f"{pref}DeepSeek 并行识别 {len(jobs)} 组,同时 {workers} 组")
|
|
822
|
+
with ThreadPoolExecutor(max_workers=workers) as pool:
|
|
823
|
+
futures = [pool.submit(run, job) for job in jobs]
|
|
824
|
+
parts: list[str] = []
|
|
825
|
+
try:
|
|
826
|
+
for future in futures:
|
|
827
|
+
text = future.result()
|
|
828
|
+
if text:
|
|
829
|
+
parts.append(text)
|
|
830
|
+
except Exception:
|
|
831
|
+
for future in futures:
|
|
832
|
+
future.cancel()
|
|
833
|
+
raise
|
|
834
|
+
return _join_texts(parts)
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
def _recognize_groups(
|
|
838
|
+
client: OpenAI,
|
|
839
|
+
model: str,
|
|
840
|
+
groups: list[list[Path]],
|
|
841
|
+
pref: str,
|
|
842
|
+
) -> str:
|
|
843
|
+
"""每一张原图单独成组。切片同组一次送出;多组并行,结果仍按原顺序拼接。"""
|
|
844
|
+
jobs: list[tuple[list[Path], str, bool]] = []
|
|
845
|
+
for group in groups:
|
|
846
|
+
if not group:
|
|
847
|
+
continue
|
|
848
|
+
if len(group) == 1:
|
|
849
|
+
jobs.append((group, _PROMPT, False))
|
|
850
|
+
else:
|
|
851
|
+
jobs.append((group, _SLICE_PROMPT, True))
|
|
852
|
+
return _run_jobs(client, model, jobs, pref)
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
def _recognize_pdf(
|
|
856
|
+
src: Path,
|
|
857
|
+
pref: str,
|
|
858
|
+
client: OpenAI,
|
|
859
|
+
model: str,
|
|
860
|
+
) -> tuple[str, bool]:
|
|
861
|
+
"""文字页用 pdf-inspector,其余页渲染后按入模像素上限切开再交给 DeepSeek。
|
|
862
|
+
|
|
863
|
+
连续扫描页各自成组,多页并行识别;遇到文字页先交完当前批次,再写入该页文字。
|
|
864
|
+
同一页的切片仍在同一次请求里。返回 ``(markdown, from_ocr)``。
|
|
865
|
+
``from_ocr`` 仅在实际调用了 DeepSeek 时为真。
|
|
866
|
+
"""
|
|
867
|
+
page_count, slots = _split_pdf_pages(src)
|
|
868
|
+
if page_count <= 0:
|
|
869
|
+
raise RuntimeError(f"{pref}PDF 没有页面: {src.name}")
|
|
870
|
+
|
|
871
|
+
ocr_indexes = [index for index, text in enumerate(slots) if text is None]
|
|
872
|
+
text_indexes = [
|
|
873
|
+
index for index in range(page_count) if index not in set(ocr_indexes)
|
|
874
|
+
]
|
|
875
|
+
logger.info(
|
|
876
|
+
f"{pref}PDF {src.name} 共 {page_count} 页,"
|
|
877
|
+
f"文字页 {_fmt_pages(text_indexes)},DeepSeek 页 {_fmt_pages(ocr_indexes)}"
|
|
878
|
+
)
|
|
879
|
+
limit = pdf_ocr_max_pages()
|
|
880
|
+
if ocr_indexes and page_count > limit:
|
|
881
|
+
raise PdfPageLimitError(page_count, limit=limit, pref=pref)
|
|
882
|
+
if not ocr_indexes:
|
|
883
|
+
return _join_pages(slots), False
|
|
884
|
+
if not llm_enabled():
|
|
885
|
+
logger.info(f"{pref}LLM_ENABLED=False,跳过 DeepSeek,仅保留文字页")
|
|
886
|
+
return _join_pages(slots), False
|
|
887
|
+
|
|
888
|
+
started = time.perf_counter()
|
|
889
|
+
with tempfile.TemporaryDirectory(prefix="deepseek-file-") as raw_dir:
|
|
890
|
+
rendered = _render_pdf(src, Path(raw_dir), pref, pages=ocr_indexes)
|
|
891
|
+
rendered_by_index = dict(zip(ocr_indexes, rendered, strict=True))
|
|
892
|
+
parts: list[str] = []
|
|
893
|
+
pending: list[list[Path]] = []
|
|
894
|
+
|
|
895
|
+
def flush() -> None:
|
|
896
|
+
if not pending:
|
|
897
|
+
return
|
|
898
|
+
text = _recognize_groups(client, model, list(pending), pref)
|
|
899
|
+
pending.clear()
|
|
900
|
+
if text:
|
|
901
|
+
parts.append(text)
|
|
902
|
+
|
|
903
|
+
for index, slot in enumerate(slots):
|
|
904
|
+
if slot is None:
|
|
905
|
+
pending.append(rendered_by_index[index])
|
|
906
|
+
continue
|
|
907
|
+
flush()
|
|
908
|
+
if slot.strip():
|
|
909
|
+
parts.append(slot.strip())
|
|
910
|
+
flush()
|
|
911
|
+
merged = _join_texts(parts)
|
|
912
|
+
elapsed = time.perf_counter() - started
|
|
913
|
+
if merged:
|
|
914
|
+
logger.info(
|
|
915
|
+
f"{pref}PDF 解析完成 {src.name},{len(merged)} 字,耗时 {elapsed:.1f}s"
|
|
916
|
+
)
|
|
917
|
+
else:
|
|
918
|
+
logger.warning(f"{pref}PDF 未解析出内容: {src.name}")
|
|
919
|
+
return merged, True
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
def _recognize_image_files(
|
|
923
|
+
sources: list[Path],
|
|
924
|
+
pref: str,
|
|
925
|
+
client: OpenAI,
|
|
926
|
+
model: str,
|
|
927
|
+
) -> str:
|
|
928
|
+
"""识别本地图片。超过入模像素上限的先切开,再按顺序拼接各段结果。"""
|
|
929
|
+
if not sources:
|
|
930
|
+
return ""
|
|
931
|
+
label = _inputs_label(sources)
|
|
932
|
+
if not llm_enabled():
|
|
933
|
+
logger.info(f"{pref}LLM_ENABLED=False,跳过 DeepSeek 识别")
|
|
934
|
+
return ""
|
|
935
|
+
|
|
936
|
+
started = time.perf_counter()
|
|
937
|
+
try:
|
|
938
|
+
with tempfile.TemporaryDirectory(prefix="deepseek-file-") as raw_dir:
|
|
939
|
+
dest = Path(raw_dir)
|
|
940
|
+
prepared = [
|
|
941
|
+
_prepare_one_image(src, dest, pref, index)
|
|
942
|
+
for index, src in enumerate(sources)
|
|
943
|
+
]
|
|
944
|
+
text = _recognize_groups(client, model, prepared, pref)
|
|
945
|
+
elapsed = time.perf_counter() - started
|
|
946
|
+
if text:
|
|
947
|
+
logger.info(
|
|
948
|
+
f"{pref}DeepSeek 识别完成 {label},{len(text)} 字,耗时 {elapsed:.1f}s"
|
|
949
|
+
)
|
|
950
|
+
else:
|
|
951
|
+
logger.warning(f"{pref}DeepSeek 未返回可识别内容: {label}")
|
|
952
|
+
return text
|
|
953
|
+
except Exception as exc:
|
|
954
|
+
logger.error(f"{pref}DeepSeek 识别失败 {label}: {exc}")
|
|
955
|
+
raise
|
ds_ocr/sniff.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""根据文件头判断图片或 PDF 扩展名。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def sniff_suffix(data: bytes) -> str | None:
|
|
7
|
+
"""根据文件头推断扩展名;无法识别时返回 None。"""
|
|
8
|
+
if not data or len(data) < 4:
|
|
9
|
+
return None
|
|
10
|
+
head = data[:16]
|
|
11
|
+
if head.startswith(b"%PDF"):
|
|
12
|
+
return ".pdf"
|
|
13
|
+
if head.startswith(b"\x89PNG\r\n\x1a\n"):
|
|
14
|
+
return ".png"
|
|
15
|
+
if head[:3] == b"\xff\xd8\xff":
|
|
16
|
+
return ".jpg"
|
|
17
|
+
if head.startswith(b"GIF87a") or head.startswith(b"GIF89a"):
|
|
18
|
+
return ".gif"
|
|
19
|
+
if head.startswith(b"BM"):
|
|
20
|
+
return ".bmp"
|
|
21
|
+
if len(data) >= 12 and head.startswith(b"RIFF") and data[8:12] == b"WEBP":
|
|
22
|
+
return ".webp"
|
|
23
|
+
if head[:4] in (b"II*\x00", b"MM\x00*"):
|
|
24
|
+
return ".tif"
|
|
25
|
+
return None
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: ds-ocr
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 用 DeepSeek 视觉模型把本地图片和 PDF 识别成 Markdown
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: deepseek,markdown,ocr,pdf
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Requires-Python: >=3.10
|
|
11
|
+
Requires-Dist: loguru>=0.7
|
|
12
|
+
Requires-Dist: openai>=1.40
|
|
13
|
+
Requires-Dist: pdf-inspector>=1.25
|
|
14
|
+
Requires-Dist: pillow>=10
|
|
15
|
+
Requires-Dist: pypdfium2>=4
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# ds-ocr
|
|
19
|
+
|
|
20
|
+
用 DeepSeek 视觉模型识别本地图片或 PDF,返回 Markdown。
|
|
21
|
+
|
|
22
|
+
文字型 PDF 由 [pdf-inspector](https://pypi.org/project/pdf-inspector/) 直接抽取;扫描页和图片交给 DeepSeek。超过入模像素上限的图会先切开,同一张图的切片放在同一次请求里。多张图、多页扫描并行识别,结果按原顺序拼接。
|
|
23
|
+
|
|
24
|
+
## 安装
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install ds-ocr
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## 调用时配置
|
|
31
|
+
|
|
32
|
+
创建一个 `OCR` 时传入密钥、接口地址和模型名,之后多次识别不用再传。包不会读取环境变量或 `.env`,也没有内置地址。
|
|
33
|
+
|
|
34
|
+
可选环境变量只控制本地行为:
|
|
35
|
+
|
|
36
|
+
| 变量 | 说明 |
|
|
37
|
+
| --- | --- |
|
|
38
|
+
| `LLM_ENABLED` | `false` 时不请求模型。图片返回空字符串,PDF 只保留文字页 |
|
|
39
|
+
| `LLM_TIMEOUT_S` | 单次请求超时秒数,默认 `180` |
|
|
40
|
+
| `PDF_OCR_MAX_PAGES` | 含待识别页的 PDF 最大页数,默认 `50`。超过则抛 `PdfPageLimitError`,不上传 |
|
|
41
|
+
|
|
42
|
+
## 作为库使用
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from ds_ocr import OCR, PdfPageLimitError
|
|
46
|
+
|
|
47
|
+
ocr = OCR(
|
|
48
|
+
api_key="你的密钥",
|
|
49
|
+
base_url="https://api.example.com",
|
|
50
|
+
model="你的模型名",
|
|
51
|
+
)
|
|
52
|
+
text = ocr.recognize("scan.pdf")
|
|
53
|
+
text = ocr.recognize(["a.png", "b.pdf"])
|
|
54
|
+
|
|
55
|
+
flags: dict[str, bool] = {}
|
|
56
|
+
try:
|
|
57
|
+
text = ocr.recognize("mixed.pdf", flags=flags)
|
|
58
|
+
except PdfPageLimitError:
|
|
59
|
+
raise
|
|
60
|
+
print(flags.get("from_ocr")) # 本轮是否调用了视觉模型
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`recognize` 只返回正文,不附加说明性前缀。
|
|
64
|
+
|
|
65
|
+
## 命令行
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
ds-ocr scan.pdf --api-key "$API_KEY" --base-url "$BASE_URL" --model "$MODEL"
|
|
69
|
+
ds-ocr a.png b.pdf --api-key "$API_KEY" --base-url "$BASE_URL" --model "$MODEL" -o out.md
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
页数超过上限时退出码为 2。
|
|
73
|
+
|
|
74
|
+
## 支持的文件
|
|
75
|
+
|
|
76
|
+
png、jpg、gif、webp、bmp、tif,以及 PDF。类型按文件头判断,不依赖扩展名是否写对。
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
ds_ocr/__init__.py,sha256=Yn4F-kzt8W92KIRCRMXEadImex5LdHsb3yk1NY8mRWo,337
|
|
2
|
+
ds_ocr/__main__.py,sha256=W42mwn3jvv5arj5Al9i41eAjy1jCIDOUHfz8r-7EdMI,67
|
|
3
|
+
ds_ocr/cli.py,sha256=h9S7FkvWGw9LSiBDVMV1zG8vsfFiAj2Eqni77hencHk,1395
|
|
4
|
+
ds_ocr/config.py,sha256=-rY7Bq52qOeljnVZNH2IsVh5mAas-DEw_gT_xIlqiuo,1223
|
|
5
|
+
ds_ocr/ocr.py,sha256=xzsj-kKX20nNw2inSrwJ5RwClwoL3gjAyg_2yKcBD6k,32548
|
|
6
|
+
ds_ocr/sniff.py,sha256=L_BepgI2lmlzQX9UYDa5DqaYQ7eWgPCAdyIbsTlwTQg,791
|
|
7
|
+
ds_ocr-0.1.0.dist-info/METADATA,sha256=-qypn5WwDgBSqbFG9xqHExCV4d9cZwDX8KcWXosiBHs,2344
|
|
8
|
+
ds_ocr-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
9
|
+
ds_ocr-0.1.0.dist-info/entry_points.txt,sha256=OiT9M96wBiD63Q7xJNtHn_QfYbGU-xGXhjHbpJhQN4s,43
|
|
10
|
+
ds_ocr-0.1.0.dist-info/licenses/LICENSE,sha256=gklisbc4sibBYT3DJTAxfiwY1OT26vfYSMf_uE4UadM,1076
|
|
11
|
+
ds_ocr-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ds-ocr contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|