
# MediaPipe Hands：端侧实时手部追踪

范张、瓦伦丁·巴扎列夫斯基、安德烈·瓦库诺夫

安德烈·特卡琴卡、乔治·宋、卓玲·张、马蒂亚斯·格伦德曼，谷歌研究院

美国加利福尼亚州山景城露天剧场大道1600号，邮编94043

{zhafang, valik, vakunov, atkach, gsung, chuoling, grundman}@google.com

## 摘要

我们提出了一种端侧实时手部追踪解决方案，它可以通过单个RGB摄像头为AR/VR应用预测人手骨骼。我们的流程包含两个模型：1）手掌检测器，用于提供手部的边界框；2）手部关键点模型，用于预测手部骨骼。该方案通过MediaPipe $^{[12]}$实现，这是一款用于构建跨平台机器学习解决方案的框架。所提出的模型和流程架构在移动GPU上可实现实时推理速度，且预测精度较高。MediaPipe Hands的开源代码地址为<https://mediapipe.dev>。

### 1. 引言

手部追踪是为AR/VR提供自然交互与沟通方式的关键组件，一直是行业内的热门研究方向[^2] [^15]。基于视觉的手部姿态估计已有多年研究历史。以往的大部分工作都需要专用硬件，例如深度传感器[^13][^16][^17][^3][^4]。其他解决方案则不够轻量，无法在普通移动设备上实时运行[^5]，因此仅能应用于配备高性能处理器的平台。在本文中，我们提出了一种无需额外硬件、可在移动设备上实时运行的创新解决方案。我们的主要贡献如下：

一种高效的两阶段手部追踪流程，能够在移动设备上实时追踪多只手。

- 一种仅需RGB输入即可预测2.5D手部姿态的手部姿态估计模型。

- 开源的手部追踪流程，可作为适用于多种平台的即用型解决方案，包括安卓、iOS、网页（Tensorflow.js[^7]）以及台式电脑。

![Image](images/3d89f62c60e004172b75b28451e2b6ecc9e5a54c234e37ac15cd13b214098c6d.jpg)

<div style="text-align: center;">Figure 1: Rendered hand tracking result. (Left): Hand landmarks with relative depth presented in different shades. The lighter and larger the circle, the closer the landmark is towards the camera. (Right): Real-time multi-hand tracking on Pixel 3.</div>

### 2. 架构

我们的手部追踪解决方案采用了由两个协同工作的模型组成的机器学习流程：

- 手掌检测器，它对整幅输入图像进行处理，通过带方向的手部边界框定位手掌。

- 手部关键点模型，它对手掌检测器提供的裁剪后的手部边界框进行处理，输出高精度的2.5D关键点。

将经过精准裁剪的手掌图像提供给手部关键点模型，可大幅减少数据增强的需求（如旋转、平移和缩放），让网络能将大部分算力用于提升关键点的定位精度。在实时追踪场景中，我们会根据上一帧的关键点预测结果生成当前帧的输入边界框，从而避免每帧都调用检测器。只有在第一帧或手部预测结果显示手部已丢失时，才会调用检测器。

---

![Image](images/50e2bd0e9e4dcacdcf52b85e156c95f558e8cde089937c8530281d7debd0e621.jpg)

<div style="text-align: center;">Figure 2: Palm detector model architecture.</div>

#### 2.1. BlazePalm检测器

为了检测初始手部位置，我们采用了一种针对移动端实时应用优化的单次检测模型，该模型类似于BlazeFace[^1]，同样也应用于MediaPipe[^12]中。手部检测是一项相当复杂的任务：我们的模型需要应对各种不同大小的手部，其尺度范围极广（$\sim20x$），同时还要能够检测出被遮挡或自身遮挡的手部。面部具有对比度较高的特征，比如眼睛和嘴巴周围的区域，而手部缺乏这类特征，因此仅依靠视觉特征来可靠地检测手部相对较为困难。

我们的解决方案通过多种策略应对上述挑战。

