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 static Interop.MediaVision;
21 namespace Tizen.Multimedia
24 /// This class represents face recognition model interface
26 public class FaceRecognitionModel : IDisposable
28 internal IntPtr _recognitionModelHandle = IntPtr.Zero;
29 private bool _disposed = false;
32 /// Construct of FaceRecognitionModel class
34 public FaceRecognitionModel()
36 int ret = Interop.MediaVision.FaceRecognitionModel.Create(out _recognitionModelHandle);
37 MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to create FaceRecognitionModel.");
41 /// Construct of FaceRecognitionModel class which creates and loads recognition model from file.
44 /// FaceRecognitionModel is loaded from the absolute path directory.\n
45 /// Models has been saved by <see cref="Save()"/> function can be loaded with this function
47 /// <param name="fileName">Name of path/file to load the model</param>
48 /// <seealso cref="Save()"/>
52 public FaceRecognitionModel(string fileName)
54 int ret = Interop.MediaVision.FaceRecognitionModel.Load(fileName, out _recognitionModelHandle);
55 MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to load FaceRecognitionModel from file.");
59 /// Destructor of the FaceRecognitionModel class.
61 ~FaceRecognitionModel()
69 public int[] FaceLabels
73 IntPtr labelsArrayPtr;
75 int ret = Interop.MediaVision.FaceRecognitionModel.QueryLabels(_recognitionModelHandle, out labelsArrayPtr, out numOfLabels);
78 Tizen.Log.Error(MediaVisionLog.Tag, "Failed to get face labels");
82 int[] labels = new int[numOfLabels];
83 for (int i = 0; i < numOfLabels; i++)
85 labels[i] = Marshal.ReadInt32(labelsArrayPtr);
86 labelsArrayPtr = IntPtr.Add(labelsArrayPtr, sizeof(int));
94 /// Calls this method to save recognition model to the file.
97 /// RecognitionModel is saved to the absolute path directory.
99 /// <param name="fileName">Name of the path/file to save the model</param>
103 public void Save(string fileName)
105 if (string.IsNullOrEmpty(fileName))
107 throw new ArgumentException("Invalid file name");
110 int ret = Interop.MediaVision.FaceRecognitionModel.Save(fileName, _recognitionModelHandle);
111 MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to save recognition model to file");
115 /// Adds face image example to be used for face recognition model learning with <see cref="Learn()"/>.
117 /// <param name="source">Source that contains face image</param>
118 /// <param name="faceLabel">The label that identifies face for which example is adding. Specify the same labels for the face images of a single person when calling this method. Has to be unique for each face</param>
119 /// <param name="location">The rectangular location of the face image at the source image.</param>
123 public void Add(MediaVisionSource source, int faceLabel, Rectangle location = null)
127 throw new ArgumentException("Invalid source");
130 IntPtr ptr = IntPtr.Zero;
131 if (location != null)
133 Interop.MediaVision.Rectangle rectangle = new Interop.MediaVision.Rectangle()
135 width = location.Width,
136 height = location.Height,
137 x = location.Point.X,
140 ptr = Marshal.AllocHGlobal(Marshal.SizeOf(rectangle));
141 Marshal.StructureToPtr(rectangle, ptr, false);
144 int ret = Interop.MediaVision.FaceRecognitionModel.Add(source._sourceHandle, _recognitionModelHandle, ptr, faceLabel);
145 MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to add face example image");
149 /// Removes from RecognitionModel all collected with <see cref="Add()"/> function face examples labeled with faceLabel.
151 /// <param name="faceLabel">The label that identifies face for which examples will be removed from the RecognitionModel.</param>
155 public void Remove(int faceLabel)
157 int ret = Interop.MediaVision.FaceRecognitionModel.Remove(_recognitionModelHandle, ref faceLabel);
158 MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to remove image example");
162 /// Removes all image examples known by RecognitionModel.
169 int ret = Interop.MediaVision.FaceRecognitionModel.Reset(_recognitionModelHandle, IntPtr.Zero);
170 MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to remove image example");
174 /// Learns face recognition model.
177 /// Before you start learning process, face recognition models has to be filled with training data - face image examples.
178 /// These examples has to be provided by <see cref="Add()"/> function. Usually, recognition accuracy is increased
179 /// when number of not identical examples is large. But it depends on the used learning algorithm.
181 /// <param name="config">The configuration of engine will be used for learning of the recognition models. If NULL, then default settings will be used</param>
185 public void Learn(FaceEngineConfiguration config = null)
187 int ret = Interop.MediaVision.FaceRecognitionModel.Learn((config != null) ? config._engineHandle : IntPtr.Zero, _recognitionModelHandle);
188 MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to learn");
191 public void Dispose()
194 GC.SuppressFinalize(this);
197 protected virtual void Dispose(bool disposing)
206 // Free managed objects
209 Interop.MediaVision.FaceRecognitionModel.Destroy(_recognitionModelHandle);