"We offer new support options and therefor the forums are now in read-only mode! Please check out our Support Center for more information." - Vuforia Engine Team

Dynamically clone ImageTargets

Hi Guys,

i want to dynamically clone my ImageTarget and recognise multiple ImageTargets via Cloud Recognition. Atm my code clones my ImageTarget but only one ImageTarget gets recognised. As soon as this gets lost i can detect another one.

Any ideas what I have to do here?

I would be very very happy!

Thanks in advance,

FeX

Here is my CloudHandler:

using System; 
using UnityEngine; 
using UnityEngine;
using Vuforia;
using UnityEngine.UI; 
public class SimpleCloudHandler : MonoBehaviour, ICloudRecoEventHandler
{
    #region PRIVATE_MEMBER_VARIABLES

    // CloudRecoBehaviour reference to avoid lookups
    private CloudRecoBehaviour mCloudRecoBehaviour;
    // ImageTracker reference to avoid lookups
    private ObjectTracker mImageTracker;

    private bool mIsScanning = false;
    private Texture2D m_Texture;
    private string mTargetMetadata = "";

   

    #endregion // PRIVATE_MEMBER_VARIABLES



    #region EXPOSED_PUBLIC_VARIABLES

    public ImageTargetBehaviour ImageTargetTemplate;
    private GameObject[] creatures;

    #endregion

    #region UNTIY_MONOBEHAVIOUR_METHODS

    void Start()
    {
        // register this event handler at the cloud reco behaviour
        m_Texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
        CloudRecoBehaviour cloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();


        if (cloudRecoBehaviour)
        {
            cloudRecoBehaviour.RegisterEventHandler(this);
        }

        // remember cloudRecoBehaviour for later
        var vuforia = VuforiaARController.Instance;
        vuforia.RegisterVuforiaStartedCallback(OnVuforiaStarted);
        vuforia.RegisterOnPauseCallback(OnPaused);
    }

    #endregion // UNTIY_MONOBEHAVIOUR_METHODS


    #region ICloudRecoEventHandler_IMPLEMENTATION

    public void OnInitialized()
    {
        // get a reference to the Image Tracker, remember it
        mImageTracker = (ObjectTracker)TrackerManager.Instance.GetTracker<ObjectTracker>();
    }

    
    public void OnStateChanged(bool scanning)
    {
        mIsScanning = scanning;
        if (scanning)
        {
            // clear all known trackables
            ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
            tracker.TargetFinder.ClearTrackables(false);
        }
    }

    public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
    {
        /*
        if (mTargetMetadata != "")
        {
            DestroyAllObjects();
        }*/
       
        // duplicate the referenced image target
        GameObject newImageTarget = Instantiate(ImageTargetTemplate.gameObject) as GameObject;
        newImageTarget.tag = "Creature";
        GameObject augmentation = null;

        string model_name = targetSearchResult.MetaData;


        if (augmentation != null)
            augmentation.transform.parent = newImageTarget.transform;

        // enable the new result with the same ImageTargetBehaviour:
        ImageTargetAbstractBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, newImageTarget);
       
        Debug.Log("Metadata value is " + model_name);
        mTargetMetadata = model_name;

        string path = "Creature_Prefabs/" + model_name;
        Debug.Log("Load path is " + path);

        GameObject creature = (GameObject)Instantiate(Resources.Load(path));
        creature.transform.SetParent(newImageTarget.transform, false);

        if (!mIsScanning)
        {
            // stop the target finder
            mCloudRecoBehaviour.CloudRecoEnabled = true;
        }
    }
    private void DestroyAllObjects()
    {
        creatures = GameObject.FindGameObjectsWithTag("Creature");

        for (var i = 0; i < creatures.Length; i++)
        {
            Destroy(creatures[i]);
        }
    }

    void OnGUI()
    {
        GUI.Box(new Rect(100, 200, 200, 50), "Metadata: " + mTargetMetadata);
        if (GUI.Button(new Rect(10, 10, 100, 100), "Quit"))
        {
            Application.Quit();
        }
        if (GUI.Button(new Rect(120, 20, 100, 100), "Screenshot"))
        {
            Application.CaptureScreenshot("Screenshot "+ mTargetMetadata + ".png");
        }
    }

    private void OnVuforiaStarted()
{
    CameraDevice.Instance.SetFocusMode(
        CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}

private void OnPaused(bool paused)
{
    if (!paused) // resumed
    {
        // Set again autofocus mode when app is resumed
        CameraDevice.Instance.SetFocusMode(
            CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    }
}

    #endregion // ICloudRecoEventHandler_IMPLEMENTATION



}

Hello FeX,

Cloud Reco can only detect a single image at a time. Regular Image Targets can detect multiple images at a time by setting the "Max Simultaneous Tracked Images" field in the Vuforia Configuration to a value greater than 1.

Sorry for the inconvenience,

Hey FeX,

You can have a Cloud Database and Device Database active at the same time in the same app.

Hey FeX,

Unfortunately you cannot instantiate a device image target after the cloud reco detects it. Sounds like you're going to want to use a device dataset with your ~200 image targets.

Sorry for the inconvenience,

-Vuforia Support

Hi Strasza,

 

Does that mean 200 device image target will need 200 instances of the image target prefab in the current scene at the same time?