Log in or register to post comments

Cloud based tracking CloudRecoEventHandler not working for me

February 28, 2015 - 7:45am #1

Hey Vuforia Team,

I am going to convert my BookWire app from localbased tracking to cloudbasedtracking.

Each book have a Booktracker.cs that passes a uniq book_id for each book and it works fom when doing localtracking.

 

But when i switch to Cloudbased tracking where i am using i simpel script   that uses a Image_Target_Template but only  recogonize  and loading the first book .

My Question is where can change my script so it takes Image_Target_Template like an array and then  loads each book seperatly. 

please see image and code.

Best Regards

Daniel

 
 
 
using System;
using UnityEngine;
 
 
public class CloudRecoEventHandler : MonoBehaviour, ICloudRecoEventHandler
{
    #region PRIVATE_MEMBER_VARIABLES
 
    // CloudRecoBehaviour reference to avoid lookups
    private CloudRecoBehaviour mCloudRecoBehaviour;
 
    // ImageTracker reference to avoid lookups
    private ImageTracker mImageTracker;
 
private BooksTracker mBookTracker;
 
 
    #endregion // PRIVATE_MEMBER_VARIABLES
 
 
 
    #region EXPOSED_PUBLIC_VARIABLES
 
    /// <summary>
    /// can be set in the Unity inspector to reference a ImageTargetBehaviour that is used for augmentations of new cloud reco results.
    /// </summary>
    public ImageTargetBehaviour ImageTargetTemplate;
public ImageTargetBehaviour ImageTargetTemplate2;
 
    #endregion
 
 
 
    #region ICloudRecoEventHandler_IMPLEMENTATION
 
    /// <summary>
    /// called when TargetFinder has been initialized successfully
    /// </summary>
    public void OnInitialized()
    {
        // get a reference to the Image Tracker, remember it
mImageTracker = TrackerManager.Instance.GetTracker<ImageTracker>();
mBookTracker = (BooksTracker)FindObjectOfType(typeof(BooksTracker));
       // mContentManager = (ContentManager)FindObjectOfType(typeof(ContentManager));
    }
 
    /// <summary>
    /// visualize initialization errors
    /// </summary>
    public void OnInitError(TargetFinder.InitState initError)
    {
        switch (initError)
        {
            case TargetFinder.InitState.INIT_ERROR_NO_NETWORK_CONNECTION:
                ErrorMsg.New("Network Unavailable", "Please check your +internet connection and try again.", null);
                break;
            case TargetFinder.InitState.INIT_ERROR_SERVICE_NOT_AVAILABLE:
                ErrorMsg.New("Service Unavailable", "Failed to initialize app because the service is not available.", null);
                break;
        }
    }
    
    /// <summary>
    /// visualize update errors
    /// </summary>
    public void OnUpdateError(TargetFinder.UpdateState updateError)
    {
        switch (updateError)
        {
            case TargetFinder.UpdateState.UPDATE_ERROR_AUTHORIZATION_FAILED:
                ErrorMsg.New("Authorization Error","The cloud recognition service access keys are incorrect or have expired.");
                break;
            case TargetFinder.UpdateState.UPDATE_ERROR_NO_NETWORK_CONNECTION:
                ErrorMsg.New("Network Unavailable","Please check your internet connection and try again.");
                break;
            case TargetFinder.UpdateState.UPDATE_ERROR_PROJECT_SUSPENDED:
                ErrorMsg.New("Authorization Error","The cloud recognition service has been suspended.");
                break;
            case TargetFinder.UpdateState.UPDATE_ERROR_REQUEST_TIMEOUT:
                ErrorMsg.New("Request Timeout","The network request has timed out, please check your internet connection and try again.");
                break;
            case TargetFinder.UpdateState.UPDATE_ERROR_SERVICE_NOT_AVAILABLE:
                ErrorMsg.New("Service Unavailable","The service is unavailable, please try again later.");
                break;
            case TargetFinder.UpdateState.UPDATE_ERROR_TIMESTAMP_OUT_OF_RANGE:
                ErrorMsg.New("Clock Sync Error","Please update the date and time and try again.");
                break;
            case TargetFinder.UpdateState.UPDATE_ERROR_UPDATE_SDK:
                ErrorMsg.New("Unsupported Version","The application is using an unsupported version of Vuforia.");
                break;
        }
    }
 
    /// <summary>
    /// when we start scanning, unregister Trackable from the ImageTargetTemplate, then delete all trackables
    /// </summary>
    public void OnStateChanged(bool scanning)
    {
        if (scanning)
        {
            // clear all known trackables
mImageTracker.TargetFinder.ClearTrackables(false);
 
//mBookTracker.OnTrackableStateChanged();
            // hide the ImageTargetTemplate
            //mContentManager.HideObject();
        }
    }
 
