MTCNN は Multi-task CNN (参考 Web ページ: https://github.com/open-face/mtcnn, 原論文: https://kpzhang93.github.io/MTCNN_face_detection_alignment/paper/spl.pdf)
手順の要点: Python 3.6, TensorFlow 1.15, Python の仮想環境(Windows では C:\venv\tf115py36)
ソフトウエア等の利用条件は,利用者で確認すること.
サイト内の関連ページ:
謝辞:MTCNN の考案者、そして、プログラムの作者に感謝します
参考Webページ https://www.github.com/ipazc/mtcnn
※ 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 -yV 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 python -m pip install -U jupyterlab jupyter jupyter-console jupytext spyder
Git の URL: https://git-scm.com/
sudo apt -yV install git
※ GPU とは,グラフィックス・プロセッシング・ユニットの略で、コンピュータグラフィックス関連の機能,乗算や加算の並列処理の機能などがある.
ダウンロードページ
詳細説明
※ CUDA とは,NVIDIA社が提供している GPU 用のプラットフォームである. ダウンロードページ
TensorFlow 2.1 以上の GPU 版での,NVIDIA CUDA のバージョンは 10.1 が指定されている. TensorFlow 1.15の GPU 版での,NVIDIA CUDA のバージョンは 10.0 が指定されている. 指定されているバージョンより高いものは使わない. その根拠は次のページ. URL: https://www.tensorflow.org/install/source#common_installation_problems
詳細説明
ダウンロードページ
ダウンロードして展開(解凍)したら,パスを通しておくこと.
URL: https://www.tensorflow.org/install/source#common_installation_problems
詳細説明
最新版ではない 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
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
※ バージョン番号が表示されれば 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 でも同様の手順になる.
venv を使い,孤立した Python 仮想環境を使っているときは,Windows でも Ubuntu でも同じで,「python -m pip install ...」. いま,venv を使っているかどうかは,プロンプトの「(venv)」で分かる.
python -m pip install -U scikit-image opencv-python dlib
C:\venv\tf115py36\.venv\Scripts\activate.bat python -m pip install -U mtcnn
必要であればダウンロードして使ってください.
https://github.com/ipazc/mtcnn で公開されているプログラムを変更して使用
import cv2
import numpy as np
from mtcnn.mtcnn import MTCNN
img = cv2.imread("C:/face-image/126.png")
detector = MTCNN()
print(detector.detect_faces(img))
img = cv2.imread("C:/face-image/127.png")
detector = MTCNN()
print(detector.detect_faces(img))
顔検出と、5点(左目、右目、鼻、口の左、口の右)の検出結果が表示されるので確認する
import cv2
import numpy as np
from mtcnn.mtcnn import MTCNN
bgr = cv2.imread("C:/face-image/126.png")
def box_label(bgr, x1, y1, x2, y2, label):
cv2.rectangle(bgr, (x1, y1), (x2, y2), (255, 0, 0), 1, 1)
cv2.rectangle(bgr, (int(x1), int(y1-25)), (x2, y1), (255,255,255), -1)
cv2.putText(bgr, label, (x1, int(y1-5)), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0,0,0), 1)
box_label(bgr, 100, 300, 200, 400, "hello")
cv2.imshow('', bgr)
cv2.waitKey(0)
cv2.destroyAllWindows()
画像が表示されるので確認. このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
import cv2
import numpy as np
from mtcnn.mtcnn import MTCNN
def box_label(bgr, x1, y1, x2, y2, label):
cv2.rectangle(bgr, (x1, y1), (x2, y2), (255, 0, 0), 1, 1)
cv2.rectangle(bgr, (int(x1), int(y1-25)), (x2, y1), (255,255,255), -1)
cv2.putText(bgr, label, (x1, int(y1-5)), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0,0,0), 1)
bgr = cv2.imread("C:/face-image/126.png")
detector = MTCNN()
a1 = detector.detect_faces(bgr)
for i, d in enumerate(a1):
x1, y1 = tuple( d['box'][0:2] )
x2, y2 = tuple( np.array( d['box'][0:2] ) + np.array( d['box'][2:4] ) )
box_label(bgr, x1, y1, x2, y2, '{:0.2f}'.format(d['confidence']))
left_eye = d['keypoints']['left_eye']
if len(left_eye) == 2:
cv2.circle(bgr, left_eye, 6, (255,255,255), -1)
right_eye = d['keypoints']['right_eye']
if len(right_eye) == 2:
cv2.circle(bgr, right_eye, 6, (255,255,255), -1)
nose = d['keypoints']['nose']
if len(nose) == 2:
cv2.circle(bgr, nose, 6, (255,255,255), -1)
mouth_left = d['keypoints']['mouth_left']
if len(mouth_left) == 2:
cv2.circle(bgr, mouth_left, 6, (255,255,255), -1)
mouth_right = d['keypoints']['mouth_right']
if len(mouth_right) == 2:
cv2.circle(bgr, mouth_right, 6, (255,255,255), -1)
cv2.imshow('', bgr)
cv2.waitKey(0)
cv2.destroyAllWindows()
画像が表示されるので確認. このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
「C:/face-image/sample1.mp4」のところは、実際のファイル名に置き換えること
import cv2
import numpy as np
from mtcnn.mtcnn import MTCNN
def box_label(bgr, x1, y1, x2, y2, label):
cv2.rectangle(bgr, (x1, y1), (x2, y2), (255, 0, 0), 1, 1)
cv2.rectangle(bgr, (int(x1), int(y1-25)), (x2, y1), (255,255,255), -1)
cv2.putText(bgr, label, (x1, int(y1-5)), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0,0,0), 1)
detector = MTCNN()
v = cv2.VideoCapture("C:/face-image/sample1.mp4")
while(v.isOpened()):
r, bgr = v.read()
if ( r == False ):
break
a1 = detector.detect_faces(bgr)
for i, d in enumerate(a1):
x1, y1 = tuple( d['box'][0:2] )
x2, y2 = tuple( np.array( d['box'][0:2] ) + np.array( d['box'][2:4] ) )
box_label(bgr, x1, y1, x2, y2, '{:0.2f}'.format(d['confidence']))
left_eye = d['keypoints']['left_eye']
if len(left_eye) == 2:
cv2.circle(bgr, left_eye, 6, (255,255,255), -1)
right_eye = d['keypoints']['right_eye']
if len(right_eye) == 2:
cv2.circle(bgr, right_eye, 6, (255,255,255), -1)
nose = d['keypoints']['nose']
if len(nose) == 2:
cv2.circle(bgr, nose, 6, (255,255,255), -1)
mouth_left = d['keypoints']['mouth_left']
if len(mouth_left) == 2:
cv2.circle(bgr, mouth_left, 6, (255,255,255), -1)
mouth_right = d['keypoints']['mouth_right']
if len(mouth_right) == 2:
cv2.circle(bgr, mouth_right, 6, (255,255,255), -1)
cv2.imshow("", bgr)
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
※ 途中で止めたいとき,右上の「x」をクリックしない.画面の中をクリックしてから,「q」のキーを押して閉じる
「v = cv2.VideoCapture(0) 」は、USB カメラを使うためのもの。他の部分は、上のプログラムと同じ。
import cv2
import numpy as np
from mtcnn.mtcnn import MTCNN
def box_label(bgr, x1, y1, x2, y2, label):
cv2.rectangle(bgr, (x1, y1), (x2, y2), (255, 0, 0), 1, 1)
cv2.rectangle(bgr, (int(x1), int(y1-25)), (x2, y1), (255,255,255), -1)
cv2.putText(bgr, label, (x1, int(y1-5)), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0,0,0), 1)
detector = MTCNN()
v = cv2.VideoCapture(0)
while(v.isOpened()):
r, bgr = v.read()
if ( r == False ):
break
a1 = detector.detect_faces(bgr)
for i, d in enumerate(a1):
x1, y1 = tuple( d['box'][0:2] )
x2, y2 = tuple( np.array( d['box'][0:2] ) + np.array( d['box'][2:4] ) )
box_label(bgr, x1, y1, x2, y2, '{:0.2f}'.format(d['confidence']))
left_eye = d['keypoints']['left_eye']
if len(left_eye) == 2:
cv2.circle(bgr, left_eye, 6, (255,255,255), -1)
right_eye = d['keypoints']['right_eye']
if len(right_eye) == 2:
cv2.circle(bgr, right_eye, 6, (255,255,255), -1)
nose = d['keypoints']['nose']
if len(nose) == 2:
cv2.circle(bgr, nose, 6, (255,255,255), -1)
mouth_left = d['keypoints']['mouth_left']
if len(mouth_left) == 2:
cv2.circle(bgr, mouth_left, 6, (255,255,255), -1)
mouth_right = d['keypoints']['mouth_right']
if len(mouth_right) == 2:
cv2.circle(bgr, mouth_right, 6, (255,255,255), -1)
cv2.imshow("", bgr)
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
※ 途中で止めたいとき,右上の「x」をクリックしない.画面の中をクリックしてから,「q」のキーを押して閉じる
本サイトは金子邦彦研究室のWebページです.サイトマップは,サイトマップのページをご覧下さい. 本サイト内の検索は,サイト内検索のページをご利用下さい.
問い合わせ先: 金子邦彦(かねこ くにひこ) ![]()