金子邦彦研究室人工知能Windows で動く人工知能関係 Pythonアプリケーション,オープンソースソフトウエア)mpatacchiola/DeepGaze のインストールと動作確認(肌色領域の抜き出し,saliency の検出)(TensorFlow 1.15.5,Python 3.7 を使用)(Windows 上)

mpatacchiola/DeepGaze のインストールと動作確認(肌色領域の抜き出し,saliency の検出)(TensorFlow 1.15.5,Python 3.7 を使用)(Windows 上)

目次

  1. 前準備
  2. mpatacchiola/DeepGaze のインストール
  3. このページで説明のために使用するビデオ、写真
  4. mpatacchiola/DeepGaze を用いて肌色領域の抜き出し
  5. 付属のサンプルプログラムを動かしてみる
  6. mpatacchiola/DeepGaze を用いて saliency の検出

手順の要点: 前準備として,NVIDIA CUDA 10.0, NVIDIA cuDNN 7.6.5, Python 3.7, TensorFlow 1.15.5 等をインストール.

ソフトウェア等の利用条件等は,利用者で確認すること.

謝辞:ソフトウェアの作者に感謝します

参考Webページ https://www.github.com/mpatacchiola/DeepGaze

前準備

Visual Studio Community 2017 のインストール

NVIDIA CUDA 10.0 は Visual Studio Commnity 2017, 2015, 2013, 2012 と連携して動く機能がある.

NVIDIA CUDA 10.0 のインストールの前に, Visual Studio Commnity 2017 のインストールを行う.

Visual Studio Commnity 2017 のインストールは, https://visualstudio.microsoft.com/ja/vs/older-downloads/ で「2017」を選び,「ダウンロード」をクリック. その後表示されるダウンロードの画面で, 「Visual Studio Commnity 2017」を選ぶ. インストール時には「C++ によるデスクトップ開発」をチェックしてインストールする.

Git のインストール

Git のページ https://git-scm.com/ からダウンロードしてインストール:

CMake のインストール

CMake の公式ダウンロードページ: https://cmake.org/download/

Python 3.7 64 ビット版のインストール,pip と setuptools の更新,Python 開発環境のインストール(Windows 上)

① Python 3.7 64 ビット版のインストール(Windows 上)

Python のインストールでの注意点

Python 3.7 のインストール手順の詳細(別ページ)

Windows での Python 3.7 のインストール: 別ページ »で説明している.

Python の公式ページ

https://www.python.org/

インストール手順の概要

  1. Python の URL を開く

    URL: https://www.python.org

  2. Windows 版の Python 3.7 をダウンロード

    ページの上の方にある「Downloads」をクリック,「Downloads」の下にメニューが出るので,その中の「Windows」をクリック.

    そして,Python 3.7.x (x は数字)を探す.

    そして,Windows の 64ビット版のインストーラをダウンロードしたいので,「Windows x86-64 executable installer」を選ぶ

    [image]
  3. インストール時の設定
    1. いまダウンロードした .exe ファイルを右クリック, 右クリックメニューで「管理者として実行」を選ぶ.

      [image]
    2. Python ランチャーをインストールするために,「Install launcher for all users (recommended)」をチェック.

      ※ すでに Python ランチャーをインストール済みのときは, 「Install launcher for all users (recommended)」がチェックできないようになっている場合がある.そのときは,チェックせずに進む.

    3. Add Python 3.7 to PATH」をチェック.

      [image]
    4. Customize installation」をクリック.

      [image]
    5. オプションの機能 (Optional Features)は,既定(デフォルト)のままでよい. 「Next」をクリック

      [image]
    6. Install for all users」を選ぶ.

      Python のインストールディレクトリは,「C:\Program Files\Python37」のように自動設定されることを確認.

      Install」をクリック

      [image]
    7. Disable path length limit」が表示される場合がある.クリックして,パス長の制限を解除する

      [image]
    8. インストールが終了したら,「Close」をクリック
  4. インストールのあと,Windows のスタートメニューに「Python 3.7」が増えていることを確認.
  5. システムの環境変数 Path の確認のため,新しくコマンドプロンプトを開き,次のコマンドを実行する.

    pypipパスが通っていることの確認である.

    where py
    where pip
    

    where py では「C:\Windows\py.exe」 が表示され, where pip では「C:\Program Files\Python37\Scripts\pip.exe」 が表示されることを確認.

    [image]

