Image Recognition

This example shows how to recognize images in the viewfinder and overlay it with images.

For a better understanding, here are some terms that will be used in the following and other section of this documentation related to vision-based augmented reality.

  • Target: A 2D target image and its associated extracted data that is used by the tracker to recognize an image.

  • Target Collection: An archive storing a collection of 2D targets that can be recognized by the tracker. A target collection can hold up to 1000 targets. Target collections are stored as .wtc files

  • ImageTracker: The tracker analyzes the live camera image and detects the 2D targets stored in its associated target collection. Multiple trackers can be created, however only one tracker can be active for recognition at any given time.

Simple Image Tracking in Unity

The Wikitude Unity Plugin is based on pre-configured prefabs. There are two types of prefabs available. One is the WikitudeCamera prefab and the other ones are tracker prefabs.

WikitudeCamera Prefab

The WikitudeCamera prefab takes care about rendering the live camera stream fullscreen behind all your augmentations. Attached to this prefab is a script component which has one parameter that is not pre filled. This parameter is for the Wikitude SDK license key and has to be filled with your very own license key. You can either buy a commercial license from our webpage or download a free trial license key and play around with our Native SDK in combination with Unity.

ImageTracker Prefab

To add a tracker prefab to the scene, simply drag the ImageTracker prefab into the scene hierarchy.

Kindly make sure that the targetName used with the Trackable prefab correspond to one of the target names in your target collection. You can also use wildcards to match any target or only a specific subset of targets.

An ImageTracker itself needs a Wikitude Target Collection (.wtc file) which contains information needed to detect those reference images. Target collections can be generated and downloaded from the Wikitude Target Manager - a free web based tool, that you can access with your developer account. You can also generate wtc files right inside Unity with the WTC Editor. Place the .wtc file into the StreamingAssets folder, so that the Wikitude Native SDK can load them at runtime. To specify which .wtc file should be used, select the ImageTracker game object in the scene. Make sure that the Target Source is set to Target Collection Resource and using the dropdown next to Target Collection you can choose the desired one.

To react on events like successfully loading of a .wtc file, you can use the Unity Events listed in in the inspector of the ImageTracker. These events are split into two groups. The first group contains events triggered by the TargetCollectionResource when the wtc file was loaded of if there was an error. The second group are events triggered by the ImageTracker itself if it was successfully initialized with the desired .wtc file or not. On the desired event, click the plus sign to add a new subscriber, drag the GameObject that should receive the event over the None (Object) field and select the function you want to be called from the No Function dropdown.

When subscribing to events that have a single basic parameter type, make sure to select your function from top list marked with Dynamic, rather than the static version from the bottom. This ensures that the parameters are passed correctly from the Wikitude plugin and are not overwritten by Unity.

Setting callbacks

For more information on working with Unity Events, please check the Unity Manual and Events Tutorial.

Define custom augmentations

Also part of the ImageTracker prefab is a Trackable object. A trackable defines which targets from your collection you want to be tracked. If the tracker is using a wtc file located in the StreamingAssets folder, the Trackable inspector will show a list of all the targets in the wtc file. By toggling the Active button, you can select which targets will be tracked by this trackable, or you can choose Select All at the top to include all the targets. You can similarly choose which targets should use extended tracking.

By pressing the Preview button, the target will be displayed in the 3D view of the scene, providing a convenient way to place your augmentation relative to the target.

If the wtc file is located somewhere else, for example if you are downloading it at runtime, or when using CloudRecognitionService instead, you can still select which targets will be included by entering the target name into the TargetPattern text field. Possible values are full target image names (e.g. pageOne, pageTwo) or wildcards (page*). You can use * if you want to include all targets.

In order to place 3D objects at the location where the reference image was found in the camera stream, add any GameObject as a child to the Trackable object. During runtime, only the transform of the camera will be changed, so you can place and scale the Trackable GameObject and its children however it is most convenient for you. Keep in mind that if you move the Trackable GameObject during runtime, the camera will follow it, so you won't see any effective changes. If you need to move augmentations relative to the camera, for example when dragging augmentations based on user input, please make sure not to move Trackable GameObject, but its children.

The Auto Toggle Visibility toggle is enabled by default. When this is checked, the Trackable GameObject will be automatically disabled when the target is out of view and enabled back when a target is tracked again.

To handle visibility manually, you can turn this toggle off and subscribe to the OnEnterFieldOfVision and OnExitFieldOfVision events on the trackable. This can be useful when you want to show different augmentations based on which target was tracked. The string parameter of these events will indicate which target was tracked or lost.

The OnEnterFieldOfVision and OnExitFieldOfVision events are called even when the Auto Toggle Visibility toggle is turned on. As an example, the Client Recognition - Extended Tracking updates the UI when the target is lost by handling OnExitFieldOfVision.

Extended Tracking

Extended tracking is an optional mode you can set for each target separately. In this mode the Wikitude SDK will continue to scan the environment even if the original target image is not in view anymore. So the tracking extends beyond the limits of the original target image. The performance of this feature depends on various factors like computing power of the device, background texture and objects.

To enable Extended Tracking, on the Trackable object you can select the Extended Tracking option and the list below should be filled with the names of the targets that should be extended. Setting it as a wildcard * will track all targets in extended mode.

When Extended Tracking is enabled, the ImageTracker will fire OnExtendedTrackingQualityChangedEvents, which will let you know how well extended tracking is working based on the factors mentioned above.

Multiple Trackers

You can have multiple trackers in the same scene, but only one can be active at a time. If you enable a second one, the first one will be automatically disabled by the plugin.

Runtime Tracker

Image trackers can be created at runtime with no restrictions on the location of the target collection used. To do this, simply create a new GameObject and add the ImageTracker component to it, select TargetCollectionResource as the TargetSourceType and create a new TargetCollectionResource object. If you are using a collection located in the StreamingAssets folder, the TargetPath property should be the path relative to the StreamingAssets folder and UseCustomUrl property should be false.

If you want to use a collection located elsewhere on the device or on the web, the TargetPath property should be set to the absolute path to the target, prefixed by the protocol file://, http:// or https:// as appropriate. The UseCustomUrl in this case should be set to true. Please see the sample Client Tracker - Runtime Tracker as an example of how to set this up.

Trackables can also be created at runtime, but make sure to add them as a child of the tracker before the Start() method is called on the parent tracker, otherwise it won't get registered in time.

GameObject trackerObject = new GameObject("ImageTracker");
ImageTracker imageTracker = trackerObject.AddComponent<ImageTracker>();

imageTracker.TargetSourceType = TargetSourceType.TargetCollectionResource;
imageTracker.TargetCollectionResource = new TargetCollectionResource();
imageTracker.TargetCollectionResource.UseCustomURL = true;
imageTracker.TargetCollectionResource.TargetPath = "";

GameObject trackableObject = GameObject.Instantiate(TrackablePrefab);
trackableObject.transform.SetParent(imageTracker.transform, false);

Creating trackers at runtime is also possible when using the CloudRecognitionService instead of a TargetCollectionResource