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.Threading.Tasks;
19 using InteropFace = Interop.MediaVision.Face;
21 namespace Tizen.Multimedia.Vision
24 /// Provides the ability to detect faces on image sources.
26 /// <since_tizen> 4 </since_tizen>
27 public static class FaceDetector
31 /// Detects faces on the source.<br/>
32 /// Each time when DetectAsync is called, a set of the detected faces at the media source are received asynchronously.
34 /// <param name="source">The source of the media where faces will be detected.</param>
35 /// <returns>A task that represents the asynchronous detect operation.</returns>
36 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
37 /// <exception cref="NotSupportedException">
38 /// The feature is not supported.<br/>
40 /// The format of <paramref name="source"/> is not supported.
42 /// <feature>http://tizen.org/feature/vision.face_recognition</feature>
43 /// <since_tizen> 4 </since_tizen>
44 public static async Task<Rectangle[]> DetectAsync(MediaVisionSource source)
46 return await DetectAsync(source, null);
50 /// Detects faces on the source.<br/>
51 /// Each time when DetectAsync is called, a set of the detected faces at the media source are received asynchronously.
53 /// <param name="source">The source of the media where faces will be detected.</param>
54 /// <param name="config">The configuration of engine will be used for detecting. This value can be null.</param>
55 /// <returns>A task that represents the asynchronous detect operation.</returns>
56 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
57 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
58 /// <feature>http://tizen.org/feature/vision.face_recognition</feature>
59 /// <since_tizen> 4 </since_tizen>
60 public static async Task<Rectangle[]> DetectAsync(MediaVisionSource source,
61 FaceDetectionConfiguration config)
65 throw new ArgumentNullException(nameof(source));
68 TaskCompletionSource<Rectangle[]> tcs = new TaskCompletionSource<Rectangle[]>();
70 using (var cb = ObjectKeeper.Get(GetCallback(tcs)))
72 InteropFace.Detect(source.Handle, EngineConfiguration.GetHandle(config), cb.Target).
73 Validate("Failed to perform face detection");
75 return await tcs.Task;
79 private static InteropFace.DetectedCallback GetCallback(TaskCompletionSource<Rectangle[]> tcs)
81 return (IntPtr sourceHandle, IntPtr engineConfig, global::Interop.MediaVision.Rectangle[] facesLocations,
82 int numberOfFaces, IntPtr _) =>
86 Log.Info(MediaVisionLog.Tag, $"Faces detected, count : {numberOfFaces}.");
87 Rectangle[] locations = new Rectangle[numberOfFaces];
88 for (int i = 0; i < numberOfFaces; i++)
90 locations[i] = facesLocations[i].ToApiStruct();
91 Log.Info(MediaVisionLog.Tag, $"Face {0} detected : {locations}.");
94 if (!tcs.TrySetResult(locations))
96 Log.Error(MediaVisionLog.Tag, "Failed to set face detection result.");
101 MultimediaLog.Info(MediaVisionLog.Tag, "Failed to handle face detection.", e);
102 tcs.TrySetException(e);