Unity_ARCore入门(一) 图片识别实例(转载)

【转载】作者:looking8k https://www.bilibili.com/read/cv2301383/

1、概述
针对AR界的图片识别功能大致有3家;

1、ARkit:苹果的开发AR的SDK;

2、ARCore:GoogleAR的开发SDK;

3、Vuforia:高通的AR开发SDK;

以上SDK都有实现图片识别的功能,但是各有特点。

ARkit只针对苹果ios开发,可以实现图片的锚点定位,并且相机可以实现在现实空间中的移动;

ARCore开发Android,和ios,同样能实现ARkit的功能,目前支持的手机机型有限;

Vuforia高通的ARSDK基本的功能就是图片识别,稳定性也不错,只是它不能实现相机的现实空间的交互;

ARkit和Vuforia的教程在网上有很多,今天就着重讲一下ARcore的图像识别功能的实现。

2.ARCore增强图片功能
https://developers.google.cn/ar/develop/unity/augmented-images/Google官方的图片识别的展示与基本入门

被识别的图片以及一些配置要求等请参考上面的官方网站

下载ARCoreSDKForUnity地址:GitHub - google-ar/arcore-unity-sdk: ARCore SDK for Unity
3、新建一个unity工程,将下载好的SDK导入到unity中

会增加这两个文件夹

然后新建一个unity场景:这里就命名为ARCoreImageScene

官方的例子功能是扫描一个图片后会在图片位置实例化一个对象,而且官方的脚本不能确定扫描的图片和此图片的对应的对象是那个;

这里就实现官方没有的功能:

1、扫描图片让想要出现的对象显示,而不是实例化出来

2、实现扫描不同的图片显示出此图片对应的对象

3、添加重置位置的功能(AR的通病:会因为不同原因可能造成不同程度的对象偏移),这样就需要在偏移后重置隐藏对象并再次扫描图片使其回到原来的位置

3.1场景内容

场景的内容:

1:重置对象位置的功能按钮

2:实现AR扫描工能的控制对象

3:ARCore功能实现的必要对象体(官方的预设体相当AR相机)

4:扫描图片出现的对象

5:扫面过程提示框(扫描到后框隐藏,反之显示。。这个也是官方的工能可以参见官方demo或者说明)

3.2功能的实现
1、首先配置文件

1.0创建场景AR工能管理器

新建一个这个命名为ImageSessionConfig

plane Finding mode :Disable

1.1创建识别的图片库

选中要使用的图片右键创建上图文件命名ARCoreImage

点击刚创建的图片库配置内容

Name:识别图片的名字

Width:图片在现实中的尺寸单位米(这个要接近现实,对于对象的放置有影响)

Quality:图片可被识别的质量,评分越高越好,建议不要低于60

这个图片库中是有顺序的,图片会有一个参数DatabaseIndex是后面我们要用到的一个用来区别不同图片对应不同对象的参数。

配置好的图片库按照上图拖到相应位置

然后配置场景ARCoreDevice

1.2核心控制脚本

新建一个脚本ARCoreImageContoller.cs挂在到上图1上;

上图3:扫描图片会出现的对象拖拽到此 (注意:此处对象的顺序就是图片库的顺序这样才能使扫描的图片对应相应的对象)

上图4:扫描的那个提示范围框拖到此

新建脚本AugmentedImageVisualizerOverride.cs.挂在到扫描图片需要显示的对象上,用来收集一些数据属性等内容的脚本。可能会被使用的内容:图片的属性啥的用来判断用

按照官方的PlayerSeting配置好,打包APK测试吧

上脚本:

ARCoreImageContoller.cs

//-----------------------------------------------------------------------

//

//

// Copyright 2018 Google Inc. All Rights Reserved.

//

// Licensed under the Apache License, Version 2.0 (the “License”);

// you may not use this file except in compliance with the License.

// You may obtain a copy of the License at

//

// http://www.apache.org/licenses/LICENSE-2.0

//

// Unless required by applicable law or agreed to in writing, software

// distributed under the License is distributed on an “AS IS” BASIS,

// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

// See the License for the specific language governing permissions and

