MediaPipe 手部地标任务可让您检测图片中手部的特征点。以下说明介绍了如何在 Python 中使用手特征器。GitHub 上提供了这些说明中描述的代码示例。
如需详细了解此任务的功能、模型和配置选项,请参阅概览。
代码示例
手点地标的示例代码在 Python 中提供了此任务的完整实现,以供您参考。此代码可帮助您测试此任务,并开始构建您自己的地标检测器。只需使用网络浏览器,您就可以查看、运行和修改 Hand Markerer 示例代码。
初始设置
本部分介绍了专门针对使用手动地标设置开发环境和代码项目的关键步骤。如需了解如何为使用 MediaPipe 任务设置开发环境的一般信息(包括平台版本要求),请参阅 Python 设置指南。
软件包
MediaPipe 手点地标任务需要使用 mediapipe PyPI 软件包。您可以使用以下代码安装并导入这些依赖项:
$ python -m pip install mediapipe
导入
导入以下类来访问 Hand Markerer 任务函数:
import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision
模型
MediaPipe 手点地标任务需要使用与此任务兼容的经过训练的模型。如需详细了解手特征点可用的经过训练的模型,请参阅任务概览“模型”部分。
选择并下载模型,然后将其存储在本地目录中:
model_path = '/absolute/path/to/gesture_recognizer.task'
使用 BaseOptions
对象 model_asset_path
参数指定要使用的模型的路径。如需查看代码示例,请参阅下一部分。
创建任务
MediaPipe 手点地标任务使用 create_from_options
函数来设置该任务。create_from_options
函数接受要处理的配置选项的值。如需详细了解配置选项,请参阅配置选项。
以下代码演示了如何构建和配置此任务。
这些示例还显示了图片、视频文件和直播的任务构造变体。
映像
import mediapipe as mp BaseOptions = mp.tasks.BaseOptions HandLandmarker = mp.tasks.vision.HandLandmarker HandLandmarkerOptions = mp.tasks.vision.HandLandmarkerOptions VisionRunningMode = mp.tasks.vision.RunningMode # Create a hand landmarker instance with the image mode: options = HandLandmarkerOptions( base_options=BaseOptions(model_asset_path='/path/to/model.task'), running_mode=VisionRunningMode.IMAGE) with HandLandmarker.create_from_options(options) as landmarker: # The landmarker is initialized. Use it here. # ...
视频
import mediapipe as mp BaseOptions = mp.tasks.BaseOptions HandLandmarker = mp.tasks.vision.HandLandmarker HandLandmarkerOptions = mp.tasks.vision.HandLandmarkerOptions VisionRunningMode = mp.tasks.vision.RunningMode # Create a hand landmarker instance with the video mode: options = HandLandmarkerOptions( base_options=BaseOptions(model_asset_path='/path/to/model.task'), running_mode=VisionRunningMode.VIDEO) with HandLandmarker.create_from_options(options) as landmarker: # The landmarker is initialized. Use it here. # ...
直播
import mediapipe as mp BaseOptions = mp.tasks.BaseOptions HandLandmarker = mp.tasks.vision.HandLandmarker HandLandmarkerOptions = mp.tasks.vision.HandLandmarkerOptions HandLandmarkerResult = mp.tasks.vision.HandLandmarkerResult VisionRunningMode = mp.tasks.vision.RunningMode # Create a hand landmarker instance with the live stream mode: def print_result(result: HandLandmarkerResult, output_image: mp.Image, timestamp_ms: int): print('hand landmarker result: {}'.format(result)) options = HandLandmarkerOptions( base_options=BaseOptions(model_asset_path='/path/to/model.task'), running_mode=VisionRunningMode.LIVE_STREAM, result_callback=print_result) with HandLandmarker.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_hands |
手部特征点检测器检测到的手部数量上限。 | Any integer > 0 |
1 |
min_hand_detection_confidence |
要在手掌检测模型中被视为成功的手部检测的最低置信度分数。 | 0.0 - 1.0 |
0.5 |
min_hand_presence_confidence |
手部特征点检测模型中手存在状态分数的最低置信度分数。在视频模式和直播模式下,如果手部特征点模型的手部存在置信度分数低于此阈值,则手部特征点会触发手掌检测模型。否则,轻量级手部跟踪算法会确定手的位置,以便进行后续地标检测。 | 0.0 - 1.0 |
0.5 |
min_tracking_confidence |
手部跟踪被视为成功所需的最低置信度分数。这是当前帧和最后一帧中手之间的边界框 IoU 阈值。在“手部特征点”的“视频”模式和“流式”模式下,如果跟踪失败,“手部特征点”会触发手部检测。否则,它会跳过手部检测。 | 0.0 - 1.0 |
0.5 |
result_callback |
设置结果监听器,以在手形地标处于直播模式时异步接收检测结果。
仅在跑步模式设为“LIVE_STREAM ”时适用 |
N/A | 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)
运行任务
手动特征点使用 detect、detect_for_video 和 detect_async 函数来触发推断。对于手部特征点检测,这涉及到预处理输入数据、检测图片中的手部以及检测手部特征点。
以下代码演示了如何使用任务模型执行处理。
映像
# Perform hand landmarks detection on the provided single image. # The hand landmarker must be created with the image mode. hand_landmarker_result = landmarker.detect(mp_image)
视频
# Perform hand landmarks detection on the provided single image. # The hand landmarker must be created with the video mode. hand_landmarker_result = landmarker.detect_for_video(mp_image, frame_timestamp_ms)
直播
# Send live image data to perform hand landmarks detection. # The results are accessible via the `result_callback` provided in # the `HandLandmarkerOptions` object. # The hand landmarker must be created with the live stream mode. landmarker.detect_async(mp_image, frame_timestamp_ms)
请注意以下几点:
- 在视频模式或直播模式下运行时,您还必须为手形地标任务提供输入帧的时间戳。
- 在图片或视频模型中运行时,手动标记任务将阻塞当前线程,直到处理完输入图片或帧。
- 在直播模式下运行时,Hander Markerer 任务不会阻塞当前线程,而是立即返回。每当处理完输入帧时,它都会使用检测结果调用其结果监听器。如果在手特征点任务正忙于处理另一帧时调用检测函数,该任务将忽略新的输入帧。
如需查看在图片上运行手标记工具的完整示例,请参阅代码示例了解详情。
处理和显示结果
手部特征点每次检测运行都会生成一个手部特征点结果对象。结果对象包含图片坐标中的手部地标、世界坐标中的手部地标,以及检测到的手的惯用手(左/右)。
下面显示了此任务的输出数据示例:
HandLandmarkerResult
输出包含三个组成部分。每个组成部分都是一个数组,其中每个元素都包含检测到的一只手的以下结果:
用手习惯
用手表示检测到的手是左手还是右手。
地标
共有 21 个手部位置标记,每个位置标记由
x
、y
和z
坐标组成。x
和y
坐标分别根据图片宽度和高度标准化为 [0.0, 1.0]。z
坐标表示地标深度,以手腕处的深度为原点。值越小,地标越靠近相机。z
的大小大小与x
的大小大致相同。世界地标
21 只手部位标也以世界坐标表示。每个地标由
x
、y
和z
组成,表示现实世界的 3D 坐标(以米为单位),原点位于手的几何中心。
HandLandmarkerResult:
Handedness:
Categories #0:
index : 0
score : 0.98396
categoryName : Left
Landmarks:
Landmark #0:
x : 0.638852
y : 0.671197
z : -3.41E-7
Landmark #1:
x : 0.634599
y : 0.536441
z : -0.06984
... (21 landmarks for a hand)
WorldLandmarks:
Landmark #0:
x : 0.067485
y : 0.031084
z : 0.055223
Landmark #1:
x : 0.063209
y : -0.00382
z : 0.020920
... (21 world landmarks for a hand)
下图显示了任务输出的可视化效果:
手特征点示例代码演示了如何显示任务返回的结果。如需了解详情,请参阅代码示例。