Files
rapidocr/assets/RapidOCRDemo.ipynb
2025-06-20 08:09:18 +08:00

149 KiB

RapidOCR Python Demo

Install rapidocr

In [1]:
!pip install rapidocr onnxruntime
Collecting rapidocr
  Downloading rapidocr-3.2.0-py3-none-any.whl.metadata (1.2 kB)
Collecting onnxruntime
  Downloading onnxruntime-1.22.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.metadata (4.5 kB)
Collecting pyclipper>=1.2.0 (from rapidocr)
  Downloading pyclipper-1.3.0.post6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (9.0 kB)
Requirement already satisfied: opencv-python>=4.5.1.48 in /usr/local/lib/python3.11/dist-packages (from rapidocr) (4.11.0.86)
Requirement already satisfied: numpy<3.0.0,>=1.19.5 in /usr/local/lib/python3.11/dist-packages (from rapidocr) (2.0.2)
Requirement already satisfied: six>=1.15.0 in /usr/local/lib/python3.11/dist-packages (from rapidocr) (1.17.0)
Requirement already satisfied: Shapely!=2.0.4,>=1.7.1 in /usr/local/lib/python3.11/dist-packages (from rapidocr) (2.1.1)
Requirement already satisfied: PyYAML in /usr/local/lib/python3.11/dist-packages (from rapidocr) (6.0.2)
Requirement already satisfied: Pillow in /usr/local/lib/python3.11/dist-packages (from rapidocr) (11.2.1)
Requirement already satisfied: tqdm in /usr/local/lib/python3.11/dist-packages (from rapidocr) (4.67.1)
Requirement already satisfied: omegaconf in /usr/local/lib/python3.11/dist-packages (from rapidocr) (2.3.0)
Requirement already satisfied: requests in /usr/local/lib/python3.11/dist-packages (from rapidocr) (2.32.3)
Collecting colorlog (from rapidocr)
  Downloading colorlog-6.9.0-py3-none-any.whl.metadata (10 kB)
Collecting coloredlogs (from onnxruntime)
  Downloading coloredlogs-15.0.1-py2.py3-none-any.whl.metadata (12 kB)
Requirement already satisfied: flatbuffers in /usr/local/lib/python3.11/dist-packages (from onnxruntime) (25.2.10)
Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from onnxruntime) (24.2)
Requirement already satisfied: protobuf in /usr/local/lib/python3.11/dist-packages (from onnxruntime) (5.29.5)
Requirement already satisfied: sympy in /usr/local/lib/python3.11/dist-packages (from onnxruntime) (1.13.1)
Collecting humanfriendly>=9.1 (from coloredlogs->onnxruntime)
  Downloading humanfriendly-10.0-py2.py3-none-any.whl.metadata (9.2 kB)