② pip と setuptools の更新(Windows 上)

  1. Windows で,コマンドプロンプト管理者として実行

    Windowspip を実行するときは,コマンドプロンプト管理者として開き,それを使って pip を実行することにする.

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

  2. 次のコマンドを実行する

    Python の使用は「py -3.7」で行う.

    py -3.7 -m pip install -U pip setuptools
    

    [image]

③ Python 開発環境として,Python の隔離された環境に,Python コンソール(Jupyter Qt Console), Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract をインストール

  1. Windows で,コマンドプロンプト管理者として実行

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

  2. venv を用いて,Python の仮想環境を作成

    次のコマンドを実行する

    py -3.7 -m pip install -U pip setuptools
    py -3.7 -m venv C:\venv\py37
    
  3. Python の仮想環境の使用開始と,Python 開発環境のインストール

    次のコマンドを実行する

    Python の使用は「C:\venv\py37\Scripts\activate.bat」の後,「python」で行う.

    C:\venv\py37\Scripts\activate.bat
    python -m pip install -U pip setuptools jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter
    

Visual Studio Community 2017,NVIDIA ドライバ,NVIDIA CUDA ツールキット 10.0,NVIDIA cuDNN 7.6.5 のインストール(Windows 上)

Windows での Visual Studio Community 2017,NVIDIA ドライバNVIDIA CUDA ツールキット 10.0NVIDIA cuDNN 7.6.5 のインストール: 別ページ »で説明している.

TensorFlow 1.15.5, Keras 2.3.1 のインストール

コマンドプロンプト管理者として実行し,次のコマンドを実行

C:\venv\py37\Scripts\activate.bat
python -m pip uninstall -y tensorflow tensorflow-cpu tensorflow-gpu tensorflow-intel tensorflow-text tensorflow-estimator tf-models-official tf_slim tensorflow_datasets tensorflow-hub keras keras-tuner keras-visualizer scipy pandas matplotlib
# TensorFlow 1.15.5 のため numpy, protobuf の古いバージョンを使用.エラーが出にくいと考えられる numpy 1.16.2, protobuf 3.19.4 を使用
python -m pip install -U numpy==1.16.2 protobuf==3.19.4 tensorflow-gpu==1.15.5 keras==2.3.1 scipy==1.5.4

mpatacchiola/DeepGaze のインストール

Windows での手順を下に示す.Ubuntu でも同様の手順になる.

  1. Windows で,コマンドプロンプト管理者として実行
  2. DeepGaze のインストールディレクトリを空にしておく

    cd %HOMEPATH%
    rmdir /s /q deepgaze
    

    [image]
  3. DeepGaze のインストール

    git clone https://github.com/mpatacchiola/deepgaze
    cd deepgaze
    py -3.7 setup.py build
    py -3.7 setup.py install 
    

    [image]
    (以下省略)
  4. 結果の確認

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

このページで説明のために使用するビデオ、写真

必要であればダウンロードして使ってください.

mpatacchiola/DeepGaze を用いて肌色領域の抜き出し

動画ファイルで動かしてみる

  1. Windows では,コマンドプロンプトを実行.
  2. 作業用のディレクトリ(フォルダ)として,c:\imageを作り, sample1.mp4 を,c:\image の下にダウンロード
    mkdir c:\image
    cd c:\image
    curl -O https://www.kkaneko.jp/sample/face/sample1.mp4
    

    [image]
  3. py -3.7 で,Python プログラムを実行する

    Python プログラムの実行: 別ページ »で説明

    Python のまとめ: 別ページ »にまとめ

    Windows でのプログラムを下に示す.Ubuntu でも同様のプログラムになる.

    OpenCV による動画表示を行う.

    「FACEIMAGEROOT + "sample1.mp4"」のところは、実際のファイル名に置き換えること

    import cv2
    import numpy as np
    from deepgaze.color_detection import RangeColorDetector
    
    #Firs image boundaries
    min_range = np.array([0, 48, 70], dtype = "uint8") #lower HSV boundary of skin color
    max_range = np.array([20, 150, 255], dtype = "uint8") #upper HSV boundary of skin color
    my_skin_detector = RangeColorDetector(min_range, max_range) #Define the detector object
    
    FACEIMAGEROOT="c:/image/"
    v = cv2.VideoCapture(FACEIMAGEROOT + "sample1.mp4")
    while(v.isOpened()):
        r, f = v.read()
        if ( r == False ):
            break
        f = f[0:885, 0:773, 0:3]
        image_filtered = my_skin_detector.returnFiltered(f, morph_opening=False, blur=False, kernel_size=3, iterations=1)
        cv2.imshow("", image_filtered)
        # Press Q to exit
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    v.release()
    cv2.destroyAllWindows()
    

    [image]

    [image]

