# 人脸检测 (/zh-CN/api-reference/rtc/unreal-blueprint/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
UFUNCTION(BlueprintCallable, Category = "Agora|IRtcEngine")
	int EnableFaceDetection(bool enabled);
```

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

:::caution
注意： 该方法仅适用于 Android 和 iOS。
:::

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

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

该方法需要在相机启动（如通过调用 StartPreview 或 EnableVideo 实现）后调用。

<a id="api_irtcengine_enablefacedetection__restriction"></a>

### 调用限制 [#调用限制]

无。

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

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

成功调用该方法后，SDK 会触发 [OnFacePositionChanged](/zh-CN/api-reference/rtc/unreal-blueprint/video/video-prenpro/face-detection#callback_irtcengineeventhandler_onfacepositionchanged) 回调向你报告人脸检测的信息：

- 摄像头采集的画面大小
- 人脸在 view 中的位置
- 人脸距设备屏幕的距离

<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/unreal-blueprint/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
UFUNCTION(BlueprintNativeEvent, Category = "Agora|Event")
	void OnFacePositionChanged(int imageWidth, int imageHeight, const TArray<FRectangle>& vecRectangle, const TArray<int>& vecDistance, int numFaces);
```

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

### 详情 [#详情]

调用 [EnableFaceDetection](/zh-CN/api-reference/rtc/unreal-blueprint/video/video-prenpro/face-detection#api_irtcengine_enablefacedetection)`(true)` 开启本地人脸检测后，你可以通过该回调实时获取以下人脸检测的信息：

- 摄像头采集的画面大小
- 人脸在 view 中的位置
- 人脸距设备屏幕的距离

其中，人脸距设备屏幕的距离由 SDK 通过摄像头采集的画面大小和人脸在 view 中的位置拟合计算得出。

:::caution
注意：

- 该回调仅适用于 Android 和 iOS 平台。
- 当检测到摄像头前的人脸消失时，该回调会立刻触发；在无人脸的状态下，该回调触发频率会降低，以节省设备耗能。
- 当人脸距离设备屏幕过近时，SDK 不会触发该回调。
- Android 平台上，人脸距设备屏幕的距离（distance）值有一定误差，请不要用它进行精确计算。
:::

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

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

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

摄像头采集画面的宽度 (px)。

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

摄像头采集画面的高度 (px)。

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

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

- `x`：人脸在 view 中的 x 坐标 (px)。以摄像头采集 view 的左上角为原点，x 坐标为人脸左上角相对于原点的横向位移。
- `y`：人脸在 view 中的 y 坐标 (px)。以摄像头采集 view 的左上角为原点，y 坐标为人脸左上角相对原点的纵向位移。
- `width`：人脸在 view 中的宽度 (px)。
- `height`：人脸在 view 中的高度 (px)。

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

是一个长度为 numFaces 的数组，表示人脸和设备屏幕之间的距离 (cm)。

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

检测的人脸数量。如果为 0，则表示没有检测到人脸。

</Parameter>
</ParameterList>

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

- [IRtcEngineEventHandler](/zh-CN/api-reference/rtc/unreal-blueprint/rtc-interface-class#class_irtcengineeventhandler)
