/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.IO;
using InteropModel = Interop.MediaVision.FaceTrackingModel;
namespace Tizen.Multimedia.Vision
{
///
/// Represents the face tracking model.
///
/// http://tizen.org/feature/vision.face_recognition
/// 4
public class FaceTrackingModel : IDisposable
{
private IntPtr _handle = IntPtr.Zero;
private bool _disposed = false;
///
/// Initializes a new instance of the class.
///
/// The feature is not supported.
/// 4
public FaceTrackingModel()
{
InteropModel.Create(out _handle).Validate("Failed to create FaceTrackingModel.");
}
///
/// Initializes a new instance of the class with the specified path.
///
///
/// Models saved by can be loaded.
///
/// Path to the model to load.
/// is null.
/// is invalid.
///
/// The feature is not supported.
/// -or-
/// is not supported format.
///
/// No permission to access the specified file.
///
/// 4
public FaceTrackingModel(string modelPath)
{
if (modelPath == null)
{
throw new ArgumentNullException(nameof(modelPath));
}
InteropModel.Load(modelPath, out _handle).Validate("Failed to load FaceTrackingModel from file.");
}
///
/// Finalizes an instance of the FaceTrackingModel class.
///
~FaceTrackingModel()
{
Dispose(false);
}
private MediaVisionError InvokePrepare(MediaVisionSource source, Quadrangle region)
{
if (region != null)
{
var quad = region.ToMarshalable();
return InteropModel.Prepare(Handle, IntPtr.Zero, source.Handle, ref quad);
}
return InteropModel.Prepare(Handle, IntPtr.Zero, source.Handle, IntPtr.Zero);
}
///
/// Initializes the tracking model by the location of the face to be tracked.
///
/// It is usually called once after the tracking model is created, and each time before tracking
/// is started for the new sequence of sources, which is not the direct continuation of
/// the sequence for which tracking has been performed before. But, it is allowed to call it
/// between tracking sessions to allow Media Vision start to track more accurately.
///
///
/// needs to be the position of the face to be tracked when called first time for the tracking model.
/// is fitted to the valid region of if has invalid points.
///
/// 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.
/// The region 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.
/// is null.
///
/// The has already been disposed of.
/// -or-
/// has already bean disposed of.
///
/// 4
public void Prepare(MediaVisionSource source, Quadrangle region)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
InvokePrepare(source, region).Validate("Failed to prepare tracking model.");
}
///
/// Saves the tracking model to the file.
///
/// Path to the file to save the model.
/// is null.
/// No permission to write to the specified path.
/// The has already been disposed of.
/// The directory for does not exist.
/// 4
public void Save(string path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
var ret = InteropModel.Save(path, Handle);
if (ret == MediaVisionError.InvalidPath)
{
throw new DirectoryNotFoundException($"The directory for the path({path}) does not exist.");
}
ret.Validate("Failed to save tracking model to file");
}
///
/// Releases all the resources used by the object.
///
/// 4
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Releases the resources used by the object.
///
///
/// true to release both managed and unmanaged resources; otherwise false to release only unmanaged resources.
///
/// 4
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
InteropModel.Destroy(_handle);
_disposed = true;
}
internal IntPtr Handle
{
get
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(FaceTrackingModel));
}
return _handle;
}
}
}
}