Tesseract OCR 5.3.3 の開発環境とツール等のインストールと動作確認(多言語の文字認識)(Windows 上)

このページでは,Alpaca.cpp のインストールと動作確認を行う. 公式ページの手順に従う. 利用条件は,利用者で確認すること.

目次

  1. 前準備
  2. Tesseract OCR 5.3.3 の開発環境とツール等のインストール(Windows 上)
  3. 動作確認(Windows 上)

関連する外部ページ

公式ページ: https://github.com/tesseract-ocr/tessdoc

サイト内の関連ページ

前準備

Build Tools for Visual Studio 2022 (ビルドツール for Visual Studio 2022)または Visual Studio 2022 のインストール(Windows 上)

インストールの判断Build Tools for Visual Studio は,開発ツールセットである. Visual Studio は統合開発環境であり,いくつかの種類があり,Build Tools for Visual Studioの機能を含むか連携して使用するものである.インストールは以下の基準で判断してください:

不明な点がある場合は,Visual Studio 全体をインストール を行う方が良い.

Build Tools for Visual Studio 2022 のインストール(Windows 上)

  1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)

    次のコマンドを実行

    次のコマンドは,Build Tools for Visual Studio 2022と VC2015 再配布可能パッケージをインストールするものである.

    winget install --scope machine Microsoft.VisualStudio.2022.BuildTools 
    winget install --scope machine Microsoft.VCRedist.2015+.x64
    
  2. Build Tools for Visual Studio 2022 での C++ によるデスクトップ開発,CLI,ATL,MFC のインストール(Windows 上)
    1. Visual Studio Installer の起動

      起動方法: スタートメニューの「Visual Studio Installer」を選ぶ.

    2. Visual Studio Build Tools 2022 で「変更」を選ぶ.
    3. C++ によるデスクトップ開発」をクリック.そして,画面右側の「インストール」の詳細で「v143 ビルドツール用 C++/CLI サポート(最新)」,「ATL」,「MFC」をチェックする.その後,「変更」をクリック.

Visual Studio のインストール(Windows 上)

  1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)

    次のコマンドを実行

    1. コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
    2. インストールコマンドの実行
      winget install Microsoft.VisualStudio.2022.Community --scope machine --override "--add Microsoft.VisualStudio.Workload.NativeDesktop Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Core Microsoft.VisualStudio.Component.VC.CLI.Support Microsoft.VisualStudio.Component.CoreEditor Microsoft.VisualStudio.Component.NuGet Microsoft.VisualStudio.Component.Roslyn.Compiler Microsoft.VisualStudio.Component.TextTemplating Microsoft.VisualStudio.Component.Windows.SDK.Latest Microsoft.VisualStudio.Component.VC.Tools.x86.x64 Microsoft.VisualStudio.Component.VC.ATL Microsoft.VisualStudio.Component.VC.ATLMFC"
      winget install Microsoft.VisualStudio.2022.Community --scope machine Microsoft.VCRedist.2015+.x64
      

      インストールされるコンポーネントの説明:

      • NativeDesktop:C++によるデスクトップアプリケーション開発のためのワークロード一式
      • NativeDesktop.Core:C++デスクトップ開発に必要な基本コンポーネント群
      • VC.CLI.Support:マネージドコードとネイティブコードの統合開発を可能にするC++/CLIサポート
      • CoreEditor:コード編集,デバッグ,検索などの基本機能を提供するVisual Studioのコアエディタ
      • NuGet:.NETライブラリの依存関係を管理するパッケージ管理システム
      • Windows.SDK.Latest:Windows 向けアプリケーション開発用SDK(Software Development Kit)
      • VC.Tools.x86.x64:32ビット及び64ビット向けC++コンパイラとビルドツール
      • VC.ATL:Windowsコンポーネント開発用のActive Template Library
      • VC.ATLMFC:デスクトップアプリケーション開発用のMicrosoft Foundation Class Library

      システム要件と注意事項:

      • 管理者権限でのインストールが必須
      • 必要ディスク容量:10GB以上
      • 推奨メモリ:8GB以上のRAM
      • インストール過程でシステムの再起動が要求される可能性がある
      • 安定したインターネット接続環境が必要

      追加のコンポーネントが必要な場合は,Visual Studio Installerを使用して個別にインストールすることが可能である.

    3. インストール完了の確認
      winget list Microsoft.VisualStudio.2022.Community
      

      トラブルシューティング:

      インストール失敗時は,以下のログファイルを確認:

      %TEMP%\dd_setup_<timestamp>.log
      %TEMP%\dd_bootstrapper_<timestamp>.log
  2. Visual Studio での C++ によるデスクトップ開発,CLI のインストール(Windows 上)
    1. Visual Studio Installer の起動

      起動方法: スタートメニューの「Visual Studio Installer」を選ぶ.

    2. Visual Studio Community 2022 で「変更」を選ぶ.
    3. C++ によるデスクトップ開発」をチェック.そして,画面右側の「インストール」の詳細で「v143 ビルドツール用 C++/CLI サポート(最新)」をチェックする.その後,「インストール」をクリック.

