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 InteropFace = Interop.MediaVision.Face;
22 namespace Tizen.Multimedia.Vision
25 /// Provides the ability to track faces on image sources.
27 /// <since_tizen> 4 </since_tizen>
28 public static class FaceTracker
32 /// Performs face tracking on the source with the trackingModel.
34 /// <param name="source">The source of the media to recognize face for.</param>
35 /// <param name="trackingModel">The model will be used for tracking.</param>
36 /// <param name="doLearn">The value indicating whether model learning while tracking. If it is true, then the model will try to learn
37 /// (if it supports learning feature), otherwise the model will be not learned during the invoking tracking iteration.
38 /// Learning process improves tracking correctness, but can decrease tracking performance.</param>
39 /// <returns>A task that represents the asynchronous tracking operation.</returns>
40 /// <exception cref="ArgumentNullException">
41 /// <paramref name="source"/> is null.<br/>
43 /// <paramref name="trackingModel"/> is null.
45 /// <exception cref="ObjectDisposedException">
46 /// <paramref name="source"/> has already been disposed of.<br/>
48 /// <paramref name="trackingModel"/> has already been disposed of.
50 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
51 /// <exception cref="InvalidOperationException"><paramref name="trackingModel"/> is not prepared.</exception>
52 /// <feature>http://tizen.org/feature/vision.face_recognition</feature>
53 /// <since_tizen> 4 </since_tizen>
54 public static async Task<FaceTrackingResult> TrackAsync(MediaVisionSource source,
55 FaceTrackingModel trackingModel, bool doLearn)
59 throw new ArgumentNullException(nameof(source));
61 if (trackingModel == null)
63 throw new ArgumentNullException(nameof(trackingModel));
66 TaskCompletionSource<FaceTrackingResult> tcs = new TaskCompletionSource<FaceTrackingResult>();
68 using (var cb = ObjectKeeper.Get(GetTrackCallback(tcs)))
70 InteropFace.Track(source.Handle, trackingModel.Handle, IntPtr.Zero,
71 cb.Target, doLearn).Validate("Failed to perform face tracking.");
73 return await tcs.Task;
77 private static InteropFace.TrackedCallback GetTrackCallback(TaskCompletionSource<FaceTrackingResult> tcs)
79 return (IntPtr sourceHandle, IntPtr trackingModelHandle, IntPtr engineCfgHandle,
80 IntPtr locationPtr, double confidence, IntPtr _) =>
84 Quadrangle area = null;
85 if (locationPtr != IntPtr.Zero)
87 area = Marshal.PtrToStructure<global::Interop.MediaVision.Quadrangle>(locationPtr).ToApiStruct();
90 Log.Info(MediaVisionLog.Tag, $"Tracked area : {area}, confidence : {confidence}");
92 if (!tcs.TrySetResult(new FaceTrackingResult(locationPtr != IntPtr.Zero, confidence, area)))
94 Log.Error(MediaVisionLog.Tag, "Failed to set tracking result");
99 MultimediaLog.Error(MediaVisionLog.Tag, "Setting tracking result failed.", e);
100 tcs.TrySetException(e);