72 lines
1.2 KiB
Markdown
72 lines
1.2 KiB
Markdown
|
|
# fastapi-vision-ocr
|
|||
|
|
|
|||
|
|
一个更轻的 macOS OCR API 示例:
|
|||
|
|
|
|||
|
|
- `FastAPI` 提供 HTTP 接口
|
|||
|
|
- `pyobjc-framework-Vision` 直接调用 `VNRecognizeTextRequest`
|
|||
|
|
- 接收 `multipart/form-data` 图片上传
|
|||
|
|
|
|||
|
|
## 要求
|
|||
|
|
|
|||
|
|
- macOS
|
|||
|
|
- Python 3.11+
|
|||
|
|
- Xcode Command Line Tools
|
|||
|
|
|
|||
|
|
## 安装
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
cd /Users/amy/Documents/workspace/ecommer-codebase/fastapi-vision-ocr
|
|||
|
|
python3 -m venv .venv
|
|||
|
|
source .venv/bin/activate
|
|||
|
|
pip install -r requirements.txt
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 启动
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
cd /Users/amy/Documents/workspace/ecommer-codebase/fastapi-vision-ocr
|
|||
|
|
source .venv/bin/activate
|
|||
|
|
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 测试
|
|||
|
|
|
|||
|
|
健康检查:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
curl http://127.0.0.1:8000/health
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
OCR:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
curl -X POST http://127.0.0.1:8000/ocr \
|
|||
|
|
-F "image=@/absolute/path/to/test.png" \
|
|||
|
|
-F "recognition_level=accurate" \
|
|||
|
|
-F "languages=zh-Hans" \
|
|||
|
|
-F "languages=en-US"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
返回:
|
|||
|
|
|
|||
|
|
```json
|
|||
|
|
{
|
|||
|
|
"text": "hello\nworld",
|
|||
|
|
"lines": [
|
|||
|
|
{
|
|||
|
|
"text": "hello",
|
|||
|
|
"confidence": 0.99
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"text": "world",
|
|||
|
|
"confidence": 0.98
|
|||
|
|
}
|
|||
|
|
]
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 说明
|
|||
|
|
|
|||
|
|
- 这个服务只适合跑在 macOS,因为底层依赖 Apple Vision.framework。
|
|||
|
|
- 这是 MVP 方案,目标是先把“上传图片返回文本”快速跑通。
|