6efe54515d375377044ff6a0ebe0d39decc2fedd
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / MediaVision / FaceTrackingModel.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using static Interop.MediaVision;
20
21 namespace Tizen.Multimedia
22 {
23     /// <summary>
24     /// This class represents face tracking model interface
25     /// </summary>
26     public class FaceTrackingModel : IDisposable
27     {
28         internal IntPtr _trackingModelHandle = IntPtr.Zero;
29         private bool _disposed = false;
30
31         /// <summary>
32         /// Construct of FaceTrackingModel class
33         /// </summary>
34         /// <code>
35         /// var model = new FaceTrackingModel();
36         /// </code>
37         public FaceTrackingModel()
38         {
39             int ret = Interop.MediaVision.FaceTrackingModel.Create(out _trackingModelHandle);
40             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to create FaceTrackingModel.");
41         }
42
43         /// <summary>
44         /// Construct of FaceTrackingModel class which creates and loads tracking model from file.
45         /// </summary>
46         /// <remarks>
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
49         /// </remarks>
50         /// <param name="fileName">Name of path/file to load the model</param>
51         /// <seealso cref="Save()"/>
52         /// <seealso cref="Prepare()"/>
53         /// <code>
54         /// 
55         /// </code>
56         public FaceTrackingModel(string fileName)
57         {
58             int ret = Interop.MediaVision.FaceTrackingModel.Load(fileName, out _trackingModelHandle);
59             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to load FaceTrackingModel from file.");
60         }
61
62         /// <summary>
63         /// Destructor of the FaceTrackingModel class.
64         /// </summary>
65         ~FaceTrackingModel()
66         {
67             Dispose(false);
68         }
69
70         /// <summary>
71         /// Calls this function to initialize tracking model by the location of the face to be tracked.
72         /// </summary>
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)
77         {
78             if (source == null)
79             {
80                 throw new ArgumentException("Invalid source");
81             }
82
83             IntPtr ptr = IntPtr.Zero;
84             if (location != null)
85             {
86                 Interop.MediaVision.Quadrangle quadrangle = new Interop.MediaVision.Quadrangle()
87                 {
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
92                 };
93                 ptr = Marshal.AllocHGlobal(Marshal.SizeOf(quadrangle));
94                 Marshal.StructureToPtr(quadrangle, ptr, false);
95             }
96
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.");
101         }
102
103         /// <summary>
104         /// Calls this method to save tracking model to the file.
105         /// </summary>
106         /// <remarks>
107         /// TrackingModel is saved to the absolute path directory.
108         /// </remarks>
109         /// <param name="fileName">Name of the path/file to save the model</param>
110         public void Save(string fileName)
111         {
112             if (string.IsNullOrEmpty(fileName))
113             {
114                 throw new ArgumentException("Invalid file name");
115             }
116
117             int ret = Interop.MediaVision.FaceTrackingModel.Save(fileName, _trackingModelHandle);
118             MediaVisionErrorFactory.CheckAndThrowException(ret, "Failed to save tracking model to file");
119         }
120
121         public void Dispose()
122         {
123             Dispose(true);
124             GC.SuppressFinalize(this);
125         }
126
127         protected virtual void Dispose(bool disposing)
128         {
129             if (_disposed)
130             {
131                 return;
132             }
133
134             if (disposing)
135             {
136                 // Free managed objects
137             }
138
139             Interop.MediaVision.FaceTrackingModel.Destroy(_trackingModelHandle);
140             _disposed = true;
141         }
142     }
143 }