首先，我们训练的是手掌检测器而非手部检测器，因为估算手掌、拳头等刚性物体的边界框，要比检测带有关节的指头要简单得多。此外，由于手掌属于较小的物体，即便在双手相互遮挡的情况下（比如握手时），非极大值抑制算法也能发挥良好作用。而且，手掌可以用正方形边界框来建模[^11]，无需考虑其他长宽比，这样就能将锚点数量减少3到5倍。

其次，我们采用了类似FPN[^9]的编码器-解码器特征提取器，以便即使对于小型物体也能具备更强的场景上下文感知能力。

最后，我们在训练过程中最小化焦点损失$^{[10]}$，以应对因尺度差异较大而产生的大量锚点。图2展示了高级手掌检测器的架构。表1则呈现了我们各设计要素的消融研究结果。

![Image](images/62daa471fc35c81d3bcfb69580a5876bf62677378736aa1d9d8ae1763c846819.jpg)

<div style="text-align: center;">Figure 3: Architecture of our hand landmark model. The model has three outputs sharing a feature extractor. Each head is trained by correspondent datasets marked in the same color. See Section 2.2 for more detail.</div>

#### 2.2. 手部关键点模型

在对整幅图像进行手掌检测后，后续的手部关键点模型会通过回归方法，在检测到的手部区域内精确定位21个2.5D坐标的关键点。该模型能够学习一致的内部手部姿态表示，即便对于部分可见或自身遮挡的手部也具有较强的鲁棒性。该模型有三个输出（见图3）：

1. 由x、y坐标和相对深度组成的21个手部关键点。

2. 用于指示输入图像中存在手部的概率的手部标志。

3. 左右手的二元分类结果。

对于这21个关键点，我们采用了与[^14]相同的拓扑结构。如下文所述，2D坐标既从真实世界图像中学习，也从合成数据集中学习；而相对于腕点的相对深度则仅从合成图像中学习。为了应对跟踪失败的情况，我们为模型设计了另一个输出，类似于[^8]，用于计算在给定裁剪区域内确实存在位置合理的手部的概率。如果该概率低于阈值，就会触发检测器重置跟踪过程。对于在AR/VR中使用手部进行有效交互而言，手的左右属性是另一个重要特征。这一点在某些应用中尤为有用，因为每只手都对应着独特的功能。因此，我们开发了二元分类头来预测输入的手部是左手还是右手。我们的方案旨在实现移动端的实时GPU推理，但我们也设计了更轻量和更复杂的模型版本，分别用于适配没有合适GPU支持的移动设备上的CPU推理，以及满足桌面端对更高精度的需求。

---

### 3. 数据集与标注

为了获取基准数据，我们创建了以下涵盖问题不同方面的数据集：

- 自然场景数据集：该数据集包含6000张种类繁多的图像，例如地理多样性、各种光照条件以及手部外观差异。该数据集的局限性在于它不包含手部的复杂关节活动。

- 内部采集的手势数据集：该数据集包含10000张图像，涵盖了所有物理上可能的手势的各种角度。该数据集的局限性在于它仅由30人采集，背景变化有限。自然场景数据集和内部采集的数据集能很好地互补，以提升模型的鲁棒性。

合成数据集：为了更全面地覆盖可能的手部姿态，并为深度信息提供额外的监督信号，我们在各种背景下渲染高质量的合成手部模型，并将其映射到对应的3D坐标。我们使用的是一款商用3D手部模型，该模型配有24根骨骼，包含36种控制手指和手掌厚度的混合变形形状。该模型还提供了5种不同肤色的纹理。我们创建了手部姿态之间的变换视频序列，并从这些视频中采样了10万张图像。每个姿态都是在随机的高动态范围光照环境下，通过三种不同的相机渲染而成的。具体示例见图4。

对于手掌检测器，我们仅使用自然场景数据集，该数据集足以定位手部并提供最丰富的外观多样性。不过，所有数据集都用于训练手部关键点模型。我们对真实图像标注了21个关键点，对合成图像则使用投影得到的3D关节真值进行标注。对于手部存在性判断，我们选择一部分真实图像作为正样本，并在标注的手部区域之外采样作为负样本。对于手性判断，我们标注了一部分真实图像的手性信息以提供相关数据。

