\

Pythonは、画像内のテキストを認識し抽出するための多くのライブラリを提供しています。これらのライブラリは、OCR(Optical Character Recognition)と呼ばれる技術を使用しています。今回は、Pythonで利用可能な主要なOCRライブラリについて紹介します。

PyTesseract

PyTesseractは、GoogleのTesseract-OCRエンジンのPythonラッパーです。このライブラリは、PillowとLeptonicaという画像ライブラリがサポートするすべての画像タイプを読み取ることができます。また、スクリプトとして使用すると、認識したテキストをファイルに書き込む代わりに表示します。

from PIL import Image
import pytesseract

# 画像を開いてテキストを抽出
print(pytesseract.image_to_string(Image.open('test.png')))

GCP Vision AI

GCP Vision AIは、Google Cloud Platformの一部であり、高精度なOCR機能を提供します。このサービスは、画像内のテキストを認識し抽出する能力に優れています。

from google.cloud import vision

def extract_text(image_file):
    client = vision.ImageAnnotatorClient()
    with open(image_file, "rb") as image_file:
        content = image_file.read()
    image = vision.Image(content=content)
    response = client.text_detection(image=image)
    texts = response.text_annotations
    result = texts[0].description if texts else None
    print(f'Text:  {result}')

これらのライブラリを使用することで、PythonでOCRを活用することが可能になります。それぞれのライブラリが提供する機能を理解し、適切なツールを選択することが重要です。

投稿者 admin

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です