2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Runtime.InteropServices;
19 using System.Threading.Tasks;
20 using InteropImage = Interop.MediaVision.Image;
22 namespace Tizen.Multimedia.Vision
25 /// Provides the ability to track images on image sources.
27 /// <since_tizen> 3 </since_tizen>
28 public static class ImageTracker
31 /// Tracks the given image tracking model on the current frame.
33 /// <param name="source">The current image of sequence where the image tracking model will be tracked.</param>
34 /// <param name="trackingModel">The image tracking model which processed as target of tracking.</param>
35 /// <returns>A task that represents the asynchronous tracking operation.</returns>
36 /// <exception cref="ArgumentNullException">
37 /// <paramref name="source"/> is null.<br/>
39 /// <paramref name="trackingModel"/> is null.
41 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
42 /// <exception cref="ObjectDisposedException">
43 /// <paramref name="source"/> has already been disposed of.<br/>
45 /// <paramref name="trackingModel"/> has already been disposed of.
47 /// <exception cref="ArgumentException"><paramref name="trackingModel"/> has no target.</exception>
48 /// <seealso cref="ImageTrackingModel.SetTarget(ImageObject)"/>
49 /// <since_tizen> 3 </since_tizen>
50 public static async Task<Quadrangle> TrackAsync(MediaVisionSource source,
51 ImageTrackingModel trackingModel)
53 return await TrackAsync(source, trackingModel, null);
57 /// Tracks the given image tracking model on the current frame and <see cref="ImageTrackingConfiguration"/>.
59 /// <param name="source">The current image of sequence where the image tracking model will be tracked.</param>
60 /// <param name="trackingModel">The image tracking model which processed as target of tracking.</param>
61 /// <param name="config">The configuration used for tracking. This value can be null.</param>
62 /// <returns>A task that represents the asynchronous tracking operation.</returns>
63 /// <exception cref="ArgumentNullException">
64 /// <paramref name="source"/> is null.<br/>
66 /// <paramref name="trackingModel"/> is null.
68 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
69 /// <exception cref="ObjectDisposedException">
70 /// <paramref name="source"/> has already been disposed of.<br/>
72 /// <paramref name="trackingModel"/> has already been disposed of.<br/>
74 /// <paramref name="config"/> has already been disposed of.
76 /// <exception cref="ArgumentException"><paramref name="trackingModel"/> has no target.</exception>
77 /// <seealso cref="ImageTrackingModel.SetTarget(ImageObject)"/>
78 /// <since_tizen> 3 </since_tizen>
79 public static async Task<Quadrangle> TrackAsync(MediaVisionSource source,
80 ImageTrackingModel trackingModel, ImageTrackingConfiguration config)
84 throw new ArgumentNullException(nameof(source));
86 if (trackingModel == null)
88 throw new ArgumentNullException(nameof(trackingModel));
91 TaskCompletionSource<Quadrangle> tcs = new TaskCompletionSource<Quadrangle>();
93 using (var cb = ObjectKeeper.Get(GetCallback(tcs)))
95 InteropImage.Track(source.Handle, trackingModel.Handle, EngineConfiguration.GetHandle(config),
96 cb.Target).Validate("Failed to perform image tracking.");
98 return await tcs.Task;
102 private static InteropImage.TrackedCallback GetCallback(TaskCompletionSource<Quadrangle> tcs)
104 return (IntPtr sourceHandle, IntPtr imageTrackingModelHandle, IntPtr engineCfgHandle, IntPtr locationPtr, IntPtr _) =>
108 Quadrangle region = null;
109 if (locationPtr != IntPtr.Zero)
111 region = Marshal.PtrToStructure<global::Interop.MediaVision.Quadrangle>(locationPtr).ToApiStruct();
114 Log.Info(MediaVisionLog.Tag, $"Image tracked, region : {region}");
116 if (!tcs.TrySetResult(region))
118 Log.Info(MediaVisionLog.Tag, "Failed to set track result");
123 MultimediaLog.Error(MediaVisionLog.Tag, "Failed to handle track result", e);
124 tcs.TrySetException(e);