# 人脸检测 (/zh-CN/api-reference/rtc/cpp-all-platforms/video/video-prenpro/face-detection)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

<a id="toc_face_detection"></a>

<a id="api_irtcengine_enablefacedetection"></a>

<a id="ariaid-title2"></a>

## enableFaceDetection [#enablefacedetection]

启用或关闭本地用户的人脸检测功能。

<a id="api_irtcengine_enablefacedetection__prototype"></a>

```cpp
virtual int enableFaceDetection(bool enabled) = 0;
```

<a id="api_irtcengine_enablefacedetection__detailed_desc"></a>

:::note
注： 该方法仅适用于 Android 和 iOS 平台。
:::

<a id="api_irtcengine_enablefacedetection__timing"></a>

### 调用时机 [#调用时机]

该方法需在调用 `startPreview` 或 [enableVideo](/zh-CN/api-reference/rtc/cpp-all-platforms/video/video-basic#api_irtcengine_enablevideo) 开启摄像头之后调用。

<a id="api_irtcengine_enablefacedetection__related"></a>

### 相关回调 [#相关回调]

成功调用该方法后，SDK 会触发 [onFacePositionChanged](/zh-CN/api-reference/rtc/cpp-all-platforms/video/video-prenpro/face-detection#callback_irtcengineeventhandler_onfacepositionchanged) 回调，报告本地用户的人脸信息，包括：

- 本地视频的宽度和高度。
- 人脸在本地视图中的位置。
- 人脸与屏幕之间的距离。

<a id="api_irtcengine_enablefacedetection__parameters"></a>

### 参数 [#参数]

<ParameterList>
<a id="enabled"></a>
<Parameter name="enabled">

是否启用本地用户的人脸检测功能：

- true：启用人脸检测。
- false：（默认）关闭人脸检测。

</Parameter>
</ParameterList>

<a id="api_irtcengine_enablefacedetection__return_values"></a>

### 返回值 [#返回值]

- 0：方法调用成功。
- &lt; 0：方法调用失败。详见[错误码](/zh-CN/api-reference/rtc/error-code)了解详情和解决建议。

### 所属接口类 [#所属接口类]

- [IRtcEngine](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_irtcengine)

<a id="callback_irtcengineeventhandler_onfacepositionchanged"></a>

<a id="ariaid-title3"></a>

## onFacePositionChanged [#onfacepositionchanged]

上报本地用户的人脸检测结果。

<a id="callback_irtcengineeventhandler_onfacepositionchanged__prototype"></a>

```cpp
virtual void onFacePositionChanged(int imageWidth, int imageHeight, const Rectangle* vecRectangle, const int* vecDistance, int numFaces)
```

<a id="callback_irtcengineeventhandler_onfacepositionchanged__detailed_desc"></a>

调用 [enableFaceDetection](/zh-CN/api-reference/rtc/cpp-all-platforms/video/video-prenpro/face-detection#api_irtcengine_enablefacedetection) 启用人脸检测后，你可以实时获取以下本地用户的信息：

- 本地视频的宽度和高度。
- 人脸在本地视图中的位置。
- 人脸与屏幕之间的距离。

该距离值基于本地视频尺寸和人脸位置的拟合计算得出。

:::note
注：

- 该回调仅适用于 Android 和 iOS 平台。
- 当检测到摄像头前的人脸消失时，会立即触发该回调。当未检测到人脸时，为降低本地设备功耗，该回调的触发频率会降低。
- 当人脸靠近屏幕时，SDK 会停止触发该回调。
- 在 Android 平台，该回调中上报的距离值可能与实际距离存在偏差，因此声网不建议将其用于精确计算。
:::

<a id="callback_irtcengineeventhandler_onfacepositionchanged__timing"></a>

### 触发时机 [#触发时机]

当检测到人脸位置变化时触发。

<a id="callback_irtcengineeventhandler_onfacepositionchanged__parameters"></a>

### 参数 [#参数-1]

<ParameterList>
<a id="imagewidth"></a>
<Parameter name="imageWidth">

本地摄像头采集的视频图像的宽度（像素）。

</Parameter>
<a id="imageheight"></a>
<Parameter name="imageHeight">

本地摄像头采集的视频图像的高度（像素）。

</Parameter>
<a id="vecrectangle"></a>
<Parameter name="vecRectangle">

长度为 `numFaces` 的数组，表示检测到的人脸信息：

- `x`：人脸在本地视图中的 x 坐标（像素），以视图左上角为原点，表示人脸相对于原点的水平位置。
- `y`：人脸在本地视图中的 y 坐标（像素），以视图左上角为原点，表示人脸相对于原点的垂直位置。
- `width`：人脸在捕获视图中的宽度（像素）。
- `height`：人脸在捕获视图中的高度（像素）。详见 [Rectangle](/zh-CN/api-reference/rtc/cpp-all-platforms/class-rectangle)。

</Parameter>
<a id="vecdistance"></a>
<Parameter name="vecDistance">

长度为 `numFaces` 的数组，表示每张人脸与设备屏幕之间的距离（厘米）。

</Parameter>
<a id="numfaces"></a>
<Parameter name="numFaces">

检测到的人脸数量。若为 0，表示未检测到人脸。

</Parameter>
</ParameterList>

### 所属接口类 [#所属接口类-1]

- [IRtcEngineEventHandler](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_irtcengineeventhandler)
