Python 用姿勢ランドマーク検出ガイド

MediaPipe Pose Landmarker タスクを使用すると、画像または動画内の人体のランドマークを検出できます。このタスクを使用すると、体の主要な位置を特定し、姿勢を分析して、動きを分類できます。このタスクでは、単一の画像または動画を処理する ML モデルを使用します。このタスクは、画像座標と 3 次元ワールド座標でボディポーズのランドマークを出力します。

これらの手順で説明するコードサンプルは GitHub で入手できます。このタスクの機能、モデル、構成オプションの詳細については、概要をご覧ください。

サンプルコード

Pose Landmarker のサンプルコードには、このタスクの Python での完全な実装が記載されています。このコードは、このタスクをテストし、独自のポーズ ランドマークの作成を開始するのに役立ちます。ウェブブラウザのみを使用して、Pose Landmarker のサンプルコードを表示、実行、編集できます。

Raspberry Pi 用のポーズ ランドマークを実装する場合は、Raspberry Pi の例アプリをご覧ください。

セットアップ

このセクションでは、Pose Landmarker を使用するように開発環境とコード プロジェクトを設定する主な手順について説明します。プラットフォーム バージョンの要件など、MediaPipe タスクを使用する開発環境の設定に関する一般的な情報については、Python の設定ガイドをご覧ください。

パッケージ

MediaPipe Pose Landmarker タスクには、mediapipe PyPI パッケージが必要です。これらの依存関係は、次のコマンドを使用してインストールしてインポートできます。

$ python -m pip install mediapipe

インポート

Pose Landmarker タスク関数にアクセスするには、次のクラスをインポートします。

import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision

モデル

MediaPipe Pose Landmarker タスクには、このタスクに対応したトレーニング済みモデルが必要です。Pose Landmarker で使用可能なトレーニング済みモデルの詳細については、タスクの概要のモデルのセクションをご覧ください。

モデルを選択してダウンロードし、ローカル ディレクトリに保存します。

model_path = '/absolute/path/to/pose_landmarker.task'

BaseOptions オブジェクトの model_asset_path パラメータを使用して、使用するモデルのパスを指定します。コード例については、次のセクションをご覧ください。

タスクを作成する

MediaPipe Pose Landmarker タスクは、create_from_options 関数を使用してタスクを設定します。create_from_options 関数は、処理する構成オプションの値を受け入れます。詳細については、構成オプションをご覧ください。

次のコードは、このタスクをビルドして構成する方法を示しています。

これらのサンプルでは、画像、動画ファイル、ライブ配信のタスクの作成方法の違いも示しています。

画像

import mediapipe as mp

BaseOptions = mp.tasks.BaseOptions
PoseLandmarker = mp.tasks.vision.PoseLandmarker
PoseLandmarkerOptions = mp.tasks.vision.PoseLandmarkerOptions
VisionRunningMode = mp.tasks.vision.RunningMode

options = PoseLandmarkerOptions(
    base_options=BaseOptions(model_asset_path=model_path),
    running_mode=VisionRunningMode.IMAGE)

with PoseLandmarker.create_from_options(options) as landmarker:
  # The landmarker is initialized. Use it here.
  # ...
    

動画

import mediapipe as mp

BaseOptions = mp.tasks.BaseOptions
PoseLandmarker = mp.tasks.vision.PoseLandmarker
PoseLandmarkerOptions = mp.tasks.vision.PoseLandmarkerOptions
VisionRunningMode = mp.tasks.vision.RunningMode

# Create a pose landmarker instance with the video mode:
options = PoseLandmarkerOptions(
    base_options=BaseOptions(model_asset_path=model_path),
    running_mode=VisionRunningMode.VIDEO)

with PoseLandmarker.create_from_options(options) as landmarker:
  # The landmarker is initialized. Use it here.
  # ...
    

ライブ配信

import mediapipe as mp

