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 tracking model interface
26 public class FaceTrackingModel : IDisposable
28 internal IntPtr _trackingModelHandle = IntPtr.Zero;
29 private bool _disposed = false;
32 /// Construct of FaceTrackingModel class
35 /// var model = new FaceTrackingModel();
37 public FaceTrackingModel()
39 int ret = Interop.MediaVision.FaceTrackingModel.Create(out _trackingModelHandle);
40 MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to create FaceTrackingModel.");
44 /// Construct of FaceTrackingModel class which creates and loads tracking model from file.
47 /// FaceTrackingModel is loaded from the absolute path directory.\n
48 /// Models has been saved by <see cref="Save()"/> function can be loaded with this function
50 /// <param name="fileName">Name of path/file to load the model</param>
51 /// <seealso cref="Save()"/>
52 /// <seealso cref="Prepare()"/>
56 public FaceTrackingModel(string fileName)
58 int ret = Interop.MediaVision.FaceTrackingModel.Load(fileName, out _trackingModelHandle);
59 MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to load FaceTrackingModel from file.");
63 /// Destructor of the FaceTrackingModel class.
71 /// Calls this function to initialize tracking model by the location of the face to be tracked.
73 /// <param name="config">The configuration of engine will be used for model preparing. If NULL, then default settings will be used.</param>
74 /// <param name="source">The source where face location is specified. Usually it is the first frame of the video or the first image in the continuous image sequence planned to be used for tracking</param>
75 /// <param name="location">The quadrangle-shaped location determining position of the face to be tracked on the source. If NULL, then tracking model will try to find previously tracked face by itself. Don't set NULL when called first time for the tracking model.</param>
76 public void Prepare(FaceEngineConfiguration config, MediaVisionSource source, Quadrangle location = null)
80 throw new ArgumentException("Invalid source");
83 IntPtr ptr = IntPtr.Zero;
86 Interop.MediaVision.Quadrangle quadrangle = new Interop.MediaVision.Quadrangle()
88 x1 = location.Points[0].X, y1 = location.Points[0].Y,
89 x2 = location.Points[1].X, y2 = location.Points[1].Y,
90 x3 = location.Points[2].X, y3 = location.Points[2].Y,
91 x4 = location.Points[3].X, y4 = location.Points[3].Y
93 ptr = Marshal.AllocHGlobal(Marshal.SizeOf(quadrangle));
94 Marshal.StructureToPtr(quadrangle, ptr, false);
97 int ret = Interop.MediaVision.FaceTrackingModel.Prepare(_trackingModelHandle,
98 (config != null) ? config._engineHandle : IntPtr.Zero,
99 source._sourceHandle, ptr);
100 MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to prepare tracking model.");
104 /// Calls this method to save tracking model to the file.
107 /// TrackingModel is saved to the absolute path directory.
109 /// <param name="fileName">Name of the path/file to save the model</param>
110 public void Save(string fileName)
112 if (string.IsNullOrEmpty(fileName))
114 throw new ArgumentException("Invalid file name");
117 int ret = Interop.MediaVision.FaceTrackingModel.Save(fileName, _trackingModelHandle);
118 MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to save tracking model to file");
121 public void Dispose()
124 GC.SuppressFinalize(this);
127 protected virtual void Dispose(bool disposing)
136 // Free managed objects
139 Interop.MediaVision.FaceTrackingModel.Destroy(_trackingModelHandle);