// limitations under the License.

//

//

//-----------------------------------------------------------------------

namespace GoogleARCore.Examples.AugmentedImage

{

using System.Collections.Generic;

using System.Runtime.InteropServices;

using GoogleARCore;

using UnityEngine;

using UnityEngine.UI;



public class ARCoreImageController : MonoBehaviour

{

    [Header("Drag Augmented Image prefab to this")]

    public List<AugmentedImageVisualizerOverride>  AugmentedImageVisualizerPrefab;

    [Header("Drag FitToScanOverlay to this")]

    public GameObject FitToScanOverlay;

    private Dictionary<int, AugmentedImageVisualizerOverride> m_Visualizers

        = new Dictionary<int, AugmentedImageVisualizerOverride>();

    private List<AugmentedImage> m_TempAugmentedImages = new List<AugmentedImage>();



    public void Update()

    {



        if (Input.GetKey(KeyCode.Escape))

        {

            Application.Quit();

        }



        if (Session.Status != SessionStatus.Tracking)

        {

            return;

        }



        Session.GetTrackables<AugmentedImage>(m_TempAugmentedImages, TrackableQueryFilter.Updated);



        foreach (var image in m_TempAugmentedImages)

        {

            Debug.Log("name1111111" + image.Name);

            Debug.Log("index1111111" + image.DatabaseIndex);

            AugmentedImageVisualizerOverride visualizer = null;

            m_Visualizers.TryGetValue(image.DatabaseIndex, out visualizer);

            if (image.TrackingState == TrackingState.Tracking && visualizer == null)

            {



                Debug.Log("name" + image.Name);

                Debug.Log("index" + image.DatabaseIndex);

                Anchor anchor = image.CreateAnchor(image.CenterPose);



                visualizer =AugmentedImageVisualizerPrefab[image.DatabaseIndex];

                visualizer.gameObject.SetActive(true);



                visualizer.transform.parent = anchor.transform;

                visualizer.transform.localPosition = Vector3.zero;

                visualizer.transform.localRotation = new Quaternion(0,0,0,0);

                visualizer.transform.parent = null;

                visualizer.Image = image;

                m_Visualizers.Add(image.DatabaseIndex, visualizer);





            }

      }



        foreach (var visualizer in m_Visualizers.Values)

        {

            if (visualizer.Image.TrackingState == TrackingState.Tracking)

            {

                FitToScanOverlay.SetActive(false);

                return;

            }

        }



    }

    //重置位置方法

    public void OnclickRe(int index)

    {

        AugmentedImageVisualizerOverride visualizer = null;

        m_Visualizers.TryGetValue(index, out visualizer);

        if (visualizer!=null)

        {

            m_Visualizers.Remove(index);

            visualizer.gameObject.SetActive(false);

            FitToScanOverlay.SetActive(true);

        }

    }

}

}

AugmentedImageVisualizerOverride.cs

//-----------------------------------------------------------------------

//

//

// Copyright 2018 Google Inc. All Rights Reserved.

//

// Licensed under the Apache License, Version 2.0 (the “License”);

// you may not use this file except in compliance with the License.

// You may obtain a copy of the License at

//

// http://www.apache.org/licenses/LICENSE-2.0

//

// Unless required by applicable law or agreed to in writing, software

// distributed under the License is distributed on an “AS IS” BASIS,

// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

// See the License for the specific language governing permissions and

// limitations under the License.

//

//

//-----------------------------------------------------------------------

namespace GoogleARCore.Examples.AugmentedImage

{

using System;

using System.Collections.Generic;

using System.Runtime.InteropServices;

using GoogleARCore;

using GoogleARCoreInternal;

using UnityEngine;

/// <summary>

/// Uses 4 frame corner objects to visualize an AugmentedImage.

/// </summary>

public class AugmentedImageVisualizerOverride : MonoBehaviour

{

    /// <summary>

    /// The AugmentedImage to visualize.

    /// </summary>

    public AugmentedImage Image;



    public void Update()

    {

        if (Image == null || Image.TrackingState != TrackingState.Tracking)

        {



            return;

        }



    }

}

}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值