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 InteropModel = Interop.MediaVision.FaceTrackingModel;
21 namespace Tizen.Multimedia
24 /// Represents face tracking model.
26 public class FaceTrackingModel : IDisposable
28 private IntPtr _handle = IntPtr.Zero;
29 private bool _disposed = false;
32 /// Initializes a new instance of the <see cref="FaceTrackingModel"/> class.
34 /// <exception cref="NotSupportedException">The feature is not supported.</exception>
35 public FaceTrackingModel()
37 InteropModel.Create(out _handle).Validate("Failed to create FaceTrackingModel.");
41 /// Initializes a new instance of the <see cref="FaceTrackingModel"/> class with the specified path.
44 /// Models has been saved by <see cref="Save()"/> can be loaded.
46 /// <param name="modelPath">Path to the model to load.</param>
47 /// <exception cref="ArgumentNullException"><paramref name="modelPath"/> is null.</exception>
48 /// <exception cref="FileNotFoundException"><paramref name="modelPath"/> is invalid.</exception>
49 /// <exception cref="NotSupportedException">
50 /// The feature is not supported.\n
52 /// <paramref name="modelPath"/> is not supported format.
54 /// <exception cref="UnauthorizedAccessException">No permission to access the specified file.</exception>
55 /// <seealso cref="Save()"/>
56 public FaceTrackingModel(string modelPath)
58 if (modelPath == null)
60 throw new ArgumentNullException(nameof(modelPath));
62 InteropModel.Load(modelPath, out _handle).Validate("Failed to load FaceTrackingModel from file.");
70 private MediaVisionError InvokePrepare(MediaVisionSource source, Quadrangle region)
74 var quad = region.ToMarshalable();
75 return InteropModel.Prepare(Handle, IntPtr.Zero, source.Handle, ref quad);
78 return InteropModel.Prepare(Handle, IntPtr.Zero, source.Handle, IntPtr.Zero);
82 /// Initializes tracking model by the location of the face to be tracked.
84 /// It is usually called once after tracking model is created and each time before tracking
85 /// is started for the new sequence of sources which is not the direct continuation of
86 /// the sequence for which tracking has been performed before. But it is allowed to call it
87 /// between tracking sessions to allow Media Vision start to track more accurately.
90 /// <paramref name="region"/> needs to be the position of the face to be tracked when called first time for the tracking model.
91 /// <paramref name="region"/> is fitted to the valid region of <paramref name="source"/> if <paramref name="region"/> has invalid points.
93 /// <param name="source">The source where face location is specified.
94 /// Usually it is the first frame of the video or the first image in the continuous
95 /// image sequence planned to be used for tracking.</param>
96 /// <param name="region">The region determining position of the face to be tracked on the source.
97 /// If null, then tracking model will try to find previously tracked face by itself.</param>
98 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
99 /// <exception cref="ObjectDisposedException">
100 /// The <see cref="FaceTrackingModel"/> has already been disposed of.\n
102 /// <paramref name="source"/> has already bean disposed of.
104 public void Prepare(MediaVisionSource source, Quadrangle region)
108 throw new ArgumentNullException(nameof(source));
111 InvokePrepare(source, region).Validate("Failed to prepare tracking model.");
115 /// Saves the tracking model to the file.
117 /// <param name="path">Path to the file to save the model.</param>
118 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
119 /// <exception cref="UnauthorizedAccessException">No permission to write to the specified path.</exception>
120 /// <exception cref="ObjectDisposedException">The <see cref="FaceRecognitionModel"/> has already been disposed of.</exception>
121 /// <exception cref="DirectoryNotFoundException">The directory for <paramref name="path"/> does not exist.</exception>
122 public void Save(string path)
126 throw new ArgumentNullException(nameof(path));
129 var ret = InteropModel.Save(path, Handle);
131 if (ret == MediaVisionError.InvalidPath)
133 throw new DirectoryNotFoundException($"The directory for the path({path}) does not exist.");
136 ret.Validate("Failed to save tracking model to file");
139 public void Dispose()
142 GC.SuppressFinalize(this);
145 protected virtual void Dispose(bool disposing)
152 InteropModel.Destroy(_handle);
156 internal IntPtr Handle
162 throw new ObjectDisposedException(nameof(FaceTrackingModel));