# 人脸检测

## 简介

| <p></p><p><strong>人脸检测</strong>是一种生物识别技术，它使用计算机视觉和深度学习算法来识别和验证个体的面部特征</p><ul><li>当前仅可识别追踪一张人脸</li><li>当前仅支持在x2，x3需后续迭代支持</li></ul> |
| ---------------------------------------------------------------------------------------------------------------------------------- |

如视频所示，绿色线条方框为人脸检测结果

{% file src="/files/w25ZWQBXTmdlbv69vsRX" %}

## API介绍

> 在使用人脸识别之前,需要先确认是否赋予相机权限.如果没有,则人脸识别无法正常工作.

[头控模式请选择SLAM模式](/rayneo-devdoc/x-xi-lie/unity-kai-fa/kuai-su-kai-shi/dao-ru-ardk-ji-pei-zhi.md#tou-kong-mo-shi-xuan-ze)

FaceDetectorManager 脚本，参考 TestRuntimeFacial 使用

<table data-header-hidden><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td>调用方法</td><td>使用说明</td><td>示例代码</td></tr><tr><td>开始人脸检测Algorithm.EnableSlamHeadTracker()<br></td><td><strong>参数与返回值细节：函数无参数和返回</strong><br><strong>异常场景说明：</strong>注意拥有相机权限<br><strong>调用时机建议：在需要人脸检测的时进行开启</strong></td><td><pre class="language-c#"><code class="lang-c#">FaceDetectorManager.Ins.StartFaceDectector();
</code></pre><p><br></p></td></tr><tr><td>获取脸部位置.FaceDetectorManager.Ins.GetFacePosition(out bool suc)<br></td><td><strong>参数与返回值细节：</strong>/// &#x3C;param name="suc">代表有没有获取到数据.&#x3C;/param>/// &#x3C;returns>如果是Vector3.zero则是没有获取到.&#x3C;/returns><br><strong>异常场景说明：</strong>注意拥有相机权限<br><strong>调用时机建议：早需要人脸检测的时进行开启</strong></td><td><pre class="language-c#"><code class="lang-c#">    private void Update()
    {
        Vector3 pos = FaceDetectorManager.Ins.GetFacePosition(out bool suc);
        //Debug.LogError("pos:"+ pos+" suc:"+suc);
        if (!suc)
        {
            //获取脸部信息失败.
            return;
        }
        transform.position = Vector3.Lerp(transform.position, Camera.main.transform.TransformPoint(new Vector3(pos.x, pos.y, pos.z + 0.2f)), Time.deltaTime * 15);

```
    Distance.text = transform.position.z.ToString("f02") + "米";


    transform.LookAt(Camera.main.transform);

}
```

</code></pre></td></tr><tr><td>关闭人脸检测FaceDetectorManager.Ins.StopFaceDectector()</td><td><strong>参数与返回值细节：函数无参数和返回</strong><br><strong>异常场景说明：</strong>注意拥有相机权限<br><strong>调用时机建议：在不需要人脸检测的时进行关闭</strong></td><td><pre class="language-c#"><code class="lang-c#">FaceDetectorManager.Ins.StopFaceDectector(); </code></pre></td></tr></tbody></table>

<br>

## 演示 Sample 路径

HelloRayNeo/Scenes/Algorithm/FacialRecog.unity

<figure><img src="/files/kNsaAEVjHaoEWwwdntI4" alt=""><figcaption></figcaption></figure>

## Sample 实现简单说明

1、创建场景，场景中添加预制体 XR Plugin

<figure><img src="/files/Q415YdZCsWUKwKjslUJV" alt=""><figcaption></figcaption></figure>

2、Canvas 下添加测试组件，用来测试脸部识别

<figure><img src="/files/chqKfdc16cWuRGwi4BXd" alt=""><figcaption></figcaption></figure>

3、其中 canvas 中的 Raycaster 是特定组件 XRGraphicRaycaster

<figure><img src="/files/aRgzjh7wL3gs6shHaUi5" alt=""><figcaption></figcaption></figure>

4、打包运行，效果如上

<figure><img src="/files/vZ3UDJzGz28FbdC4kcb3" alt=""><figcaption></figcaption></figure>

5、其中，实现镜腿上的基础操作的脚本可参见 TestRuntimeFacial、FaceDetectorManager

<figure><img src="/files/pswmF8jCZpjtVutJC028" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rayneo.gitbook.io/rayneo-devdoc/x-xi-lie/unity-kai-fa/ar-neng-li-api/ren-lian-jian-ce.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
