ds-ocr
用 DeepSeek 视觉模型识别本地图片或 PDF,返回 Markdown。
文字型 PDF 由 LiteParse 直接抽取;扫描页和图片交给 DeepSeek。超过所选切片档位的图会先切开,同一张图的切片放在同一次请求里。多张图、多页扫描并行识别,结果按原顺序拼接。
安装
pip install ds-ocr
调用时配置
创建一个 OCR 时传入密钥、接口地址和模型名,之后多次识别不用再传。包不会读取环境变量或 .env,也没有内置地址。
可选环境变量只控制本地行为:
| 变量 | 说明 |
|---|---|
LLM_ENABLED |
false 时不请求模型。图片返回空字符串,PDF 只保留文字页 |
LLM_TIMEOUT_S |
单次请求超时秒数,默认 180 |
PDF_OCR_MAX_PAGES |
含待识别页的 PDF 最大页数,默认 50。超过则抛 PdfPageLimitError,不上传 |
作为库使用
from ds_ocr import OCR, PdfPageLimitError
ocr = OCR(
api_key="你的密钥",
base_url="https://api.example.com",
model="你的模型名",
)
text = ocr.recognize("scan.pdf")
text = ocr.recognize(["a.png", "b.pdf"])
text = ocr.recognize("scan.png", tile_edge=800)
flags: dict[str, bool] = {}
try:
text = ocr.recognize("mixed.pdf", flags=flags)
except PdfPageLimitError:
raise
print(flags.get("from_ocr")) # 本轮是否调用了视觉模型
recognize 只返回正文,不附加说明性前缀。
tile_edge 选择切片档位,只能是 1300(默认)、1000 或 800。总像素超过该档(边长的平方)才切开。DeepSeek 只把总像素低于大约 544×544 的图放大,不会把 1000 和 800 拉到 1300,因此切片在送出前按比例放大到总像素约 1300×1300。
命令行
ds-ocr scan.pdf --api-key "$API_KEY" --base-url "$BASE_URL" --model "$MODEL"
ds-ocr a.png b.pdf --api-key "$API_KEY" --base-url "$BASE_URL" --model "$MODEL" -o out.md
ds-ocr scan.png --api-key "$API_KEY" --base-url "$BASE_URL" --model "$MODEL" --tile-edge 800
页数超过上限时退出码为 2。
支持的文件
png、jpg、gif、webp、bmp、tif,以及 PDF。类型按文件头判断,不依赖扩展名是否写对。
Metadata
Release files for ds-ocr 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ds_ocr-0.3.0.tar.gz | 89.2 kB | Without provenance | Details for ds_ocr-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ds_ocr-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for ds_ocr-0.3.0-py3-none-any.whl |
Total release size: 107.0 kB
Release files / ds_ocr-0.3.0.tar.gz
| Download URL | ds_ocr-0.3.0.tar.gz |
|---|---|
| Size | 89.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
267f91cc8794020910c0b065098982fc9547dc4c4ac7b5e6c8f5ffcc486bd905
|
|
BLAKE2b-256 checksum How to use checksums |
b0286b403125ff115966366b687c9fb49a874a2f42d31fb032adaabcae6f6b83
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / ds_ocr-0.3.0-py3-none-any.whl
| Download URL | ds_ocr-0.3.0-py3-none-any.whl |
|---|---|
| Size | 17.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
67193ee80451d7a8bbd2af19841b67b80576390cc1f480bbfe171ef91526f919
|
|
BLAKE2b-256 checksum How to use checksums |
9acc5ff0a22d2a420174ba51d6c25eeddf0765df41214b613accee19d1bd77eb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for ds-ocr 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.