    /// <summary>
    /// Handles new search results
    /// </summary>
    /// <param name="targetSearchResult"></param>
    public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
    {
      
        //Check if the metadata isn't null
        if(targetSearchResult.MetaData == null)
        {
            return;
        }
 
if(targetSearchResult.MetaData != null)
{
 
}
 
        // enable the new result with the same ImageTargetBehaviour:
        
GameObject newImageTarget = Instantiate(ImageTargetTemplate.gameObject) as GameObject;
 
GameObject augmentation = null;
 
if( augmentation != null )
augmentation.transform.parent = newImageTarget.transform;
 
if (ImageTargetTemplate == true) {
// enable the new result with the same ImageTargetBehaviour:
ImageTracker tracker = TrackerManager.Instance.GetTracker<ImageTracker>();
ImageTargetBehaviour ImageTargetBehaviour = (ImageTargetBehaviour)tracker.TargetFinder.EnableTracking(targetSearchResult, ImageTargetTemplate.gameObject)as ImageTargetBehaviour;
//mBookTracker.(targetSearchResult.MetaData);
}
 
 
mCloudRecoBehaviour.CloudRecoEnabled = true;
 
 
    }
 
 
public void OnNewSearchResult2(TargetFinder.TargetSearchResult targetSearchResult2)
{
 
if(targetSearchResult2.MetaData == null)
{
return;
}
 
if(targetSearchResult2.MetaData != null)
{
 
}
 
// enable the new result with the same ImageTargetBehaviour:
 
GameObject newImageTarget = Instantiate(ImageTargetTemplate2.gameObject) as GameObject;
 
GameObject augmentation = null;
 
if( augmentation != null )
augmentation.transform.parent = newImageTarget.transform;
 
if (ImageTargetTemplate2 == true) {
// enable the new result with the same ImageTargetBehaviour:
ImageTracker tracker2 = TrackerManager.Instance.GetTracker<ImageTracker>();
ImageTargetBehaviour ImageTargetBehaviour = (ImageTargetBehaviour)tracker2.TargetFinder.EnableTracking(targetSearchResult2, ImageTargetTemplate2.gameObject)as ImageTargetBehaviour;
//mBookTracker.(targetSearchResult.MetaData);
}
 
 
mCloudRecoBehaviour.CloudRecoEnabled = true;
 
 
}
 
    #endregion // ICloudRecoEventHandler_IMPLEMENTATION
 
 
 
    /// <summary>
    /// register for events at the CloudRecoBehaviour
    /// </summary>
    void Start()
    {
        // look up the gameobject containing the ImageTargetTemplate:
       // mParentOfImageTargetTemplate = ImageTargetTemplate.gameObject;
 
        // intialize the ErrorMsg class
        ErrorMsg.Init();
 
        // register this event handler at the cloud reco behaviour
        CloudRecoBehaviour cloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();
        if (cloudRecoBehaviour)
        {
            cloudRecoBehaviour.RegisterEventHandler(this);
        }
 
        mCloudRecoBehaviour = cloudRecoBehaviour;
 
    }
 
 
    void OnGUI()
    {
 
        ErrorMsg.Draw();
    }
 
    #region PRIVATE_METHODS
 
    
 
    #endregion // PRIVATE_METHODS
}

 

AttachmentSize
Image icon CloudError.png155.69 KB

Cloud based tracking CloudRecoEventHandler not working for me

March 11, 2015 - 3:53pm #4
I Updated the  CloudRecoEventHandler with a for loop that is looping though my ImageTargetTemplate , but It dont load correct bookTracker.cs for ImageTargetTemplate array when using cloudReco  
Please see attached image an any help will be appriciated :-) 

public class CloudRecoEventHandler : MonoBehaviour, ICloudRecoEventHandler
{
    #region PRIVATE_MEMBER_VARIABLES
    private CloudRecoBehaviour mCloudRecoBehaviour;
 private bool mIsScanning = false;
    public ImageTracker mImageTracker;
 private BooksTracker mBookTracker;
 #endregion // PRIVATE_MEMBER_VARIABLES
 private GameObject[] mParentOfImageTargetTemplate;
 public  GameObject[] ImageTargetTemplate;
   
    #region EXPOSED_PUBLIC_VARIABLES

    #endregion



    #region ICloudRecoEventHandler_IMPLEMENTATION

    public void OnInitialized()
    {
       
  mImageTracker = TrackerManager.Instance.GetTracker<ImageTracker>();
  mBookTracker = (BooksTracker)FindObjectOfType(typeof(BooksTracker));
     
    }
  