BaseOptions = mp.tasks.BaseOptions
PoseLandmarker = mp.tasks.vision.PoseLandmarker
PoseLandmarkerOptions = mp.tasks.vision.PoseLandmarkerOptions
PoseLandmarkerResult = mp.tasks.vision.PoseLandmarkerResult
VisionRunningMode = mp.tasks.vision.RunningMode

# Create a pose landmarker instance with the live stream mode:
def print_result(result: PoseLandmarkerResult, output_image: mp.Image, timestamp_ms: int):
    print('pose landmarker result: {}'.format(result))

options = PoseLandmarkerOptions(
    base_options=BaseOptions(model_asset_path=model_path),
    running_mode=VisionRunningMode.LIVE_STREAM,
    result_callback=print_result)

with PoseLandmarker.create_from_options(options) as landmarker:
  # The landmarker is initialized. Use it here.
  # ...
    

画像で使用するポーズ ランドマークの作成例については、コード例をご覧ください。

設定オプション

このタスクには、Python アプリケーション用の次の構成オプションがあります。

オプション名 説明 値の範囲 デフォルト値
running_mode タスクの実行モードを設定します。モードは次の 3 つです。

IMAGE: 単一画像入力のモード。

動画: 動画のデコードされたフレームのモード。

LIVE_STREAM: カメラなどからの入力データのライブ配信モード。 このモードでは、resultListener を呼び出して、結果を非同期で受信するリスナーを設定する必要があります。
{IMAGE, VIDEO, LIVE_STREAM} IMAGE
num_poses Pose Landmarker で検出できるポーズの最大数。 Integer > 0 1
min_pose_detection_confidence ポーズ検出が成功と見なされるための最小信頼度スコア。 Float [0.0,1.0] 0.5
min_pose_presence_confidence ポーズランドマーク検出でのポーズ存在スコアの最小信頼度スコア。 Float [0.0,1.0] 0.5
min_tracking_confidence ポーズ トラッキングが成功とみなされるための最小信頼スコア。 Float [0.0,1.0] 0.5
output_segmentation_masks Pose Landmarker が検出されたポーズのセグメンテーション マスクを出力するかどうか。 Boolean False
result_callback Pose Landmarker がライブ配信モードの場合に、ランドマークの結果を非同期で受信するように結果リスナーを設定します。実行モードが LIVE_STREAM に設定されている場合にのみ使用できます。 ResultListener N/A

データの準備

入力を画像ファイルまたは NumPy 配列として準備し、mediapipe.Image オブジェクトに変換します。入力が動画ファイルまたはウェブカメラからのライブ配信の場合は、OpenCV などの外部ライブラリを使用して、入力フレームを numpy 配列として読み込むことができます。

画像

import mediapipe as mp

# Load the input image from an image file.
mp_image = mp.Image.create_from_file('/path/to/image')

# Load the input image from a numpy array.
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=numpy_image)
    

動画

import mediapipe as mp

# Use OpenCV’s VideoCapture to load the input video.

# Load the frame rate of the video using OpenCV’s CV_CAP_PROP_FPS
# You’ll need it to calculate the timestamp for each frame.

# Loop through each frame in the video using VideoCapture#read()

# Convert the frame received from OpenCV to a MediaPipe’s Image object.
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=numpy_frame_from_opencv)
    

ライブ配信

import mediapipe as mp

# Use OpenCV’s VideoCapture to start capturing from the webcam.

# Create a loop to read the latest frame from the camera using VideoCapture#read()

# Convert the frame received from OpenCV to a MediaPipe’s Image object.
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=numpy_frame_from_opencv)
    

タスクを実行する

Pose Landmarker は、detectdetect_for_videodetect_async 関数を使用して推論をトリガーします。ポーズ ランドマークの場合、これは入力データの前処理と画像内のポーズの検出を伴います。

次のコードは、タスクモデルを使用して処理を実行する方法を示しています。