Requirement already satisfied: antlr4-python3-runtime==4.9.* in /usr/local/lib/python3.11/dist-packages (from omegaconf->rapidocr) (4.9.3)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests->rapidocr) (3.4.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests->rapidocr) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests->rapidocr) (2.4.0)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests->rapidocr) (2025.6.15)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.11/dist-packages (from sympy->onnxruntime) (1.3.0)
Downloading rapidocr-3.2.0-py3-none-any.whl (15.0 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 15.0/15.0 MB 57.3 MB/s eta 0:00:00
[?25hDownloading onnxruntime-1.22.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (16.4 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 16.4/16.4 MB 50.3 MB/s eta 0:00:00
[?25hDownloading pyclipper-1.3.0.post6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (969 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 969.6/969.6 kB 36.2 MB/s eta 0:00:00
[?25hDownloading coloredlogs-15.0.1-py2.py3-none-any.whl (46 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 46.0/46.0 kB 4.0 MB/s eta 0:00:00
[?25hDownloading colorlog-6.9.0-py3-none-any.whl (11 kB)
Downloading humanfriendly-10.0-py2.py3-none-any.whl (86 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 86.8/86.8 kB 4.7 MB/s eta 0:00:00
[?25hInstalling collected packages: pyclipper, humanfriendly, colorlog, rapidocr, coloredlogs, onnxruntime
Successfully installed coloredlogs-15.0.1 colorlog-6.9.0 humanfriendly-10.0 onnxruntime-1.22.0 pyclipper-1.3.0.post6 rapidocr-3.2.0

Run

In [2]:
from rapidocr import RapidOCR

engine = RapidOCR()

img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516"
result = engine(img_url, return_word_box=True, return_single_char_box=True)
print(result)

result.vis("vis_result.jpg")
Out [2]:
[INFO] 2025-06-19 13:01:55,418 [RapidOCR] base.py:24: Using engine_name: onnxruntime
[INFO] 2025-06-19 13:01:55,466 [RapidOCR] download_file.py:56: File exists and is valid: /usr/local/lib/python3.11/dist-packages/rapidocr/models/ch_PP-OCRv4_det_infer.onnx
[INFO] 2025-06-19 13:01:55,467 [RapidOCR] main.py:55: Using /usr/local/lib/python3.11/dist-packages/rapidocr/models/ch_PP-OCRv4_det_infer.onnx
[INFO] 2025-06-19 13:01:55,562 [RapidOCR] base.py:24: Using engine_name: onnxruntime
[INFO] 2025-06-19 13:01:55,568 [RapidOCR] download_file.py:56: File exists and is valid: /usr/local/lib/python3.11/dist-packages/rapidocr/models/ch_ppocr_mobile_v2.0_cls_infer.onnx
[INFO] 2025-06-19 13:01:55,570 [RapidOCR] main.py:55: Using /usr/local/lib/python3.11/dist-packages/rapidocr/models/ch_ppocr_mobile_v2.0_cls_infer.onnx
[INFO] 2025-06-19 13:01:55,617 [RapidOCR] base.py:24: Using engine_name: onnxruntime
[INFO] 2025-06-19 13:01:55,653 [RapidOCR] download_file.py:56: File exists and is valid: /usr/local/lib/python3.11/dist-packages/rapidocr/models/ch_PP-OCRv4_rec_infer.onnx
[INFO] 2025-06-19 13:01:55,655 [RapidOCR] main.py:55: Using /usr/local/lib/python3.11/dist-packages/rapidocr/models/ch_PP-OCRv4_rec_infer.onnx
[INFO] 2025-06-19 13:01:58,410 [RapidOCR] download_file.py:64: Initiating download: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.1.0/resources/fonts/FZYTK.TTF
RapidOCROutput(img=array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [254, 254, 254],
        [254, 254, 254],
        [254, 254, 254]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [254, 254, 254],
        [254, 254, 254],
        [254, 254, 254]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [254, 254, 254],
        [254, 254, 254],
        [254, 254, 254]]], dtype=uint8), boxes=array([[[ 71., 363.],
        [419., 363.],
        [419., 382.],
        [ 71., 382.]],

       [[ 71., 392.],
        [410., 392.],
        [410., 412.],
        [ 71., 412.]],

       [[185., 466.],
        [317., 466.],
        [317., 488.],
        [185., 488.]]], dtype=float32), txts=('曲曲折折的荷塘上面,弥望的是田田的叶', '子。叶子出水很高,像亭亭的舞女的裙。', '—《荷塘月色》一'), scores=(0.99052, 0.99123, 0.89048), word_results=((('曲', 0.99946, [[71, 363], [86, 363], [86, 382], [71, 382]]), ('曲', 0.99871, [[89, 363], [105, 363], [105, 382], [89, 382]]), ('折', 0.99464, [[108, 363], [124, 363], [124, 382], [108, 382]]), ('折', 0.99041, [[131, 363], [146, 363], [146, 382], [131, 382]]), ('的', 0.99863, [[146, 363], [162, 363], [162, 382], [146, 382]]), ('荷', 0.9973, [[169, 363], [184, 363], [184, 382], [169, 382]]), ('塘', 0.99974, [[188, 363], [203, 363], [203, 382], [188, 382]]), ('上', 0.99861, [[207, 363], [222, 363], [222, 382], [207, 382]]), ('面', 0.9998, [[226, 363], [241, 363], [241, 382], [226, 382]]), (',', 0.99184, [[241, 363], [257, 363], [257, 382], [241, 382]]), ('弥', 0.97042, [[263, 363], [279, 363], [279, 382], [263, 382]]), ('望', 0.99984, [[282, 363], [298, 363], [298, 382], [282, 382]]), ('的', 0.99935, [[305, 363], [320, 363], [320, 382], [305, 382]]), ('是', 0.99761, [[320, 363], [336, 363], [336, 382], [320, 382]]), ('田', 0.91549, [[343, 363], [358, 363], [358, 382], [343, 382]]), ('田', 0.98056, [[362, 363], [377, 363], [377, 382], [362, 382]]), ('的', 0.99915, [[381, 363], [396, 363], [396, 382], [381, 382]]), ('叶', 0.99785, [[400, 363], [415, 363], [415, 382], [400, 382]])), (('子', 0.97145, [[71, 392], [85, 392], [85, 412], [71, 412]]), ('。', 0.99549, [[85, 392], [100, 392], [100, 412], [85, 412]]), ('叶', 0.9971, [[107, 392], [124, 392], [124, 412], [107, 412]]), ('子', 0.9842, [[127, 392], [144, 392], [144, 412], [127, 412]]), ('出', 0.99946, [[147, 392], [164, 392], [164, 412], [147, 412]]), ('水', 0.99925, [[167, 392], [184, 392], [184, 412], [167, 412]]), ('很', 0.99851, [[187, 392], [204, 392], [204, 412], [187, 412]]), ('高', 0.99839, [[207, 392], [222, 392], [222, 412], [207, 412]]), (',', 0.99541, [[222, 392], [237, 392], [237, 412], [222, 412]]), ('像', 0.99658, [[244, 392], [260, 392], [260, 412], [244, 412]]), ('亭', 0.99799, [[264, 392], [280, 392], [280, 412], [264, 412]]), ('亭', 0.99763, [[284, 392], [300, 392], [300, 412], [284, 412]]), ('的', 0.9994, [[304, 392], [320, 392], [320, 412], [304, 412]]), ('舞', 0.99999, [[324, 392], [340, 392], [340, 412], [324, 412]]), ('女', 0.99948, [[340, 392], [357, 392], [357, 412], [340, 412]]), ('的', 0.99899, [[360, 392], [377, 392], [377, 412], [360, 412]]), ('裙', 0.91906, [[380, 392], [397, 392], [397, 412], [380, 412]]), ('。', 0.99379, [[397, 392], [410, 392], [410, 412], [397, 412]])), (('—', 0.4499, [[185, 466], [199, 466], [199, 488], [185, 488]]), ('《', 0.99835, [[199, 466], [215, 466], [215, 488], [199, 488]]), ('荷', 0.99948, [[215, 466], [232, 466], [232, 488], [215, 488]]), ('塘', 0.99998, [[232, 466], [248, 466], [248, 488], [232, 488]]), ('月', 0.99904, [[248, 466], [265, 466], [265, 488], [248, 488]]), ('色', 0.99988, [[265, 466], [281, 466], [281, 488], [265, 488]]), ('》', 0.99965, [[281, 466], [298, 466], [298, 488], [281, 488]]), ('一', 0.67754, [[298, 466], [316, 466], [316, 488], [298, 488]]))), elapse_list=[0.4662188659999913, 0.0055959224700927734, 0.36265224000001695], elapse=0.834467028470101, lang_type=<LangRec.CH: 'ch'>)
[INFO] 2025-06-19 13:02:01,302 [RapidOCR] download_file.py:78: Download size: 3.09MB
[INFO] 2025-06-19 13:02:04,440 [RapidOCR] download_file.py:91: Successfully saved to: /usr/local/lib/python3.11/dist-packages/rapidocr/models/FZYTK.TTF
[INFO] 2025-06-19 13:02:04,485 [RapidOCR] output.py:79: Single word visualization saved as vis_result.jpg
ndarray (516, 1000, 3) show data
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [ ]: