MediaPipe 0.8 : Colab : MediaPipe 全身用 (Holistic) (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 03/10/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 全身用 (Holistic)
Python の MediaPipe 全身用 (= Holistic) ソリューション API の使用サンプルです (http://solutions.mediapipe.dev/holistic もまた見てください)。
!pip install mediapipe
可視な上半身を持つ人物を持つ任意の画像を Colab にアップロードします。web: https://unsplash.com/photos/4jqfc2vbHJQ と https://unsplash.com/photos/72zsd_fnxYc から 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 全身用ソリューションについては、mp_holistic = mp.solutions.holistic としてこのモジュールにアクセスできます。
初期化の間に static_image_mode と min_detection_confidence のようなパラメータを変更しても良いです。パラメータについてのより多くの情報を得るためには help(mp_holistic.Holistic) を実行します。
import mediapipe as mp
mp_holistic = mp.solutions.holistic
help(mp_holistic.Holistic)
Help on class Holistic in module mediapipe.python.solutions.holistic: class Holistic(mediapipe.python.solution_base.SolutionBase) | Holistic(static_image_mode=False, upper_body_only=False, smooth_landmarks=True, min_detection_confidence=0.5, min_tracking_confidence=0.5) | | MediaPipe Holistic. | | MediaPipe Holistic processes an RGB image and returns pose landmarks, left and | right hand landmarks, and face mesh landmarks on the most prominent person | detected. | | Please refer to https://solutions.mediapipe.dev/holistic#python-solution-api | for usage examples. | | Method resolution order: | Holistic | mediapipe.python.solution_base.SolutionBase | builtins.object | | Methods defined here: | | __init__(self, static_image_mode=False, upper_body_only=False, smooth_landmarks=True, min_detection_confidence=0.5, min_tracking_confidence=0.5) | Initializes a MediaPipe Holistic 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/holistic#static_image_mode. | upper_body_only: Whether to track the full set of 33 pose landmarks or | only the 25 upper-body pose landmarks. See details in | https://solutions.mediapipe.dev/holistic#upper_body_only. | smooth_landmarks: Whether to filter landmarks across different input | images to reduce jitter. See details in | https://solutions.mediapipe.dev/holistic#smooth_landmarks. | min_detection_confidence: Minimum confidence value ([0.0, 1.0]) for person | detection to be considered successful. See details in | https://solutions.mediapipe.dev/holistic#min_detection_confidence. | min_tracking_confidence: Minimum confidence value ([0.0, 1.0]) for the | pose landmarks to be considered tracked successfully. See details in | https://solutions.mediapipe.dev/holistic#min_tracking_confidence. | | process(self, image: numpy.ndarray) ->| Processes an RGB image and returns the pose landmarks, left and right hand landmarks, and face landmarks on the most prominent person detected. | | 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 that has four fields: | 1) "pose_landmarks" field that contains the pose landmarks on the most | prominent person detected. | 2) "left_hand_landmarks" and "right_hand_landmarks" fields that contain | the left and right hand landmarks of the most prominent person detected. | 3) "face_landmarks" field that contains the face landmarks of the most | prominent person detected. | | ---------------------------------------------------------------------- | 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)
# Initialize MediaPipe Holistic.
with mp_holistic.Holistic(
static_image_mode=True, min_detection_confidence=0.5) as holistic:
for name, image in images.items():
# Convert the BGR image to RGB and process it with MediaPipe Pose.
results = holistic.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Print nose coordinates.
image_hight, image_width, _ = image.shape
if results.pose_landmarks:
print(
f'Nose coordinates: ('
f'{results.pose_landmarks.landmark[mp_holistic.PoseLandmark.NOSE].x * image_width}, '
f'{results.pose_landmarks.landmark[mp_holistic.PoseLandmark.NOSE].y * image_hight})'
)
# Draw pose landmarks.
print(f'Pose landmarks of {name}:')
annotated_image = image.copy()
mp_drawing.draw_landmarks(annotated_image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS)
mp_drawing.draw_landmarks(annotated_image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS)
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=results.face_landmarks,
connections=mp_holistic.FACE_CONNECTIONS,
landmark_drawing_spec=drawing_spec,
connection_drawing_spec=drawing_spec)
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=results.pose_landmarks,
connections=mp_holistic.POSE_CONNECTIONS,
landmark_drawing_spec=drawing_spec,
connection_drawing_spec=drawing_spec)
cv2_imshow(annotated_image)
以上