パソコンの USB カメラで動かしてみる

Python プログラムを実行する

v = cv2.VideoCapture(0) 」は、USB カメラを使うためのもの。他の部分は、上のプログラムと同じ。

import cv2
import numpy as np
from deepgaze.color_detection import RangeColorDetector

#Firs image boundaries
min_range = np.array([0, 48, 70], dtype = "uint8") #lower HSV boundary of skin color
max_range = np.array([20, 150, 255], dtype = "uint8") #upper HSV boundary of skin color
my_skin_detector = RangeColorDetector(min_range, max_range) #Define the detector object

v = cv2.VideoCapture(0)
while(v.isOpened()):
    r, f = v.read()
    if ( r == False ):
        break
    f = f[0:885, 0:773, 0:3]
    image_filtered = my_skin_detector.returnFiltered(f, morph_opening=False, blur=False, kernel_size=3, iterations=1)
    cv2.imshow("", image_filtered)
    # Press Q to exit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

v.release()
cv2.destroyAllWindows()

[image]

パソコンの USB カメラで動かし,結果を動画像ファイルに保存

Python プログラムを実行する

Windows でのプログラムを下に示す.Ubuntu でも同様のプログラムになる.

「out = cv2.VideoWriter('C:/image/output.avi', fourcc, 20.0, (640, 480), True) 」の「True」は、カラーという意味

import cv2
import numpy as np
from deepgaze.color_detection import RangeColorDetector

#Firs image boundaries
min_range = np.array([0, 48, 70], dtype = "uint8") #lower HSV boundary of skin color
max_range = np.array([20, 150, 255], dtype = "uint8") #upper HSV boundary of skin color
my_skin_detector = RangeColorDetector(min_range, max_range) #Define the detector object

v = cv2.VideoCapture(0) 
fourcc = cv2.VideoWriter_fourcc(*'XVID')
FACEIMAGEROOT="c:/image/"
out = cv2.VideoWriter(FACEIMAGEROOT + 'output.avi', fourcc, 20.0, (640, 480), True) 
while(v.isOpened()):
    r, f = v.read()
    if ( r == False ):
        break
    f = f[0:885, 0:773, 0:3]
    image_filtered = my_skin_detector.returnFiltered(f, morph_opening=False, blur=False, kernel_size=3, iterations=1)
    cv2.imshow("", image_filtered)
    out.write(image_filtered)   
    # Press Q to exit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

v.release()
out.release()
cv2.destroyAllWindows()

[image]

