tag:crieit.net,2005:https://crieit.net/tags/OpenCV/feed 「OpenCV」の記事 - Crieit Crieitでタグ「OpenCV」に投稿された最近の記事 2021-04-05T21:17:41+09:00 https://crieit.net/tags/OpenCV/feed tag:crieit.net,2005:PublicArticle/16819 2021-04-05T20:50:12+09:00 2021-04-05T21:17:41+09:00 https://crieit.net/posts/Tesseract-Ubuntu-Docker TesseractをUbuntu(Docker)で試してみる <p>DockerでJupyterLab環境で構築し、その中でTesseractを使って画像から文字を抽出する。</p> <h1 id="DockerでJupyter Lab環境を構築"><a href="#Docker%E3%81%A7Jupyter+Lab%E7%92%B0%E5%A2%83%E3%82%92%E6%A7%8B%E7%AF%89">DockerでJupyter Lab環境を構築</a></h1> <h2 id="Dockerfileを作る"><a href="#Dockerfile%E3%82%92%E4%BD%9C%E3%82%8B">Dockerfileを作る</a></h2> <p>任意のフォルダにDockerfileという名前のファイルを作成する。</p> <pre><code class="shell">$ mkdir ~/Desktop/docker_build $ cd Desktop/docker_build/ $ touch Dockerfile </code></pre> <p>Dockerfileの中身</p> <pre><code class="docker">FROM ubuntu:latest # update RUN apt-get -y update && apt-get install -y \ sudo \ wget \ vim #install anaconda3 WORKDIR /opt # download anaconda package and install anaconda # archive -&gt; https://repo.continuum.io/archive/ RUN wget https://repo.continuum.io/archive/Anaconda3-2019.10-Linux-x86_64.sh && \ sh /opt/Anaconda3-2019.10-Linux-x86_64.sh -b -p /opt/anaconda3 && \ rm -f Anaconda3-2019.10-Linux-x86_64.sh # set path ENV PATH /opt/anaconda3/bin:$PATH # update pip and conda RUN pip install --upgrade pip WORKDIR / RUN mkdir /work # execute jupyterlab as a default command CMD ["jupyter", "lab", "--ip=0.0.0.0", "--allow-root", "--LabApp.token=''"] </code></pre> <h2 id="Dockerをビルド"><a href="#Docker%E3%82%92%E3%83%93%E3%83%AB%E3%83%89">Dockerをビルド</a></h2> <pre><code class="shell">$ docker build . Successfully built d723190a8650 </code></pre> <h2 id="Dockerを起動"><a href="#Docker%E3%82%92%E8%B5%B7%E5%8B%95">Dockerを起動</a></h2> <pre><code class="shell">$ docker run -p 8888:8888 -v ~/Desktop/ds_python:/work --name my-lab d723190a8650 </code></pre> <p>ブラウザからlocalhost:8888にアクセスしてJupyter Labに入る。</p> <h1 id="Docker上でTesseract"><a href="#Docker%E4%B8%8A%E3%81%A7Tesseract">Docker上でTesseract</a></h1> <h2 id="Tesseractインストール"><a href="#Tesseract%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB">Tesseractインストール</a></h2> <h3 id="ターミナル起動"><a href="#%E3%82%BF%E3%83%BC%E3%83%9F%E3%83%8A%E3%83%AB%E8%B5%B7%E5%8B%95">ターミナル起動</a></h3> <ul> <li>workディレクトリをクリック</li> <li>File -> New -> Terminal</li> </ul> <h2 id="本体インストール"><a href="#%E6%9C%AC%E4%BD%93%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB">本体インストール</a></h2> <p>インストール途中で、ロケーションとタイムゾーンを聞かれるので、Asia、Tokyoを選択</p> <pre><code class="shell">$ sudo apt-get update $ sudo apt install tesseract-ocr $ sudo apt install libtesseract-dev </code></pre> <h2 id="バージョン確認"><a href="#%E3%83%90%E3%83%BC%E3%82%B8%E3%83%A7%E3%83%B3%E7%A2%BA%E8%AA%8D">バージョン確認</a></h2> <pre><code class="shell">$ tesseract -v tesseract 4.1.0-rc1-184-g497d leptonica-1.75.3 libgif 5.1.4 : libjpeg 8d (libjpeg-turbo 1.5.2) : libpng 1.6.34 : libtiff 4.0.9 : zlib 1.2.11 : libwebp 0.6.1 : libopenjp2 2.3.0 Found AVX2 Found AVX Found SSE </code></pre> <h2 id="訓練済みモジュールインストール"><a href="#%E8%A8%93%E7%B7%B4%E6%B8%88%E3%81%BF%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB">訓練済みモジュールインストール</a></h2> <pre><code class="shell">sudo apt install tesseract-ocr-jpn tesseract-ocr-jpn-vert sudo apt install tesseract-ocr-script-jpan tesseract-ocr-script-jpan-vert </code></pre> <p>モジュールがインストールされたか確認</p> <pre><code class="shell">$ tesseract --list-langs List of available languages (6): Japanese Japanese_vert eng jpn jpn_vert osd </code></pre> <h1 id="Tesseract実行"><a href="#Tesseract%E5%AE%9F%E8%A1%8C">Tesseract実行</a></h1> <p>第一引数は画像の名前、第二引数はOCRの結果を出力するファイル名。デフォルトで.txt拡張子が付く。</p> <pre><code class="shell">$ tesseract image.png ocr_out -l jpn </code></pre> <h1 id="PythonでTesseract実行"><a href="#Python%E3%81%A7Tesseract%E5%AE%9F%E8%A1%8C">PythonでTesseract実行</a></h1> <h2 id="pytesseractインストール"><a href="#pytesseract%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB">pytesseractインストール</a></h2> <pre><code class="shell">pip install pytesseract </code></pre> <h2 id="pytesseract実行"><a href="#pytesseract%E5%AE%9F%E8%A1%8C">pytesseract実行</a></h2> <pre><code class="python">import Image import pytesseract FILE_NAME = './image.jpg' print(pytesseract.image_to_string(Image.open(FILE_NAME), lang=('jpn')) </code></pre> <h1 id="おまけ"><a href="#%E3%81%8A%E3%81%BE%E3%81%91">おまけ</a></h1> <h2 id="OpenCVでグレースケール変換"><a href="#OpenCV%E3%81%A7%E3%82%B0%E3%83%AC%E3%83%BC%E3%82%B9%E3%82%B1%E3%83%BC%E3%83%AB%E5%A4%89%E6%8F%9B">OpenCVでグレースケール変換</a></h2> <h3 id="OpenCVインストール"><a href="#OpenCV%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB">OpenCVインストール</a></h3> <pre><code class="shell">$ pip install opencv-python $ apt-get install -y libgl1-mesa-dev </code></pre> <h3 id="グレースケール変換"><a href="#%E3%82%B0%E3%83%AC%E3%83%BC%E3%82%B9%E3%82%B1%E3%83%BC%E3%83%AB%E5%A4%89%E6%8F%9B">グレースケール変換</a></h3> <pre><code class="python">import cv2 im = cv2.imread('./image.jpeg') im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) cv2.imwrite('./image_gray.jpeg', im_gray) print(pytesseract.image_to_string(Image.open('./image_gray.jpeg'), lang='jpn')) </code></pre> skybee8