Log in or register to post comments

Dynamically clone ImageTargets

July 24, 2017 - 1:56pm #1

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



}

Dynamically clone ImageTargets

February 3, 2018 - 10:13am #7

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?

Dynamically clone ImageTargets

July 27, 2017 - 12:13pm #6

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

Dynamically clone ImageTargets

July 25, 2017 - 5:24pm #5

Hey Strasza,

my goal is an app which recognises ~200 different cards which need only 40 models because the displayed creatures have multiple card variations.

Atm I have 2 versions of my app. One version works with the cloud recognition from Vuforia and can detect each image target and displays the right prefab with the code I provided in my initial post. But only one at a time. The second version has 40 image targets in it, only one card variation per creature prefab. 

I would like to instantiate a device image target, as soon as the cloud recognition finds the card. This way I don't have to fill my unity setup with 200 image targets.

Do you think this is possible, or is it better to just use 200 image targets?

Thanks for your help,

FeX

Dynamically clone ImageTargets

July 25, 2017 - 5:11pm #4

Hey FeX,

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

I'm not sure I'm understanding your second question. What prefab is displayed when an image recognition (device or cloud) is entirely defined within your app's code so if you want ~200 images to only display 1 of 40 different Unity prefabs that shouldn't be a problem.

Thanks,

-Vuforia Support

Dynamically clone ImageTargets

July 25, 2017 - 2:21pm #3

Hi Strasza,

thank you for your fast reply!

I saw in this forum, that at some stage it was possible to combine Cloud Recognition and Device Recognition. Is this still possible, as I can't find any new documentation.

If not, how would it be best to implement ~200 imagetargets which only display 40 different prefabs? Is there any way of combining some imagetargets in the Device Database?

Hopefully you can help,

FeX

Dynamically clone ImageTargets

July 24, 2017 - 3:33pm #2

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,

-Vuforia Support


Log in or register to post comments