付属のサンプルプログラムを動かしてみる

  1. C:\face-image のような作業用のディレクトリ(フォルダ)を作る

    [image]
  2. 顔写真のデータを、先ほど作成した「作業用のディレクトリ(フォルダ)」に入れる

    Webブラウザを使い、次の画像を「名前を付けて画像を保存」。 ファイル名は「126.png」とする。 「作業用のディレクトリ(フォルダ)」に保存する

    [image]

    次の画像も、「名前を付けて画像を保存」。 ファイル名は「127.png」とする。 「作業用のディレクトリ(フォルダ)」に保存する

    [image]

    次のようになる

    [image]
  3. サンプルプログラム

    Web ブラウザで、 https://github.com/mpatacchiola/deepgaze/blob/master/examples/ex_skin_detection_images/ex_skin_detection_images.py を開く

  4. ここに掲載されているプログラムをコピー
  5. Windows のメモ帳を開き, 丸ごと、貼り付け

    [image]
  6. このファイルの中の画像ファイル名を書き換え(2か所).

    c:/image/126.png, c:/image/126-filtered.png,

    [image]
  7. 後半部分は削除
  8. メモ帳で、ファイルを「名前を付けて保存」する。

    このとき、ファイル名は、skin.py のようなファイル名で保存(拡張子は「.py」

    作業用のディレクトリ(フォルダ)」である C:\face-image に保存する

    [image]
  9. メモ帳を閉じる
  10. Windowsコマンドプロンプトを開く
  11. 次のように実行

    先ほど名前を付けて保存したときのファイル名を使う

    cd C:\face-image
    python skin.py
    

    [image]
  12. 結果の確認

    [image]

もう1つの画像で確認

skin.py の中の画像ファイル名を 126 から 127 に書き換えて、python skin.py をもう一度実行。次のような結果になる

[image]

[image]

[image]

mpatacchiola/DeepGaze を用いて saliency の検出

動画ファイルで動かしてみる

  1. Windows では,コマンドプロンプトを実行.
  2. 作業用のディレクトリ(フォルダ)として,c:\imageを作り, sample1.mp4 を,c:\image の下にダウンロード
    mkdir c:\image
    cd c:\image
    curl -O https://www.kkaneko.jp/sample/face/sample1.mp4
    

    [image]
  3. py -3.7 で,Python プログラムを実行する

    Windows でのプログラムを下に示す.Ubuntu でも同様のプログラムになる.

    OpenCV による動画表示を行う.

    「FACEIMAGEROOT + "sample1.mp4"」のところは、実際のファイル名に置き換えること

    import numpy as np
    import cv2
    from deepgaze.saliency_map import FasaSaliencyMapping 
    
    # Using OpenCV the resolution of the webcam is set to these values.
    # You must check which resolution your webcam support and adjust the values in accordance.
    RESOLUTION_WIDTH = 320
    RESOLUTION_HEIGHT = 180
    
    FACEIMAGEROOT="c:/image/"
    v = cv2.VideoCapture(FACEIMAGEROOT + "sample1.mp4")
    
    # Create the main window and move it
    cv2.namedWindow('Video')
    
    # Obtaining the CAM dimension
    w = int(v.get(3))
    h = int(v.get(4))
    
    # Defining the FASA object using the camera resolution
    my_map = FasaSaliencyMapping(h, w)
    
    while True:
        # Capture frame-by-frame
        r, f = v.read()
        if ( r == False ):
            break
        image_salient = my_map.returnMask(f, tot_bins=8, format='BGR2LAB')
        cv2.imshow("", image_salient)
        # Press Q to exit
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    v.release()
    cv2.destroyAllWindows()
    

    [image]

    [image]

ビデオカメラで動かしてみる

Windows での手順を下に示す.Ubuntu でも同様の手順になる.

  1. Windows で,コマンドプロンプト管理者として実行
  2. py -3.7 で,Python プログラムを実行する

    OpenCV による動画表示を行う.

    import numpy as np
    import cv2
    from deepgaze.saliency_map import FasaSaliencyMapping 
    
    # Using OpenCV the resolution of the webcam is set to these values.
    # You must check which resolution your webcam support and adjust the values in accordance.
    RESOLUTION_WIDTH = 320
    RESOLUTION_HEIGHT = 180
    
    # Open the video stream and set the webcam resolution.
    # It may give problem if your webcam does not support the particular resolution used.
    video_capture = cv2.VideoCapture(0)
    
    # Create the main window and move it
    cv2.namedWindow('Video')
    
    # Obtaining the CAM dimension
    cam_w = int(video_capture.get(3))
    cam_h = int(video_capture.get(4))
    
    # Defining the FASA object using the camera resolution
    my_map = FasaSaliencyMapping(cam_h, cam_w)
    
    while True:
        # Capture frame-by-frame
        r, f = video_capture.read()
        image_salient = my_map.returnMask(f, tot_bins=8, format='BGR2LAB')
        cv2.imshow("", image_salient)
        # Press Q to exit
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    v.release()
    cv2.destroyAllWindows()
    

    [image]

    [image]