 public void OnStateChanged(bool scanning)
 {
  mIsScanning = scanning;
  if (scanning)
  {
   //ImageTracker tracker = TrackerManager.Instance.GetTracker<ImageTracker>() as ImageTracker;
   mImageTracker.TargetFinder.ClearTrackables(true);
   mBookTracker.mImageTracker.TargetFinder.StartRecognition();


  }
 
 } 

 public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
 {

  if(targetSearchResult.MetaData == null)
  {
   return;
  }
  if (targetSearchResult.MetaData != null)
  {
   for(int i = 0; i < ImageTargetTemplate.Length; i++)
   {

    mParentOfImageTargetTemplate = ImageTargetTemplate;
    String targetMetadata = targetSearchResult.MetaData;
    ImageTargetBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult,mParentOfImageTargetTemplate[i]) as ImageTargetBehaviour;
    mBookTracker.TargetCreated(targetSearchResult.MetaData);

   }

   }
   mCloudRecoBehaviour.CloudRecoEnabled = true;
 }

    #endregion // ICloudRecoEventHandler_IMPLEMENTATION
 FehrenheitUIComponent fehrenheitUIComp = null;
 void Start()
 {
//  GameObject fehrenheitUI = GameObject.Find ("FehrenheitUI");
//  if(fehrenheitUI != null)
//  {
//   fehrenheitUIComp = fehrenheitUI.GetComponent<FehrenheitUIComponent>();
//  }
  //mParentOfImageTargetTemplate = ImageTargetTemplate2.gameObject;
  ErrorMsg.Init();
  CloudRecoBehaviour cloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();
  if (cloudRecoBehaviour)
  {
   cloudRecoBehaviour.RegisterEventHandler(this);
  }
      mCloudRecoBehaviour = cloudRecoBehaviour;
  }
  
    void OnGUI()
    {
 ErrorMsg.Draw();
    }

    #region PRIVATE_METHODS

    

    #endregion // PRIVATE_METHODS
}

 

AttachmentSize
Image icon errorCloud.jpg419.17 KB

Cloud based tracking CloudRecoEventHandler not working for me

March 11, 2015 - 3:46am #3

HI dm3d,

thanks but my bookshop project works a bit different it should call my bookTracker.cs for book_id everytime it detected  a bookcover but it only calls the same targetSearchResult  pr templete .  

 

public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
	{
		// This code demonstrates how to reuse an ImageTargetBehaviour for new search results and modifying it according to the metadata
		// Depending on your application, it can make more sense to duplicate the ImageTargetBehaviour using Instantiate(), 
		// or to create a new ImageTargetBehaviour for each new result
		
		// Vuforia will return a new object with the right script automatically if you use
		// TargetFinder.EnableTracking(TargetSearchResult result, string gameObjectName)
		
		//Check if the metadata isn't null
		if(targetSearchResult.MetaData == null)
		{
			return;
		}
		
		// enable the new result with the same ImageTargetBehaviour:
		ImageTargetBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, mParentOfImageTargetTemplate) as ImageTargetBehaviour;
		
		if (imageTargetBehaviour != null)
		{
			// stop the target finder
			mCloudRecoBehaviour.CloudRecoEnabled = false;
			
			// Calls the TargetCreated Method of the SceneManager object to start loading
			// the BookData from the JSON
			mBookTracker.TargetCreated(targetSearchResult.MetaData);


		}
	}

 

here is my BookTracker.cs 

	public void OnTrackableStateChanged(
		TrackableBehaviour.Status previousStatus,
		TrackableBehaviour.Status newStatus)
	{
		if (newStatus == TrackableBehaviour.Status.DETECTED ||
		    newStatus == TrackableBehaviour.Status.TRACKED)
		{
			TargetCreated(null);
			OnTrackingFound();
		}
		else
		{
			OnTrackingLost();
		}
	}

	public void TargetCreated(string targetMetadata)
	{
		BookVO bookItem = null;
		if(book_id > 0 && FehrenheitUIComponent.booksModel != null)
		{
			bookItem = FehrenheitUIComponent.booksModel.getBookByID (book_id);
		}

		if(bookItem != null)
		{
			
			OnTrackingFound();
			
			fehrenheitUIComp.OnTrackingFound(bookItem);
			
		}else
		{


			OnTrackingLost();
		
		//if(bookItem != null)
		//{
		//	fehrenheitUIComp.OnTrackingLost(bookItem);
		//}
		 }

	}

 

Cloud based tracking CloudRecoEventHandler not working for me

March 4, 2015 - 7:03pm #2

Our Books sample app in the Best Practices section shows how to use Cloud Reco to display different book information and pricing.

https://developer.vuforia.com/downloads/samples

 

Log in or register to post comments