### 4. 实验结果

对于手部关键点模型，实验表明真实数据集与合成数据集的结合能取得最佳效果。详情见表2。我们仅在真实图像上进行评估。除了质量提升外，使用大规模合成数据集训练还能减少帧与帧之间的视觉抖动现象。这一观察结果让我们相信，扩大真实数据集的规模有助于提升模型的泛化能力。

我们的目标是实现在移动设备上的实时性能。我们尝试了不同尺寸的模型，发现“完整”模型（见表3）在质量和速度之间取得了良好的平衡。

![Image](images/e58fcfc19a9301a78d1a16b1ab48696e3b94d9cd8c97b41a58aaf5e5ae8e02be.jpg)

<div style="text-align: center;">Figure 4: Examples of our datasets. (Top): Annotated real-world images. (Bottom): Rendered synthetic hand images with ground truth annotation. See Section 3 for details.</div>

<table border=1 style='margin: auto; word-wrap: break-word;'><tr><td style='text-align: center; word-wrap: break-word;'>Model Variation</td><td style='text-align: center; word-wrap: break-word;'>Average Precision</td></tr><tr><td style='text-align: center; word-wrap: break-word;'>No decoder + cross entropy loss</td><td style='text-align: center; word-wrap: break-word;'>86.22%</td></tr><tr><td style='text-align: center; word-wrap: break-word;'>Decoder + cross entropy loss</td><td style='text-align: center; word-wrap: break-word;'>94.07%</td></tr><tr><td style='text-align: center; word-wrap: break-word;'>Decoder + focal loss</td><td style='text-align: center; word-wrap: break-word;'>95.7%</td></tr></table>

<div style="text-align: center;">Table 1: Ablation study of palm detector design elements of palm detector.</div>

<table border=1 style='margin: auto; word-wrap: break-word;'><tr><td style='text-align: center; word-wrap: break-word;'>Dataset</td><td style='text-align: center; word-wrap: break-word;'>MSE normalized by palm size</td></tr><tr><td style='text-align: center; word-wrap: break-word;'>Only real-world</td><td style='text-align: center; word-wrap: break-word;'>16.1%</td></tr><tr><td style='text-align: center; word-wrap: break-word;'>Only synthetic</td><td style='text-align: center; word-wrap: break-word;'>25.7%</td></tr><tr><td style='text-align: center; word-wrap: break-word;'>Combined</td><td style='text-align: center; word-wrap: break-word;'>13.4%</td></tr></table>

<div style="text-align: center;">Table 2: Results of our model trained from different datasets.</div>

进一步增加模型容量只会带来微小的质量提升，却会显著降低速度（详情见表3）。我们使用TensorFlow Lite GPU后端进行设备端推理 $^{[6]}$。

<table border=1 style='margin: auto; word-wrap: break-word;'><tr><td style='text-align: center; word-wrap: break-word;'>Model</td><td style='text-align: center; word-wrap: break-word;'>Params (M)</td><td style='text-align: center; word-wrap: break-word;'>MSE</td><td style='text-align: center; word-wrap: break-word;'>Time(ms) Pixel 3</td><td style='text-align: center; word-wrap: break-word;'>Time(ms) Samsung S20</td><td style='text-align: center; word-wrap: break-word;'>Time(ms) iPhone11</td></tr><tr><td style='text-align: center; word-wrap: break-word;'>Light</td><td style='text-align: center; word-wrap: break-word;'>1</td><td style='text-align: center; word-wrap: break-word;'>11.83</td><td style='text-align: center; word-wrap: break-word;'>6.6</td><td style='text-align: center; word-wrap: break-word;'>5.6</td><td style='text-align: center; word-wrap: break-word;'>1.1</td></tr><tr><td style='text-align: center; word-wrap: break-word;'>Full</td><td style='text-align: center; word-wrap: break-word;'>1.98</td><td style='text-align: center; word-wrap: break-word;'>10.05</td><td style='text-align: center; word-wrap: break-word;'>16.1</td><td style='text-align: center; word-wrap: break-word;'>11.1</td><td style='text-align: center; word-wrap: break-word;'>5.3</td></tr><tr><td style='text-align: center; word-wrap: break-word;'>Heavy</td><td style='text-align: center; word-wrap: break-word;'>4.02</td><td style='text-align: center; word-wrap: break-word;'>9.817</td><td style='text-align: center; word-wrap: break-word;'>36.9</td><td style='text-align: center; word-wrap: break-word;'>25.8</td><td style='text-align: center; word-wrap: break-word;'>7.5</td></tr></table>