Git,CMake のインストール(Windows 上)

Gitは,分散型のバージョン管理システム. CMakeは,クロスプラットフォームのビルドシステム生成ツール.

手順

  1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)

    次のコマンドを実行

    次のコマンドは,GitCMakeをインストールし,Gitパスを通すものである.

    次のコマンドでインストールされるGitは 「git for Windows」と呼ばれるものであり, Git,MinGW などから構成されている.

    winget install --scope machine Git.Git
    powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\Program Files\Git\cmd\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
    winget install --scope machine Kitware.CMake
    powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\Program Files\CMake\bin\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
    

関連する外部ページ

関連項目Git バージョン管理システム, Git の利用, CMake ビルドシステム生成ツール, CMake の使用方法

Tesseract OCR 5.3.3 の開発環境とツール等のインストール(Windows 上)

公式ページの手順に従う.

Software Network のセットアップ

  1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
  2. Software Network のセットアップ

    次のコマンドを実行.

    cd c:\
    rmdir /s /q sw
    mkdir sw
    cd sw
    curl -L -O https://software-network.org/client/sw-master-windows_x86_64-client.zip
    curl -L -O https://software-network.org/client/swgui-master-windows_x86_64-client.zip
    powershell -command "Expand-Archive -Path sw-master-windows_x86_64-client.zip"
    powershell -command "Expand-Archive -Path swgui-master-windows_x86_64-client.zip"
    rmdir /s /q %HOMEPATH%\.sw
    cd /d c:%HOMEPATH%
    c:\sw\sw-master-windows_x86_64-client\sw setup
    
  3. 終了の確認

    エラーメッセージが出ていないこと.

  4. ファイルが %HOMEPATH%\.sw にダウンロードされていることを確認
    dir %HOMEPATH%\.sw
    
  5. Windowsシステム環境変数 Pathc:\sw\sw-master-windows_x86_64-client追加することにより,パスを通す

    次のコマンドを実行

    powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\sw\sw-master-windows_x86_64-client\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
    

Tesseract 関連ライブラリのビルド(Software Network を使用)

  1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
  2. Tesseract OCR の開発環境のビルド(Windows 上)

    次のコマンドを実行. 終了までしばらく待つ.

    cd /d c:%HOMEPATH%
    sw build -platform=x64 org.sw.demo.google.tesseract.tesseract
    
  3. 終了の確認

    エラーメッセージが出ていないこと.

  4. ファイルが %HOMEPATH%\.sw にダウンロードされていることを確認
    dir %HOMEPATH%\.sw
    

Tesseract OCR の開発環境とツール等のインストール(Windows 上)

