NullReferenceException

This commit is contained in:
2022-08-13 11:19:08 -07:00
parent 3aeab88384
commit 0392de1920
33 changed files with 1278 additions and 1030 deletions

@ -80,12 +80,12 @@ public sealed class FaceRecognition : DisposableObject
/// Initializes a new instance of the <see cref="FaceRecognition"/> class with the instance that contains model binary datum.
/// </summary>
/// <param name="parameter">The instance that contains model binary datum.</param>
/// <exception cref="ArgumentNullException"><paramref name="parameter"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="parameter"/> is null.</exception>
/// <exception cref="NullReferenceException">The model data is null.</exception>
private FaceRecognition(ModelParameter parameter)
{
if (parameter == null)
throw new ArgumentNullException(nameof(parameter));
throw new NullReferenceException(nameof(parameter));
if (parameter.PosePredictor5FaceLandmarksModel == null)
throw new NullReferenceException(nameof(parameter.PosePredictor5FaceLandmarksModel));
@ -149,11 +149,11 @@ public sealed class FaceRecognition : DisposableObject
/// <param name="numberOfTimesToUpsample">The number of image looking for faces. Higher numbers find smaller faces.</param>
/// <param name="batchSize">The number of images to include in each GPU processing batch.</param>
/// <returns>An enumerable collection of array of found face locations.</returns>
/// <exception cref="ArgumentNullException"><paramref name="images"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="images"/> is null.</exception>
public IEnumerable<Location[]> BatchFaceLocations(IEnumerable<Image> images, int numberOfTimesToUpsample, int batchSize = 128)
{
if (images == null)
throw new ArgumentNullException(nameof(images));
throw new NullReferenceException(nameof(images));
List<Location[]>? results = new();
@ -183,14 +183,14 @@ public sealed class FaceRecognition : DisposableObject
/// <param name="faceEncodingToCheck">A single face encoding to compare against a known face encoding.</param>
/// <param name="tolerance">The distance between faces to consider it a match. Lower is more strict. The default value is 0.6.</param>
/// <returns>A True/False value indicating which known a face encoding matches the face encoding to check.</returns>
/// <exception cref="ArgumentNullException"><paramref name="knownFaceEncoding"/> or <paramref name="faceEncodingToCheck"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="knownFaceEncoding"/> or <paramref name="faceEncodingToCheck"/> is null.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="knownFaceEncoding"/> or <paramref name="faceEncodingToCheck"/>.</exception>
public static bool CompareFace(FaceEncoding knownFaceEncoding, FaceEncoding faceEncodingToCheck, double tolerance = 0.6d)
{
if (knownFaceEncoding == null)
throw new ArgumentNullException(nameof(knownFaceEncoding));
throw new NullReferenceException(nameof(knownFaceEncoding));
if (faceEncodingToCheck == null)
throw new ArgumentNullException(nameof(faceEncodingToCheck));
throw new NullReferenceException(nameof(faceEncodingToCheck));
knownFaceEncoding.ThrowIfDisposed();
faceEncodingToCheck.ThrowIfDisposed();
@ -205,14 +205,14 @@ public sealed class FaceRecognition : DisposableObject
/// <param name="faceEncodingToCheck">A single face encoding to compare against the enumerable collection.</param>
/// <param name="tolerance">The distance between faces to consider it a match. Lower is more strict. The default value is 0.6.</param>
/// <returns>An enumerable collection of True/False values indicating which known face encodings match the face encoding to check.</returns>
/// <exception cref="ArgumentNullException"><paramref name="knownFaceEncodings"/> or <paramref name="faceEncodingToCheck"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="knownFaceEncodings"/> or <paramref name="faceEncodingToCheck"/> is null.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="faceEncodingToCheck"/> is disposed. Or <paramref name="knownFaceEncodings"/> contains disposed object.</exception>
public static IEnumerable<bool> CompareFaces(IEnumerable<FaceEncoding> knownFaceEncodings, FaceEncoding faceEncodingToCheck, double tolerance = 0.6d)
{
if (knownFaceEncodings == null)
throw new ArgumentNullException(nameof(knownFaceEncodings));
throw new NullReferenceException(nameof(knownFaceEncodings));
if (faceEncodingToCheck == null)
throw new ArgumentNullException(nameof(faceEncodingToCheck));
throw new NullReferenceException(nameof(faceEncodingToCheck));
faceEncodingToCheck.ThrowIfDisposed();
@ -242,7 +242,7 @@ public sealed class FaceRecognition : DisposableObject
/// Create a new instance of the <see cref="FaceRecognition"/> class.
/// </summary>
/// <param name="parameter">The instance that contains model binary datum.</param>
/// <exception cref="ArgumentNullException"><paramref name="parameter"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="parameter"/> is null.</exception>
/// <exception cref="NullReferenceException">The model data is null.</exception>
public static FaceRecognition Create(ModelParameter parameter) => new(parameter);
@ -252,14 +252,14 @@ public sealed class FaceRecognition : DisposableObject
/// <param name="image">The image contains a face.</param>
/// <param name="locations">The enumerable collection of location rectangle for faces.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"><paramref name="image"/> or <paramref name="locations"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="image"/> or <paramref name="locations"/> is null.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="image"/> is disposed.</exception>
public static IEnumerable<Image> CropFaces(Image image, IEnumerable<Location> locations)
{
if (image == null)
throw new ArgumentNullException(nameof(image));
throw new NullReferenceException(nameof(image));
if (locations == null)
throw new ArgumentNullException(nameof(locations));
throw new NullReferenceException(nameof(locations));
image.ThrowIfDisposed();
@ -302,14 +302,14 @@ public sealed class FaceRecognition : DisposableObject
/// <param name="faceEncoding">The face encoding to compare.</param>
/// <param name="faceToCompare">The face encoding to compare against.</param>
/// <returns>The euclidean distance for comparison face. If 0, faces are completely equal.</returns>
/// <exception cref="ArgumentNullException"><paramref name="faceEncoding"/> or <paramref name="faceToCompare"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="faceEncoding"/> or <paramref name="faceToCompare"/> is null.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="faceEncoding"/> or <paramref name="faceToCompare"/> is disposed.</exception>
public static double FaceDistance(FaceEncoding faceEncoding, FaceEncoding faceToCompare)
{
if (faceEncoding == null)
throw new ArgumentNullException(nameof(faceEncoding));
throw new NullReferenceException(nameof(faceEncoding));
if (faceToCompare == null)
throw new ArgumentNullException(nameof(faceToCompare));
throw new NullReferenceException(nameof(faceToCompare));
faceEncoding.ThrowIfDisposed();
faceToCompare.ThrowIfDisposed();
@ -327,14 +327,14 @@ public sealed class FaceRecognition : DisposableObject
/// <param name="faceEncodings">The enumerable collection of face encoding to compare.</param>
/// <param name="faceToCompare">The face encoding to compare against.</param>
/// <returns>The enumerable collection of euclidean distance for comparison face. If 0, faces are completely equal.</returns>
/// <exception cref="ArgumentNullException"><paramref name="faceEncodings"/> or <paramref name="faceToCompare"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="faceEncodings"/> or <paramref name="faceToCompare"/> is null.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="faceToCompare"/> is disposed. Or <paramref name="faceEncodings"/> contains disposed object.</exception>
public static List<double> FaceDistances(IEnumerable<FaceEncoding> faceEncodings, FaceEncoding faceToCompare)
{
if (faceEncodings == null)
throw new ArgumentNullException(nameof(faceEncodings));
throw new NullReferenceException(nameof(faceEncodings));
if (faceToCompare == null)
throw new ArgumentNullException(nameof(faceToCompare));
throw new NullReferenceException(nameof(faceToCompare));
faceToCompare.ThrowIfDisposed();
@ -362,14 +362,14 @@ public sealed class FaceRecognition : DisposableObject
/// <param name="predictorModel">The dimension of vector which be returned from detector.</param>
/// <param name="model">The model of face detector to detect in image. If <paramref name="knownFaceLocation"/> is not null, this value is ignored.</param>
/// <returns>An enumerable collection of face feature data corresponds to all faces in specified image.</returns>
/// <exception cref="ArgumentNullException"><paramref name="image"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="image"/> is null.</exception>
/// <exception cref="InvalidOperationException"><paramref name="knownFaceLocation"/> contains no elements.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="image"/> or this object or custom face landmark detector is disposed.</exception>
/// <exception cref="NotSupportedException"><see cref="PredictorModel.Custom"/> is not supported.</exception>
public List<FaceEncoding> FaceEncodings(Image image, int numberOfTimesToUpsample, IEnumerable<Location>? knownFaceLocation, int numberOfJitters, PredictorModel predictorModel, Model model)
{
if (image == null)
throw new ArgumentNullException(nameof(image));
throw new NullReferenceException(nameof(image));
if (predictorModel == PredictorModel.Custom)
throw new NotSupportedException("FaceRecognition.PredictorModel.Custom is not supported.");
@ -408,7 +408,7 @@ public sealed class FaceRecognition : DisposableObject
/// <param name="predictorModel">The dimension of vector which be returned from detector.</param>
/// <param name="model">The model of face detector to detect in image. If <paramref name="faceLocations"/> is not null, this value is ignored.</param>
/// <returns>An enumerable collection of dictionary of face parts locations (eyes, nose, etc).</returns>
/// <exception cref="ArgumentNullException"><paramref name="faceImage"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="faceImage"/> is null.</exception>
/// <exception cref="InvalidOperationException"><paramref name="faceLocations"/> contains no elements.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="faceImage"/> or this object or custom face landmark detector is disposed.</exception>
/// <exception cref="NotSupportedException">The custom face landmark detector is not ready.</exception>
@ -416,7 +416,7 @@ public sealed class FaceRecognition : DisposableObject
{
List<(FacePart, FacePoint[])[]> results = new();
if (faceImage == null)
throw new ArgumentNullException(nameof(faceImage));
throw new NullReferenceException(nameof(faceImage));
if (faceLocations != null && !faceLocations.Any())
throw new InvalidOperationException($"{nameof(faceLocations)} contains no elements.");
faceImage.ThrowIfDisposed();
@ -469,12 +469,12 @@ public sealed class FaceRecognition : DisposableObject
/// <param name="numberOfTimesToUpsample">The number of times to up-sample the image when finding faces.</param>
/// <param name="model">The model of face detector to detect in image.</param>
/// <returns>An enumerable collection of face location correspond to all faces in specified image.</returns>
/// <exception cref="ArgumentNullException"><paramref name="image"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="image"/> is null.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="image"/> or this object is disposed.</exception>
public List<Location> FaceLocations(Model model, Image image, int numberOfTimesToUpsample, bool sortByPixelPercentage)
{
if (image == null)
throw new ArgumentNullException(nameof(image));
throw new NullReferenceException(nameof(image));
image.ThrowIfDisposed();
ThrowIfDisposed();
@ -497,12 +497,12 @@ public sealed class FaceRecognition : DisposableObject
/// </summary>
/// <param name="encoding">The <see cref="double"/> array contains face encoding data.</param>
/// <returns>The <see cref="FaceEncoding"/> this method creates.</returns>
/// <exception cref="ArgumentNullException"><paramref name="encoding"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="encoding"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="encoding"/> must be 128.</exception>
public static FaceEncoding LoadFaceEncoding(double[] encoding)
{
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
throw new NullReferenceException(nameof(encoding));
if (encoding.Length != 128)
{
string message = $"{nameof(encoding)}.{nameof(encoding.Length)} must be 128.";
@ -523,7 +523,7 @@ public sealed class FaceRecognition : DisposableObject
/// </summary>
/// <param name="bitmap">The <see cref="Bitmap"/> from which to create the new <see cref="Image"/>.</param>
/// <returns>The <see cref="Image"/> this method creates.</returns>
/// <exception cref="ArgumentNullException"><paramref name="bitmap"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="bitmap"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">The specified <see cref="PixelFormat"/> is not supported.</exception>
public static Image? LoadImage(Bitmap bitmap)
{
@ -635,7 +635,7 @@ public sealed class FaceRecognition : DisposableObject
/// <param name="stride">The stride width in bytes.</param>
/// <param name="mode">A image color mode.</param>
/// <returns>The <see cref="Image"/> this method creates.</returns>
/// <exception cref="ArgumentNullException"><paramref name="array"/> is null.</exception>
/// <exception cref="NullReferenceException"><paramref name="array"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="row"/> is less than 0.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="column"/> is less than 0.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="stride"/> is less than 0.</exception>
@ -644,7 +644,7 @@ public sealed class FaceRecognition : DisposableObject
public static Image? LoadImage(byte[] array, int row, int column, int stride, Mode mode)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
throw new NullReferenceException(nameof(array));
if (row < 0)
throw new ArgumentOutOfRangeException($"{nameof(row)}", $"{nameof(row)} is less than 0.");
if (column < 0)
@ -757,7 +757,7 @@ public sealed class FaceRecognition : DisposableObject
if (predictorModel == PredictorModel.Custom)
{
if (CustomFaceLandmarkDetector is null)
throw new ArgumentNullException(nameof(CustomFaceLandmarkDetector));
throw new NullReferenceException(nameof(CustomFaceLandmarkDetector));
foreach (Location? rect in locations)
{
FullObjectDetection? ret = CustomFaceLandmarkDetector.Detect(faceImage, rect);