using DlibDotNet;
using System.Runtime.Serialization;
namespace View_by_Distance.FaceRecognitionDotNet.Models;
///
/// Represents a feature data of face. This class cannot be inherited.
///
[Serializable]
public sealed class FaceEncoding : DisposableObject, ISerializable
{
#region Fields
[NonSerialized]
private readonly Matrix _Encoding;
#endregion
#region Constructors
internal FaceEncoding(Matrix encoding) => _Encoding = encoding;
private FaceEncoding(SerializationInfo info, StreamingContext context)
{
if (info == null)
throw new NullReferenceException(nameof(info));
double[]? array = info.GetValue(nameof(_Encoding), typeof(double[])) as double[];
int? row = (int?)info.GetValue(nameof(_Encoding.Rows), typeof(int));
int? column = (int?)info.GetValue(nameof(_Encoding.Columns), typeof(int));
if (row is null)
throw new NullReferenceException(nameof(row));
if (column is null)
throw new NullReferenceException(nameof(column));
_Encoding = new Matrix(array, row.Value, column.Value);
}
#endregion
#region Properties
internal Matrix Encoding => _Encoding;
///
/// Gets the size of feature data.
///
/// This object is disposed.
public int Size
{
get
{
ThrowIfDisposed();
return _Encoding.Size;
}
}
#endregion
#region Methods
///
/// Gets a feature data of face as raw format.
///
/// A array that represents a feature data.
/// class supports serialization. This method is for interoperability between FaceRecognitionotNet and dlib.
/// This object is disposed.
public double[] GetRawEncoding()
{
ThrowIfDisposed();
return _Encoding.ToArray();
}
#region Overrides
///
/// Releases all unmanaged resources.
///
protected override void DisposeUnmanaged()
{
base.DisposeUnmanaged();
_Encoding?.Dispose();
}
#endregion
#endregion
#region ISerializable Members
///
/// Populates a with the data needed to serialize the target object.
///
/// The to populate with data.
/// The destination (see ) for this serialization.
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(_Encoding), _Encoding.ToArray());
info.AddValue(nameof(_Encoding.Rows), _Encoding.Rows);
info.AddValue(nameof(_Encoding.Columns), _Encoding.Columns);
}
#endregion
}