<div style="text-align: center;">Table 3: Hand landmark model performance characteristics.</div>

---

### 5. 在MediaPipe中的实现

借助MediaPipe $^{[12]}$，我们的手部追踪流程可以构建为由模块化组件组成的有向图，这些组件被称为计算器。MediaPipe提供了一系列可扩展的计算器，用于解决模型推理、媒体处理以及各种设备和平台上的数据转换等任务。诸如裁剪、渲染和神经网络计算等单个计算器还经过了进一步优化，以利用GPU加速。例如，我们在大多数现代手机上都采用了TFLite GPU推理技术。

我们的手部追踪MediaPipe图如图5所示。该图由两个子图组成，一个用于手部检测，另一个用于关键点计算。MediaPipe提供的一项关键优化是：手掌检测器仅在必要时才运行（频率较低），从而节省大量计算资源。我们通过利用前一帧中计算出的手部关键点来确定当前视频帧中的手部位置，从而无需在每一帧都应用手掌检测器。为了增强鲁棒性，手部追踪模型还会输出一个额外的标量值，用以表示输入图像中存在手部且对齐程度合理的置信度。只有当该置信度低于特定阈值时，才会对下一帧重新应用手部检测模型。

### 6. 应用示例

我们的手部追踪解决方案可广泛应用于手势识别和AR特效等多种场景。基于预测出的手部骨骼数据，我们采用简单算法来计算手势，详见图6。首先，通过关节的累积角度来确定每个手指的状态，例如弯曲或伸直。接着，将手指状态集合映射到预定义的手势集合中。这种简单却有效的方法能够让我们以较高的质量识别基本的静态手势。除了静态手势识别外，还可以利用一系列关键点来预测动态手势。另一种应用是在骨骼之上叠加AR特效。基于手部的AR特效目前颇受欢迎。图7展示了采用霓虹灯风格渲染的手部骨骼AR示例。

### 7. 结论

在本文中，我们提出了MediaPipe Hands——一种端到端的手部追踪解决方案，能够在多个平台上实现实时性能。我们的流程无需任何专用硬件即可预测2.5D关键点，因此可轻松部署到普通设备上。我们已将该流程开源，旨在鼓励研究人员和工程师利用它来开发手势控制和创意AR/VR应用。

![Image](images/1d99f8cf9ca1ade93bbd7d70cfc5a1428e7b32b6a124e2442972ef102f96eb00.jpg)

<div style="text-align: center;">Figure 5: The hand landmark models output controls when the hand detection model is triggered. This behavior is achieved by MediaPipes powerful synchronization building blocks, resulting in high performance and optimal throughput of the ML pipeline.</div>

![Image](images/bc6457abd3644e8e8d59ab382c02e6d7d3d508795589751d2662439204f18877.jpg)

<div style="text-align: center;">Figure 6: Screenshots of real-time gesture recognition. Semantics of gestures are rendered at top of the images.</div>

## 参考文献

## [^1]: Valentin Bazarevsky, Yury Kartynnik, Andrey Vakunov, Karthik Raveendran, and Matthias Grundmann. Blazeface: Sub-millisecond neural face detection on mobile gpus

---

![Image](images/abf083f8517cfabf0e9242d5cf3833a0f52fb67897bca474d29166aa7dc42431.jpg)

![Image](images/925434c7339b12c0a1982ea8a4ebbee7b7a05f948bb75821b7261c09ac2eb3e9.jpg)

