Skip to main content

ds-ocr

用 DeepSeek 视觉模型识别本地图片或 PDF,返回 Markdown。

文字型 PDF 由 pdf-inspector 直接抽取;扫描页和图片交给 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.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ds-ocr 0.2.0
File Size Uploaded Provenance Actions
ds_ocr-0.2.0.tar.gz 87.5 kB Without provenance Details for ds_ocr-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ds-ocr 0.2.0
File Interpreter ABI Platform Actions
ds_ocr-0.2.0-py3-none-any.whl Python 3 none any Details for ds_ocr-0.2.0-py3-none-any.whl

Total release size: 104.8 kB

Release files / ds_ocr-0.2.0.tar.gz

Download URL ds_ocr-0.2.0.tar.gz
Size 87.5 kB
Tags Source
SHA-256 checksum
How to use checksums
487f5bafadd252fd34f5c01e14d31246cd909746be11ba173e18459f5c0d62f9
BLAKE2b-256 checksum
How to use checksums
b3ba9c152d612a15a49bce0d8a57f6d7641acb13988e061b33fa695cf444b875
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.2.0-py3-none-any.whl

Download URL ds_ocr-0.2.0-py3-none-any.whl
Size 17.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
98902a58804d4bc933775b7d100135383946868de2c20d45918dcd5c4c035aa9
BLAKE2b-256 checksum
How to use checksums
e1fb12e437696df92f52c2bc73d0e7dd18ad85b60d09643382d230cbaa5453fd
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.2.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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page