4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows環境でNimを使ってOCRツールを作成する(Tesseract + Leptonica)

Posted at

1. Tesseractのインストール

1.1 インストーラのダウンロード

  1. Tesseract GitHub Release ページから最新のWindowsインストーラ(exe形式)をダウンロードします。

1.2 インストール手順

  1. ダウンロードしたexeファイルを実行。

  2. インストール手順に従い、次のポイントを確認してください:

    • インストール先ディレクトリ(例: C:\Program Files\Tesseract-OCR)。
    • 必要な言語データ(例: 日本語)を追加。
    • 「Add Tesseract to the system PATH for current user」をチェック
  3. インストールが完了したら、コマンドプロンプトで以下を実行し、インストール確認。

    tesseract --version
    

2. Leptonicaを使った画像前処理

Tesseractの精度を上げるためには、画像の前処理が重要になるので、Leptonicaを使って画像のグレースケール変換とリサイズを行います。

2.1 必要な準備

LeptonicaはTesseractに依存する画像処理ライブラリで、Tesseractインストーラに同梱されています。

leptonica.nim: Leptonica APIのバインディング
{.passL: "-llept".}  # Leptonicaライブラリをリンク

# 必要なモジュールをインポート
import os, strutils

# Leptonicaの型と関数を定義
type
  PIX* = object

proc pixRead*(filename: cstring): ptr PIX {.cdecl, importc: "pixRead", dynlib: "leptonica".}
proc pixDestroy*(ppix: ptr ptr PIX) {.cdecl, importc: "pixDestroy", dynlib: "leptonica".}
proc pixConvertTo8*(pixs: ptr PIX, cmapflag: int): ptr PIX {.cdecl, importc: "pixConvertTo8", dynlib: "leptonica".}
proc pixScale*(pixs: ptr PIX, scalex, scaley: float32): ptr PIX {.cdecl, importc: "pixScale", dynlib: "leptonica".}
proc pixWrite*(filename: cstring, pix: ptr PIX, format: int): int {.cdecl, importc: "pixWrite", dynlib: "leptonica".}

const IFF_PNG* = 3  # PNG形式で画像を保存する
image_preprocess.nim: 画像の前処理
import leptonica, os

# グレースケール変換とリサイズ
proc preprocessImage(inputPath: string, outputPath: string, scaleX: float32 = 1.0, scaleY: float32 = 1.0): bool =
  if not fileExists(inputPath):
    echo "エラー: 入力画像が存在しません: ", inputPath
    return false

  let pix = pixRead(inputPath.cstring)
  if pix.isNil:
    echo "エラー: 画像の読み込みに失敗しました。"
    return false

  let grayPix = pixConvertTo8(pix, 0)
  pixDestroy(addr pix)
  if grayPix.isNil:
    echo "エラー: グレースケール変換に失敗しました。"
    return false

  let scaledPix = pixScale(grayPix, scaleX, scaleY)
  pixDestroy(addr grayPix)
  if scaledPix.isNil:
    echo "エラー: リサイズに失敗しました。"
    return false

  let result = pixWrite(outputPath.cstring, scaledPix, IFF_PNG)
  pixDestroy(addr scaledPix)
  if result != 0:
    echo "エラー: 画像の保存に失敗しました。"
    return false

  echo "画像の前処理が完了しました: ", outputPath
  return true

3. Tesseractを使ったOCR実装

画像前処理を行った後、Tesseractを使ってOCRを実行します。

ocr_tool.nim: Tesseractの使用
import os, image_preprocess

# Tesseract APIの関数を宣言
proc tesseractOCR(imagePath: string, lang: string = "eng"): string =
  let command = fmt"tesseract {imagePath} stdout -l {lang}"
  let (output, exitCode) = execCmdEx(command)
  if exitCode != 0:
    return "エラー: OCR処理に失敗しました。"
  return output

# メイン関数
when isMainModule:
  if paramCount() < 2:
    echo "Usage: nimOCR <画像パス> <出力パス> [言語]"
    quit(1)

  let inputPath = paramStr(1)
  let outputPath = paramStr(2)
  let lang = if paramCount() > 3: paramStr(3) else "eng"

  # 前処理
  if not preprocessImage(inputPath, outputPath, 1.0, 1.0):
    quit(1)

  # OCR処理
  let ocrResult = tesseractOCR(outputPath, lang)
  echo "OCR結果:\n", ocrResult

4. 実行方法

上記のコードを保存:

  • leptonica.nim
  • image_preprocess.nim
  • ocr_tool.nim

コマンドプロンプトで下記を実行してビルドします。

nim c -r ocr_tool.nim

使うときは以下のように実行します。

ocr_tool.exe <入力画像パス> <前処理後画像パス> [言語コード]
#例: ocr_tool.exe sample.png processed.png eng
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?