Files
xocr-api/README.md
2026-02-01 21:30:08 +08:00

96 lines
1.8 KiB
Markdown
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.

# XOcr Api - 文字识别 API
基于 FastAPI + Tesseract OCR 的文字识别服务,支持中英文识别和文本坐标返回。
## 环境配置
### 开发环境 (Windows)
1. 安装依赖:
```bash
pip install -r requirements.txt
```
2. 配置 `.env` 文件(已创建):
```env
TESSERACT_CMD=C:\Program Files\Tesseract-OCR\tesseract.exe
```
3. 启动服务:
```bash
uvicorn main:app --host 0.0.0.0 --port 8080 --reload
```
### 生产环境 (Docker)
```bash
# 构建镜像
docker-compose build
# 启动服务
docker-compose up -d
# 查看日志
docker-compose logs -f
```
## API 使用
### POST /ocr
上传图片进行文字识别,返回识别文本和坐标信息。
**请求:**
```bash
curl -X POST "http://localhost:8080/ocr" \
-F "file=@test_image.png"
```
**响应:**
```json
{
"text": "完整识别文本",
"details": [
{
"text": "文本内容",
"confidence": 95,
"bbox": {
"left": 10,
"top": 30,
"width": 100,
"height": 20
}
}
]
}
```
## 测试
运行测试脚本:
```bash
python test_ocr_bbox.py
```
会自动创建测试图片,调用 API并生成带有边界框的可视化结果。
## 项目结构
```
XOcr_Api/
├── main.py # FastAPI 应用
├── requirements.txt # Python 依赖
├── Dockerfile # Docker 镜像构建
├── docker-compose.yml # Docker Compose 配置
├── .env # 开发环境配置(不提交)
├── .env.example # 环境配置模板
├── .gitignore # Git 忽略文件
└── test_ocr_bbox.py # 测试脚本
```
## 配置说明
- **TESSERACT_CMD**: Tesseract OCR 可执行文件路径
- Windows: `C:\Program Files\Tesseract-OCR\tesseract.exe`
- Linux: `/usr/bin/tesseract`Docker 环境已自动配置)