Skip to content

Installation & Usage

Shows an illustrated sun in light mode and a moon with stars in dark mode.
PyPI SemVer2.0

📖 Introduction

Note

The output of the OCR API is the most raw result; you can extend it further as needed.

Source repository: https://github.com/RapidAI/RapidOCRAPI

This package wraps the rapidocr library into an API, implemented using FastAPI + uvicorn.

It is positioned as an API interface for quickly calling rapidocr, and does not take multi-process handling of concurrent requests into account. If you have such needs, you can take a look at gunicorn and similar tools.

📌 Version Dependencies

rapidocr_api rapidocr
v0.2.x rapidocr>1.0.0,<3.0.0
v0.1.x rapidocr_onnxruntime

Installation

pip install rapidocr_api

Start the Server

In rapidocr_api>=0.1.0, model parameters can be passed through environment variables: det_model_path, cls_model_path, rec_model_path; parameters can also be passed in the interface to control whether to use the three parts of models: detection, orientation classification and recognition; for specific calls, see the documentation below.

1
2
3
set det_model_path=I:\models\图像相关\OCR\RapidOCR\PP-OCRv4\ch_PP-OCRv4_det_server_infer.onnx
set rec_model_path=I:\models\图像相关\OCR\RapidOCR\PP-OCRv4\ch_PP-OCRv4_rec_server_infer.onnx
rapidocr_api
# Start with default parameters
rapidocr_api

# Specify parameters: port and number of processes;
rapidocr_api -ip 0.0.0.0 -p 9005 -workers 2

# Specify models
export det_model_path=/mnt/sda1/models/PP-OCRv4/ch_PP-OCRv4_det_server_infer.onnx
export rec_model_path=/mnt/sda1/models/PP-OCRv4/ch_PP-OCRv4_rec_server_infer.onnx
rapidocr_api -ip 0.0.0.0 -p 9005 -workers 2

Dockerfile source

Build the image:

1
2
3
4
5
6
7
8
git clone https://github.com/RapidAI/RapidOCR.git
cd api

# Build method 1: use the host's network
docker build -t="rapidocr_api:0.1.4" --network host .

# Build method 2: use the proxy on the host
docker build -t rapidocr_api:0.1.4 --network host --build-arg HTTP_PROXY=http://127.0.0.1:8888 --build-arg HTTPS_PROXY=http://127.0.0.1:8888 .

Debug run:

docker run --rm -p 9003:9003 --name rapidocr_api -e TZ=Asia/Shanghai rapidocr_api:0.1.4

Run:

docker run -d -p 9003:9003 --name rapidocr_api -e TZ=Asia/Shanghai rapidocr_api:0.1.4

API web interface:

http://<ip>:9003/docs

How to temporarily modify and verify in Docker

docker run -p 9003:9003 --name rapidocr_api -e TZ=Asia/Shanghai rapidocr_api:0.1.4

Enter the container to modify the Python source files; it is best to add apt-get install vim to the Dockerfile for installation

1
2
3
4
5
6
docker exec -it rapidocr_api /bin/bash
cd /usr/local/lib/python3.10/site-packages/rapidocr_api
...
# Modify the parameter file
vi /usr/local/lib/python3.10/site-packages/rapidocr_onnxruntime/config.yaml
# Type exit to quit after making the changes

Restart the container

docker restart rapidocr_api

View logs:

docker logs -f rapidocr_api

Call the API

Info

Calling is essentially sending a POST request; Curl and Python call examples are given below, and other programming languages work the same way.

Curl call

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

Python call

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())
1
2
3
4
5
6
7
8
9
import requests

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

with open(img_path, 'rb') as f:
    data = {"use_det": False, "use_cls": True, "use_rec": True}
    response = requests.post(url, files=file_dict, data=data, timeout=60)
print(response.json())

Output Result Description

If there is text in the image, a dictionary format will be output, described in detail as follows:

{
 "0": {
  "rec_txt": "香港深圳抽血,", # Recognized text
  "dt_boxes": [  # In order: top-left → top-right → bottom-right → bottom-left
   [265, 18],
   [472, 231],
   [431, 271],
   [223, 59]
  ],
  "score": "0.8176"   # Confidence
 }
}

If no text is detected, an empty dictionary ({}) will be output.

Example Results

{
    "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"
    }
}

Comments