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.
17 using System.Collections.Generic;
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 recognize images on image sources.
27 /// <since_tizen> 3 </since_tizen>
28 public static class ImageRecognizer
31 /// Recognizes the given image objects on the source image.\n
33 /// <param name="source">The source image on which image objects will be recognized.</param>
34 /// <param name="imageObjects">The array of image objects which will be processed as targets of recognition.</param>
35 /// <returns>A task that represents the asynchronous recognition operation.</returns>
36 /// <exception cref="ArgumentNullException">
37 /// <paramref name="source"/> is null.\n
39 /// <paramref name="imageObjects"/> is null.\n
41 /// <paramref name="imageObjects"/> contains null reference.
43 /// <exception cref="ArgumentException"><paramref name="imageObjects"/> has no elements.(The length is zero.)</exception>
44 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
45 /// <exception cref="ObjectDisposedException"><paramref name="source"/> has already been disposed of.</exception>
46 /// <since_tizen> 3 </since_tizen>
47 public static async Task<IEnumerable<ImageRecognitionResult>> RecognizeAsync(
48 MediaVisionSource source, ImageObject[] imageObjects)
50 return await RecognizeAsync(source, imageObjects, null);
54 /// Recognizes the given image objects on the source image.\n
56 /// <param name="source">The source image on which image objects will be recognized.</param>
57 /// <param name="imageObjects">The array of image objects which will be processed as targets of recognition.</param>
58 /// <param name="config">The configuration used for recognition. This value can be null.</param>
59 /// <returns>A task that represents the asynchronous recognition operation.</returns>
60 /// <exception cref="ArgumentNullException">
61 /// <paramref name="source"/> is null.\n
63 /// <paramref name="imageObjects"/> is null.\n
65 /// <paramref name="imageObjects"/> contains null elements.
67 /// <exception cref="ArgumentException"><paramref name="imageObjects"/> has no elements.(The length is zero.)</exception>
68 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
69 /// <exception cref="ObjectDisposedException">
70 /// <paramref name="source"/> has already been disposed of.\n
72 /// <paramref name="config"/> has already been disposed of.
74 /// <since_tizen> 3 </since_tizen>
75 public static async Task<IEnumerable<ImageRecognitionResult>> RecognizeAsync(MediaVisionSource source,
76 ImageObject[] imageObjects, ImageRecognitionConfiguration config)
80 throw new ArgumentNullException(nameof(source));
82 if (imageObjects == null)
84 throw new ArgumentNullException(nameof(imageObjects));
86 if (imageObjects.Length == 0)
88 throw new ArgumentException("No image object to recognize.", nameof(imageObjects));
91 var tcs = new TaskCompletionSource<IEnumerable<ImageRecognitionResult>>();
93 using (var cb = ObjectKeeper.Get(GetCallback(tcs)))
94 using (var imageHandles = ObjectKeeper.Get(GetHandles(imageObjects)))
96 InteropImage.Recognize(source.Handle, imageHandles.Target, imageHandles.Target.Length,
97 EngineConfiguration.GetHandle(config), cb.Target).
98 Validate("Failed to perform image recognition.");
100 return await tcs.Task;
104 private static ImageRecognitionResult[] CreateResults(IntPtr[] locations, uint numOfObjects)
106 ImageRecognitionResult[] results = new ImageRecognitionResult[numOfObjects];
108 for (int i = 0; i < numOfObjects; i++)
110 Quadrangle quadrangle = locations[i] != IntPtr.Zero ?
111 Marshal.PtrToStructure<global::Interop.MediaVision.Quadrangle>(locations[i]).ToApiStruct() : null;
113 results[i] = new ImageRecognitionResult(locations[i] != IntPtr.Zero, quadrangle);
119 private static InteropImage.RecognizedCallback GetCallback(
120 TaskCompletionSource<IEnumerable<ImageRecognitionResult>> tcs)
122 return (IntPtr source, IntPtr engineConfig, IntPtr imageObjectHandles,
123 IntPtr[] locations, uint numOfObjects, IntPtr _) =>
127 if (!tcs.TrySetResult(CreateResults(locations, numOfObjects)))
129 Log.Info(MediaVisionLog.Tag, "Failed to set recognition result");
134 MultimediaLog.Error(MediaVisionLog.Tag, "Failed to handle recognition result", e);
135 tcs.TrySetException(e);
140 private static IntPtr[] GetHandles(ImageObject[] imageObjects)
142 IntPtr[] imageHandles = new IntPtr[imageObjects.Length];
143 for (int i = 0; i < imageObjects.Length; i++)
145 if (imageObjects[i] == null)
147 throw new ArgumentNullException($"{nameof(imageObjects)}[{i}]");
150 imageHandles[i] = imageObjects[i].Handle;