MediaPipe 0.8 : Colab : MediaPipe ポーズ (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 03/11/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/pose もまた見てください)。
!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.pose としてこのモジュールにアクセスできます。
初期化の間に static_image_mode と min_detection_confidence のようなパラメータを変更しても良いです。パラメータについてのより多くの情報を得るためには help(mp_holistic.Pose) を実行します。
import mediapipe as mp
mp_pose = mp.solutions.pose
help(mp_pose.Pose)
Help on class Pose in module mediapipe.python.solutions.pose: class Pose(mediapipe.python.solution_base.SolutionBase) | Pose(static_image_mode=False, upper_body_only=False, smooth_landmarks=True, min_detection_confidence=0.5, min_tracking_confidence=0.5) | | MediaPipe Pose. | | MediaPipe Pose processes an RGB image and returns pose landmarks on the most | prominent person detected. | | Please refer to https://solutions.mediapipe.dev/pose#python-solution-api for | usage examples. | | Method resolution order: | Pose | 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 Pose 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/pose#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/pose#upper_body_only. | smooth_landmarks: Whether to filter landmarks across different input | images to reduce jitter. See details in | https://solutions.mediapipe.dev/pose#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/pose#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/pose#min_tracking_confidence. | | process(self, image: numpy.ndarray) ->| Processes an RGB image and returns the pose 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 object with a "pose_landmarks" field that contains the pose | landmarks on 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)
with mp_pose.Pose(
static_image_mode=True, min_detection_confidence=0.5) as pose:
for name, image in images.items():
# Convert the BGR image to RGB and process it with MediaPipe Pose.
results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Print nose landmark.
image_hight, image_width, _ = image.shape
if not results.pose_landmarks:
continue
print(
f'Nose coordinates: ('
f'{results.pose_landmarks.landmark[mp_pose.PoseLandmark.NOSE].x * image_width}, '
f'{results.pose_landmarks.landmark[mp_pose.PoseLandmark.NOSE].y * image_hight})'
)
# Draw pose landmarks.
print(f'Pose landmarks of {name}:')
annotated_image = image.copy()
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=results.pose_landmarks,
connections=mp_pose.POSE_CONNECTIONS,
landmark_drawing_spec=drawing_spec,
connection_drawing_spec=drawing_spec)
cv2_imshow(annotated_image)
以上