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

```ts
public abstract enableFaceDetection(enabled: boolean): number;
```

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

<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/harmonyos/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)了解详情和解决建议。

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

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

## onFacePositionChanged [#onfacepositionchanged]

报告本地人脸检测结果。

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

```ts
onFacePositionChanged?:(
  imageWidth:number, imageHeight:number, faceRectArr:Array<AgoraFacePositionInfo>) => void;
```

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

### 详情 [#详情]

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

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

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

:::caution
注意：

- 当检测到摄像头前的人脸消失时，该回调会立刻触发；在无人脸的状态下，该回调触发频率会降低，以节省设备耗能。
- 当人脸距离设备屏幕过近时，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="facerectarr"></a>
<Parameter name="faceRectArr">

检测到的人脸信息，详见 [AgoraFacePositionInfo](/zh-CN/api-reference/rtc/harmonyos/class-agorafacepositioninfo)。检测到几张人脸，就会报告几个 AgoraFacePositionInfo 数组。数组长度可以为 0，表示没有检测到摄像头前出现人脸。

</Parameter>
</ParameterList>
