MediaPipe 0.8 : Colab : MediaPipe 顔メッシュ (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 03/07/2021 (0.8.3)
* 本ページは、MediaPipe の以下のドキュメントを翻訳した上で適宜、補足説明したものです:
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
- お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。
- Windows PC のブラウザからご参加が可能です。スマートデバイスもご利用可能です。
人工知能研究開発支援 | 人工知能研修サービス | テレワーク & オンライン授業を支援 |
PoC(概念実証)を失敗させないための支援 (本支援はセミナーに参加しアンケートに回答した方を対象としています。) |
◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。
株式会社クラスキャット セールス・マーケティング本部 セールス・インフォメーション |
E-Mail:sales-info@classcat.com ; WebSite: https://www.classcat.com/ |
Facebook: https://www.facebook.com/ClassCatJP/ |
MediaPipe 0.8 : Colab : MediaPipe 顔メッシュ
Python の MediaPipe 顔メッシュ・ソリューション API の使用サンプルです (http://solutions.mediapipe.dev/face_mesh もまた見てください)。
!pip install mediapipe
顔を含む任意の画像を Colab にアップロードします。web: https://unsplash.com/photos/JyVcAIUAcPM と https://unsplash.com/photos/auTAb39ImXg から 2 つのサンプル画像を取ります。
from google.colab import files
uploaded = files.upload()
import cv2
from google.colab.patches import cv2_imshow
# Read images with OpenCV.
images = {name: cv2.imread(name) for name in uploaded.keys()}
# Preview the images.
for name, image in images.items():
print(name)
cv2_imshow(image)
![]() |
![]() |
総ての MediaPipe ソリューション Python API サンプルは mp.solutions 下にあります。
MediaPipe Face Mesh ソリューションについては、mp_face_mesh = mp.solutions.face_mesh としてこのモジュールにアクセスできます。
初期化の間に static_image_mode, max_num_faces と min_detection_confidence のようなパラメータを変更しても良いです。パラメータについてのより多くの情報を得るためには help(mp_face_mesh.FaceMesh) を実行します。
import mediapipe as mp
mp_face_mesh = mp.solutions.face_mesh
help(mp_face_mesh.FaceMesh)
Help on class FaceMesh in module mediapipe.python.solutions.face_mesh: class FaceMesh(mediapipe.python.solution_base.SolutionBase) | FaceMesh(static_image_mode=False, max_num_faces=1, min_detection_confidence=0.5, min_tracking_confidence=0.5) | | MediaPipe FaceMesh. | | MediaPipe FaceMesh processes an RGB image and returns the face landmarks on | each detected face. | | Please refer to https://solutions.mediapipe.dev/face_mesh#python-solution-api | for usage examples. | | Method resolution order: | FaceMesh | mediapipe.python.solution_base.SolutionBase | builtins.object | | Methods defined here: | | __init__(self, static_image_mode=False, max_num_faces=1, min_detection_confidence=0.5, min_tracking_confidence=0.5) | Initializes a MediaPipe FaceMesh object. | | Args: | static_image_mode: Whether to treat the input images as a batch of static | and possibly unrelated images, or a video stream. See details in | https://solutions.mediapipe.dev/face_mesh#static_image_mode. | max_num_faces: Maximum number of faces to detect. See details in | https://solutions.mediapipe.dev/face_mesh#max_num_faces. | min_detection_confidence: Minimum confidence value ([0.0, 1.0]) for face | detection to be considered successful. See details in | https://solutions.mediapipe.dev/face_mesh#min_detection_confidence. | min_tracking_confidence: Minimum confidence value ([0.0, 1.0]) for the | face landmarks to be considered tracked successfully. See details in | https://solutions.mediapipe.dev/face_mesh#min_tracking_confidence. | | process(self, image: numpy.ndarray) ->| Processes an RGB image and returns the face landmarks on each detected face. | | Args: | image: An RGB image represented as a numpy ndarray. | | Raises: | RuntimeError: If the underlying graph throws any error. | ValueError: If the input image is not three channel RGB. | | Returns: | A NamedTuple object with a "multi_face_landmarks" field that contains the | face landmarks on each detected face. | | ---------------------------------------------------------------------- | Methods inherited from mediapipe.python.solution_base.SolutionBase: | | __enter__(self) | A "with" statement support. | | __exit__(self, exc_type, exc_val, exc_tb) | Closes all the input sources and the graph. | | close(self) -> None | Closes all the input sources and the graph. | | ---------------------------------------------------------------------- | Data descriptors inherited from mediapipe.python.solution_base.SolutionBase: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined)
# Prepare DrawingSpec for drawing the face landmarks later.
mp_drawing = mp.solutions.drawing_utils
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
with mp_face_mesh.FaceMesh(
static_image_mode=True,
max_num_faces=2,
min_detection_confidence=0.5) as face_mesh:
for name, image in images.items():
# Convert the BGR image to RGB and process it with MediaPipe Face Mesh.
results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Draw face landmarks of each face.
print(f'Face landmarks of {name}:')
if not results.multi_face_landmarks:
continue
annotated_image = image.copy()
for face_landmarks in results.multi_face_landmarks:
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACE_CONNECTIONS,
landmark_drawing_spec=drawing_spec,
connection_drawing_spec=drawing_spec)
cv2_imshow(annotated_image)
以上