'
【目次】
手順の要点: Python 3.6, TensorFlow 1.15, Python の隔離された環境(Windows では C:\venv\tf115py36)
ソフトウエア等の利用条件等は,利用者で確認すること.
【サイト内の関連ページ】:
謝辞:ソフトウエアの作者に感謝します
TensorFlow 1.15 を使う.
「Visual Studio Community 2019 vesion 16.2, マイクロソフト C++ ビルドツールのインストール(Windows 上)」で説明している.
すでに TensorFlow 2 を使っている,あるいは使う予定ということがありえる. 単純には,TensorFlow 2 と TensorFlow 1.15 を共存させて Python で使うということはできないが, 少しの手間で,共存できるようになる. そこで,TensorFlow 2 とTensorFlow 1.15 の共存を前提として, TensorFlow 1.15 のインストールを行う.
【Python 開発環境のインストール】
Python を使うときは,Python開発環境や Python コンソール(Jupyter Qt Console,Spyder,PyCharm,PyScripter など)の利用も便利である
Windows, Ubuntu での Python 開発環境,Python コンソール(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, spyder)のインストール: 別ページで,インストール手順を説明している.
Windows でのPython3.6,TensorFlow 1.15 のインストール:別ページで説明している.
すでにPython 3.9 あるいは Python 3.8 をインストールしている,あるいは,インストール予定という場合を想定し, あとのトラブルが起きにくい,そして,簡単に運用できるように 「Python 3.6 をインストールし,その上に,TensorFlow 1.15.5 をインストールする」という手順を案内している.
Ubuntu でのPython,TensorFlow 1.15 のインストール:別ページで案内している.
Ubuntu のシステム Python に影響を与えないように,隔離された Python 3.6 仮想環境の新規作成し,その上にTensorFlow 1.15.5 をインストールするという手順(venv を使用)(Ubuntu 上)を案内している.
Git の URL: https://git-scm.com/
sudo apt -y update sudo apt -y install git
Git の URL: https://git-scm.com/
cmake のダウンロード URL: https://cmake.org/download/
sudo apt -y update sudo apt -y install git cmake cmake-curses-gui
Windows での手順を下に示す.Ubuntu でも同様の手順になる.
cd c:\pytools rmdir /s /q deepgaze
c:\pytools\.venv\Scripts\activate.bat git clone https://github.com/mpatacchiola/deepgaze cd deepgaze python setup.py build python setup.py install
エラーメッセージが出ていないこと.
必要であればダウンロードして使ってください.
まず,メモ帳を開く(メモ帳以外のエディタでも問題ない)
notepad c:\do.bat
次のように編集する.
sed -i "s/import tensorflow as tf/import tensorflow.compat.v1 as tf/g" sed -i "s/from tensorflow import/from tensorflow.compat.v1 import" sed -i "s/from keras import/from tensorflow.compat.v1.keras import"
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:/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:/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:/image/126.png, c:/image/126-filtered.png,
このとき、ファイル名は、skin.py のようなファイル名で保存(拡張子は「.py」)
「作業用のディレクトリ(フォルダ)」である C:\face-image に保存する
先ほど名前を付けて保存したときのファイル名を使う
cd C:\face-image python skin.py
skin.py の中の画像ファイル名を 126 から 127 に書き換えて、python skin.py をもう一度実行。次のような結果になる