ソースコードをダウンロードしてインストールする.

  1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
  2. Tesseract OCR の開発環境のビルド(Windows 上)

    次のコマンドを実行. 終了までしばらく待つ.

    cd c:\
    rmdir /s /q tesseract
    git clone https://github.com/tesseract-ocr/tesseract
    cd tesseract
    mkdir build
    cd build
    cmake .. -G "Visual Studio 17 2022" -A x64 -T host=x64 -DBUILD_TESTS=ON -DCMAKE_INSTALL_PREFIX=c:/tesseract
    
    # いくつかのファイルをUTF8 を UTF8BOM有りに変換
    cd /d c:%HOMEPATH%
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gsocks4aproxy.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gsocks4aproxy.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/84/1c/ec76/src/sdir/gmodule/gmodule.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/84/1c/ec76/src/sdir/gmodule/gmodule.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gnetworkaddress.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gnetworkaddress.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/c7/3e/272a/src/sdir/glib/gatomic.h' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/c7/3e/272a/src/sdir/glib/gatomic.h' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/c7/3e/272a/src/sdir/glib/gtestutils.h' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/c7/3e/272a/src/sdir/glib/gtestutils.h' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/c4/6e/a0a7/src/sdir/gobject/gtype.h' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/c4/6e/a0a7/src/sdir/gobject/gtype.h' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gactiongroupexporter.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gactiongroupexporter.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gappinfo.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gappinfo.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gdbusconnection.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gdbusconnection.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gdbusaddress.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gdbusaddress.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/c7/3e/272a/src/sdir/glib/gstdioprivate.h' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/c7/3e/272a/src/sdir/glib/gstdioprivate.h' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gdatagrambased.h' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gdatagrambased.h' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gfile.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gfile.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gdbusmessage.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gdbusmessage.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gdbusauthmechanismsha1.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gdbusauthmechanismsha1.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gmount.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gmount.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gapplicationimpl-dbus.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gapplicationimpl-dbus.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gdbusserver.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gdbusserver.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/giomodule.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/giomodule.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gproxyresolver.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gproxyresolver.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gthreadedresolver.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gthreadedresolver.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/c4/1e/68f3/src/sdir/src/hb-style.h' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/c4/1e/68f3/src/sdir/src/hb-style.h' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/8f/09/0cc9/src/sdir/pango/pango-language-sample-table.h' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/8f/09/0cc9/src/sdir/pango/pango-language-sample-table.h' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/c4/6e/a0a7/src/sdir/gobject/gsignalgroup.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/c4/6e/a0a7/src/sdir/gobject/gsignalgroup.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gcharsetconverter.c' -Encoding UTF8; $content | Set-Content -Path '.sw/storage/pkg/7b/bc/eeda/src/sdir/gio/gcharsetconverter.c' -Encoding UTF8"
    powershell -Command "$content = Get-Content '.sw\storage\pkg\04\dc\e58a\src\sdir\pango\pango-language-sample-table.h' -Encoding UTF8; $content | Set-Content -Path '.sw\storage\pkg\04\dc\e58a\src\sdir\pango\pango-language-sample-table.h' -Encoding UTF8"
    
    cd c:\tesseract\build
    cmake --build . --config Release
    cmake --build . --config Release --target install
    
  3. 終了の確認

    エラーメッセージが出ていないこと.

  4. tessdata のダウンロード

    次のコマンドを実行.

    cd c:\tesseract\share
    rmdir /s /q tessdata
    git clone https://github.com/tesseract-ocr/tessdata
    
  5. Windowsシステム環境変数 Pathc:\tesseract\bin追加することにより,パスを通す

    次のコマンドを実行

    powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\tesseract\bin\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
    
  6. Windowsシステム環境変数 TESSDATA_PREFIX の設定を行う.

    Windows では, コマンドプロンプトを管理者として開き, 次のコマンドを実行することにより, システム環境変数 TESSDATA_PREFIX の設定を行うことができる.

    コマンドプロンプトを管理者として実行: 別ページ »で説明

    powershell -command "[System.Environment]::SetEnvironmentVariable(\"TESSDATA_PREFIX\", \"C:\tesseract\share\tessdata\", \"Machine\")"
    

動作確認(Windows 上)

  1. 日本語の文章が書かれた画像を用意する

    次の画像は、Wikipedia「日本国憲法前文」から取得

    Wikipedia「日本国憲法前文」のURL: https://ja.wikipedia.org/wiki/%E6%97%A5%E6%9C%AC%E5%9B%BD%E6%86%B2%E6%B3%95%E5%89%8D%E6%96%87#%E5%89%8D%E6%96%87

  2. 画像から文字を認識してみる

    まず,コマンドプロンプトで,カレントディレクトリを,画像のファイルのあるディレクトリに移動する.

    「-l jpn」のところ: 日本語の横書き文書なら「-l jpn」、 日本語の書き文書なら「-l jpn_vert」、

    tesseract <画像ファイル名> outbase -l jpn
    
  3. outbase.txtというファイルができるので,メモ帳で確認
    notepad outbase.txt