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.
19 using System.Runtime.InteropServices;
20 using InteropModel = Interop.MediaVision.FaceRecognitionModel;
22 namespace Tizen.Multimedia.Vision
25 /// Represents the face recognition model interface.
27 /// <since_tizen> 3 </since_tizen>
28 public class FaceRecognitionModel : IDisposable
30 private IntPtr _handle = IntPtr.Zero;
31 private bool _disposed = false;
34 /// Initializes a new instance of the <see cref="FaceRecognitionModel"/> class.
36 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
37 /// <since_tizen> 3 </since_tizen>
38 public FaceRecognitionModel()
40 InteropModel.Create(out _handle).Validate("Failed to create FaceRecognitionModel");
44 /// Initializes a new instance of the <see cref="FaceRecognitionModel"/> class with the specified path.
47 /// Models saved by <see cref="Save(string)"/> can be loaded.
49 /// <param name="modelPath">Path to the model to load.</param>
50 /// <exception cref="ArgumentNullException"><paramref name="modelPath"/> is null.</exception>
51 /// <exception cref="FileNotFoundException"><paramref name="modelPath"/> is invalid.</exception>
52 /// <exception cref="NotSupportedException">
53 /// The feature is not supported.<br/>
55 /// <paramref name="modelPath"/> is not supported format.
57 /// <exception cref="UnauthorizedAccessException">No permission to access the specified file.</exception>
58 /// <seealso cref="Save(string)"/>
59 /// <since_tizen> 3 </since_tizen>
60 public FaceRecognitionModel(string modelPath)
62 if (modelPath == null)
64 throw new ArgumentNullException(nameof(modelPath));
67 InteropModel.Load(modelPath, out _handle).
68 Validate("Failed to load FaceRecognitionModel from file");
72 /// Finalizes an instance of the FaceRecognitionModel class.
74 ~FaceRecognitionModel()
80 /// Gets labels that had been learned by the model.
82 /// <since_tizen> 3</since_tizen>
87 IntPtr unmangedArray = IntPtr.Zero;
92 InteropModel.QueryLabels(Handle, out unmangedArray, out numOfLabels).
93 Validate("Failed to retrieve face labels.");
95 int[] labels = new int[numOfLabels];
96 Marshal.Copy(unmangedArray, labels, 0, (int)numOfLabels);
102 if (unmangedArray != IntPtr.Zero)
104 LibcSupport.Free(unmangedArray);
111 /// Saves the recognition model to the file.
113 /// <param name="path">Path to the file to save the model.</param>
114 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
115 /// <exception cref="UnauthorizedAccessException">No permission to write to the specified path.</exception>
116 /// <exception cref="ObjectDisposedException">The <see cref="FaceRecognitionModel"/> has already been disposed of.</exception>
117 /// <exception cref="DirectoryNotFoundException">The directory for <paramref name="path"/> does not exist.</exception>
118 /// <since_tizen> 3 </since_tizen>
119 public void Save(string path)
123 throw new ArgumentNullException(nameof(path));
126 var ret = InteropModel.Save(path, Handle);
128 if (ret == MediaVisionError.InvalidPath)
130 throw new DirectoryNotFoundException($"The directory for the path({path}) does not exist.");
133 ret.Validate("Failed to save recognition model to file");
136 private MediaVisionError InvokeAdd(MediaVisionSource source, int label, Rectangle? area)
140 var rect = area.Value.ToMarshalable();
141 return InteropModel.Add(source.Handle, Handle, ref rect, label);
144 return InteropModel.Add(source.Handle, Handle, IntPtr.Zero, label);
148 /// Adds the face image example to be used for face recognition model learning.
150 /// <param name="source">The <see cref="MediaVisionSource"/> that contains face image.</param>
151 /// <param name="label">The label that identifies face for which example is adding.
152 /// Specify the same labels for the face images of a single person when calling this method.
153 /// Has to be unique for each face.</param>
154 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
155 /// <exception cref="ObjectDisposedException">
156 /// The <see cref="FaceRecognitionModel"/> has already been disposed of.<br/>
158 /// <paramref name="source"/> has already been dispose of.
160 /// <seealso cref="Learn(FaceRecognitionConfiguration)"/>
161 /// <since_tizen> 3 </since_tizen>
162 public void Add(MediaVisionSource source, int label)
166 throw new ArgumentNullException(nameof(source));
169 InvokeAdd(source, label, null).Validate("Failed to add face example image");
173 /// Adds the face image example to be used for face recognition model learning.
175 /// <param name="source">The <see cref="MediaVisionSource"/> that contains face image.</param>
176 /// <param name="label">The label that identifies face for which example is adding.
177 /// Specify the same labels for the face images of a single person when calling this method.
178 /// Has to be unique for each face.</param>
179 /// <param name="area">The rectangular region of the face image at the source image.</param>
180 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
181 /// <exception cref="ObjectDisposedException">
182 /// The <see cref="FaceRecognitionModel"/> has already been disposed of.<br/>
184 /// <paramref name="source"/> has already been dispose of.
186 /// <seealso cref="Learn(FaceRecognitionConfiguration)"/>
187 /// <since_tizen> 3 </since_tizen>
188 public void Add(MediaVisionSource source, int label, Rectangle area)
192 throw new ArgumentNullException(nameof(source));
195 InvokeAdd(source, label, area).Validate("Failed to add face example image");
199 /// Removes all face examples added with the specified label.
201 /// <param name="label">The label that identifies face for which examples will be removed.</param>
202 /// <exception cref="ObjectDisposedException">The <see cref="FaceRecognitionModel"/> has already been disposed of.</exception>
203 /// <returns>true if the examples are successfully removed; otherwise, false if there is no example labeled with the specified label.</returns>
204 /// <seealso cref="Add(MediaVisionSource, int)"/>
205 /// <seealso cref="Add(MediaVisionSource, int, Rectangle)"/>
206 /// <since_tizen> 3 </since_tizen>
207 public bool Remove(int label)
209 var ret = InteropModel.Remove(Handle, ref label);
211 if (ret == MediaVisionError.KeyNotAvailable)
216 ret.Validate("Failed to remove image example");
221 /// Removes all face examples.
223 /// <exception cref="ObjectDisposedException">The <see cref="FaceRecognitionModel"/> has already been disposed of.</exception>
224 /// <since_tizen> 3 </since_tizen>
227 InteropModel.Reset(Handle).Validate("Failed to reset image example");
232 /// Learns the face recognition model.
235 /// Before you start the learning process, face recognition models have to be filled with the training data - face image examples.
236 /// These examples have to be provided by <see cref="Add(MediaVisionSource, int)"/> or <see cref="Add(MediaVisionSource, int, Rectangle)"/>.
237 /// Recognition accuracy is usually increased when the different examples of the identical faces are added more and more.
238 /// But it depends on the used learning algorithm.
240 /// <exception cref="ObjectDisposedException">The <see cref="FaceRecognitionModel"/> has already been disposed of.</exception>
241 /// <exception cref="InvalidOperationException">No examples added.</exception>
242 /// <seealso cref="Add(MediaVisionSource, int)"/>
243 /// <seealso cref="Add(MediaVisionSource, int, Rectangle)"/>
244 /// <since_tizen> 3 </since_tizen>
251 /// Learns the face recognition model with <see cref="FaceRecognitionConfiguration"/>.
254 /// Before you start the learning process, face recognition models have to be filled with the training data - face image examples.
255 /// These examples have to be provided by <see cref="Add(MediaVisionSource, int)"/> or <see cref="Add(MediaVisionSource, int, Rectangle)"/>.
256 /// Recognition accuracy is usually increased when the different examples of the identical faces are added more and more.
257 /// But it depends on the used learning algorithm.
259 /// <param name="config">The configuration used for learning of the recognition models. This value can be null.</param>
260 /// <exception cref="ObjectDisposedException">
261 /// The <see cref="FaceRecognitionModel"/> has already been disposed of.<br/>
263 /// <paramref name="config"/> has already been disposed of.
265 /// <exception cref="InvalidOperationException">No examples added.</exception>
266 /// <seealso cref="Add(MediaVisionSource, int)"/>
267 /// <seealso cref="Add(MediaVisionSource, int, Rectangle)"/>
268 /// <since_tizen> 3 </since_tizen>
269 public void Learn(FaceRecognitionConfiguration config)
271 InteropModel.Learn(EngineConfiguration.GetHandle(config), Handle).
272 Validate("Failed to learn");
276 /// Releases all the resources used by the <see cref="FaceRecognitionModel"/> object.
278 public void Dispose()
281 GC.SuppressFinalize(this);
285 /// Releases the resources used by the <see cref="FaceRecognitionModel"/> object.
287 /// <param name="disposing">
288 /// true to release both managed and unmanaged resources; otherwise false to release only unmanaged resources.
290 protected virtual void Dispose(bool disposing)
297 InteropModel.Destroy(_handle);
301 internal IntPtr Handle
307 throw new ObjectDisposedException(nameof(FaceRecognitionModel));