Unity APP 实例
Xvisio SDK 文档主页

Apriltag Demo开发


1. Apriltag简介

资料:https://april.eecs.umich.edu/software/apriltag.html

AprilTag是一个视觉基准系统,可用于各种任务,包括AR,机器人和相机校准。这个tag可以直接用打印机打印出来,而AprilTag检测程序可以计算相对于相机的精确3D位置,方向和id。对于OpenMV来说,这个特别有用! 它大概长这个样子:

image

简单来说,只要把这个tag贴到目标上,就可以在OpenMV上识别出这个标签的3D位置,id。

2. Apriltag应用场景

场景:Apriltag.unity
在此场景中我们将展示基于诠视AR眼镜开发的Apriltag识别跟踪功能,我们将通过AR眼镜两侧的鱼眼摄像识别场景中的Apriltag图片并将Cube锚定跟随在Apriltag图片上,通过此项技术我们可以将Apriltag贴在工业设备或产品上,加上三维信息比如模型、图片、文字等多媒体内容来展示我们对该设备的数字孪生信息,在此Demo我们将简单的使用Cube来代替这些多媒体信息。

image

3. 开发教程

如下图所示,我们提供了一个简单的手势开发场景
Step 1:
创建一个Unity空场景,删除场景中原有的摄像机,拖拽XvXR/Prefabs目录下的XvXRManager预制体到场景中(注意transform和rotation都为0),此时场景中就合入了AR眼镜的6tof模块。
image

Step 2:
拖拽XvXR/Prefabs目录下的MixedRealityToolkit、XvXRInput预制体到场景中,此时场景中就和合入了手势功能
image

Step 3:
创建一个空GameOjbect取个名字,添加一个cs代码,在cs代码中我们将在udpata()函数中调用xslam_detect_tags接口开获取Apriltag识别数据,并通过整理返回数据将cube的translation和rotation赋值。就可以将cube和空间中的Apriltag图片产生追踪关系。当你移动Apriltag图片的时候AR眼镜中可以看到一个Cube也同时跟着图片移动。

Demo中的Apriltag图片可以在下面连接下载到:
https://download.xvisiotech.com/unitydevtool/apriltag36h11x16cm.zip

注意:apriltag的对齐位置是图片的左上角对应三维位图的中心位置

    public string TagGroupName = "36h11";
    public double size = 0.16f;//单位是米

    void Update()
    {
        StartDetect();
    }

    void StartDetect()
    {
        tagDetection = AprilTag.StartDetector(TagGroupName, size);

        for (int i = 0; i < tagDetection.Length; i++)
        {
            TagDetection tag = tagDetection[i];
            Debug.Log("AprilTagDemo##StartDetect translation:" + string.Format($"{i}=id:{tag.id},translation:{tag.translation.ToString()}"));
            Debug.Log("AprilTagDemo##StartDetect rotation:" + string.Format($"{i}=id:{tag.id},{tag.rotation.ToString()}"));
            Debug.Log("AprilTagDemo##StartDetect quaternion:" + string.Format($"{i}=id:{tag.id},{tag.quaternion.ToString()}"));
        }

        cube_trans = tagDetection[0].translation;
        cube_rotation = new Quaternion(tagDetection[0].quaternion[0], tagDetection[0].quaternion[1], tagDetection[0].quaternion[2], tagDetection[0].quaternion[3]);

        if (idTxt != null && confidenceTxt != null)
        {
            idTxt.text = tagDetection[0].id.ToString();
            confidenceTxt.text = tagDetection[0].confidence.ToString();
        }

        try
        {
            if (tagDetection.Length > 0)
            {
                //识别出来后对cube位置和姿态进行赋值
                for (int i = 0; i < tagDetection.Length; i++)
                {
                    //从数组中获取translation和rotation数值
                    cube_trans = tagDetection[0].translation;
                    cube_rotation = new Quaternion(tagDetection[0].quaternion[0], tagDetection[0].quaternion[1], tagDetection[0].quaternion[2], tagDetection[0].quaternion[3]);

                    //判断了当识别度大于0.05的时候才回去对cube的姿态进行矫正
                    if (tagDetection[0].confidence > 0.05f)
                    {
                        cube.transform.position = cube_trans;
                        cube.transform.rotation = cube_rotation;
                    }
                    idTxt.text = tagDetection[0].id.ToString();
                    confidenceTxt.text = tagDetection[0].confidence.ToString();
                }
            }
            else
            {
                cube.SetActive(false);
            }
        }
        catch (Exception e)
        {
            cube.SetActive(false);
            //处理异常
            Debug.Log("tagDetection is Null!!!" + e.ToString());
        }
    }

image

Step 4:
打包apk即可看到如下场景
image


Unity APP 实例
Xvisio SDK 文档主页