基于 Flask 的 OCR(光学字符识别)服务,支持图片和 PDF 文档的文字识别。
- 支持图片格式(JPG, PNG 等)
- 支持 PDF 文档(多页识别)
- 使用 Tesseract OCR 引擎
- 支持中文简体识别
- REST API 接口
- Python 3.7+
- Tesseract OCR 引擎(需单独安装)
pip install -r requirements.txtWindows:
- 下载安装:https://github.com/UB-Mannheim/tesseract/wiki
- 安装后可能需要在代码中设置路径:
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
Linux (Ubuntu/Debian):
sudo apt-get install tesseract-ocr
sudo apt-get install tesseract-ocr-chi-simmacOS:
brew install tesseract
brew install tesseract-lang前台运行:
python3 main.py后台运行:
nohup python3 main.py > app.log 2>&1 &查看运行状态:
ps aux | grep main.py查看日志:
tail -f app.log停止服务:
pkill -f "python3 main.py"服务将在 http://0.0.0.0:8090 启动
POST /api/ocr
上传图片或 PDF 文件进行 OCR 识别。
请求示例:
curl -X POST -F "file=@test.jpg" http://localhost:8090/api/ocr响应示例:
{
"pages": [
{
"text": "识别到的文字内容"
}
]
}Python 请求示例:
import requests
with open('test.jpg', 'rb') as f:
response = requests.post('http://localhost:8090/api/ocr', files={'file': f})
print(response.json())ocr_service/
├── main.py # 主程序
├── requirements.txt # Python 依赖
├── .gitignore # Git 忽略文件
└── README.md # 项目说明
- Tesseract OCR 需要单独安装
- 中文识别需要下载中文语言包(chi_sim)
- PDF 处理需要安装 poppler(Windows 需要添加到 PATH)