Python 的姿勢地標偵測指南

MediaPipe Pose 地標工作可讓您偵測圖片或影片中的人體地標。您可以使用這項工作來識別關鍵主體位置、分析姿勢,以及將運動分類。這項工作採用的機器學習 (ML) 模型 適用於單一圖片或影片工作會輸出圖片座標和 3D 世界座標中的身體姿勢地標。

您可以前往 GitHub 取得這些操作說明中提及的程式碼範例。如要進一步瞭解這項工作的功能、模型和設定選項,請參閱總覽

程式碼範例

Posemarker 的範例程式碼提供這項工作在 Python 中的完整實作,供您參考。這個程式碼可協助您測試這項工作,並開始建構自己的姿勢地標。只要使用網路瀏覽器,就可以查看、執行及編輯Pose Marker 範例程式碼

如果您要實作 Raspberry Pi 的 Pose Ranker,請參閱 Raspberry Pi 範例應用程式

設定

本節說明設定開發環境及專門為使用 Posemarker 的程式碼專案的重要步驟。如需瞭解如何使用 MediaPipe 工作設定開發環境的一般資訊,包括平台版本需求,請參閱 Python 設定指南

套裝組合

MediaPipe Posemarker 工作需要 mediapipe PyPI 套件。 您可以使用下列程式碼安裝並匯入這些依附元件:

$ python -m pip install mediapipe

匯入

匯入下列類別,即可使用 Posemarker 工作功能:

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

型號

MediaPipe Posemarker 工作需要使用與這項工作相容的已訓練模型。如要進一步瞭解 Pose Marker 可用的已訓練模型,請參閱工作總覽「模型」一節

選取並下載模型,然後儲存至本機目錄:

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

使用 BaseOptions 物件 model_asset_path 參數指定要使用的模型路徑。如需程式碼範例,請參閱下一節。

建立工作

MediaPipe Posemarker 工作會使用 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 設定工作的執行模式。共有三種模式:

IMAGE:單一圖片輸入的模式。

影片:影片已解碼影格的模式。

LIVE_STREAM:輸入資料串流 (例如攝影機) 的直播模式。在這個模式下,必須呼叫 resultListener 才能設定事件監聽器,以非同步方式接收結果。
{IMAGE, VIDEO, LIVE_STREAM} IMAGE
num_poses 姿勢地標可以偵測到的姿勢數量上限。 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 是否要為偵測到的姿勢輸出區隔遮罩。 Boolean False
result_callback 設定結果事件監聽器,就能在直播模式下以非同步方式接收地標結果。 只能在執行模式設為 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 Ranker」會使用 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)
    

注意事項:

  • 以影片模式或直播模式執行時,請一併提供 Posemarker 任務的輸入影格的時間戳記。
  • 在圖片或影片模型中執行時,「Posemarker」工作會封鎖目前的執行緒,直到完成輸入圖片或影格的處理為止。
  • 以直播模式執行時,「Pose 地標」工作會立即回傳,且不會封鎖目前的執行緒。每當它處理完輸入影格時,就會透過偵測結果叫用結果監聽器。如果 Pose 地標工作正忙於處理另一個影格時呼叫偵測功能,則任務會忽略新的輸入影格。

如需在圖片上執行姿勢地標的完整範例,請參閱程式碼範例

處理並顯示結果

Pose 地標 er 會在每次執行偵測時傳回 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:地標座標介於 0.0 到 1.0 之間,依照圖片寬度 (x) 和高度 (y) 進行正規化。

  • z:地標深度,以臀部中間點為起點。值越小,地標與相機鏡頭越近。Z 規模使用的縮放比例與 x 大致相同。

  • visibility:地標在圖片中可見的可能性。

輸出結果包含以下世界座標 (WorldLandmarks):

  • xyz:實際 3 維座標 (以公尺為單位),以臀部中間點做為起點。

  • visibility:地標在圖片中可見的可能性。

下圖以視覺化方式呈現工作輸出內容:

選用的區隔遮罩代表每個像素屬於偵測到人員的可能性。下圖是工作輸出的區隔遮罩:

Posemarker 範例程式碼會示範如何顯示工作傳回的結果,詳情請參閱程式碼範例