今回は、paddleocr利用して、OCR Demoを作成します。
開発環境
Mac OS X
Python 3.11.5
library インストール
brew install swig
仮想環境のアクティベート
% python3 -m venv venv
% . venv/bin/activate
python lib インストール
pip install paddlepaddle
pip install paddleocr
実装
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
ocr = PaddleOCR(use_angle_cls=True, use_gpu=False, lang="japan")
img_path = 'imgs/1.png'
result = ocr.ocr(img_path, cls=True)[0]
for line in result:
print(line)
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='NotoSansCJK-Regular.ttc')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')
実行
[2023/10/04 16:41:29] ppocr DEBUG: dt_boxes num : 7, elapsed : 1.001108169555664
[2023/10/04 16:41:29] ppocr DEBUG: cls num : 7, elapsed : 0.08505511283874512
[2023/10/04 16:41:31] ppocr DEBUG: rec_res num : 7, elapsed : 1.2580108642578125
[[[53.0, 144.0], [249.0, 144.0], [249.0, 165.0], [53.0, 165.0]], ('000111222-1003', 0.9989709258079529)]
[[[133.0, 254.0], [178.0, 254.0], [178.0, 281.0], [133.0, 281.0]], ('氏名', 0.9995503425598145)]
[[[245.0, 256.0], [312.0, 256.0], [312.0, 278.0], [245.0, 278.0]], ('ルキア', 0.999983549118042)]
[[[586.0, 257.0], [785.0, 257.0], [785.0, 278.0], [586.0, 278.0]], ('00年00月00日生', 0.997613251209259)]
[[[131.0, 354.0], [179.0, 354.0], [179.0, 382.0], [131.0, 382.0]], ('住所', 0.9438973069190979)]
[[[244.0, 359.0], [511.0, 359.0], [511.0, 379.0], [244.0, 379.0]], ('00県00市000-2 2 3', 0.8933746218681335)]
[[[538.0, 508.0], [564.0, 508.0], [564.0, 667.0], [538.0, 667.0]], ('運転免許証', 0.9850293397903442)]
以上です。