# PPOcrv6 **Repository Path**: mfcom/ppocrv6 ## Basic Information - **Project Name**: PPOcrv6 - **Description**: PPOcrv6做的ocr识别项目 - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-07-10 - **Last Updated**: 2026-07-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # PP-OCRv6 Small OCR Test API 这是一个本地测试项目:上传图片,服务端用 PaddleOCR 的 `PP-OCRv6_small_det` + `PP-OCRv6_small_rec` 识别文字并返回 JSON。 ## 1. 创建本地虚拟环境 ```powershell python -m venv .venv .\.venv\Scripts\python.exe -m pip install -U pip ``` ## 2. 安装依赖 CPU 版本: ```powershell .\.venv\Scripts\python.exe -m pip install paddlepaddle==3.2.0 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/ .\.venv\Scripts\python.exe -m pip install -r requirements.txt ``` 如果服务器有 NVIDIA GPU,部署时再按服务器 CUDA/驱动情况安装 `paddlepaddle-gpu`。 ## 3. 启动服务 本地开发建议先用前台启动,方便直接看到日志: ```powershell .\.venv\Scripts\python.exe -m uvicorn app.main:app --host 127.0.0.1 --port 8000 ``` 也可以用脚本启动: ```powershell .\.venv\Scripts\python.exe scripts\run_server.py ``` 停止时在终端按 `Ctrl+C`。如果使用后台脚本启动: ```powershell .\.venv\Scripts\python.exe scripts\start_server.py .\.venv\Scripts\python.exe scripts\stop_server.py ``` 打开: - 可视化页面:http://127.0.0.1:8000/ - Swagger 文档:http://127.0.0.1:8000/docs - ReDoc 文档:http://127.0.0.1:8000/redoc - 健康检查:http://127.0.0.1:8000/health ## 4. 调用接口 ```powershell curl.exe -X POST "http://127.0.0.1:8000/ocr" -F "file=@D:\path\to\image.jpg" ``` 返回格式示例: ```json { "model": { "ocr_version": "PP-OCRv6", "text_detection_model": "PP-OCRv6_small_det", "text_recognition_model": "PP-OCRv6_small_rec", "device": "cpu" }, "text": "识别出的完整文本", "pages": [ { "text": "当前图片文本", "lines": [ { "text": "单行文本", "score": 0.99, "box": [0, 0, 100, 30] } ] } ], "filename": "image.jpg" } ``` ## 模型切换与对比 当前支持三个 PP-OCRv6 模型档位: - `tiny`:`PP-OCRv6_tiny_det` + `PP-OCRv6_tiny_rec` - `small`:`PP-OCRv6_small_det` + `PP-OCRv6_small_rec` - `medium`:`PP-OCRv6_medium_det` + `PP-OCRv6_medium_rec` 查看模型配置: ```powershell Invoke-WebRequest http://127.0.0.1:8000/models -UseBasicParsing ``` 文件上传指定单模型: ```powershell curl.exe -X POST "http://127.0.0.1:8000/ocr?model_profile=tiny" -F "file=@D:\path\to\image.jpg" ``` 文件上传三模型对比: ```powershell curl.exe -X POST "http://127.0.0.1:8000/ocr/compare?model_profiles=tiny&model_profiles=small&model_profiles=medium" -F "file=@D:\path\to\image.jpg" ``` 图片 URL 单模型: ```powershell curl.exe -X POST "http://127.0.0.1:8000/ocr/url" -H "Content-Type: application/json" -d "{\"image_url\":\"https://example.com/image.jpg\",\"model_profile\":\"small\",\"include_raw\":false}" ``` 图片 URL 三模型对比: ```powershell curl.exe -X POST "http://127.0.0.1:8000/ocr/url/compare" -H "Content-Type: application/json" -d "{\"image_url\":\"https://example.com/image.jpg\",\"model_profiles\":[\"tiny\",\"small\",\"medium\"],\"include_raw\":false}" ``` ## 自动方向纠正 前端页面提供“自动纠正方向”开关。开启后流程为: 1. 先用 PaddleOCR 文档方向分类和文本行方向分类自动纠正。 2. 如果识别结果平均置信度低于阈值,或没有识别到文本行,则继续尝试 `0/90/180/270` 四个方向。 3. 最后按“平均置信度最高、文本行数最多、文本长度最长”的顺序选择结果。 默认低置信度阈值为: ```text 0.85 ``` 可以通过环境变量调整: ```powershell $env:OCR_AUTO_CORRECT_SCORE_THRESHOLD='0.85' ``` 接口参数: ```text auto_correct=true ``` 示例: ```powershell curl.exe -X POST "http://127.0.0.1:8000/ocr?model_profile=small&auto_correct=true" -F "file=@D:\path\to\image.jpg" ``` ## 日志 服务会把本地操作日志写入: ```text logs\ocr_api.log ``` 日志会记录访问时间、客户端 IP、请求路径、上传文件名、文件大小、是否返回 raw JSON、OCR 耗时、识别行数、文本长度和短文本预览。 查看最近日志: ```powershell Get-Content logs\ocr_api.log -Tail 50 -Encoding UTF8 ``` ## 配置 可以通过环境变量调整: - `OCR_DEVICE`:默认 `cpu`,有 GPU 可设为 `gpu:0` - `OCR_VERSION`:默认 `PP-OCRv6` - `OCR_DET_MODEL`:默认 `PP-OCRv6_small_det` - `OCR_REC_MODEL`:默认 `PP-OCRv6_small_rec`