Python 姿势特征点检测指南

通过 MediaPipe 姿势特征点标记器任务,您可以检测图像中人体的特征点,或 视频。您可以使用此任务来识别身体的关键位置、分析姿势、 以及对动作进行分类。此任务使用机器学习 (ML) 模型, 处理单一图片或视频。该任务会输出图片中的身体姿势特征点 坐标和三维世界坐标。

您可在 GitHub 如需详细了解功能、模型和配置选项 部分,请参阅概览

代码示例

姿势特征点的示例代码完全实现该定位点。 供您参考。此代码可帮助您测试此任务, 开始构建自己的姿势地标器。您可以查看、运行 修改 姿势地标器示例代码 只需使用网络浏览器即可。

如果您要实现适用于 Raspberry Pi 的姿势特征点器,请参阅 Raspberry Pi 示例 app

设置

本部分介绍了设置开发环境和 专门用于使用姿势特征点 (Pose Markerer) 的代码项目。有关 设置开发环境以使用 MediaPipe 任务,包括 平台版本要求,请参阅 Python 设置指南

<ph type="x-smartling-placeholder">

软件包

MediaPipe 姿势地标器任务需要 mediapipe PyPI 软件包。 您可以使用以下命令安装并导入这些依赖项:

$ python -m pip install mediapipe

导入

导入以下类以访问姿势特征点任务函数:

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

型号

MediaPipe 姿势地标器任务需要一个与此训练兼容的模型, 任务。如需详细了解姿势特征点工具可用的经过训练的模型,请参阅 任务概览的“模型”部分

选择并下载模型,然后将其存储在本地目录中:

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

使用 BaseOptions 对象 model_asset_path 参数指定路径 要使用的模型。如需查看代码示例,请参阅下一部分。

创建任务

MediaPipe 姿势地标器任务使用 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:单图输入的模式。

VIDEO:视频已解码帧的模式。

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)
    

运行任务

姿势特征点器使用 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)
    

请注意以下几点:

  • 在视频模式或直播模式下运行时, 为姿势特征点器任务提供输入帧的时间戳。
  • 在图片或视频模型中运行时,姿势定位器任务 阻塞当前线程,直到它处理完输入图像,或者 帧。
  • 在直播模式下运行时,姿势地标器任务会返回 而且不会阻塞当前线程它将调用结果 并在每次处理完一个监听器后都返回检测结果 输入帧。如果在执行姿势定位器任务时调用检测函数, 正忙于处理另一个帧,则任务将忽略新的输入帧。

有关对图片运行姿势地标器的完整示例,请参阅 代码示例 了解详情。

处理和显示结果

姿势特征点器会针对每次检测返回一个 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)。

  • z:地标深度,臀部中点的深度作为 来源。值越小,地标就越靠近镜头。通过 z 的量级使用的比例与 x 大致相同。

  • visibility:地标在图片中可见的可能性。

输出包含以下世界坐标 (WorldLandmarks):

  • xyz:真实的三维坐标(以米为单位), 作为原点。

  • visibility:地标在图片中可见的可能性。

下图直观显示了任务输出:

可选的分割掩码表示每个像素 。下图是 任务输出:

姿势特征点的示例代码演示了如何显示 结果,请参阅 代码示例 了解详情。