【このページの目次】
手順の要点: Python 3.6, TensorFlow 1.15, Python の仮想環境(Windows では C:\venv\tf115py36)
ソフトウエア等の利用条件等は,利用者で確認すること.
サイト内の関連ページ:
謝辞:ソフトウエアの作者に感謝します
※ TensorFlow 1.15.3 に対応する Python は,3.6 や 3.7 など(3.8 は対応していない)(2020/06 時点).このページでは 3.6 を使って説明する.3.7 でも同様の手順になる.
Python の URL: http://www.python.org/
インストール手順の詳細は: 別ページで説明している.
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
py -3.6 -m pip install -U pip setuptools
次の手順により,システム Python とは別に,pyenv を用いて Pytnon 3.6 をインストールする.(システム Python の設定は変えたくないため).
図などの入った詳しい説明は別ページ.
sudo apt -y install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev cd /tmp curl https://pyenv.run | bash echo 'export PYENV_ROOT="${HOME}/.pyenv"' >> ~/.bashrc echo 'if [ -d "${PYENV_ROOT}" ]; then' >> ~/.bashrc echo ' export PATH=${PYENV_ROOT}/bin:$PATH' >> ~/.bashrc echo ' eval "$(pyenv init -)"' >> ~/.bashrc echo ' eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc echo 'fi' >> ~/.bashrc exec $SHELL -l
pyenv install 3.6.10
端末で,次のコマンドを実行.
pyenv shell 3.6.10 python -m pip install -U pip setuptools
Git の URL: https://git-scm.com/
cmake の URL: https://cmake.org/download/
sudo apt -y install git cmake cmake-curses-gui
「Visual Studio Community 2019 vesion 16.2, Visual C++ ビルドツールのインストール(Windows 上)」で説明している.
以下,Windows に Python, git, Visual C++ ビルドツール (Build Tools) をインストール済みであるものとして説明を続ける.
※ GPU とは,グラフィックス・プロセッシング・ユニットの略で、コンピュータグラフィックス関連の機能,乗算や加算の並列処理の機能などがある.
ダウンロードページ
詳細説明
※ CUDA とは,NVIDIA社が提供している GPU 用のプラットフォームである. ダウンロードページ
指定されているバージョンより高いものは使わない. その根拠は次のページ. URL: https://www.tensorflow.org/install/source#common_installation_problems
詳細説明
ダウンロードページ
ダウンロードして展開(解凍)したら,パスを通しておくこと.
TensorFlow 2.4 の GPU 版での,cuDNN のバージョンは 8.0.5(現時点で,NVIDIA CUDA ツールキット 11.0 で動く最新版).
TensorFlow 2.3, 2.2, 2.1 の GPU 版での,cuDNN のバージョンは 7.6.そして,TensorFlow 1.13 以上 TensorFlow 2.0 までの GPU 版での,cuDNN のバージョンは7.4 .それより高いバージョンよりは使わないこと.
詳細説明
最新版ではない TensorFlow を使う.そうした場合,運用を簡単にする(バージョン指定のもの間違ってアップデートしないなど)のために,venv を用いて,隔離された Python 仮想環境を作る.
今から作成する隔離された Python 仮想環境の名前と、Pythonのバージョンを決めておく
ここで行う「隔離された Python 仮想環境の新規作成と,TensorFlow 1.15.3(旧バージョン)のインストール」ついては,より詳しくは,次のページで説明している.
Windows での Python 3.6 の準備は,このページの上の方で説明している.
下の例では,隔離された Python 仮想環境のためのディレクトリを C:\venv\tf115py36に作成している.
py -3.6 -m venv C:\venv\tf115py36
venv を使い,孤立した Python 仮想環境を使っているときは,Windows でも Ubuntu でも同じで,「python -m pip install ...」. いま,venv を使っているかどうかは,プロンプトの「(venv)」で分かる.
C:\venv\tf115py36\Scripts\activate.bat python -m pip install -U pip setuptools python -m pip install -U tensorflow-gpu==1.15.3 tensorflow_datasets python -m pip install -U jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder
Ubuntu での Python 3.6 の準備は,このページの上の方で説明している.
下の例では,隔離された Python 仮想環境のためのディレクトリを ~/tf115py36に作成している.
pyenv shell 3.6.10 python -m venv ~/tf115py36
venv を使い,孤立した Python 仮想環境を使っているときは,Windows でも Ubuntu でも同じで,「python -m pip install ...」. いま,venv を使っているかどうかは,プロンプトの「(venv)」で分かる.
source ~/tf115py36/bin/activate python -m pip install -U pip setuptools python -m pip install -U tensorflow-gpu==1.15.3 tensorflow_datasets python -m pip install -U jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder
※ バージョン番号が表示されれば OK.下の図とは違うバージョンが表示されることがある.
python -c "import tensorflow as tf; print( tf.__version__ )"
TensorFlow が GPU を認識できているかの確認は,端末で,次を実行して行う.
python -c "from tensorflow.python.client import device_lib; print(device_lib.list_local_devices())"
Windows での手順を下に示す.Ubuntu でも同様の手順になる.
※ 以下,Windows での手順を示す.Ubuntu でも同様の手順になる.
cd C:\venv\tf115py36 rmdir /s /q deepgaze
このとき,virtualenv の隔離された Python 仮想環境を有効化する(「tf115py36」のところは、Python仮想環境の名前)
C:\venv\tf115py36\.venv\Scripts\activate.bat git clone https://github.com/mpatacchiola/deepgaze cd deepgaze python setup.py build python setup.py install
エラーメッセージが出ていないこと.
必要であればダウンロードして使ってください.
以下,Windows での手順を示す.Ubuntu でも同様の手順になる.
Windows でのプログラムを下に示す.Ubuntu でも同様のプログラムになる.
「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:/face-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) if cv2.waitKey(1) & 0xFF == ord('q'): break v.release() cv2.destroyAllWindows()
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) if cv2.waitKey(1) & 0xFF == ord('q'): break v.release() cv2.destroyAllWindows()
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:/face-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) if cv2.waitKey(1) & 0xFF == ord('q'): break v.release() out.release() cv2.destroyAllWindows()
Webブラウザを使い、次の画像を「名前を付けて画像を保存」。 ファイル名は「126.png」とする。 「作業用のディレクトリ(フォルダ)」に保存する
次の画像も、「名前を付けて画像を保存」。 ファイル名は「127.png」とする。 「作業用のディレクトリ(フォルダ)」に保存する
次のようになる
Web ブラウザで、 https://github.com/mpatacchiola/deepgaze/blob/master/examples/ex_skin_detection_images/ex_skin_detection_images.py を開く
C:/face-image/126.png, C:/face-image/126-filtered.png,
このとき、ファイル名は、skin.py のようなファイル名で保存(拡張子は「.py」)
「作業用のディレクトリ(フォルダ)」である C:\face-image に保存する
先ほど名前を付けて保存したときのファイル名を使う
cd C:\face-image python skin.py
skin.py の中の画像ファイル名を 126 から 127 に書き換えて、python skin.py をもう一度実行。次のような結果になる
本サイトは金子邦彦研究室のWebページです.サイトマップは,サイトマップのページをご覧下さい. 本サイト内の検索は,サイト内検索のページをご利用下さい.
問い合わせ先: 金子邦彦(かねこ くにひこ)