画像

# Perform pose landmarking on the provided single image.
# The pose landmarker must be created with the image mode.
pose_landmarker_result = landmarker.detect(mp_image)
    

動画

# Perform pose landmarking on the provided single image.
# The pose landmarker must be created with the video mode.
pose_landmarker_result = landmarker.detect_for_video(mp_image, frame_timestamp_ms)
    

ライブ配信

# Send live image data to perform pose landmarking.
# The results are accessible via the `result_callback` provided in
# the `PoseLandmarkerOptions` object.
# The pose landmarker must be created with the live stream mode.
landmarker.detect_async(mp_image, frame_timestamp_ms)
    

次の点にご注意ください。

  • 動画モードまたはライブ配信モードで実行する場合は、Pose Landmarker タスクに入力フレームのタイムスタンプも指定します。
  • 画像モデルまたは動画モデルで実行する場合、Pose Landmarker タスクは、入力画像またはフレームの処理が完了するまで現在のスレッドをブロックします。
  • ライブ配信モードで実行すると、Pose Landmarker タスクはすぐに返され、現在のスレッドはブロックされません。入力フレームの処理が完了するたびに、検出結果とともに結果リスナーが呼び出されます。Pose Landmarker タスクが別のフレームの処理でビジー状態になっているときに検出関数が呼び出されると、タスクは新しい入力フレームを無視します。

画像で Pose Landmarker を実行する完全な例については、コードサンプルをご覧ください。

結果を処理して表示する

Pose Landmarker は、検出実行ごとに poseLandmarkerResult オブジェクトを返します。結果オブジェクトには、各ポーズ ランドマークの座標が含まれます。

このタスクの出力データの例を次に示します。

PoseLandmarkerResult:
  Landmarks:
    Landmark #0:
      x            : 0.638852
      y            : 0.671197
      z            : 0.129959
      visibility   : 0.9999997615814209
      presence     : 0.9999984502792358
    Landmark #1:
      x            : 0.634599
      y            : 0.536441
      z            : -0.06984
      visibility   : 0.999909
      presence     : 0.999958
    ... (33 landmarks per pose)
  WorldLandmarks:
    Landmark #0:
      x            : 0.067485
      y            : 0.031084
      z            : 0.055223
      visibility   : 0.9999997615814209
      presence     : 0.9999984502792358
    Landmark #1:
      x            : 0.063209
      y            : -0.00382
      z            : 0.020920
      visibility   : 0.999976
      presence     : 0.999998
    ... (33 world landmarks per pose)
  SegmentationMasks:
    ... (pictured below)

出力には、各ランドマークの正規化された座標(Landmarks)とワールド座標(WorldLandmarks)の両方が含まれます。

出力には、次の正規化された座標(Landmarks)が含まれます。

  • xy: 画像の幅(x)と高さ(y)で 0.0 ~ 1.0 に正規化されたランドマークの座標。

  • z: ランドマークの深さ。腰の中央を原点として測定します。値が小さいほど、ランドマークはカメラに近くなります。z の振幅は、x とほぼ同じスケールを使用します。

  • visibility: ランドマークが画像内に表示される確率。

出力には、次のワールド座標(WorldLandmarks)が含まれます。

  • xyz: ヒップの中間点を原点とする、メートル単位の現実世界の 3 次元座標。

  • visibility: ランドマークが画像内に表示される確率。

次の図は、タスク出力の可視化を示しています。

瞑想のポーズをとっている女性。ポーズがワイヤーフレームでハイライト表示され、四肢と胴体の位置が示されます。

省略可能なセグメンテーション マスクは、各ピクセルが検出された人物に属する可能性を表します。次の画像は、タスク出力の分割マスクです。

女性の形状を示す前の画像のセグメンテーション マスク

Pose Landmarker のサンプルコードは、タスクから返された結果を表示する方法を示しています。詳細については、コードサンプルをご覧ください。