# 人脸检测 (/zh-CN/api-reference/rtc/unity/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>

```csharp
public abstract int EnableFaceDetection(bool enabled);
```

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

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

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

该方法需要在相机启动（如通过调用 [StartPreview [2/2]](/zh-CN/api-reference/rtc/unity/video/video-basic#api_irtcengine_startpreview2) 或 [EnableVideo](/zh-CN/api-reference/rtc/unity/video/video-basic#api_irtcengine_enablevideo) 实现）后调用。

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

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

成功调用该方法后，SDK 会触发 [OnFacePositionChanged](/zh-CN/api-reference/rtc/unity/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/unity/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>

```csharp
public virtual void OnFacePositionChanged(int imageWidth, int imageHeight, Rectangle vecRectangle, int[] vecDistance, int numFaces) {}
```

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

调用 `EnableFaceDetection(true)` 开启本地人脸检测后，你可以通过该回调实时获取以下人脸检测的信息：

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

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

:::note
注：

- 该回调仅适用于 Android 和 iOS 平台。
- 当检测到摄像头前的人脸消失时，该回调会立刻触发；在无人脸的状态下，该回调触发频率会降低，以节省设备耗能。
- 当人脸距离设备屏幕过近时，SDK 不会触发该回调。
:::

<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">

检测到的人脸信息。详见 [Rectangle](/zh-CN/api-reference/rtc/unity/class-rectangle)。

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

人脸和设备屏幕之间的距离 (cm)。

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

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

</Parameter>
</ParameterList>

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

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