Cloud Recognition
This example shows how to recognize images on a cloud server and then overlay it with augmentations utilizing the CloudTracker
class.
For a better understanding, here are some terms that will be used in the following and other sections of this documentation related to vision-based augmented reality.
Target: An image and its associated extracted data that is used to recognize an image.
Target Collection: A group of
targets
that are searched together. Think of it as a directory, which contains all your images you want to search. The Wikitude SDK can work with two different sorts ofTarget Collections
- On-device Target Collection: a static
wtc
file containing the extracted data of your images. Can consist of up to 1,000 images. - Cloud Target Collection: A target collection stored on the Wikitude server. See
Cloud Archive
below. Can consist of up to 50,000 images.
- On-device Target Collection: a static
Cloud Archive: An archive stored on the server that is optimized for cloud-based recognition. It is generated from a
Target Collection
and is used in combination withCloudTracker
.CloudTracker: Instead of analysing and computing the live camera feed directly on the device like the
ClientTracker
, theCloudTracker
will send the image(s) taken by the camera to the Wikitude Cloud Recognition server. The server will then do the hard work of trying to match the image with your targets in the specified cloud archive. Beside the benefit of searching in large image database using theCloudTracker
instead ofClientTracker
has also a positive impact on the general performance in most cases. Especially when using a large target collection and on older devices.
For both Cloud Recognition samples below we will use external rendering if you don't know what that means please go through the section on rendering before starting here.
The CloudTracker
is able to run in two modes, we call them on-click and continuous. In On-Click mode a single recognition cycle will be executed, in continuous mode the recognition will be started in a variable interval.
Both view controller are located in Controller/CloudTracking
.
Regional server endpoints
Before we get started please note that you have to choose which regional-distributed Wikitude server the SDK should contact.
The cloud recognition server region can be selected by calling -setCloudRecognitionServerRegion:
which is a method of WTTrackerManager
. As a parameter, pass a value of WTCloudRecognitionServerRegion
to specify the regional server. Every cloud tracker that was created afterwards, will contact the specified server.
On-Click Cloud Tracking
The starting point for on-click recognition is WTOnClickCloudRecognition
. In -viewDidLoad
we create a new instance of WTWikitudeNativeSDK
and initialize it with a valid license key.
self.wikitudeSDK = [[WTWikitudeNativeSDK alloc] initWithRenderingMode:WTRenderingMode_External delegate:self];
[self.wikitudeSDK setLicenseKey:kWTLicenseKey];
Then in -viewDidAppear:
the Wikitude Native SDK is started using the -start:completion:
method. If the SDK could be started, a object of type WTCloudTracker
is created using the WTTrackerManager
factory method -createCloudTrackerWithToken:targetCollectionId:extendedTargets:andDelegate
. The returned pointer needs to be retained in order to keep it alive, so assign it to a strong property.
[self.wikitudeSDK start:nil completion:^(BOOL isRunning, NSError * __nonnull error) {
if ( !isRunning ) {
NSLog(@"Wikitude SDK is not running. Reason: %@", [error localizedDescription]);
}
else
{
self.cloudTracker = [self.wikitudeSDK.trackerManager createCloudTrackerWithToken:@"b277eeadc6183ab57a83b07682b3ceba" targetCollectionId:@"54e4b9fe6134bb74351b2aa3" extendedTargets:nil andDelegate:self];
}
}];
Creating a cloud tracker object will automatically load it in the background. To get notified when the cloud tracker finished loading, implement the WTCloudTrackerDelegate
protocol. It provides two methods that are called once the cloud tracker finished loading or failed to load.
- (void)cloudTrackerFinishedLoading:(WTCloudTracker * __nonnull)cloudTracker
{
NSLog(@"cloud tracker finished loading");
}
- (void)cloudTracker:(WTCloudTracker * __nonnull)cloudTracker failedToLoadWithError:(NSError *)error
{
NSLog(@"Cloud tracker failed to load with error: %@", [error localizedDescription]);
}
To start a single recognition, call the cloud tracker -recognize:errorHandler:
method. This method takes two block objects as parameter. The first on is called if a successful server connection could be established and the cloud tracker received a valid server response. The second one is called if any kind of error occurred during this communication. To call the -recognize:errorHandler:
method, a UIButton
is set up to call a specific method once it was touched.
- (IBAction)sendCloudRecognitionRequest:(id)sender
{
if ( self.cloudTracker )
{
if (self.cloudTracker.isLoaded)
{
[self.cloudTracker recognize:^(WTCloudRecognitionResponse *response) {
if ( response.recognized )
{
NSLog(@"Recognized target '%@' which has a rating of '%ld'", [response.targetInformations objectForKey:WTCloudTrackerResponseKey_TargetName], (long)[[response.targetInformations objectForKey:WTCloudTrackerResponseKey_TargetRating] integerValue]);
NSLog(@"Associated (custom) metadata: %@", response.metadata);
}
else
{
NSLog(@"No target image found in the analysed camera frame.\nKeep Calm \n\t&\nKeep Looking");
}
} errorHandler:^(NSError *error) {
NSLog(@"Cloud recognition recognition failed. Reason: %@", [error localizedDescription]);
}];
}
else
{
NSLog(@"Cloud tracker is not yet loaded. No request send.");
}
}
}
Please note that only a recognize call is made if the cloud tracker finished loading.
In case the server communication was successful, the success handler is called with an object of type WTCloudRecognitionResponse
that represents the server result. This object contains information if a image target was found or not, its name and, if specified, its associated meta data.
In case of an error, a NSError
object is passed to the errorHandler which contains detailed information about why the communication failed.
Since the WTCloudTrackerDelegate
protocol also conforms to WTBaseTrackerDelegate
, all tracking related methods to draw a augmentation around the recognized target are available as well.
Continuous Cloud Tracking
On-Click recognition is useful in some particular cases, but more often than not you probably want to use continuous recognition. For continuous cloud recognition we set an interval in which the CloudTracker
automatically calls the recognize function.
To start continuous recognition, simply call the -startContinuousRecognitionWithInterval:successHandler:interruptionHandler:errorHandler:
cloud tracker method once it finished loading.
if ( self.cloudTracker && self.cloudTracker.isLoaded )
{
[self.cloudTracker startContinuousRecognitionWithInterval:1.5 successHandler:^(WTCloudRecognitionResponse *response) {
NSLog(@"received continuous response...");
if ( response.recognized ) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.cloudTracker stopContinuousRecognition];
[self.continuousRecognitionButton setTitle:@"Start Continuous Recognition" forState:UIControlStateNormal];
});
}
} interruptionHandler:nil errorHandler:^(NSError *error) {
}];
[self.continuousRecognitionButton setTitle:@"Stop Continuous Recognition" forState:UIControlStateNormal];
}
else
{
NSLog(@"Cloud tracker is not ready yet.");
}
As before, this method is called once a UIButton
was touched.
The first parameter defines in which interval new camera frames should be sent to the server for processing. The second parameter is again a success handler which contains information about a recognized target. The third parameter is a block that is invoked if the given interval is to short for the server to respond. The last parameter is again a error handle which is invoked in case of any error the occurred during the communication.
To stop a continuous recognition session, e.g. once a target was found, simply call the -stopContinuousRecognition
method.