Files
ocr-server/scripts/api-client.sh
T
zimoyin ec6b9c7ae3 feat: 新增 bat/sh 版 API 客户端,修复 bat 吞退出码与 CRLF 问题
新增 scripts/api-client.bat 与 api-client.sh:上传图片 -> 轮询 -> 打印识别文本与
抽取字段。自动挑解释器(优先 .venv,退回系统 Python);api_client.py 只用标准库,
所以一台只装 Python、不装本项目依赖的机器也能当客户端用。

修掉两个 bat 上的真 bug(都是实测出来的,不是看代码看出来的):

1. 退出码被吞。`if exist ... ( ... exit /b %errorlevel% )` 里的 %errorlevel%
   是**解析时**展开的,取到的是启动程序之前的值,于是失败也返回 0。
   start.bat 同样中招(启动失败会被吞成成功)。两处都改成 goto 结构。

2. .bat 必须 CRLF。同一份内容,CRLF 正常跑完;LF 会让 cmd 解析挂住——
   无任何输出、也不退出,看起来像服务卡死。download-models.bat 原本也是 LF,
   一并修正;新增 .gitattributes 钉死 *.bat=eol:crlf、*.sh=eol:lf。

另:参数校验挪到探活之前,参数写错时不会因为连不上服务报出误导性错误;
stdout 加 flush,重定向到文件时输出顺序不再错乱。

测试:新增两条回归用例(bat 必须 CRLF;Windows 上 bat 能被 cmd 跑通,带超时
以免坏掉时把测试卡死)。99 passed。

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-14 12:15:39 +08:00

27 lines
1.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 命令行调用 OCR 接口:上传图片 -> 轮询 -> 打印识别文本与抽取字段。
#
# 用法:
# ./scripts/api-client.sh 图片.png
# ./scripts/api-client.sh 图片.png --min-confidence 0.6
# ./scripts/api-client.sh 图片.png --host http://192.168.1.10:8000
# ./scripts/api-client.sh --health
#
# 服务默认在 http://127.0.0.1:8000。完整参数见 scripts/api_client.py --help
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."
# 优先用 .venv 里的解释器:不经过 uv,也就不会触发环境同步。
# (uv run 会按 pyproject.toml 同步环境,而 paddle 不在依赖里,会被整个移除)
if [[ -x .venv/bin/python ]]; then
exec .venv/bin/python scripts/api_client.py "$@"
fi
# 退回系统 Python:api_client.py 只用标准库,任何 Python 3.8+ 都能跑。
if command -v python3 >/dev/null 2>&1; then
exec python3 scripts/api_client.py "$@"
fi
printf '%s\n' '[错误] 找不到 Python 解释器。请先运行 scripts/switch_env.py --cpu 建好 .venv,或安装 Python 3.8+。' >&2
exit 1