<div style="text-align: center;">Figure 7: Example of real-time AR effects based on our predicted hand skeleton.</div>

CoRR，abs/1907.05047，2019年2月

[^2]: Facebook. Oculus Quest Hand Tracking.

<https://www.oculus.com/blog/oculus-connect-6-introducing-hand-tracking-on-oculus-quest-facebook-horizon-and-more/>.

[^3]: Liuhao Ge, Hui Liang, Junsong Yuan, and Daniel Thalmann. Robust 3d hand pose estimation in single depth images: from single-view cnn to multi-view cnns. In Proceedings of the IEEE conference on computer vision and pattern recognition, pages 3593–3601, 2016. 1

[^4]: Liuhao Ge, Hui Liang, Junsong Yuan, and Daniel Thalmann. Robust 3d hand pose estimation from single depth images using multi-view cnns. IEEE Transactions on Image Processing, 27(9):4422–4436, 2018. 1

[^5]: Liuhao Ge, Zhou Ren, Yuncheng Li, Zehao Xue, Yingying Wang, Jianfei Cai, and Junsong Yuan. 3d hand shape and pose estimation from a single rgb image. In Proceedings of the IEEE conference on computer vision and pattern recognition, pages 10833–10842, 2019. 1

[^6]: Google. Tensorflow lite on GPU.

<https://www.tensorflow.org/lite/performance/gpu_advanced.3>

[^7]: Google. Tensorflow.js Handpose.

<https://blog.tensorflow.org/2020/03/>

face-and-hand-tracking-in-browser-with-

mediapipe-and-tensorflowjs.html.1

[^8]: Yury Kartynnik, Artsiom Ablavatski, Ivan Grishchenko, and Matthias Grundmann. Real-time facial surface geometry from monocular video on mobile gpus. CoRR, abs/1907.06724, 2019. 2

[^9]: Tsung-Yi Lin, Piotr Dollár, Ross B. Girshick, Kaiming He, Bharath Hariharan, and Serge J. Belongie. Feature pyramid networks for object detection. CoRR, abs/1612.03144, 2016.

[^10]: Tsung-Yi Lin, Priya Goyal, Ross B. Girshick, Kaiming He, and Piotr Dollár. Focal loss for dense object detection. CoRR, abs/1708.02002, 2017. 2

[^11]: Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, Scott E. Reed, Cheng-Yang Fu, and Alexan-

C. Berg. SSD：单次多框检测器。CoRR, abs/1512.02325, 2015. 2

[^12]: Camillo Lugaresi, Jiuqiang Tang, Hadon Nash, Chris McClanahan, Esha Uboweja, Michael Hays, Fan Zhang, Chuo-Ling Chang, Ming Guang Yong, Juhyun Lee, Wan-Teh Chang, Wei Hua, Manfred Georg, and Matthias Grundmann. Mediapipe: A framework for building perception pipelines. volume abs/1906.08172, 2019. 1, 2, 4

[^13]: Iason Oikonomidis, Nikolaos Kyriazis, and Antonis A Argyros. Efficient model-based 3d tracking of hand articulations using kinect. 1

[^14]: Tomas Simon, Hanbyul Joo, Iain A. Matthews, and Yaser Sheikh. Hand keypoint detection in single images using multiview bootstrapping. CoRR, abs/1704.07809, 2017. 2

[^15]: Snapchat. Lens Studio by Snap Inc.

<https://lensstudio.snapchat.com/templates/object/hand/.1>

[^16]: Andrea Tagliasacchi, Matthias Schröder, Anastasia Tkach, Sofien Bouaziz, Mario Botsch, and Mark Pauly. Robust articulated-icp for real-time hand tracking. In Computer Graphics Forum, volume 34, pages 101–114. Wiley Online Library, 2015. 1

[^17]: Chengde Wan, Thomas Probst, Luc Van Gool, and Angela Yao. Self-supervised 3d hand pose estimation through training by fitting. In Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, pages 10853–10862, 2019. 1
