Search Unity

Loading and unloading Datasets

Discussion in 'Vuforia' started by Development-Boldly-XR, Apr 29, 2020.

  1. Development-Boldly-XR

    Development-Boldly-XR

    Joined:
    Mar 8, 2019
    Posts:
    12
    Hi there,

    I am currently trying to load and unload datasets, but without any succes thusfar. My projects consist of 12 different databases (each with several ImageTargets) and I need the user to be able to select which one to use. Currently I'm doing this by simply setting the parent of the targets to active and inactive, but this causes the image tracking to work really bad. For this reason I thought loading and unloading datasets would be perfect. Loading a dataset works like a charm, but for some reason I can't seem to find out how to unload a dataset. I think activating and deactivating would be fine as well.

    Below is the code I currently use for loading a dataset, something I did find strange though, was that I am not able to edit this method like so;
    void LoadDataSet(string dataset)
    , since it's called like this;

    VuforiaARController.Instance.RegisterVuforiaStartedCallback(LoadDataSet);


    But that's simply a sidenote.

    Code (CSharp):
    1. void LoadDataSet()
    2.     {
    3.  
    4.         ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
    5.  
    6.         DataSet dataSet = objectTracker.CreateDataSet();
    7.  
    8.         if (dataSet.Load(dataSetName))
    9.         {
    10.  
    11.             objectTracker.Stop();  // stop tracker so that we can add new dataset
    12.  
    13.             if (!objectTracker.ActivateDataSet(dataSet))
    14.             {
    15.                 // Note: ImageTracker cannot have more than 100 total targets activated
    16.                 Debug.Log("<color=yellow>Failed to Activate DataSet: " + dataSetName + "</color>");
    17.             }
    18.  
    19.             if (!objectTracker.Start())
    20.             {
    21.                 Debug.Log("<color=yellow>Tracker Failed to Start.</color>");
    22.             }
    23.  
    24.             int counter = 0;
    25.  
    26.             IEnumerable<TrackableBehaviour> tbs = TrackerManager.Instance.GetStateManager().GetTrackableBehaviours();
    27.             foreach (TrackableBehaviour tb in tbs)
    28.             {
    29.                 if (tb.name == "New Game Object")
    30.                 {
    31.  
    32.                     // change generic name to include trackable name
    33.                     tb.gameObject.name = ++counter + ":DynamicImageTarget-" + tb.TrackableName;
    34.  
    35.                     // add additional script components for trackable
    36.                     tb.gameObject.AddComponent<DefaultTrackableEventHandler>();
    37.                     tb.gameObject.AddComponent<TurnOffBehaviour>();
    38.  
    39.                     if (augmentationObject != null)
    40.                     {
    41.                         // instantiate augmentation object and parent to trackable
    42.                         GameObject augmentation = (GameObject)GameObject.Instantiate(augmentationObject);
    43.                         augmentation.transform.parent = tb.gameObject.transform;
    44.                         augmentation.transform.localPosition = new Vector3(0f, 0f, 0f);
    45.                         augmentation.transform.localRotation = Quaternion.identity;
    46.                         augmentation.transform.localScale = new Vector3(0.005f, 0.005f, 0.005f);
    47.                         augmentation.gameObject.SetActive(true);
    48.                     }
    49.                     else
    50.                     {
    51.                         Debug.Log("<color=yellow>Warning: No augmentation object specified for: " + tb.TrackableName + "</color>");
    52.                     }
    53.                 }
    54.             }
    55.         }
    56.         else
    57.         {
    58.             Debug.LogError("<color=yellow>Failed to load dataset: '" + dataSetName + "'</color>");
    59.         }
    60.     }
    And the following to deactivate/destroy the datasets, but this unfortunately isn't working.

    Code (CSharp):
    1. private void DestroyDataSets()
    2.     {
    3.  
    4.         ObjectTracker imageTracker = (ObjectTracker)TrackerManager.Instance.GetTracker<ObjectTracker>();
    5.  
    6.         List<DataSet> dataSets = imageTracker.GetActiveDataSets().ToList();
    7.  
    8.         for (int i = 0; i < dataSets.Count; i++)
    9.         {
    10.  
    11.             StateManager stateManager = TrackerManager.Instance.GetStateManager();
    12.             List<TrackableBehaviour> tb = stateManager.GetTrackableBehaviours().ToList();
    13.             foreach (TrackableBehaviour t in tb)
    14.             {
    15.                 if (dataSets[i].Contains(t.Trackable))
    16.                 {
    17.                     stateManager.DestroyTrackableBehavioursForTrackable(t.Trackable, true);
    18.                 }
    19.             }
    20.  
    21.             imageTracker.DeactivateDataSet(dataSets[i]);
    22.             imageTracker.DestroyDataSet(dataSets[i], true);
    23.  
    24.         }
    25.  
    26.     }
     
  2. Sika_MC

    Sika_MC

    Vuforia

    Joined:
    Jul 17, 2019
    Posts:
    96
    Hi @Development-Boldly-XR, the key is to first disable the object tracker when you want to make changes.

    Below an example:

    Code (CSharp):
    1. public void SwitchTargetByName(string activateThisDataset)
    2. {
    3.     TrackerManager trackerManager = (TrackerManager)TrackerManager.Instance;
    4.     ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
    5.     IEnumerable<DataSet> datasets = objectTracker.GetDataSets();
    6.     IEnumerable<DataSet> activeDataSets = objectTracker.GetActiveDataSets();
    7.     List<DataSet> activeDataSetsToBeRemoved = activeDataSets.ToList();
    8.        
    9.     //Loop through all the active datasets and deactivate them.
    10.     foreach (DataSet ads in activeDataSetsToBeRemoved)
    11.     {
    12.         objectTracker.DeactivateDataSet(ads);
    13.     }
    14.        
    15.     //Swapping of the datasets should not be done while the ObjectTracker is working at the same time.
    16.     //So, Stop the tracker first.
    17.     objectTracker.Stop();
    18.        
    19.     //Then, look up the new dataset and if one exists, activate it.
    20.     foreach (DataSet ds in datasets)
    21.     {
    22.         if (ds.Path.Contains(activateThisDataset))
    23.         {
    24.             objectTracker.ActivateDataSet(ds);
    25.         }
    26.        
    27.     }
    28.     //Finally, start the object tracker.
    29.     objectTracker.Start();
    30. }
    Thank you.
    Vuforia Engine Support