【问题标题】:Can't add images to AugmentedImageDatabase in ARCore无法将图像添加到 ARCore 中的 AugmentedImageDatabase
【发布时间】:2019-08-10 00:50:10
【问题描述】:

我正在统一编写应用程序。将来我需要能够从网络上提取图像并将它们用作跟踪器,因此我目前正在编写代码,在运行时将图像从列表中添加到 AugmentedImageDatabase。

但是,每次我添加图像时,AugmentedImageDatabase.AddImage() 方法都会返回 -1。这意味着添加图像时出错,但没有说明是哪种错误。我检查了 API 文档,但它们也没有添加任何信息。

为什么我的代码没有将图像添加到 AugmentedImageDatabase?

public class DataBaseGenerator : MonoBehaviour {
    [SerializeField]
    ARCoreSessionConfig session;

    [SerializeField]
    AugmentedImageDatabase imageDatabase;

    [SerializeField]
    List<Texture2D> image;

    private int ErrorIndex = 0;

    // What happens on the first frame
    void Start ()
    {
        CreateDatabase();
    }
    private void CreateDatabase()
    {
        int i = 0;
        foreach (Texture2D texture in image)
        {
            string name = "Tracker";
            Texture2D rgbTexture = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
            rgbTexture.SetPixels(texture.GetPixels());
            rgbTexture.Apply();

            ErrorIndex = imageDatabase.AddImage(name, rgbTexture, 0);
            GameUtility.ShowToastMessage(ErrorIndex.ToString());
            Debug.Log(name + ": " + ErrorIndex);
            i++;
        }
        session.AugmentedImageDatabase = imageDatabase;
    }
}

列表中的所有图像都已保存为 Sprite。 所有带有[SerializeField] 标签的变量都在统一编辑器中定义。

【问题讨论】:

  • 你的纹理格式是什么。 ARCore 仅支持两种格式。你可以看看这个answer
  • 尝试将格式更改为 rgb32,仍然无效。使用受支持的 .png 文件扩展名。
  • 据我所知,代码错误-1 是您的nativeSession == null 的情况。您是否尝试在 ARCore 会话中添加图像?
  • @AliKanat 我将 ARCoreSession 文件传递​​给代码 sn-p 顶部的 session 变量。这与传递给相机的会话相同。我还有一个被加载到imageDatabase 中的AugmentedImageDatabase,“DB”。然后最后,我做session.AugmentedImageDatabase = imageDatabase
  • 另一个原因可能是您在Start 中启动协程,但可能在那个阶段会话刚刚初始化。在 Session 初始化后尝试通过触摸来执行此操作。您可以查看Session.Status

标签: c# unity3d arcore


【解决方案1】:

由于 OP 没有回复我假设我的评论解决了问题并将我的评论转换为答案。

ARCore 仅支持Augmented Image Database 的两种纹理格式(RGBA32 或 RGB24)。因此,必须首先转换纹理才能将图像添加到数据库。

OP 代码中的第二个问题是他试图将图像添加到Start 的数据库中,这显然会在应用程序启动时执行。因此,会话状态是NoneInitializing,这导致LifecycleManager.Instance.NativeSession 返回空值。由于目前没有Session,因此您无法将图像添加到数据库中。

【讨论】:

  • 确实,这个错误已经解决了。您可以在 Session 生效时使用 private void OnEnable() { LifecycleManager.Instance.EarlyUpdate += CreateDatabase } 执行操作,但我现在遇到了一个不同的错误,这可能是由这种方式引起的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2022-01-18
  • 2017-07-16
  • 2014-11-30
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多