OpenMMLab の MMOCR==1.0.0rc を試してみる

公開日:2022年10月15日
最終更新日:2022年12月8日

はじめに

2022年10月15日現在MMOCRの最新は0.6.2です。

公式ページによると1.xの公開に向けて開発を進めているとのことです。

0.xから1.xになると少なからず変化があるようです。

さっそくベータ版で試してみました。

動作環境

1

Windows 11
CUDA 11.6.2
Python 3.10.8
pip install torch==1.12.1 torchvision==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu116
pip install openmim==0.3.3
pip install mmengine==0.3.1
pip install mmcv==2.0.0rc2 -f https://download.openmmlab.com/mmcv/dist/cu116/torch1.12.0/index.html
pip install mmdet==3.0.0rc3
pip install mmocr==1.0.0rc3

2

Windows 11
CUDA 11.6.2
Python 3.10.9
pip install torch==1.13.0 torchvision==0.14.0 --extra-index-url https://download.pytorch.org/whl/cu116
pip install openmim==0.3.3
pip install mmengine==0.3.2
pip install mmcv==2.0.0rc3 -f https://download.openmmlab.com/mmcv/dist/cu116/torch1.13.0/index.html
pip install mmdet==3.0.0rc4
pip install mmocr==1.0.0rc4

Pythonスクリプト

import os
from mmocr.ocr import MMOCR
from mim.commands.download import download
from mmengine.config import Config
from torchvision.datasets.utils import download_url

os.makedirs('models', exist_ok=True)

# download sample image
img_url = 'https://github.com/open-mmlab/mmocr/raw/main/demo/demo_text_ocr.jpg'
img_fname = img_url.split('/')[-1]
download_url(img_url, root = '.', filename = img_fname)

# download dict file
dict_url = 'https://raw.githubusercontent.com/open-mmlab/mmocr/dev-1.x/dicts/english_digits_symbols.txt'
dict_fname = dict_url.split('/')[-1]
download_url(dict_url, root = 'models', filename = dict_fname)

############ Build OCR model ############
# Detection: textsnake
det_checkpoint_name = 'textsnake_resnet50_fpn-unet_1200e_ctw1500'
# Recognition: satrn
recog_checkpoint_name = 'satrn_shallow_5e_st_mj'

configs = [det_checkpoint_name, recog_checkpoint_name]
checkpoints = download(package='mmocr', configs=configs, dest_root="models")
config_paths = [os.path.join('models', x + '.py') for x in configs]
checkpoint_paths = [os.path.join('models', x) for x in checkpoints]

cfg = Config.fromfile(config_paths[1])
cfg.model.decoder.dictionary.dict_file = os.path.join('models', dict_fname)
cfg.dump(config_paths[1])

ocr_model = MMOCR(
    det_config = config_paths[0], 
    det_ckpt = checkpoint_paths[0],
    recog_config = config_paths[1], 
    recog_ckpt = checkpoint_paths[1],
    device = 'cuda'
    )
############ Build OCR model ############

results = ocr_model.readtext('demo_text_ocr.jpg', print_result=True, show=True)

結果

{
'rec_texts': ['OocbcBank', 'new', 'Nntp-Posting-Host:', '<1493TAL@randos.orgate.mi', '70%', 'ROUND', 'SALE', 'ALLYEAR', 'SALE'], 
'rec_scores': [[0.4959149658679962, 0.999910831451416, 0.9999004602432251, 0.98697829246521, 0.9958814382553101, 0.5461381077766418, 0.9999599456787109, 0.9999560117721558, 0.9999988079071045], [0.14993084967136383, 0.552449643611908, 0.9798926115036011], 
...
'det_polygons': [array([441.83694413, 355.55706522, 441.00798176, 356.38586957,
...

補足(辞書ファイルのダウンロードについて)

Recognitionモデルを使用する際に辞書ファイルのダウンロードが必要になります。
モデルによってそのファイルの種類が異なるので以下に組み合わせを載せておきます。

abinet_20e_st-an_mj

dict_url = 'https://raw.githubusercontent.com/open-mmlab/mmocr/dev-1.x/dicts/lower_english_digits.txt'
recog_checkpoint_name = 'abinet_20e_st-an_mj'

crnn_mini-vgg_5e_mj

dict_url = 'https://raw.githubusercontent.com/open-mmlab/mmocr/dev-1.x/dicts/lower_english_digits.txt'
recog_checkpoint_name = 'crnn_mini-vgg_5e_mj'

master_resnet31_12e_st_mj_sa

dict_url = 'https://raw.githubusercontent.com/open-mmlab/mmocr/dev-1.x/dicts/english_digits_symbols.txt'
recog_checkpoint_name = 'master_resnet31_12e_st_mj_sa'

nrtr_resnet31-1by8-1by4_6e_st_mj

dict_url = 'https://raw.githubusercontent.com/open-mmlab/mmocr/dev-1.x/dicts/english_digits_symbols.txt'
recog_checkpoint_name = 'nrtr_resnet31-1by8-1by4_6e_st_mj'

robustscanner_resnet31_5e_st-sub_mj-sub_sa_real

dict_url = 'https://raw.githubusercontent.com/open-mmlab/mmocr/dev-1.x/dicts/english_digits_symbols.txt'
recog_checkpoint_name = 'robustscanner_resnet31_5e_st-sub_mj-sub_sa_real'

satrn_shallow_5e_st_mj

dict_url = 'https://raw.githubusercontent.com/open-mmlab/mmocr/dev-1.x/dicts/english_digits_symbols.txt'
recog_checkpoint_name = 'satrn_shallow_5e_st_mj'