2 * Copyright (c) 2019 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.Collections.Generic;
20 using System.Threading.Tasks;
21 using InteropInference = Interop.MediaVision.Inference;
23 namespace Tizen.Multimedia.Vision
26 /// Provides the ability to detect objects and get its locations on image source using inference engine.
28 /// <since_tizen> 6 </since_tizen>
29 public static class ObjectDetector
32 /// Detects objects and gets its locations on the source image using inference engine set in <paramref name="config"/>.<br/>
33 /// Each time when DetectAsync is called, a set of the detected objects at the media source are received asynchronously.
35 /// <feature>http://tizen.org/feature/vision.inference</feature>
36 /// <feature>http://tizen.org/feature/vision.inference.image</feature>
37 /// <param name="source">The source of the media where faces will be detected.</param>
38 /// <param name="config">The engine's configuration that will be used for detecting.</param>
40 /// A task that represents the asynchronous detect operation.<br/>
41 /// If there's no detected object, empty collection will be returned.
43 /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="config"/> is null.</exception>
44 /// <exception cref="InvalidOperationException">Internal error.</exception>
45 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
46 /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
47 /// <seealso cref="InferenceModelConfiguration"/>
48 /// <since_tizen> 6 </since_tizen>
49 public static async Task<IEnumerable<ObjectDetectionResult>> DetectAsync(MediaVisionSource source,
50 InferenceModelConfiguration config)
52 // `vision.inference` feature is already checked, when config is created.
53 ValidationUtil.ValidateFeatureSupported(VisionFeatures.InferenceImage);
57 throw new ArgumentNullException(nameof(source));
61 throw new ArgumentNullException(nameof(config));
64 var tcs = new TaskCompletionSource<IEnumerable<ObjectDetectionResult>>();
66 using (var cb = ObjectKeeper.Get(GetCallback(tcs)))
68 InteropInference.DetectObject(source.Handle, config.GetHandle(), cb.Target).
69 Validate("Failed to detect object.");
71 return await tcs.Task;
75 private static InteropInference.ObjectDetectedCallback GetCallback(TaskCompletionSource<IEnumerable<ObjectDetectionResult>> tcs)
77 return (IntPtr sourceHandle, int numberOfObjects, int[] indices, string[] names, float[] confidences,
78 global::Interop.MediaVision.Rectangle[] locations, IntPtr _) =>
82 if (!tcs.TrySetResult(GetResults(numberOfObjects, indices, names, confidences, locations)))
84 Log.Error(MediaVisionLog.Tag, "Failed to set object detection result.");
89 tcs.TrySetException(e);
94 private static IEnumerable<ObjectDetectionResult> GetResults(int number, int[] indices,
95 string[] names, float[] confidences, global::Interop.MediaVision.Rectangle[] locations)
99 return Enumerable.Empty<ObjectDetectionResult>();
102 var results = new List<ObjectDetectionResult>();
104 for (int i = 0; i < number; i++)
106 results.Add(new ObjectDetectionResult(indices[i], names[i], confidences[i], locations[i]));