跳转至

安装及使用

PyPI

Note

OCR API的输出结果为最原始结果,大家可按需进一步扩展。

简介

  • 该包是将rapidocr_onnxruntime库做了API封装,采用FastAPI + uvicorn实现。
  • 定位是一个快速调用rapidocr_onnxruntime的API接口,没有考虑多进程处理并发请求,如果有这需求的小伙伴,可以看看gunicorn等。

安装

pip install rapidocr_api

启动服务端

  • 用法:

    1
    2
    3
    4
    5
    6
    7
    $ rapidocr_api -h
    usage: rapidocr_api [-h] [-ip IP] [-p PORT]
    
    optional arguments:
    -h, --help            show this help message and exit
    -ip IP, --ip IP       IP Address
    -p PORT, --port PORT  IP port
    
  • 启动:

    rapidocr_api -ip 0.0.0.0 -p 9003
    

调用

Info

调用本质就是发送一个POST请求,以下给出Curl和Python的调用示例,其他编程语言同理。

Curl调用

curl -F image_file=@1.png http://0.0.0.0:9003/ocr

Python调用

import requests

url = 'http://localhost:9003/ocr'
img_path = 'tests/test_files/ch_en_num.jpg'

with open(img_path, 'rb') as f:
    file_dict = {'image_file': (img_path, f, 'image/png')}
    response = requests.post(url, files=file_dict, timeout=60)

print(response.json())
import base64
import requests

url = 'http://localhost:9003/ocr'
img_path = 'tests/test_files/ch_en_num.jpg'

with open(img_path, 'rb') as fa:
    img_str = base64.b64encode(fa.read())

payload = {'image_data': img_str}
response = requests.post(url, data=payload, timeout=60)

print(response.json())

API输出

  • 输出结果说明:

    • 如果图像中存在文字,则会输出字典格式,具体介绍如下:

      {
      "0": {
          "rec_txt": "香港深圳抽血,",  # 识别的文本
          "dt_boxes": [  # 依次为左上角 → 右上角 → 右下角 → 左下角
              [265, 18],
              [472, 231],
              [431, 271],
              [223, 59]
          ],
          "score": "0.8176"  # 置信度
          }
      }
      
    • 如果没有检测到文字,则会输出空字典({})。

    • 示例结果:
    {
        "0": {
            "rec_txt": "8月26日!",
            "dt_boxes": [
                [333.0, 72.0],
                [545.0, 40.0],
                [552.0, 90.0],
                [341.0, 122.0]
            ],
            "score": "0.7342"
        },
        "1": {
            "rec_txt": "澳洲名校招生信息",
            "dt_boxes": [
                [266.0, 163.0],
                [612.0, 116.0],
                [619.0, 163.0],
                [272.0, 210.0]
            ],
            "score": "0.8262"
        },
        "2": {
            "rec_txt": "解读!!",
            "dt_boxes": [
                [341.0, 187.0],
                [595.0, 179.0],
                [598.0, 288.0],
                [344.0, 296.0]
            ],
            "score": "0.6152"
        },
        "3": {
            "rec_txt": "Rules...",
            "dt_boxes": [
                [446.0, 321.0],
                [560.0, 326.0],
                [559.0, 352.0],
                [445.0, 347.0]
            ],
            "score": "0.8704"
        }
    }
    

评论