[AI PaddleOCR] PaddleOCR: Awesome multilingual Optical Character Recognition (OCR) toolkits based on PaddlePaddle

PaddleOCR

PaddleOCR aims to create multilingual, awesome, leading, and practical OCR tools that help users train better models and apply them into practice.

PaddleOCR is the awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices).

Features

  • PPOCR series of high-quality pre-trained models, comparable to commercial effects

    • Ultra lightweight ppocr_mobile series models: detection (3.0M) + direction classifier (1.4M) + recognition (5.0M) = 9.4M

    • General ppocr_server series models: detection (47.1M) + direction classifier (1.4M) + recognition (94.9M) = 143.4M

    • Support Chinese, English, and digit recognition, vertical text recognition, and long text recognition

    • Support multi-language recognition: Korean, Japanese, German, French

  • Rich toolkits related to the OCR areas

    • Semi-automatic data annotation tool, i.e., PPOCRLabel: support fast and efficient data annotation

    • Data synthesis tool, i.e., Style-Text: easy to synthesize a large number of images which are similar to the target scene image

  • Support user-defined training, provides rich predictive inference deployment solutions

  • Support PIP installation, easy to use

  • Support Linux, Windows, MacOS and other systems

Visualization

PaddleOCR Visualization

The above pictures are the visualizations of the general ppocr_server model. For more effect pictures, please see More visualizations - https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.1/doc/doc_en/visualization_en.md.

Installation

Install PaddlePaddle

PaddleOCR working environment:

  • PaddlePaddle 2.0.0

  • python3.7

  • glibc 2.23

It is recommended to use the docker provided by us to run PaddleOCR, please refer to the use of Docker - https://docs.docker.com/get-started/.

If you want to directly run the prediction code on mac or windows, you can start from step 2.

    1. (Recommended) Prepare a docker environment. The first time you use this docker image, it will be downloaded automatically. Please be patient.
1
2
3
4
5
6
7
8
# Switch to the working directory
$ cd /home/Projects

# You need to create a docker container for the first run, and do not need to run the current command when you run it again
# Create a docker container named ppocr and map the current directory to the /paddle directory of the container

#If using CPU, use docker instead of nvidia-docker to create docker
$ sudo docker run --name ppocr -v $PWD:/paddle --network=host -it paddlepaddle/paddle:latest-dev-cuda10.1-cudnn7-gcc82 /bin/bash

If using CUDA10, please run the following command to create a container. It is recommended to set a shared memory greater than or equal to 32G through the --shm-size parameter:

1
$ sudo nvidia-docker run --name ppocr -v $PWD:/paddle --shm-size=64G --network=host -it paddlepaddle/paddle:latest-dev-cuda10.1-cudnn7-gcc82 /bin/bash

You can also visit DockerHub to get the image that fits your machine.

1
2
# ctrl+P+Q to exit docker, to re-enter docker using the following command:
$ sudo docker container exec -it ppocr /bin/bash
    1. Install PaddlePaddle 2.0
1
2
3
4
5
6
7
$ pip3 install --upgrade pip

# If you have cuda9 or cuda10 installed on your machine, please run the following command to install
$ python3 -m pip install paddlepaddle-gpu==2.0.0 -i https://mirror.baidu.com/pypi/simple

# If you only have cpu on your machine, please run the following command to install
$ python3 -m pip install paddlepaddle==2.0.0 -i https://mirror.baidu.com/pypi/simple

For more software version requirements, please refer to the instructions in Installation Document for operation.

  1. Clone PaddleOCR repo
1
$ git clone https://github.com/PaddlePaddle/PaddleOCR
    1. Install third-party libraries
1
2
cd PaddleOCR
pip3 install -r requirements.txt

Install PaddleOCR

(Quickly) Install paddleocr package via pip

1
$ pip install "paddleocr>=2.0.1" # Recommend to use version 2.0.1+

Or build own whl package and install

1
2
3
python3 setup.py bdist_wheel

pip3 install dist/paddleocr-x.x.x-py3-none-any.whl # x.x.x is the version of paddleocr

Usage

The paddleocr whl package will automatically download the ppocr lightweight model as the default model, which can be customized and replaced according to the section 3 Custom Model - https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.1/doc/doc_en/whl_en.md#3-use-custom-model.

detection angle classification and recognition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from paddleocr import PaddleOCR,draw_ocr
# Paddleocr supports Chinese, English, French, German, Korean and Japanese.
# You can set the parameter `lang` as `ch`, `en`, `french`, `german`, `korean`, `japan`
# to switch the language model in order.
ocr = PaddleOCR(use_angle_cls=True, lang='en') # need to run only once to download and load model into memory
img_path = 'PaddleOCR/doc/imgs_en/img_12.jpg'
result = ocr.ocr(img_path, cls=True)
for line in result:
print(line)


# draw result
from PIL import Image
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result]
txts = [line[1][0] for line in result]
scores = [line[1][1] for line in result]
im_show = draw_ocr(image, boxes, txts, scores, font_path='/path/to/PaddleOCR/doc/fonts/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')

Output will be a list, each item contains bounding box, text and recognition confidence

1
2
3
4
[[[442.0, 173.0], [1169.0, 173.0], [1169.0, 225.0], [442.0, 225.0]], ['ACKNOWLEDGEMENTS', 0.99283075]]
[[[393.0, 340.0], [1207.0, 342.0], [1207.0, 389.0], [393.0, 387.0]], ['We would like to thank all the designers and', 0.9357758]]
[[[399.0, 398.0], [1204.0, 398.0], [1204.0, 433.0], [399.0, 433.0]], ['contributors whohave been involved in the', 0.9592447]]
......

Visualization of results

PaddlePaddle Visualization

See PaddleOCR Use - https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.1/doc/doc_en/whl_en.md#2-use to learn more usages.

FAQs

No matching distribution found for opencv-contrib-python==4.2.0.32

1
2
3
$ pip3 install -r requirements.txt
ERROR: Could not find a version that satisfies the requirement opencv-contrib-python==4.2.0.32 (from versions: 3.4.11.45, 3.4.13.47, 3.4.14.51, 3.4.14.53, 3.4.15.55, 4.4.0.46, 4.5.1.48, 4.5.2.52, 4.5.2.54, 4.5.3.56)
ERROR: No matching distribution found for opencv-contrib-python==4.2.0.32

PaddleOCR currently only supports python 3.7. Please check python version.

References

[1] PaddlePaddle/PaddleOCR: Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices) - https://github.com/PaddlePaddle/PaddleOCR

[2] PaddleOCR/installation_en.md at release/2.1 · PaddlePaddle/PaddleOCR - https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.1/doc/doc_en/installation_en.md

[3] PaddleOCR/whl_en.md at release/2.1 · PaddlePaddle/PaddleOCR - https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.1/doc/doc_en/whl_en.md