diff --git a/Instance/DlibDotNet.cs b/Instance/DlibDotNet.cs index 2e083a2..d008b28 100644 --- a/Instance/DlibDotNet.cs +++ b/Instance/DlibDotNet.cs @@ -15,7 +15,7 @@ using View_by_Distance.Shared.Models.Stateless; namespace View_by_Distance.Instance; -public class DlibDotNet +public partial class DlibDotNet { private readonly D_Face _Faces; @@ -262,12 +262,12 @@ public class DlibDotNet throw new Exception("Input directory should be the source and not a resized directory!"); } - private void FullParallelForWork(A_Property propertyLogic, string outputResolution, string bResultsFullGroupDirectory, string cResultsFullGroupDirectory, string dResultsFullGroupDirectory, string d2ResultsFullGroupDirectory, List> sourceDirectoryChanges, List propertyFileHolderCollection, List propertyCollection, List>> metadataCollections, List> resizeKeyValuePairs, List> imageFaceCollections, string sourceDirectory, int index, Item item) + private void FullParallelForWork(A_Property propertyLogic, string outputResolution, string bResultsFullGroupDirectory, string cResultsFullGroupDirectory, string dResultsFullGroupDirectory, string d2ResultsFullGroupDirectory, string eResultsFullGroupDirectory, List> sourceDirectoryChanges, List propertyFileHolderCollection, List propertyCollection, List>> metadataCollections, List> resizeKeyValuePairs, List?> imageFaceCollections, List faceEncodingRequired, Container container, int index, Item item) { if (item.ImageFileHolder is null) throw new NullReferenceException(nameof(item.ImageFileHolder)); Shared.Models.Property property; - List faceCollection; + List? faceCollection; string original = "Original"; FileHolder? resizedFileHolder; long ticks = DateTime.Now.Ticks; @@ -313,7 +313,7 @@ public class DlibDotNet File.WriteAllBytes(path, bytes); } if (!_Configuration.LoadOrCreateThenSaveImageFacesResultsForOutputResolutions.Contains(outputResolution)) - faceCollection = new(); + faceCollection = null; else { int[] outputResolutionCollection = imageResizeKeyValuePairs[outputResolution]; @@ -330,11 +330,14 @@ public class DlibDotNet int normalizedPixelPercentageDistinctCount = normalizedPixelPercentageCollection.Distinct().Count(); if (normalizedPixelPercentageDistinctCount != normalizedPixelPercentageCollection.Length || _Configuration.SaveFaceLandmarkForOutputResolutions.Contains(outputResolution)) { + bool saveRotated = _Configuration.SaveFaceLandmarkForOutputResolutions.Contains(outputResolution); - _FaceParts.SaveFaceLandmarkImages(d2ResultsFullGroupDirectory, sourceDirectory, subFileTuples, parseExceptions, item, faceCollection, saveRotated); + _FaceParts.SaveFaceLandmarkImages(d2ResultsFullGroupDirectory, container, subFileTuples, parseExceptions, item, faceCollection, saveRotated); if (_AppSettings.MaxDegreeOfParallelism < 2) ticks = LogDelta(ticks, nameof(D2_FaceParts.SaveFaceLandmarkImages)); } + if (_Configuration.LoadOrCreateThenSaveDistanceResultsForOutputResolutions.Contains(outputResolution)) + _Distance.GetFaceDistances(eResultsFullGroupDirectory, faceEncodingRequired, subFileTuples, parseExceptions, item, faceCollection); } lock (sourceDirectoryChanges) { @@ -347,20 +350,20 @@ public class DlibDotNet } } - private int FullParallelWork(long ticks, A_Property propertyLogic, string outputResolution, string bResultsFullGroupDirectory, string cResultsFullGroupDirectory, string dResultsFullGroupDirectory, string d2ResultsFullGroupDirectory, List> sourceDirectoryChanges, List propertyFileHolderCollection, List propertyCollection, List>> metadataCollection, List> resizeKeyValuePairs, List> faceCollections, int containersCount, Container container, Item[] filteredItems) + private int FullParallelWork(long ticks, A_Property propertyLogic, string outputResolution, string bResultsFullGroupDirectory, string cResultsFullGroupDirectory, string dResultsFullGroupDirectory, string d2ResultsFullGroupDirectory, string eResultsFullGroupDirectory, List> sourceDirectoryChanges, List propertyFileHolderCollection, List propertyCollection, List>> metadataCollection, List> resizeKeyValuePairs, List?> imageFaceCollections, List faceEncodingRequired, int containersCount, Container container, Item[] filteredItems) { int result = 0; if (_Log is null) throw new NullReferenceException(nameof(_Log)); ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = _AppSettings.MaxDegreeOfParallelism }; ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true }; - if (faceCollections.Count != filteredItems.Length || metadataCollection.Count != filteredItems.Length || resizeKeyValuePairs.Count != filteredItems.Length || propertyCollection.Count != filteredItems.Length) + if (imageFaceCollections.Count != filteredItems.Length || metadataCollection.Count != filteredItems.Length || resizeKeyValuePairs.Count != filteredItems.Length || propertyCollection.Count != filteredItems.Length) { for (int i = 0; i < filteredItems.Length; i++) { - faceCollections.Add(new()); propertyCollection.Add(null); metadataCollection.Add(new()); + imageFaceCollections.Add(null); resizeKeyValuePairs.Add(new()); propertyFileHolderCollection.Add(null); } @@ -373,7 +376,7 @@ public class DlibDotNet { try { - FullParallelForWork(propertyLogic, outputResolution, bResultsFullGroupDirectory, cResultsFullGroupDirectory, dResultsFullGroupDirectory, d2ResultsFullGroupDirectory, sourceDirectoryChanges, propertyFileHolderCollection, propertyCollection, metadataCollection, resizeKeyValuePairs, faceCollections, container.SourceDirectory, index: i, filteredItems[i]); + FullParallelForWork(propertyLogic, outputResolution, bResultsFullGroupDirectory, cResultsFullGroupDirectory, dResultsFullGroupDirectory, d2ResultsFullGroupDirectory, eResultsFullGroupDirectory, sourceDirectoryChanges, propertyFileHolderCollection, propertyCollection, metadataCollection, resizeKeyValuePairs, imageFaceCollections, faceEncodingRequired, container, index: i, filteredItems[i]); if (i == 0 || sourceDirectoryChanges.Any()) progressBar.Tick(); } @@ -424,7 +427,7 @@ public class DlibDotNet } } - private void WriteGroup(Property.Models.Configuration configuration, A_Property propertyLogic, Shared.Models.Property[] propertyCollection, List>> metadataCollection, List> faceCollections, List> resizeKeyValuePairs, string outputResolution, Container container, Item[] filteredItems) + private void WriteGroup(Property.Models.Configuration configuration, A_Property propertyLogic, Shared.Models.Property[] propertyCollection, List>> metadataCollection, List> resizeKeyValuePairs, List?> imageFaceCollections, string outputResolution, Container container, Item[] filteredItems) { Item item; string key; @@ -437,7 +440,7 @@ public class DlibDotNet if (!(from l in propertyCollection where l?.Width is null select true).Any()) { string checkDirectory; - List>> faceCollectionsKeyValuePairs = new(); + List?>> imageFaceCollectionsKeyValuePairs = new(); List> propertyCollectionKeyValuePairs = new(); List>> resizeKeyValuePairsCollections = new(); List>>> metadataCollectionKeyValuePairs = new(); @@ -456,7 +459,7 @@ public class DlibDotNet _FileKeyValuePairs.Add(new KeyValuePair(container.SourceDirectory, key)); _FilePropertiesKeyValuePairs[container.SourceDirectory].Add(new Tuple(key, propertyCollection[i])); } - faceCollectionsKeyValuePairs.Add(new KeyValuePair>(key, faceCollections[i])); + imageFaceCollectionsKeyValuePairs.Add(new KeyValuePair?>(key, imageFaceCollections[i])); propertyCollectionKeyValuePairs.Add(new KeyValuePair(key, propertyCollection[i])); resizeKeyValuePairsCollections.Add(new KeyValuePair>(key, resizeKeyValuePairs[i])); metadataCollectionKeyValuePairs.Add(new KeyValuePair>>(key, metadataCollection[i])); @@ -498,7 +501,7 @@ public class DlibDotNet if (File.Exists(checkFile)) File.Move(checkFile, Path.Combine(checkDirectory, fileName)); checkFile = Path.Combine(checkDirectory, fileName); - json = JsonSerializer.Serialize(faceCollectionsKeyValuePairs, writeIndentedJsonSerializerOptions); + json = JsonSerializer.Serialize(imageFaceCollectionsKeyValuePairs, writeIndentedJsonSerializerOptions); _ = Shared.Models.Stateless.Methods.IPath.WriteAllText(checkFile, json, updateDateWhenMatches: false, compareBeforeWrite: true); } } @@ -521,48 +524,187 @@ public class DlibDotNet return new(aResultsFullGroupDirectory, bResultsFullGroupDirectory, cResultsFullGroupDirectory, dResultsFullGroupDirectory, d2ResultsFullGroupDirectory, eResultsFullGroupDirectory); } - private void SetAngleBracketCollections(Property.Models.Configuration configuration, A_Property propertyLogic, string outputResolution, Container container, string aResultsFullGroupDirectory, string bResultsFullGroupDirectory, string cResultsFullGroupDirectory, string dResultsFullGroupDirectory) + private void SetAngleBracketCollections(Property.Models.Configuration configuration, A_Property propertyLogic, string outputResolution, Container container, string aResultsFullGroupDirectory, string bResultsFullGroupDirectory, string cResultsFullGroupDirectory, string dResultsFullGroupDirectory, string eResultsFullGroupDirectory, bool onlyE) { _Faces.AngleBracketCollection.Clear(); _Resize.AngleBracketCollection.Clear(); _Metadata.AngleBracketCollection.Clear(); + _Distance.AngleBracketCollection.Clear(); propertyLogic.AngleBracketCollection.Clear(); - propertyLogic.AngleBracketCollection.AddRange(Property.Models.Stateless.IResult.GetDirectoryInfoCollection(configuration, - container.SourceDirectory, - aResultsFullGroupDirectory, - contentDescription: string.Empty, - singletonDescription: "Properties for each image", - collectionDescription: string.Empty, - converted: false)); - _Metadata.AngleBracketCollection.AddRange(Property.Models.Stateless.IResult.GetDirectoryInfoCollection(configuration, - container.SourceDirectory, - bResultsFullGroupDirectory, - contentDescription: string.Empty, - singletonDescription: "Metadata as key value pairs", - collectionDescription: string.Empty, - converted: true)); - _Resize.AngleBracketCollection.AddRange(Property.Models.Stateless.IResult.GetDirectoryInfoCollection(configuration, - container.SourceDirectory, - cResultsFullGroupDirectory, - contentDescription: "Resized image", - singletonDescription: "Resize dimensions for each resolution", - collectionDescription: string.Empty, - converted: true)); - if (_Configuration.LoadOrCreateThenSaveImageFacesResultsForOutputResolutions.Contains(outputResolution)) - _Faces.AngleBracketCollection.AddRange(Property.Models.Stateless.IResult.GetDirectoryInfoCollection(configuration, + if (!onlyE) + { + propertyLogic.AngleBracketCollection.AddRange(Property.Models.Stateless.IResult.GetDirectoryInfoCollection(configuration, container.SourceDirectory, - dResultsFullGroupDirectory, - contentDescription: "n png file(s) for each face found", - singletonDescription: string.Empty, - collectionDescription: "For each image a json file with all faces found", + aResultsFullGroupDirectory, + contentDescription: string.Empty, + singletonDescription: "Properties for each image", + collectionDescription: string.Empty, + converted: false)); + _Metadata.AngleBracketCollection.AddRange(Property.Models.Stateless.IResult.GetDirectoryInfoCollection(configuration, + container.SourceDirectory, + bResultsFullGroupDirectory, + contentDescription: string.Empty, + singletonDescription: "Metadata as key value pairs", + collectionDescription: string.Empty, + converted: true)); + _Resize.AngleBracketCollection.AddRange(Property.Models.Stateless.IResult.GetDirectoryInfoCollection(configuration, + container.SourceDirectory, + cResultsFullGroupDirectory, + contentDescription: "Resized image", + singletonDescription: "Resize dimensions for each resolution", + collectionDescription: string.Empty, + converted: true)); + if (_Configuration.LoadOrCreateThenSaveImageFacesResultsForOutputResolutions.Contains(outputResolution)) + _Faces.AngleBracketCollection.AddRange(Property.Models.Stateless.IResult.GetDirectoryInfoCollection(configuration, + container.SourceDirectory, + dResultsFullGroupDirectory, + contentDescription: "n png file(s) for each face found", + singletonDescription: string.Empty, + collectionDescription: "For each image a json file with all faces found", + converted: true)); + } + if (_Configuration.LoadOrCreateThenSaveDistanceResultsForOutputResolutions.Contains(outputResolution)) + _Distance.AngleBracketCollection.AddRange(Property.Models.Stateless.IResult.GetDirectoryInfoCollection(configuration, + container.SourceDirectory, + eResultsFullGroupDirectory, + contentDescription: string.Empty, + singletonDescription: "n json file(s) for each face found", + collectionDescription: string.Empty, converted: true)); } - private void FullDoWork(string argZero, Property.Models.Configuration configuration, Model? model, PredictorModel? predictorModel, string propertyRoot, long ticks, MapLogic mapLogic, A_Property propertyLogic, Container[] containers) + private (Sorting Sorting, FaceRecognitionDotNet.FaceEncoding FaceEncoding)[] GetValues(string argZero, Container[] containers) + { + (Sorting Sorting, FaceRecognitionDotNet.FaceEncoding FaceEncoding)[] results; + Sorting sorting; + Item[] filteredItems; + List<(Sorting Sorting, FaceRecognitionDotNet.FaceEncoding FaceEncoding)> collection = new(); + foreach (Container container in containers) + { + if (!container.Items.Any()) + continue; + if (!_ArgZeroIsConfigurationRootDirectory && !container.SourceDirectory.StartsWith(argZero)) + continue; + filteredItems = (from l in container.Items where l.ImageFileHolder is not null && (l.Abandoned is null || !l.Abandoned.Value) && l.ValidImageFormatExtension && !_Configuration.IgnoreExtensions.Contains(l.ImageFileHolder.ExtensionLowered) select l).ToArray(); + if (!filteredItems.Any()) + continue; + foreach (Item item in filteredItems) + { + if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) + continue; + if (!item.Faces.Any()) + continue; + foreach (Face face in item.Faces) + { + if (face.FaceEncoding is null || face.Location?.Confidence is null || face.Location?.NormalizedPixelPercentage is null) + continue; + if (!face.Tags.Any() || face.Tags[0] is not FaceRecognitionDotNet.FaceEncoding faceEncoding) + continue; + sorting = new(face.Location.Confidence, item.Property.Id.Value, face.Location.NormalizedPixelPercentage.Value); + collection.Add(new(sorting, faceEncoding)); + face.Tags.Clear(); + } + } + } + results = (from l in collection orderby l.FaceEncoding is not null, l.Sorting.Id select l).ToArray(); + if (results.Any() && results[0].FaceEncoding is null) + throw new Exception("Sorting failed!"); + return results; + } + + private void SaveFaceNumbers(string eResultsFullGroupDirectory, Item item, (Sorting Sorting, FaceRecognitionDotNet.FaceEncoding FaceEncoding)[] inputCollection, List faceEncodings) + { + Sorting sorting; + List doubles; + List faceNumbers; + List sortingOutputCollection; + FaceRecognitionDotNet.FaceEncoding faceEncoding; + if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) + throw new Exception(); + foreach (Face face in item.Faces) + { + if (face.FaceEncoding is null || face.Location?.Confidence is null || face.Location?.NormalizedPixelPercentage is null) + continue; + sortingOutputCollection = new(); + faceEncoding = FaceRecognition.LoadFaceEncoding(face.FaceEncoding.RawEncoding); + doubles = FaceRecognition.FaceDistances(faceEncodings, faceEncoding); + for (int j = 0; j < doubles.Count; j++) + { + sorting = new(inputCollection[j].Sorting, doubles[j]); + lock (sortingOutputCollection) + sortingOutputCollection.Add(sorting); + } + faceNumbers = Shared.Models.Stateless.Methods.ISorting.GetFaceNumbers(sortingOutputCollection); + lock (face) + face.SetFaceNumbers(faceNumbers); + _Distance.SaveFaceNumbers(eResultsFullGroupDirectory, item, face, faceNumbers); + } + } + + private void SaveFaceNumbers(string argZero, A_Property propertyLogic, string outputResolution, string aResultsFullGroupDirectory, string bResultsFullGroupDirectory, string cResultsFullGroupDirectory, string dResultsFullGroupDirectory, string eResultsFullGroupDirectory, Container[] containers, (Sorting Sorting, FaceRecognitionDotNet.FaceEncoding FaceEncoding)[] collection) + { + if (collection.Any() && collection[0].FaceEncoding is null) + throw new Exception("Sorting failed!"); + Item[] filteredItems; + ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = _AppSettings.MaxDegreeOfParallelism }; + foreach (Container container in containers) + { + if (!container.Items.Any()) + continue; + if (!_ArgZeroIsConfigurationRootDirectory && !container.SourceDirectory.StartsWith(argZero)) + continue; + filteredItems = (from l in container.Items where l.ImageFileHolder is not null && (l.Abandoned is null || !l.Abandoned.Value) && l.ValidImageFormatExtension && !_Configuration.IgnoreExtensions.Contains(l.ImageFileHolder.ExtensionLowered) select l).ToArray(); + if (!filteredItems.Any()) + continue; + List faceEncodings = new(); + foreach ((Sorting _, FaceRecognitionDotNet.FaceEncoding faceEncoding) in collection) + { + if (faceEncoding is null) + continue; + faceEncodings.Add(faceEncoding); + } + SetAngleBracketCollections(_Configuration.PropertyConfiguration, propertyLogic, outputResolution, container, aResultsFullGroupDirectory, bResultsFullGroupDirectory, cResultsFullGroupDirectory, dResultsFullGroupDirectory, eResultsFullGroupDirectory, onlyE: true); + _ = Parallel.For(0, filteredItems.Length, parallelOptions, (i, state) => SaveFaceNumbers(eResultsFullGroupDirectory, filteredItems[i], collection, faceEncodings)); + } + } + + private static void SetFaceTags(Item item) + { + FaceRecognitionDotNet.FaceEncoding faceEncoding; + foreach (Face face in item.Faces) + { + if (face.FaceEncoding is null || face.Location?.Confidence is null || face.Location?.NormalizedPixelPercentage is null) + continue; + faceEncoding = FaceRecognition.LoadFaceEncoding(face.FaceEncoding.RawEncoding); + lock (face.Tags) + face.Tags.Add(faceEncoding); + } + } + + private void SetFaceTags(string argZero, List faceEncodingRequired, Container[] containers) + { + Item[] filteredItems; + faceEncodingRequired.Clear(); + ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = _AppSettings.MaxDegreeOfParallelism }; + foreach (Container container in containers) + { + if (!container.Items.Any()) + continue; + if (!_ArgZeroIsConfigurationRootDirectory && !container.SourceDirectory.StartsWith(argZero)) + continue; + filteredItems = (from l in container.Items where l.ImageFileHolder is not null && (l.Abandoned is null || !l.Abandoned.Value) && l.ValidImageFormatExtension && !_Configuration.IgnoreExtensions.Contains(l.ImageFileHolder.ExtensionLowered) select l).ToArray(); + if (!filteredItems.Any()) + continue; + _ = Parallel.For(0, filteredItems.Length, parallelOptions, (i, state) => SetFaceTags(filteredItems[i])); + } + } + + private void FullDoWork(string argZero, Model? model, PredictorModel? predictorModel, string propertyRoot, long ticks, MapLogic mapLogic, A_Property propertyLogic, Container[] containers) { if (_Log is null) throw new NullReferenceException(nameof(_Log)); int[] ids; + bool needToSave; int distinctCount; int exceptionCount; Item[] filteredItems; @@ -572,19 +714,21 @@ public class DlibDotNet string dResultsFullGroupDirectory; string eResultsFullGroupDirectory; string d2ResultsFullGroupDirectory; - List> faceCollections = new(); + List faceEncodingRequired = new(); Shared.Models.Property[] propertyCollection; + List?> imageFaceCollections = new(); List propertyFileHolderCollection = new(); List> resizeKeyValuePairs = new(); List> sourceDirectoryChanges = new(); List nullablePropertyCollection = new(); List>> metadataCollection = new(); - string aPropertySingletonDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(A_Property), "{}"); + string aPropertySingletonDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(_Configuration.PropertyConfiguration, nameof(A_Property), "{}"); foreach (string outputResolution in _Configuration.OutputResolutions) { _FileKeyValuePairs.Clear(); + faceEncodingRequired.Clear(); _FilePropertiesKeyValuePairs.Clear(); - (aResultsFullGroupDirectory, bResultsFullGroupDirectory, cResultsFullGroupDirectory, dResultsFullGroupDirectory, d2ResultsFullGroupDirectory, eResultsFullGroupDirectory) = GetResultsFullGroupDirectories(configuration, model, predictorModel, outputResolution); + (aResultsFullGroupDirectory, bResultsFullGroupDirectory, cResultsFullGroupDirectory, dResultsFullGroupDirectory, d2ResultsFullGroupDirectory, eResultsFullGroupDirectory) = GetResultsFullGroupDirectories(_Configuration.PropertyConfiguration, model, predictorModel, outputResolution); foreach (Container container in containers) { if (!container.Items.Any()) @@ -594,33 +738,31 @@ public class DlibDotNet filteredItems = (from l in container.Items where l.ImageFileHolder is not null && (l.Abandoned is null || !l.Abandoned.Value) && l.ValidImageFormatExtension && !_Configuration.IgnoreExtensions.Contains(l.ImageFileHolder.ExtensionLowered) select l).ToArray(); if (!filteredItems.Any()) continue; - faceCollections.Clear(); metadataCollection.Clear(); resizeKeyValuePairs.Clear(); + imageFaceCollections.Clear(); sourceDirectoryChanges.Clear(); nullablePropertyCollection.Clear(); propertyFileHolderCollection.Clear(); - SetAngleBracketCollections(configuration, propertyLogic, outputResolution, container, aResultsFullGroupDirectory, bResultsFullGroupDirectory, cResultsFullGroupDirectory, dResultsFullGroupDirectory); - exceptionCount = FullParallelWork(ticks, propertyLogic, outputResolution, bResultsFullGroupDirectory, cResultsFullGroupDirectory, dResultsFullGroupDirectory, d2ResultsFullGroupDirectory, sourceDirectoryChanges, propertyFileHolderCollection, nullablePropertyCollection, metadataCollection, resizeKeyValuePairs, faceCollections, containers.Length, container, filteredItems); + SetAngleBracketCollections(_Configuration.PropertyConfiguration, propertyLogic, outputResolution, container, aResultsFullGroupDirectory, bResultsFullGroupDirectory, cResultsFullGroupDirectory, dResultsFullGroupDirectory, eResultsFullGroupDirectory, onlyE: false); + exceptionCount = FullParallelWork(ticks, propertyLogic, outputResolution, bResultsFullGroupDirectory, cResultsFullGroupDirectory, dResultsFullGroupDirectory, d2ResultsFullGroupDirectory, eResultsFullGroupDirectory, sourceDirectoryChanges, propertyFileHolderCollection, nullablePropertyCollection, metadataCollection, resizeKeyValuePairs, imageFaceCollections, faceEncodingRequired, containers.Length, container, filteredItems); #pragma warning disable ids = (from l in filteredItems where l.Property?.Id is not null select l.Property.Id.Value).ToArray(); #pragma warning restore distinctCount = ids.Distinct().Count(); if (ids.Length != distinctCount) _Log.Information($"{ids.Length} != {distinctCount} <{container.SourceDirectory}>"); - if (metadataCollection.Count != filteredItems.Length || nullablePropertyCollection.Count != filteredItems.Length || resizeKeyValuePairs.Count != filteredItems.Length || faceCollections.Count != filteredItems.Length) + if (metadataCollection.Count != filteredItems.Length || nullablePropertyCollection.Count != filteredItems.Length || resizeKeyValuePairs.Count != filteredItems.Length || imageFaceCollections.Count != filteredItems.Length) throw new Exception("Counts don't match!"); if (exceptionCount != 0) _Exceptions.Add(container.SourceDirectory); - for (int i = 0; i < faceCollections.Count; i++) - filteredItems[i].Faces.AddRange(from l in faceCollections[i] select l); + for (int i = 0; i < imageFaceCollections.Count; i++) + filteredItems[i].Faces.AddRange(from l in imageFaceCollections[i] select l); propertyCollection = (from l in nullablePropertyCollection where l is not null select l).ToArray(); if (_ArgZeroIsConfigurationRootDirectory && exceptionCount == 0) - WriteGroup(configuration, propertyLogic, propertyCollection, metadataCollection, faceCollections, resizeKeyValuePairs, outputResolution, container, filteredItems); - if (_ArgZeroIsConfigurationRootDirectory && exceptionCount == 0 && outputResolution == _Configuration.OutputResolutions[0]) - mapLogic.AddToMapLogicAllCollection(filteredItems); - if (exceptionCount == 0 && _Configuration.LoadOrCreateThenSaveDistanceResultsForOutputResolutions.Contains(outputResolution)) - _Distance.LoadOrCreateThenSaveDistanceResults(configuration, eResultsFullGroupDirectory, outputResolution, container, sourceDirectoryChanges, filteredItems); + WriteGroup(_Configuration.PropertyConfiguration, propertyLogic, propertyCollection, metadataCollection, resizeKeyValuePairs, imageFaceCollections, outputResolution, container, filteredItems); + if (false && exceptionCount == 0 && _Configuration.LoadOrCreateThenSaveDistanceResultsForOutputResolutions.Contains(outputResolution)) + _Distance.LoadOrCreateThenSaveDistanceResults(_Configuration.PropertyConfiguration, eResultsFullGroupDirectory, outputResolution, container, sourceDirectoryChanges, filteredItems); if (Directory.GetFiles(propertyRoot, "*.txt", SearchOption.TopDirectoryOnly).Any()) { for (int y = 0; y < int.MaxValue; y++) @@ -644,6 +786,19 @@ public class DlibDotNet // if (isSilent && container.SourceDirectory.EndsWith("Texas 2015")) //46#.4 // break; } + if (_Configuration.LoadOrCreateThenSaveDistanceResultsForOutputResolutions.Contains(outputResolution)) + { + needToSave = faceEncodingRequired.Any(); + if (needToSave) + SetFaceTags(argZero, faceEncodingRequired, containers); + if (faceEncodingRequired.Any()) + throw new Exception("Why!"); + if (needToSave) + { + (Sorting Sorting, FaceRecognitionDotNet.FaceEncoding FaceEncoding)[] collection = GetValues(argZero, containers); + SaveFaceNumbers(argZero, propertyLogic, outputResolution, aResultsFullGroupDirectory, bResultsFullGroupDirectory, cResultsFullGroupDirectory, dResultsFullGroupDirectory, eResultsFullGroupDirectory, containers, collection); + } + } } } @@ -677,36 +832,28 @@ public class DlibDotNet return results; } - private void MapLogic(string argZero, Container[] containers, long ticks, string dResultsFullGroupDirectory, string d2ResultsFullGroupDirectory, MapLogic mapLogic, string outputResolution) + private void MapLogic(long ticks, string dResultsFullGroupDirectory, string d2ResultsFullGroupDirectory, MapLogic mapLogic, string outputResolution, Item[] items, int totalNotMapped) { - int totalNotMapped = 0; - mapLogic.UseKeyValuePairsSaveFaceEncoding(containers); - foreach (Container container in containers) - { - totalNotMapped += mapLogic.AddToMapping(container.Items); - if (_Configuration.SaveShortcutsForOutputResolutions.Contains(outputResolution)) - mapLogic.SaveShortcuts(_Configuration.JuliePhares, container.Items); - } - mapLogic.SaveAllCollection(); + mapLogic.UseKeyValuePairsSaveFaceEncoding(items); if (_Configuration.SaveResizedSubfiles) { string dFacesContentDirectory; dFacesContentDirectory = Path.Combine(dResultsFullGroupDirectory, "()"); mapLogic.SaveNotMappedTicks(); _ = LogDeltaInMinutes(ticks, nameof(mapLogic.SaveNotMappedTicks)); - Dictionary> keyValuePairs = _Distance.ParallelWork(_AppSettings.MaxDegreeOfParallelism, _Configuration.IgnoreRelativePaths, argZero, ticks, mapLogic, containers, totalNotMapped); + Dictionary> keyValuePairs = _Distance.ParallelWork(_AppSettings.MaxDegreeOfParallelism, ticks, mapLogic, items, totalNotMapped); _ = LogDeltaInSeconds(ticks, nameof(E_Distance.ParallelWork)); Dictionary> strippedKeyValuePairs = Strip(keyValuePairs); List<(string, int, Mapping, DateTime, bool?, List<(FaceRecognitionDotNet.FaceEncoding, MappingContainer)>)> collection = Convert(keyValuePairs); - mapLogic.SaveMapping(argZero, containers, dFacesContentDirectory, d2ResultsFullGroupDirectory); + mapLogic.SaveMapping(items, dFacesContentDirectory, d2ResultsFullGroupDirectory); _ = LogDeltaInMinutes(ticks, nameof(mapLogic.SaveMapping)); - _Distance.AddToFaceDistance(_AppSettings.MaxDegreeOfParallelism, argZero, ticks, mapLogic, containers, outputResolution, collection); + E_Distance.AddToFaceDistance(_AppSettings.MaxDegreeOfParallelism, mapLogic, items, collection); _ = LogDeltaInSeconds(ticks, nameof(_Distance.AddToFaceDistance)); - mapLogic.AddToClosest(_AppSettings.MaxDegreeOfParallelism, argZero, containers); + mapLogic.AddToClosest(_AppSettings.MaxDegreeOfParallelism, items); _ = LogDeltaInMinutes(ticks, nameof(mapLogic.AddToClosest)); - mapLogic.SaveClosest(argZero, containers, dFacesContentDirectory, d2ResultsFullGroupDirectory); + mapLogic.SaveClosest(items, dFacesContentDirectory, d2ResultsFullGroupDirectory); _ = LogDeltaInMinutes(ticks, nameof(mapLogic.SaveClosest)); - mapLogic.SavePropertyHolders(argZero, containers); + mapLogic.SavePropertyHolders(items); _ = LogDeltaInSeconds(ticks, nameof(mapLogic.SavePropertyHolders)); } _ = LogDeltaInSeconds(ticks, nameof(MapLogic)); @@ -739,7 +886,7 @@ public class DlibDotNet containers = A_Property.Get(configuration, propertyLogic); else containers = Property.Models.Stateless.Container.GetContainers(configuration, _FirstRun, propertyLogic); - FullDoWork(argZero, configuration, model, predictorModel, propertyRoot, ticks, mapLogic, propertyLogic, containers); + FullDoWork(argZero, model, predictorModel, propertyRoot, ticks, mapLogic, propertyLogic, containers); foreach (string outputResolution in _Configuration.OutputResolutions) { if (_FirstRun) @@ -747,7 +894,18 @@ public class DlibDotNet (aResultsFullGroupDirectory, bResultsFullGroupDirectory, cResultsFullGroupDirectory, dResultsFullGroupDirectory, d2ResultsFullGroupDirectory, eResultsFullGroupDirectory) = GetResultsFullGroupDirectories(configuration, model, predictorModel, outputResolution); if (_ArgZeroIsConfigurationRootDirectory && _Exceptions.Count == 0 && outputResolution == _Configuration.OutputResolutions[0]) { - MapLogic(argZero, containers, ticks, dResultsFullGroupDirectory, d2ResultsFullGroupDirectory, mapLogic, outputResolution); + int totalNotMapped = 0; + DateTime dateTime = new(ticks); + List<(int Id, Item Item)> idAndItemCollection = new(); + foreach (Container container in containers) + { + totalNotMapped += mapLogic.AddToMapping(ticks, container.Items); + Map.Models.MapLogic.AppendToItems(container.Items, idAndItemCollection); + if (_Configuration.SaveShortcutsForOutputResolutions.Contains(outputResolution)) + mapLogic.SaveShortcuts(_Configuration.JuliePhares, container.Items); + } + Item[] items = (from l in idAndItemCollection orderby l.Id select l.Item).ToArray(); + MapLogic(ticks, dResultsFullGroupDirectory, d2ResultsFullGroupDirectory, mapLogic, outputResolution, items, totalNotMapped); if (!_Configuration.LoadOrCreateThenSaveImageFacesResultsForOutputResolutions.Any()) break; if (_FileKeyValuePairs.Any()) diff --git a/Instance/Models/_D2_FaceParts.cs b/Instance/Models/_D2_FaceParts.cs index cb935dd..54c446d 100644 --- a/Instance/Models/_D2_FaceParts.cs +++ b/Instance/Models/_D2_FaceParts.cs @@ -118,7 +118,7 @@ internal class D2_FaceParts #pragma warning restore CA1416 - internal void SaveFaceLandmarkImages(string d2ResultsFullGroupDirectory, string sourceDirectory, List> subFileTuples, List parseExceptions, Item item, List faceCollection, bool saveRotated) + internal void SaveFaceLandmarkImages(string d2ResultsFullGroupDirectory, Container container, List> subFileTuples, List parseExceptions, Item item, List faceCollection, bool saveRotated) { if (item.ImageFileHolder is null) throw new NullReferenceException(nameof(item.ImageFileHolder)); @@ -137,7 +137,7 @@ internal class D2_FaceParts List<(Face, string, string)> collection = new(); angleBracketCollection.AddRange(Property.Models.Stateless.IResult.GetDirectoryInfoCollection( _Configuration.PropertyConfiguration, - sourceDirectory, + container.SourceDirectory, d2ResultsFullGroupDirectory, contentDescription: "n x 2 gif file(s) for each face found", singletonDescription: string.Empty, diff --git a/Instance/Models/_E_Distance.cs b/Instance/Models/_E_Distance.cs index 1106cb6..0b9299c 100644 --- a/Instance/Models/_E_Distance.cs +++ b/Instance/Models/_E_Distance.cs @@ -11,6 +11,8 @@ namespace View_by_Distance.Instance.Models; internal class E_Distance { + internal List AngleBracketCollection { get; } + private readonly Serilog.ILogger? _Log; private readonly Configuration _Configuration; private readonly JsonSerializerOptions _WriteIndentedJsonSerializerOptions; @@ -18,6 +20,7 @@ internal class E_Distance internal E_Distance(Configuration configuration) { _Configuration = configuration; + AngleBracketCollection = new List(); _Log = Serilog.Log.ForContext(); _WriteIndentedJsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true }; } @@ -411,6 +414,76 @@ internal class E_Distance } } + internal void SaveFaceNumbers(string eResultsFullGroupDirectory, Item item, Face face, List faceDistances) + { + if (item.Property?.Id is null) + throw new NullReferenceException(nameof(item.Property.Id)); + if (item.ImageFileHolder is null) + throw new NullReferenceException(nameof(item.ImageFileHolder)); + if (face.Location?.NormalizedPixelPercentage is null) + throw new NullReferenceException(nameof(face.Location.NormalizedPixelPercentage)); + if (string.IsNullOrEmpty(eResultsFullGroupDirectory)) + throw new NullReferenceException(nameof(eResultsFullGroupDirectory)); + string json; + List<(Face, string, string)> collection = new(); + string[] changesFrom = new string[] { nameof(A_Property), nameof(B_Metadata), nameof(C_Resize), nameof(D_Face) }; + string eSingletonFile = Path.Combine(eResultsFullGroupDirectory, "{}", Property.Models.Stateless.IResult.AllInOne, $"{item.Property.Id.Value}.{face.Location.NormalizedPixelPercentage.Value}{item.ImageFileHolder.ExtensionLowered}.json"); + FileInfo fileInfo = new(eSingletonFile); + json = JsonSerializer.Serialize(faceDistances, _WriteIndentedJsonSerializerOptions); + _ = Shared.Models.Stateless.Methods.IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null); + } + + internal void GetFaceDistances(string eResultsFullGroupDirectory, List faceEncodingRequired, List> subFileTuples, List parseExceptions, Item item, List faceCollection) + { + List? results; + if (item.Property?.Id is null) + throw new NullReferenceException(nameof(item.Property.Id)); + if (item.ImageFileHolder is null) + throw new NullReferenceException(nameof(item.ImageFileHolder)); + if (string.IsNullOrEmpty(eResultsFullGroupDirectory)) + throw new NullReferenceException(nameof(eResultsFullGroupDirectory)); + string json; + FileInfo fileInfo; + string eSingletonFile; + List<(Face, string, string)> collection = new(); + string[] changesFrom = new string[] { nameof(A_Property), nameof(B_Metadata), nameof(C_Resize), nameof(D_Face) }; + List dateTimes = (from l in subFileTuples where changesFrom.Contains(l.Item1) select l.Item2).ToList(); + foreach (Face face in faceCollection) + { + results = null; + if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) + continue; + eSingletonFile = Path.Combine(eResultsFullGroupDirectory, "{}", Property.Models.Stateless.IResult.AllInOne, $"{item.Property.Id.Value}.{face.Location.NormalizedPixelPercentage.Value}{item.ImageFileHolder.ExtensionLowered}.json"); + fileInfo = new(eSingletonFile); + if (_Configuration.PropertiesChangedForDistance) + results = null; + else if (!fileInfo.Exists) + results = null; + else if (dateTimes.Any() && dateTimes.Max() > fileInfo.LastWriteTime) + results = null; + else + { + json = File.ReadAllText(fileInfo.FullName); + try + { + results = JsonSerializer.Deserialize>(json); + if (results is null) + throw new NullReferenceException(nameof(results)); + subFileTuples.Add(new Tuple(nameof(E_Distance), fileInfo.LastWriteTime)); + lock (face) + face.SetFaceNumbers(results); + } + catch (Exception) + { + results = null; + parseExceptions.Add(nameof(E_Distance)); + } + } + if (results is null && !faceEncodingRequired.Any()) + faceEncodingRequired.Add(true); + } + } + public static double GetStandardDeviation(IEnumerable values, double average) { double result = 0; @@ -661,55 +734,40 @@ internal class E_Distance return results; } - internal Dictionary> ParallelWork(int maxDegreeOfParallelism, string[] ignoreRelativePaths, string argZero, long ticks, Map.Models.MapLogic mapLogic, Container[] containers, int totalNotMapped) + internal Dictionary> ParallelWork(int maxDegreeOfParallelism, long ticks, Map.Models.MapLogic mapLogic, Item[] items, int totalNotMapped) { Dictionary> results; Random random = new((int)ticks); - mapLogic.ForceSingleImage(ignoreRelativePaths, argZero, containers, totalNotMapped, random); - Dictionary> keyValuePairs = Map.Models.Stateless.IMapLogic.GetKeyValuePairs(ignoreRelativePaths, argZero, containers); + mapLogic.ForceSingleImage(ticks, items, totalNotMapped, random); + Dictionary> keyValuePairs = Map.Models.Stateless.IMapLogic.GetKeyValuePairs(items); results = GetThreeSigmaFaceEncodings(maxDegreeOfParallelism, ticks, random, keyValuePairs); return results; } - public void AddToFaceDistance(int maxDegreeOfParallelism, string argZero, long ticks, Map.Models.MapLogic mapLogic, Container[] containers, string outputResolution, List<(string, int, Mapping, DateTime, bool?, List<(FaceRecognitionDotNet.FaceEncoding, MappingContainer)>)> collection) + public static void AddToFaceDistance(int maxDegreeOfParallelism, Map.Models.MapLogic mapLogic, Item[] items, List<(string, int, Mapping, DateTime, bool?, List<(FaceRecognitionDotNet.FaceEncoding, MappingContainer)>)> collection) { - if (_Log is null) - throw new NullReferenceException(nameof(_Log)); Face face; - string message; - int totalSeconds; double deterministicHashCodeKey; DateTime dateTime = DateTime.Now; List faceDistances; - int containersCount = containers.Length; - foreach (Container container in containers) + foreach (Item item in items) { - if (!container.Items.Any()) + if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) continue; - if (!container.SourceDirectory.StartsWith(argZero)) - continue; - foreach (Item item in container.Items) + for (int i = 0; i < item.Faces.Count; i++) { - if (item.ImageFileHolder is null || item.Property?.Id is null) + face = item.Faces[i]; + face.FaceDistances.Clear(); + if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) continue; - for (int i = 0; i < item.Faces.Count; i++) - { - face = item.Faces[i]; - face.FaceDistances.Clear(); - if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) - continue; - if ((from l in item.Mapping where l.NormalizedPixelPercentage.HasValue && l.NormalizedPixelPercentage.Value == face.Location.NormalizedPixelPercentage.Value select true).Any()) - continue; - deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face); - if (mapLogic.Skip(deterministicHashCodeKey)) - continue; - faceDistances = GetFaceDistanceCollection(maxDegreeOfParallelism, collection, face); - face.FaceDistances.AddRange(faceDistances); - } + if ((from l in item.Mapping where l.NormalizedPixelPercentage.HasValue && l.NormalizedPixelPercentage.Value == face.Location.NormalizedPixelPercentage.Value select true).Any()) + continue; + deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face); + if (mapLogic.Skip(deterministicHashCodeKey)) + continue; + faceDistances = GetFaceDistanceCollection(maxDegreeOfParallelism, collection, face); + face.FaceDistances.AddRange(faceDistances); } - totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds); - message = $"{container.R:000}.{container.G} / {containersCount:000}) {container.Items.Count:000} file(s) - {totalSeconds} total second(s) - {outputResolution} - {container.SourceDirectory}"; - _Log.Information(message); } } diff --git a/Map/Models/MapLogic.cs b/Map/Models/MapLogic.cs index 1955736..26341c0 100644 --- a/Map/Models/MapLogic.cs +++ b/Map/Models/MapLogic.cs @@ -1,4 +1,3 @@ -using System.Diagnostics; using System.Text.Json; using View_by_Distance.Property.Models; using View_by_Distance.Shared.Models; @@ -10,9 +9,8 @@ namespace View_by_Distance.Map.Models; public class MapLogic { - protected readonly List _NotMappedTicks; protected readonly List _SkipCollection; - protected readonly List<(int, string[])> _AllCollection; + protected readonly List _NotMappedPersonKeys; protected readonly Dictionary _KeyValuePairs; protected readonly Dictionary _IndicesFromNew; protected readonly Dictionary _SixCharacterNamedFaceInfo; @@ -38,7 +36,6 @@ public class MapLogic public MapLogic(int maxDegreeOfParallelism, Configuration configuration, string resizeFilenameExtension, string facesFilenameExtension, string facesHiddenFilenameExtension, string facePartsFilenameExtension, long ticks, Person[] people, string peopleDateGroupDirectory, string zResultsFullGroupDirectory) { _Ticks = ticks; - _AllCollection = new(); _Configuration = configuration; _Log = Serilog.Log.ForContext(); _FacesFilenameExtension = facesFilenameExtension; @@ -130,7 +127,7 @@ public class MapLogic _KeyValuePairs = keyValuePairs; _IndicesFromNew = indicesFromNew; _SkipCollection = skipCollection; - _NotMappedTicks = notMappedTicks; + _NotMappedPersonKeys = notMappedTicks; _PeopleKeyValuePairs = peopleKeyValuePairs; _SixCharacterNamedFaceInfo = sixCharacterNamedFaceInfo; _ZPropertyHolderContentDirectory = zPropertyHolderContentDirectory; @@ -175,8 +172,8 @@ public class MapLogic { string fileName; string fullName; - string personKey; DateTime minimumDateTime; + string personKeyFormatted; PersonBirthday personBirthday; WindowsShortcut windowsShortcut; (string DisplayDirectoryName, int? ApproximateYears, PersonBirthday[] _, long Ticks) person; @@ -205,8 +202,8 @@ public class MapLogic if (ticks is null) continue; personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(ticks.Value); - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); - if (juliePhares.Contains(personKey) && !string.IsNullOrEmpty(copyDirectory)) + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); + if (juliePhares.Contains(personKeyFormatted) && !string.IsNullOrEmpty(copyDirectory)) { if (!Directory.Exists(copyDirectory)) _ = Directory.CreateDirectory(copyDirectory); @@ -227,28 +224,26 @@ public class MapLogic } } - public void UseKeyValuePairsSaveFaceEncoding(Container[] containers) + public void UseKeyValuePairsSaveFaceEncoding(Item[] items) { Dictionary> keyValuePairs = new(); List<(PersonBirthday PersonBirthday, double IdAndNormalizedPixelPercentage)> deterministicHashCodeCollection = new(); List<(PersonBirthday PersonBirthday, double IdAndNormalizedPixelPercentage)> incorrectDeterministicHashCodeCollection = new(); - foreach (Container container in containers) + foreach (Item item in items) { - foreach (Item item in container.Items) + if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) + continue; + if (!item.Faces.Any()) + continue; + if (keyValuePairs.ContainsKey(item.Property.Id.Value)) { - if (item.ImageFileHolder is null || item.Property?.Id is null || !item.Faces.Any()) - continue; - if (keyValuePairs.ContainsKey(item.Property.Id.Value)) - { - if (keyValuePairs[item.Property.Id.Value].Count != item.Faces.Count) - throw new Exception(); - continue; - } - keyValuePairs.Add(item.Property.Id.Value, item.Faces); + if (keyValuePairs[item.Property.Id.Value].Count != item.Faces.Count) + throw new Exception(); + continue; } + keyValuePairs.Add(item.Property.Id.Value, item.Faces); } Stateless.ByDeterministicHashCode.SetKeyValuePairs(_ZPropertyHolderContentDirectory, deterministicHashCodeCollection, incorrectDeterministicHashCodeCollection, keyValuePairs); - } public override string ToString() @@ -268,64 +263,27 @@ public class MapLogic return result; } - public void AddToMapLogicAllCollection(Item[] filteredItems) + public static void AppendToItems(List items, List<(int, Item)> idAndItemCollection) { - if (_SixCharacterNamedFaceInfo.Any()) + foreach (Item item in items) { - string[] keys; - Item item; - for (int i = 0; i < filteredItems.Length; i++) - { - item = filteredItems[i]; - if (item.Property?.Id is null) - continue; - foreach (int sixCharacterIndex in item.Property.Indices) - { - if (!_SixCharacterNamedFaceInfo.ContainsKey(sixCharacterIndex)) - continue; - keys = _SixCharacterNamedFaceInfo[sixCharacterIndex]; - _AllCollection.Add(new(item.Property.Id.Value, keys)); - } - } + if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) + continue; + idAndItemCollection.Add(new(item.Property.Id.Value, item)); } } - public void SaveAllCollection() - { - if (_AllCollection.Any()) - { - string[] keys; - long ticks = DateTime.Now.Ticks; - string? rootDirectoryParent = Path.GetDirectoryName(_Configuration.RootDirectory); - if (string.IsNullOrEmpty(rootDirectoryParent)) - throw new NullReferenceException(nameof(rootDirectoryParent)); - Dictionary namedFaceInfoDeterministicHashCodeIndices = new(); - List<(int, string[])> allCollection = _AllCollection.OrderBy(l => l.Item1).ToList(); - foreach ((int deterministicHashCode, string[] values) in allCollection) - { - if (namedFaceInfoDeterministicHashCodeIndices.ContainsKey(deterministicHashCode)) - { - keys = namedFaceInfoDeterministicHashCodeIndices[deterministicHashCode]; - if (JsonSerializer.Serialize(values) == JsonSerializer.Serialize(keys)) - continue; - throw new Exception(); - } - namedFaceInfoDeterministicHashCodeIndices.Add(deterministicHashCode, values); - } - string json = JsonSerializer.Serialize(namedFaceInfoDeterministicHashCodeIndices, new JsonSerializerOptions { WriteIndented = true }); - string checkFile = Path.Combine(rootDirectoryParent, "NamedFaceInfoDeterministicHashCodeIndices.json"); - _ = Shared.Models.Stateless.Methods.IPath.WriteAllText(checkFile, json, updateDateWhenMatches: true, compareBeforeWrite: true); - _ = LogDelta(ticks, new StackFrame().GetMethod()?.Name); - } - } - - public int AddToMapping(List items) + public int AddToMapping(long ticks, List items) { int result = 0; - long ticks; + long personKey; Mapping mapping; + bool? isWrongYear; bool forced = false; int? approximateYears; + string mappingSegmentB; + DateTime minimumDateTime; + string personKeyFormatted; string displayDirectoryName; PersonBirthday personBirthday; double deterministicHashCodeKey; @@ -333,9 +291,7 @@ public class MapLogic (string DisplayDirectoryName, int? ApproximateYears, PersonBirthday[] PersonBirthdays, long Ticks) person; foreach (Item item in items) { - if (item.ImageFileHolder is null) - continue; - if (item.Property?.Id is null || item.ResizedFileHolder is null) + if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) continue; foreach (Face face in item.Faces) { @@ -351,14 +307,18 @@ public class MapLogic personBirthdays.AddRange(_DeterministicHashCodeKeyValuePairs[deterministicHashCodeKey]); for (int i = 0; i < personBirthdays.Count; i++) { - ticks = personBirthdays[i].Value.Ticks; - if (!_PeopleKeyValuePairs.ContainsKey(ticks)) + personKey = personBirthdays[i].Value.Ticks; + if (!_PeopleKeyValuePairs.ContainsKey(personKey)) continue; - person = _PeopleKeyValuePairs[ticks]; + person = _PeopleKeyValuePairs[personKey]; personBirthday = person.PersonBirthdays[0]; approximateYears = person.ApproximateYears; displayDirectoryName = person.DisplayDirectoryName; - mapping = new(approximateYears, displayDirectoryName, forced, face.Location.NormalizedPixelPercentage, personBirthday); + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); + minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property); + (isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime); + mappingSegmentB = Stateless.MapLogic.GetMappingSegmentB(ticks, personBirthday, approximateYears, minimumDateTime, isWrongYear); + mapping = new(approximateYears, displayDirectoryName, forced, face.Location.NormalizedPixelPercentage, personBirthday, mappingSegmentB); item.Mapping.Add(mapping); } } @@ -369,14 +329,18 @@ public class MapLogic personBirthdays.AddRange(_DeterministicHashCodeUnknownFaceKeyValuePairs[item.Property.Id.Value]); for (int i = 0; i < personBirthdays.Count; i++) { - ticks = personBirthdays[i].Value.Ticks; - if (!_PeopleKeyValuePairs.ContainsKey(ticks)) + personKey = personBirthdays[i].Value.Ticks; + if (!_PeopleKeyValuePairs.ContainsKey(personKey)) continue; - person = _PeopleKeyValuePairs[ticks]; + person = _PeopleKeyValuePairs[personKey]; personBirthday = person.PersonBirthdays[0]; approximateYears = person.ApproximateYears; displayDirectoryName = person.DisplayDirectoryName; - mapping = new(approximateYears, displayDirectoryName, forced, personBirthday); + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); + minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property); + (isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime); + mappingSegmentB = Stateless.MapLogic.GetMappingSegmentB(ticks, personBirthday, approximateYears, minimumDateTime, isWrongYear); + mapping = new(approximateYears, displayDirectoryName, forced, personBirthday, mappingSegmentB); item.Mapping.Add(mapping); } } @@ -438,16 +402,16 @@ public class MapLogic public void SaveNotMappedTicks() { string directory; - string personKey; + string personKeyFormatted; SaveContainer saveContainer; PersonBirthday personBirthday; List saveContainers = new(); const string facePopulatedKey = nameof(Closest); - foreach (long ticks in _NotMappedTicks) + foreach (long personKey in _NotMappedPersonKeys) { - personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(ticks); - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); - directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"{facePopulatedKey}NotMapped", personKey, Property.Models.Stateless.IResult.AllInOne); + personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKey); + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); + directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"{facePopulatedKey}NotMapped", personKeyFormatted, Property.Models.Stateless.IResult.AllInOne); saveContainer = new(directory); saveContainers.Add(saveContainer); } @@ -460,9 +424,8 @@ public class MapLogic int years; Face face; Item item; - long? ticks; + long? personKey; string directory; - string personKey; bool? isWrongYear; const int zero = 0; TimeSpan? timeSpan; @@ -475,6 +438,7 @@ public class MapLogic DateTime minimumDateTime; List indices = new(); List faceCollection; + string personKeyFormatted; PersonBirthday personBirthday; PersonBirthday[] personBirthdays; DateTime dateTime = DateTime.Now; @@ -490,13 +454,13 @@ public class MapLogic relativePath = Path.GetDirectoryName($"C:{item.RelativePath}"); if (string.IsNullOrEmpty(relativePath) || relativePath.Length < 3) continue; - if (item.Property?.Id is null || item.ResizedFileHolder is null) + if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) continue; collection = new(); if (!_DeterministicHashCodeUnknownFaceKeyValuePairs.ContainsKey(item.Property.Id.Value)) { faceCollection = new(); - ticks = null; + personKey = null; directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"Unnamed{relativePath[2..]}"); } else @@ -509,24 +473,24 @@ public class MapLogic subDirectoryName = $"{isWrongYearFlag}{minimumDateTime:yyyy}"; if (!faceCollection.Any()) { - ticks = null; + personKey = null; directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"None{relativePath[2..]}", subDirectoryName); } else if (personBirthdays.Length != 1) { - ticks = null; + personKey = null; directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"Not Supported{relativePath[2..]}", subDirectoryName); } else if (faceCollection.Count != 1) { - ticks = null; + personKey = null; directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"Many{relativePath[2..]}", subDirectoryName); } else { indices.Add(zero); personBirthday = personBirthdays[zero]; - ticks = personBirthday.Value.Ticks; + personKey = personBirthday.Value.Ticks; timeSpan = Shared.Models.Stateless.Methods.IPersonBirthday.GetTimeSpan(minimumDateTime, isWrongYear, personBirthday); if (timeSpan.HasValue) { @@ -539,128 +503,170 @@ public class MapLogic } } face = faceCollection[zero]; - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); - directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, "Shortcuts", personKey, subDirectoryName); + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); + directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, "Shortcuts", personKeyFormatted, subDirectoryName); if (face.FaceEncoding is not null && face.Location?.NormalizedPixelPercentage is not null) - copyDirectory = Path.Combine(_ZPropertyHolderContentTicksDirectory, "Images", personKey, subDirectoryName); + copyDirectory = Path.Combine(_ZPropertyHolderContentTicksDirectory, "Images", personKeyFormatted, subDirectoryName); else - copyDirectory = Path.Combine(_ZPropertyHolderContentTicksDirectory, "ImagesBut", personKey, subDirectoryName); + copyDirectory = Path.Combine(_ZPropertyHolderContentTicksDirectory, "ImagesBut", personKeyFormatted, subDirectoryName); copyFileName = Path.Combine(copyDirectory, $"{item.Property.Id.Value}{item.ResizedFileHolder.ExtensionLowered}"); } } shortcutFileName = Path.Combine(directory, $"{item.Property.Id.Value}.lnk"); - if (ticks is null || !indices.Any()) - collection.Add(new(ticks, null, (directory, copyDirectory, copyFileName, shortcutFileName))); + if (personKey is null || !indices.Any()) + collection.Add(new(personKey, null, (directory, copyDirectory, copyFileName, shortcutFileName))); else { foreach (int index in indices) - collection.Add(new(ticks, faceCollection[index], (directory, copyDirectory, copyFileName, shortcutFileName))); + collection.Add(new(personKey, faceCollection[index], (directory, copyDirectory, copyFileName, shortcutFileName))); } results.Add(new(item, collection.ToArray())); } return results; } - public void AddToClosest(int maxDegreeOfParallelism, string argZero, Container[] containers) + public void AddToClosest(int maxDegreeOfParallelism, Item[] items) { string key; - string dateKey; Closest closest; - string personKey; DateTime minimumDateTime; + string personKeyFormatted; Closest[] closestCollection; double deterministicHashCodeKey; - DateTime dateTime = DateTime.Now; Dictionary keyValuePairs = new(); - foreach (Container container in containers) + foreach (Item item in items) { - if (!container.Items.Any()) + if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) continue; - if (!container.SourceDirectory.StartsWith(argZero)) - continue; - foreach (Item item in container.Items) + foreach (Face face in item.Faces) { - if (item.ImageFileHolder is null || item.Property is null) + if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) continue; - foreach (Face face in item.Faces) + deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face); + if (_DeterministicHashCodeKeyValuePairs.ContainsKey(deterministicHashCodeKey)) + continue; + minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property); + closestCollection = Shared.Models.Stateless.Methods.IClosest.GetCollection(face, minimumDateTime, face.FaceDistances); + face.FaceDistances.Clear(); + for (int j = 0; j < closestCollection.Length; j++) { - if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) + closest = closestCollection[j]; + if (_IncorrectDeterministicHashCodeKeyValuePairs.ContainsKey(deterministicHashCodeKey) && _IncorrectDeterministicHashCodeKeyValuePairs[deterministicHashCodeKey].Contains(closest.Mapping.PersonBirthday)) continue; - deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face); - if (_DeterministicHashCodeKeyValuePairs.ContainsKey(deterministicHashCodeKey)) + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(closest.Mapping.PersonBirthday); + key = string.Concat(personKeyFormatted, closest.Mapping.SegmentB); + if (!keyValuePairs.ContainsKey(key)) + keyValuePairs.Add(key, 0); + else if (keyValuePairs[key] > IClosest.MaximumPer) continue; - minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property); - closestCollection = Shared.Models.Stateless.Methods.IClosest.GetCollection(face, minimumDateTime, face.FaceDistances); - face.FaceDistances.Clear(); - for (int j = 0; j < closestCollection.Length; j++) - { - closest = closestCollection[j]; - if (_IncorrectDeterministicHashCodeKeyValuePairs.ContainsKey(deterministicHashCodeKey) && _IncorrectDeterministicHashCodeKeyValuePairs[deterministicHashCodeKey].Contains(closest.Mapping.PersonBirthday)) - continue; - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(closest.Mapping.PersonBirthday); - dateKey = Stateless.MapLogic.GetDateKey(dateTime, closest.Mapping, closest.MinimumDateTime, closest.IsWrongYear); - key = string.Concat(personKey, dateKey); - if (!keyValuePairs.ContainsKey(key)) - keyValuePairs.Add(key, 0); - else if (keyValuePairs[key] > IClosest.MaximumPer) - continue; - keyValuePairs[key] += 1; - item.Closest.Add(closest); - break; - } + keyValuePairs[key] += 1; + item.Closest.Add(closest); + break; } } } } - private List GetMappingSaveContainers(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory) + private List GetMappingSaveContainers(Item[] items, string dFacesContentDirectory, string d2ResultsFullGroupDirectory) { List results = new(); - string key; string json; - string dateKey; Mapping? match; - string personKey; string checkFile; string directory; - bool? isWrongYear; string shortcutFile; string? directoryName; string facesDirectory; string personDirectory; List used = new(); - DateTime minimumDateTime; + string personKeyFormatted; FileHolder faceFileHolder; string facePartsDirectory; SaveContainer saveContainer; FileHolder facePartsFileHolder; FileHolder hiddenFaceFileHolder; double deterministicHashCodeKey; - DateTime dateTime = DateTime.Now; const string facePopulatedKey = nameof(Mapping); bool deterministicHashCodeKeyValuePairsAny = _DeterministicHashCodeKeyValuePairs.Any(); - foreach (Container container in containers) + foreach (Item item in items) { - if (!container.Items.Any()) + used.Clear(); + if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) continue; - if (!container.SourceDirectory.StartsWith(argZero)) - continue; - foreach (Item item in container.Items) + directoryName = Path.GetDirectoryName(item.RelativePath); + if (directoryName is null) + throw new Exception(); + foreach (Face face in item.Faces) { - used.Clear(); - if (item.ImageFileHolder is null || item.Property?.Id is null || item.ResizedFileHolder is null) + match = null; + if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) continue; - directoryName = Path.GetDirectoryName(item.RelativePath); - if (directoryName is null) + foreach (Mapping mapping in item.Mapping) + { + if (mapping.NormalizedPixelPercentage is null || mapping.NormalizedPixelPercentage.Value != face.Location.NormalizedPixelPercentage.Value) + continue; + match = mapping; + break; + } + if (match is null) + continue; + foreach (Closest closest in item.Closest) + { + if (closest.Mapping.Forced) + continue; + if (closest.NormalizedPixelPercentage != face.Location.NormalizedPixelPercentage.Value) + continue; throw new Exception(); + } + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(match.PersonBirthday); + if (match.Filtered is null) + directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"{facePopulatedKey}Null", personKeyFormatted, match.SegmentB); + else if (match.Filtered.Value) + directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"{facePopulatedKey}OutOfControl", personKeyFormatted, match.SegmentB); + else + continue; + personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk"); + saveContainer = new(personDirectory); + results.Add(saveContainer); + facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension)); + facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension)); + deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face); + checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}"); + faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}")); + hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}")); + facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}")); + if (string.IsNullOrEmpty(personDirectory)) + shortcutFile = string.Empty; + else + shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk"); + saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile); + results.Add(saveContainer); + if (!string.IsNullOrEmpty(checkFile) && IMapping.SaveFaceEncoding) + { + checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.json"); + json = JsonSerializer.Serialize(face.FaceEncoding); + saveContainer = new(checkFile, directory, json); + results.Add(saveContainer); + } + personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk", match.DisplayDirectoryName); + saveContainer = new(personDirectory); + results.Add(saveContainer); + used.Add(face.Location.NormalizedPixelPercentage.Value); + } + if (deterministicHashCodeKeyValuePairsAny && IMapping.UseDeterministicHashCodeUnknownFaceKeyValuePairsForSaveMapping) + { foreach (Face face in item.Faces) { match = null; if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) continue; + if (used.Contains(face.Location.NormalizedPixelPercentage.Value)) + continue; + // if (item.Faces.Count != 1 || item.Mapping.Count != 1) + // continue; foreach (Mapping mapping in item.Mapping) { - if (mapping.NormalizedPixelPercentage is null || mapping.NormalizedPixelPercentage.Value != face.Location.NormalizedPixelPercentage.Value) + if (mapping.NormalizedPixelPercentage is not null) continue; match = mapping; break; @@ -675,15 +681,11 @@ public class MapLogic continue; throw new Exception(); } - minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property); - (isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime); - dateKey = Stateless.MapLogic.GetDateKey(dateTime, match, minimumDateTime, isWrongYear); - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(match.PersonBirthday); - key = string.Concat(personKey, dateKey); + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(match.PersonBirthday); if (match.Filtered is null) - directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"{facePopulatedKey}Null", personKey, dateKey); - else if (match.Filtered.Value) - directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"{facePopulatedKey}OutOfControl", personKey, dateKey); + directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"{facePopulatedKey}WithButNull", personKeyFormatted, match.SegmentB); + else if (!match.Filtered.Value) + directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"{facePopulatedKey}WithButOutOfControl", personKeyFormatted, match.SegmentB); else continue; personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk"); @@ -714,92 +716,24 @@ public class MapLogic results.Add(saveContainer); used.Add(face.Location.NormalizedPixelPercentage.Value); } - if (deterministicHashCodeKeyValuePairsAny && IMapping.UseDeterministicHashCodeUnknownFaceKeyValuePairsForSaveMapping) - { - foreach (Face face in item.Faces) - { - match = null; - if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) - continue; - if (used.Contains(face.Location.NormalizedPixelPercentage.Value)) - continue; - // if (item.Faces.Count != 1 || item.Mapping.Count != 1) - // continue; - foreach (Mapping mapping in item.Mapping) - { - if (mapping.NormalizedPixelPercentage is not null) - continue; - match = mapping; - break; - } - if (match is null) - continue; - foreach (Closest closest in item.Closest) - { - if (closest.Mapping.Forced) - continue; - if (closest.NormalizedPixelPercentage != face.Location.NormalizedPixelPercentage.Value) - continue; - throw new Exception(); - } - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(match.PersonBirthday); - minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property); - (isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime); - dateKey = Stateless.MapLogic.GetDateKey(dateTime, match, minimumDateTime, isWrongYear); - if (match.Filtered is null) - directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"{facePopulatedKey}WithButNull", personKey, dateKey); - else if (!match.Filtered.Value) - directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"{facePopulatedKey}WithButOutOfControl", personKey, dateKey); - else - continue; - personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk"); - saveContainer = new(personDirectory); - results.Add(saveContainer); - facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension)); - facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension)); - deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face); - checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}"); - faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}")); - hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}")); - facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}")); - if (string.IsNullOrEmpty(personDirectory)) - shortcutFile = string.Empty; - else - shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk"); - saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile); - results.Add(saveContainer); - if (!string.IsNullOrEmpty(checkFile) && IMapping.SaveFaceEncoding) - { - checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.json"); - json = JsonSerializer.Serialize(face.FaceEncoding); - saveContainer = new(checkFile, directory, json); - results.Add(saveContainer); - } - personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk", match.DisplayDirectoryName); - saveContainer = new(personDirectory); - results.Add(saveContainer); - used.Add(face.Location.NormalizedPixelPercentage.Value); - } - } } } return results; } - private List GetClosestSaveContainers(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory) + private List GetClosestSaveContainers(Item[] items, string dFacesContentDirectory, string d2ResultsFullGroupDirectory) { List results = new(); Closest? match; - string dateKey; string checkFile; string directory; - string personKey; string shortcutFile; string? directoryName; string facesDirectory; string personDirectory; List used = new(); FileHolder faceFileHolder; + string personKeyFormatted; string facePartsDirectory; SaveContainer saveContainer; PersonBirthday personBirthday; @@ -808,139 +742,132 @@ public class MapLogic double deterministicHashCodeKey; DateTime dateTime = DateTime.Now; const string facePopulatedKey = nameof(Closest); - foreach (Container container in containers) + foreach (Item item in items) { - if (!container.Items.Any()) + used.Clear(); + if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) continue; - if (!container.SourceDirectory.StartsWith(argZero)) + if (!item.Closest.Any()) continue; - foreach (Item item in container.Items) + directoryName = Path.GetDirectoryName(item.RelativePath); + if (directoryName is null) + throw new Exception(); + foreach (Face face in item.Faces) { - used.Clear(); - if (item.ImageFileHolder is null || item.Property?.Id is null || item.ResizedFileHolder is null) + match = null; + if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) continue; - if (!item.Closest.Any()) - continue; - directoryName = Path.GetDirectoryName(item.RelativePath); - if (directoryName is null) - throw new Exception(); - foreach (Face face in item.Faces) - { - match = null; - if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) - continue; - foreach (Closest closest in item.Closest) - { - if (closest.NormalizedPixelPercentage != face.Location.NormalizedPixelPercentage.Value) - continue; - match = closest; - break; - } - if (match is null) - continue; - foreach (Mapping mapping in item.Mapping) - { - if (mapping.Forced) - continue; - if (mapping.NormalizedPixelPercentage is null || mapping.NormalizedPixelPercentage.Value != face.Location.NormalizedPixelPercentage.Value) - continue; - throw new Exception(); - } - if (!match.AboveTolerance) - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(match.Mapping.PersonBirthday); - else - { - personKey = string.Empty; - foreach (long ticks in _NotMappedTicks) - { - if (ticks == match.Mapping.PersonBirthday.Value.Ticks) - continue; - personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(ticks); - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); - break; - } - if (string.IsNullOrEmpty(personKey)) - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(match.Mapping.PersonBirthday); - } - dateKey = Stateless.MapLogic.GetDateKey(dateTime, match.Mapping, match.MinimumDateTime, match.IsWrongYear); - directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, facePopulatedKey, personKey, dateKey); - personDirectory = Path.Combine(directory, match.Mapping.DisplayDirectoryName[..1], "lnk"); - saveContainer = new(personDirectory); - results.Add(saveContainer); - facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension)); - facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension)); - deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face); - checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}"); - faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}")); - hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}")); - facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}")); - if (string.IsNullOrEmpty(personDirectory)) - shortcutFile = string.Empty; - else - shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk"); - saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile); - results.Add(saveContainer); - personDirectory = Path.Combine(directory, match.Mapping.DisplayDirectoryName[..1], "lnk", match.Mapping.DisplayDirectoryName); - saveContainer = new(personDirectory); - results.Add(saveContainer); - used.Add(face.Location.NormalizedPixelPercentage.Value); - } foreach (Closest closest in item.Closest) { - if (used.Contains(closest.NormalizedPixelPercentage)) + if (closest.NormalizedPixelPercentage != face.Location.NormalizedPixelPercentage.Value) continue; - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(closest.Mapping.PersonBirthday); - dateKey = Stateless.MapLogic.GetDateKey(dateTime, closest.Mapping, closest.MinimumDateTime, closest.IsWrongYear); - directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, facePopulatedKey, personKey, dateKey); - personDirectory = Path.Combine(directory, closest.Mapping.DisplayDirectoryName, "lnk"); - saveContainer = new(personDirectory); - results.Add(saveContainer); - facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension)); - facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension)); - deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, closest); - checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}"); - faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}")); - hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}")); - facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}")); - if (string.IsNullOrEmpty(personDirectory)) - shortcutFile = string.Empty; - else - shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk"); - saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile); - results.Add(saveContainer); - personDirectory = Path.Combine(directory, closest.Mapping.DisplayDirectoryName[..1], "lnk", closest.Mapping.DisplayDirectoryName); - saveContainer = new(personDirectory); - results.Add(saveContainer); - used.Add(closest.NormalizedPixelPercentage); + match = closest; + break; } + if (match is null) + continue; + foreach (Mapping mapping in item.Mapping) + { + if (mapping.Forced) + continue; + if (mapping.NormalizedPixelPercentage is null || mapping.NormalizedPixelPercentage.Value != face.Location.NormalizedPixelPercentage.Value) + continue; + throw new Exception(); + } + if (!match.AboveTolerance) + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(match.Mapping.PersonBirthday); + else + { + personKeyFormatted = string.Empty; + foreach (long personKey in _NotMappedPersonKeys) + { + if (personKey == match.Mapping.PersonBirthday.Value.Ticks) + continue; + personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKey); + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); + break; + } + if (string.IsNullOrEmpty(personKeyFormatted)) + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(match.Mapping.PersonBirthday); + } + directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, facePopulatedKey, personKeyFormatted, match.Mapping.SegmentB); + personDirectory = Path.Combine(directory, match.Mapping.DisplayDirectoryName[..1], "lnk"); + saveContainer = new(personDirectory); + results.Add(saveContainer); + facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension)); + facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension)); + deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face); + checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}"); + faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}")); + hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}")); + facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}")); + if (string.IsNullOrEmpty(personDirectory)) + shortcutFile = string.Empty; + else + shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk"); + saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile); + results.Add(saveContainer); + personDirectory = Path.Combine(directory, match.Mapping.DisplayDirectoryName[..1], "lnk", match.Mapping.DisplayDirectoryName); + saveContainer = new(personDirectory); + results.Add(saveContainer); + used.Add(face.Location.NormalizedPixelPercentage.Value); + } + foreach (Closest closest in item.Closest) + { + if (used.Contains(closest.NormalizedPixelPercentage)) + continue; + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(closest.Mapping.PersonBirthday); + directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, facePopulatedKey, personKeyFormatted, closest.Mapping.SegmentB); + personDirectory = Path.Combine(directory, closest.Mapping.DisplayDirectoryName, "lnk"); + saveContainer = new(personDirectory); + results.Add(saveContainer); + facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension)); + facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension)); + deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, closest); + checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}"); + faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}")); + hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}")); + facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}")); + if (string.IsNullOrEmpty(personDirectory)) + shortcutFile = string.Empty; + else + shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk"); + saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile); + results.Add(saveContainer); + personDirectory = Path.Combine(directory, closest.Mapping.DisplayDirectoryName[..1], "lnk", closest.Mapping.DisplayDirectoryName); + saveContainer = new(personDirectory); + results.Add(saveContainer); + used.Add(closest.NormalizedPixelPercentage); } } return results; } - public void SaveMapping(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory) + public void SaveMapping(Item[] items, string dFacesContentDirectory, string d2ResultsFullGroupDirectory) { - List saveContainers = GetMappingSaveContainers(argZero, containers, dFacesContentDirectory, d2ResultsFullGroupDirectory); + List saveContainers = GetMappingSaveContainers(items, dFacesContentDirectory, d2ResultsFullGroupDirectory); SaveContainers(saveContainers); } - public void SaveClosest(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory) + public void SaveClosest(Item[] items, string dFacesContentDirectory, string d2ResultsFullGroupDirectory) { - List saveContainers = GetClosestSaveContainers(argZero, containers, dFacesContentDirectory, d2ResultsFullGroupDirectory); + List saveContainers = GetClosestSaveContainers(items, dFacesContentDirectory, d2ResultsFullGroupDirectory); SaveContainers(saveContainers); } - public void ForceSingleImage(string[] ignoreRelativePaths, string argZero, Container[] containers, int totalNotMapped, Random random) + public void ForceSingleImage(long ticks, Item[] items, int totalNotMapped, Random random) { int count = 0; Closest closest; Mapping mapping; bool? isWrongYear; - long? ticks = null; - const int zero = 0; bool forced = true; + const int zero = 0; int randomSelection; + string mappingSegmentB; + long? personKey = null; DateTime minimumDateTime; + string personKeyFormatted; bool aboveTolerance = false; int? approximateYears = null; PersonBirthday personBirthday; @@ -949,77 +876,65 @@ public class MapLogic { count = 0; randomSelection = random.Next(0, totalNotMapped); - foreach (Container container in containers) + foreach (Item item in items) { - if (!_NotMappedTicks.Any()) - break; - if (!container.Items.Any()) + if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) continue; - if (!container.SourceDirectory.StartsWith(argZero)) + if (item.Mapping.Any()) continue; - if (ignoreRelativePaths.Contains(Path.GetFileName(container.SourceDirectory))) - continue; - foreach (Item item in container.Items) + foreach (Face face in item.Faces) { - if (item.ImageFileHolder is null || item.Property?.Id is null || item.Mapping.Any()) + if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) continue; - foreach (Face face in item.Faces) - { - if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) - continue; - if (face.Location.Confidence < ILocation.MinimumConfidence) - continue; - count += 1; - if (count < randomSelection) - continue; - ticks = _NotMappedTicks[zero]; - minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property); - (isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime); - personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(ticks.Value); - mapping = new(approximateYears, displayDirectoryName, forced, face.Location.NormalizedPixelPercentage, personBirthday); - closest = new(aboveTolerance, zero, face.Location.NormalizedPixelPercentage.Value, isWrongYear, mapping, zero, minimumDateTime, null); - item.Closest.Add(closest); - item.Mapping.Add(mapping); - if (ticks is not null) - break; - } - if (ticks is not null) + if (face.Location.Confidence < ILocation.MinimumConfidence) + continue; + count += 1; + if (count < randomSelection) + continue; + personKey = _NotMappedPersonKeys[zero]; + minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property); + (isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime); + personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKey.Value); + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); + minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property); + (isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime); + mappingSegmentB = Stateless.MapLogic.GetMappingSegmentB(ticks, personBirthday, approximateYears, minimumDateTime, isWrongYear); + mapping = new(approximateYears, displayDirectoryName, forced, face.Location.NormalizedPixelPercentage, personBirthday, mappingSegmentB); + closest = new(aboveTolerance, zero, face.Location.NormalizedPixelPercentage.Value, isWrongYear, mapping, zero, minimumDateTime, null); + item.Closest.Add(closest); + item.Mapping.Add(mapping); + if (personKey is not null) break; } - if (ticks is not null) + if (personKey is not null) break; } - if (ticks is not null || count == 0) + if (personKey is not null || count == 0) break; } - if (ticks is null && count != 0) + if (personKey is null && count != 0) throw new Exception(); } - public void SavePropertyHolders(string argZero, Container[] containers) + public void SavePropertyHolders(Item[] items) { string json; FileInfo fileInfo; bool updateDateWhenMatches = false; JsonSerializerOptions jsonSerializerOptions = new() { WriteIndented = true }; - foreach (Container container in containers) + foreach (Item item in items) { - if (!container.Items.Any()) + if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) continue; - if (!container.SourceDirectory.StartsWith(argZero)) + if (!item.Faces.Any() || !item.Closest.Any()) continue; - foreach (Item item in container.Items) - { - if (item.ImageFileHolder is null || item.Property is null || !item.Faces.Any() || !item.Closest.Any()) - continue; - json = JsonSerializer.Serialize(item, jsonSerializerOptions); - fileInfo = new(Path.GetFullPath(string.Concat(_ZPropertyHolderSingletonTicksDirectory, item.RelativePath, ".json"))); - if (fileInfo.Directory is null) - continue; - if (!fileInfo.Directory.Exists) - continue; - _ = Shared.Models.Stateless.Methods.IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches, compareBeforeWrite: true); - } + json = JsonSerializer.Serialize(item, jsonSerializerOptions); + fileInfo = new(Path.GetFullPath(string.Concat(_ZPropertyHolderSingletonTicksDirectory, item.RelativePath, ".json"))); + if (fileInfo.Directory is null) + continue; + if (!fileInfo.Directory.Exists) + continue; + _ = Shared.Models.Stateless.Methods.IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches, compareBeforeWrite: true); } } diff --git a/Map/Models/Stateless/IMapLogic.cs b/Map/Models/Stateless/IMapLogic.cs index 846fbbe..cb3198a 100644 --- a/Map/Models/Stateless/IMapLogic.cs +++ b/Map/Models/Stateless/IMapLogic.cs @@ -7,12 +7,8 @@ public interface IMapLogic static (bool?, string[]) IsWrongYear(Shared.Models.Item item) => MapLogic.IsWrongYear(item); - string TestStatic_GetDateKey(DateTime dateTime, Shared.Models.Mapping mapping, DateTime minimumDateTime, bool? isWrongYear); - static string GetDateKey(DateTime dateTime, Shared.Models.Mapping mapping, DateTime minimumDateTime, bool? isWrongYear) => - MapLogic.GetDateKey(dateTime, mapping, minimumDateTime, isWrongYear); - - Dictionary> TestStatic_GetKeyValuePairs(string[] ignoreRelativePaths, string argZero, Shared.Models.Container[] containers); - static Dictionary> GetKeyValuePairs(string[] ignoreRelativePaths, string argZero, Shared.Models.Container[] containers) => - MapLogic.GetKeyValuePairs(ignoreRelativePaths, argZero, containers); + Dictionary> TestStatic_GetKeyValuePairs(Shared.Models.Item[] items); + static Dictionary> GetKeyValuePairs(Shared.Models.Item[] items) => + MapLogic.GetKeyValuePairs(items); } \ No newline at end of file diff --git a/Map/Models/Stateless/MapLogic.cs b/Map/Models/Stateless/MapLogic.cs index fc637c0..3349d3a 100644 --- a/Map/Models/Stateless/MapLogic.cs +++ b/Map/Models/Stateless/MapLogic.cs @@ -5,21 +5,22 @@ namespace View_by_Distance.Map.Models.Stateless; internal abstract class MapLogic { - internal static string GetDateKey(DateTime dateTime, Mapping mapping, DateTime minimumDateTime, bool? isWrongYear) + internal static string GetMappingSegmentB(long ticks, PersonBirthday personBirthday, int? approximateYears, DateTime minimumDateTime, bool? isWrongYear) { int years; string result; - TimeSpan? timeSpan = Shared.Models.Stateless.Methods.IPersonBirthday.GetTimeSpan(minimumDateTime, isWrongYear, mapping.PersonBirthday); + TimeSpan? timeSpan = Shared.Models.Stateless.Methods.IPersonBirthday.GetTimeSpan(minimumDateTime, isWrongYear, personBirthday); if (timeSpan.HasValue && timeSpan.Value.Ticks < 0) result = "!---"; else if (timeSpan.HasValue) { - (years, _) = Shared.Models.Stateless.Methods.IPersonBirthday.GetAge(minimumDateTime, mapping.PersonBirthday); + (years, _) = Shared.Models.Stateless.Methods.IPersonBirthday.GetAge(minimumDateTime, personBirthday); result = $"^{years:000}"; } - else if (mapping.ApproximateYears.HasValue) + else if (approximateYears.HasValue) { - (years, _) = Shared.Models.Stateless.Methods.IAge.GetAge(minimumDateTime, dateTime.AddYears(-mapping.ApproximateYears.Value)); + DateTime dateTime = new(ticks); + (years, _) = Shared.Models.Stateless.Methods.IAge.GetAge(minimumDateTime, dateTime.AddYears(-approximateYears.Value)); result = $"~{years:000}"; } else @@ -30,50 +31,40 @@ internal abstract class MapLogic return result; } - internal static Dictionary> GetKeyValuePairs(string[] ignoreRelativePaths, string argZero, Container[] containers) + internal static Dictionary> GetKeyValuePairs(Item[] items) { Dictionary> results = new(); string key; - string dateKey; bool? isWrongYear; - string personKey; + string personKeyFormatted; DateTime minimumDateTime; - DateTime dateTime = DateTime.Now; MappingContainer mappingContainer; - foreach (Container container in containers) + foreach (Item item in items) { - if (!container.Items.Any()) + if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) continue; - if (!container.SourceDirectory.StartsWith(argZero)) + if (!item.Mapping.Any()) continue; - if (ignoreRelativePaths.Contains(Path.GetFileName(container.SourceDirectory))) - continue; - foreach (Item item in container.Items) + foreach (Face face in item.Faces) { - if (item.ImageFileHolder is null || item.Property?.Id is null || !item.Mapping.Any()) + if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) continue; - foreach (Face face in item.Faces) + foreach (Mapping mapping in item.Mapping) { - if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null) + if (mapping.NormalizedPixelPercentage.HasValue && mapping.NormalizedPixelPercentage.Value != face.Location.NormalizedPixelPercentage.Value) continue; - foreach (Mapping mapping in item.Mapping) - { - if (mapping.NormalizedPixelPercentage.HasValue && mapping.NormalizedPixelPercentage.Value != face.Location.NormalizedPixelPercentage.Value) - continue; - // if (named.NormalizedPixelPercentage is null && (Shared.Models.Stateless.INamed.OnlyUseNamedWithNormalizedPixelPercentagePopulatedForGetKeyValuePairs || item.Named.Count != 1 || item.Faces.Count != 1)) - // continue; - minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property); - (isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime); - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(mapping.PersonBirthday); - dateKey = GetDateKey(dateTime, mapping, minimumDateTime, isWrongYear); - key = string.Concat(personKey, dateKey); - if (!results.ContainsKey(key)) - results.Add(key, new()); - mappingContainer = new(face, item.Property.Id.Value, isWrongYear, key, mapping, minimumDateTime); - results[key].Add(mappingContainer); - // if (named.NormalizedPixelPercentage is null) - // break; - } + // if (named.NormalizedPixelPercentage is null && (Shared.Models.Stateless.INamed.OnlyUseNamedWithNormalizedPixelPercentagePopulatedForGetKeyValuePairs || item.Named.Count != 1 || item.Faces.Count != 1)) + // continue; + minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property); + (isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime); + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(mapping.PersonBirthday); + key = string.Concat(personKeyFormatted, mapping.SegmentB); + if (!results.ContainsKey(key)) + results.Add(key, new()); + mappingContainer = new(face, item.Property.Id.Value, isWrongYear, key, mapping, minimumDateTime); + results[key].Add(mappingContainer); + // if (named.NormalizedPixelPercentage is null) + // break; } } } diff --git a/Map/Models/Stateless/SetByDeterministicHashCode.cs b/Map/Models/Stateless/SetByDeterministicHashCode.cs index e0c2ea6..845d191 100644 --- a/Map/Models/Stateless/SetByDeterministicHashCode.cs +++ b/Map/Models/Stateless/SetByDeterministicHashCode.cs @@ -11,10 +11,10 @@ public class ByDeterministicHashCode { long ticks; string json; - string personKey; string[] segments; int? approximateYears; string groupDirectoryName; + string personKeyFormatted; PersonBirthday[] collection; string personKeyJsonFileName; List personKeys = new(); @@ -28,10 +28,10 @@ public class ByDeterministicHashCode Dictionary personKeyValuePairs = new(); foreach (Person person in people) { - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(person.Birthday); - if (personKeyValuePairs.ContainsKey(personKey)) + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(person.Birthday); + if (personKeyValuePairs.ContainsKey(personKeyFormatted)) break; - personKeyValuePairs.Add(personKey, person); + personKeyValuePairs.Add(personKeyFormatted, person); } string[] groupDirectories = Directory.GetDirectories(deterministicHashCodePeopleDirectory, "*", SearchOption.TopDirectoryOnly); foreach (string groupDirectory in groupDirectories) @@ -65,14 +65,14 @@ public class ByDeterministicHashCode personKeyDirectories = Directory.GetDirectories(personDisplayDirectory, "*", SearchOption.TopDirectoryOnly); foreach (string personKeyDirectory in personKeyDirectories) { - personKey = Path.GetFileName(personKeyDirectory); - if (!DateTime.TryParseExact(personKey, "MM.dd.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime birthday)) - personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKey); + personKeyFormatted = Path.GetFileName(personKeyDirectory); + if (!DateTime.TryParseExact(personKeyFormatted, "MM.dd.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime birthday)) + personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKeyFormatted); else { personBirthday = new PersonBirthday(birthday); - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); - convertedPersonKeyDirectory = Path.Combine(personDisplayDirectory, personKey); + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); + convertedPersonKeyDirectory = Path.Combine(personDisplayDirectory, personKeyFormatted); if (!Directory.Exists(convertedPersonKeyDirectory)) Directory.Move(personKeyDirectory, convertedPersonKeyDirectory); } @@ -84,17 +84,17 @@ public class ByDeterministicHashCode continue; foreach (string personKeyDirectory in personKeyDirectories) { - personKey = Path.GetFileName(personKeyDirectory); - personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKey); + personKeyFormatted = Path.GetFileName(personKeyDirectory); + personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKeyFormatted); if (personBirthday is null) continue; - if (personKeyValuePairs.ContainsKey(personKey)) + if (personKeyValuePairs.ContainsKey(personKeyFormatted)) { - personKeyJsonDirectory = Path.Combine(personDisplayDirectory, personKey); + personKeyJsonDirectory = Path.Combine(personDisplayDirectory, personKeyFormatted); if (!Directory.Exists(personKeyJsonDirectory)) Directory.Move(personKeyDirectory, personKeyJsonDirectory); - personKeyJsonFileName = Path.Combine(personKeyJsonDirectory, $"{personKey}.json"); - json = JsonSerializer.Serialize(personKeyValuePairs[personKey], new JsonSerializerOptions() { WriteIndented = true }); + personKeyJsonFileName = Path.Combine(personKeyJsonDirectory, $"{personKeyFormatted}.json"); + json = JsonSerializer.Serialize(personKeyValuePairs[personKeyFormatted], new JsonSerializerOptions() { WriteIndented = true }); _ = Shared.Models.Stateless.Methods.IPath.WriteAllText(personKeyJsonFileName, json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null); } collection = personBirthdays.OrderByDescending(l => l.Value).ToArray(); @@ -116,7 +116,7 @@ public class ByDeterministicHashCode ticks = incrementDate.Ticks; personBirthday = new(incrementDate); incrementDate = incrementDate.AddDays(1); - personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); + personKeyFormatted = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday); if (personKeys.Contains(ticks)) continue; personKeys.Add(ticks); @@ -212,13 +212,13 @@ public class ByDeterministicHashCode private static List GetTicks(Dictionary deterministicHashCodeUnknownFaceKeyValuePairs) { List results = new(); - long ticks; + long personKey; foreach (KeyValuePair keyValuePair in deterministicHashCodeUnknownFaceKeyValuePairs) { foreach (PersonBirthday personBirthday in keyValuePair.Value) { - ticks = personBirthday.Value.Ticks; - results.Add(ticks); + personKey = personBirthday.Value.Ticks; + results.Add(personKey); } } results = results.Distinct().ToList(); @@ -227,15 +227,15 @@ public class ByDeterministicHashCode private static void Set(List<(PersonBirthday PersonBirthday, double IdAndNormalizedPixelPercentage)> deterministicHashCodeCollection, List deterministicHashCodePersonKeys, Dictionary> deterministicHashCodeScope) { - long ticks; + long personKey; deterministicHashCodeCollection = (from l in deterministicHashCodeCollection orderby l.IdAndNormalizedPixelPercentage select l).ToList(); foreach ((PersonBirthday personBirthday, double idAndNormalizedPixelPercentage) in deterministicHashCodeCollection) { if (!deterministicHashCodeScope.ContainsKey(idAndNormalizedPixelPercentage)) deterministicHashCodeScope.Add(idAndNormalizedPixelPercentage, new()); deterministicHashCodeScope[idAndNormalizedPixelPercentage].Add(personBirthday); - ticks = personBirthday.Value.Ticks; - deterministicHashCodePersonKeys.Add(ticks); + personKey = personBirthday.Value.Ticks; + deterministicHashCodePersonKeys.Add(personKey); } } @@ -281,20 +281,20 @@ public class ByDeterministicHashCode int? approximateYears = null; PersonBirthday? personBirthday; const string displayDirectoryName = Property.Models.Stateless.IResult.AllInOne; - foreach (long ticks in deterministicHashCodeUnknownFacePersonKeys) + foreach (long personKey in deterministicHashCodeUnknownFacePersonKeys) { - if (!peopleKeyValuePairs.ContainsKey(ticks)) + if (!peopleKeyValuePairs.ContainsKey(personKey)) { - personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(ticks); - peopleKeyValuePairs.Add(ticks, new(displayDirectoryName, approximateYears, new PersonBirthday[] { personBirthday }, ticks)); + personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKey); + peopleKeyValuePairs.Add(personKey, new(displayDirectoryName, approximateYears, new PersonBirthday[] { personBirthday }, personKey)); } } - foreach (long ticks in deterministicHashCodePersonKeys) + foreach (long personKey in deterministicHashCodePersonKeys) { - if (!peopleKeyValuePairs.ContainsKey(ticks)) + if (!peopleKeyValuePairs.ContainsKey(personKey)) { - personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(ticks); - peopleKeyValuePairs.Add(ticks, new(displayDirectoryName, approximateYears, new PersonBirthday[] { personBirthday }, ticks)); + personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKey); + peopleKeyValuePairs.Add(personKey, new(displayDirectoryName, approximateYears, new PersonBirthday[] { personBirthday }, personKey)); } } } diff --git a/Shared/Models/Face.cs b/Shared/Models/Face.cs index e81da60..e850dc4 100644 --- a/Shared/Models/Face.cs +++ b/Shared/Models/Face.cs @@ -10,21 +10,25 @@ public class Face : Properties.IFace protected List _FaceDistances; protected FaceEncoding? _FaceEncoding; protected Dictionary? _FaceParts; + protected List? _FaceNumbers; protected readonly OutputResolution? _OutputResolution; protected Location? _Location; protected readonly int? _LocationIndex; protected readonly string _RelativePath; + protected List _Tags; public DateTime DateTime => _DateTime; public List FaceDistances => _FaceDistances; public FaceEncoding? FaceEncoding => _FaceEncoding; public Dictionary? FaceParts => _FaceParts; + public List? FaceNumbers => _FaceNumbers; public Location? Location => _Location; public int? LocationIndex => _LocationIndex; public OutputResolution? OutputResolution => _OutputResolution; public string RelativePath => _RelativePath; + public List Tags => _Tags; [JsonConstructor] - public Face(DateTime dateTime, List faceDistances, FaceEncoding? faceEncoding, Dictionary? faceParts, Location? location, int? locationIndex, OutputResolution? outputResolution, string relativePath) + public Face(DateTime dateTime, List faceDistances, FaceEncoding? faceEncoding, Dictionary? faceParts, List? faceNumbers, Location? location, int? locationIndex, OutputResolution? outputResolution, string relativePath) { if (faceDistances is null) faceDistances = new(); @@ -32,36 +36,38 @@ public class Face : Properties.IFace _FaceDistances = faceDistances; _FaceEncoding = faceEncoding; _FaceParts = faceParts; + _FaceNumbers = faceNumbers; _Location = location; _LocationIndex = locationIndex; _OutputResolution = outputResolution; _RelativePath = relativePath; + _Tags = new(); } public Face() : - this(DateTime.MinValue, new(), null, null, null, null, null, string.Empty) + this(DateTime.MinValue, new(), null, null, null, null, null, null, string.Empty) { } public Face(Location location) : - this(DateTime.MinValue, new(), null, null, location, null, null, string.Empty) + this(DateTime.MinValue, new(), null, null, null, location, null, null, string.Empty) { } public Face(int facesCount, Face face) : - this(face.DateTime, new(), face.FaceEncoding, face.FaceParts, face.Location, face.LocationIndex, face.OutputResolution, face.RelativePath) + this(face.DateTime, new(), face.FaceEncoding, face.FaceParts, null, face.Location, face.LocationIndex, face.OutputResolution, face.RelativePath) { if (face.Location?.Confidence is not null && face.OutputResolution is not null) _Location = new(face.Location.Confidence, face.OutputResolution.Height, face.Location, face.OutputResolution.Width, facesCount); } public Face(int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation, Face face) : - this(face.DateTime, new(), face.FaceEncoding, face.FaceParts, face.Location, face.LocationIndex, null, face.RelativePath) + this(face.DateTime, new(), face.FaceEncoding, face.FaceParts, null, face.Location, face.LocationIndex, null, face.RelativePath) { if (outputResolutionHeight > 0) _OutputResolution = new(outputResolutionHeight, outputResolutionOrientation, outputResolutionWidth); } public Face(Property property, int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation, string relativePath, int? i, Location? location) : - this(DateTime.MinValue, new(), null, null, location, i, null, relativePath) + this(DateTime.MinValue, new(), null, null, null, location, i, null, relativePath) { DateTime?[] dateTimes; _OutputResolution = new(outputResolutionHeight, outputResolutionOrientation, outputResolutionWidth); @@ -78,4 +84,7 @@ public class Face : Properties.IFace public void SetFaceEncoding(FaceEncoding faceEncoding) => _FaceEncoding = faceEncoding; public void SetFaceParts(Dictionary faceParts) => _FaceParts = faceParts; + + public void SetFaceNumbers(List faceNumbers) => _FaceNumbers = faceNumbers; + } \ No newline at end of file diff --git a/Shared/Models/Mapping.cs b/Shared/Models/Mapping.cs index 8ed36fa..e57d7d5 100644 --- a/Shared/Models/Mapping.cs +++ b/Shared/Models/Mapping.cs @@ -12,15 +12,17 @@ public class Mapping : Properties.IMapping protected bool _Forced; protected readonly int? _NormalizedPixelPercentage; protected PersonBirthday _PersonBirthday; + protected readonly string _SegmentB; public int? ApproximateYears => _ApproximateYears; public string DisplayDirectoryName => _DisplayDirectoryName; public bool? Filtered => _Filtered; public bool Forced => _Forced; public int? NormalizedPixelPercentage => _NormalizedPixelPercentage; public PersonBirthday PersonBirthday => _PersonBirthday; + public string SegmentB => _SegmentB; [JsonConstructor] - public Mapping(int? approximateYears, string displayDirectoryName, bool? filtered, bool forced, int? normalizedPixelPercentage, PersonBirthday personBirthday) + public Mapping(int? approximateYears, string displayDirectoryName, bool? filtered, bool forced, int? normalizedPixelPercentage, PersonBirthday personBirthday, string segmentB) { _ApproximateYears = approximateYears; _DisplayDirectoryName = displayDirectoryName; @@ -28,14 +30,15 @@ public class Mapping : Properties.IMapping _Forced = forced; _NormalizedPixelPercentage = normalizedPixelPercentage; _PersonBirthday = personBirthday; + _SegmentB = segmentB; } - public Mapping(int? approximateYears, string displayDirectoryName, bool forced, int? normalizedPixelPercentage, PersonBirthday personBirthday) : - this(approximateYears, displayDirectoryName, null, forced, normalizedPixelPercentage, personBirthday) + public Mapping(int? approximateYears, string displayDirectoryName, bool forced, int? normalizedPixelPercentage, PersonBirthday personBirthday, string segmentB) : + this(approximateYears, displayDirectoryName, null, forced, normalizedPixelPercentage, personBirthday, segmentB) { } - public Mapping(int? approximateYears, string displayDirectoryName, bool forced, PersonBirthday personBirthday) : - this(approximateYears, displayDirectoryName, null, forced, null, personBirthday) + public Mapping(int? approximateYears, string displayDirectoryName, bool forced, PersonBirthday personBirthday, string segmentB) : + this(approximateYears, displayDirectoryName, null, forced, null, personBirthday, segmentB) { } public override string ToString() diff --git a/Shared/Models/Properties/IFace.cs b/Shared/Models/Properties/IFace.cs index c320cb2..204be53 100644 --- a/Shared/Models/Properties/IFace.cs +++ b/Shared/Models/Properties/IFace.cs @@ -7,6 +7,7 @@ public interface IFace public List FaceDistances { get; } public FaceEncoding? FaceEncoding { get; } public Dictionary? FaceParts { get; } + public List? FaceNumbers { get; } public Location? Location { get; } public int? LocationIndex { get; } public OutputResolution? OutputResolution { get; } diff --git a/Shared/Models/Properties/IMapping.cs b/Shared/Models/Properties/IMapping.cs index 1eff0a4..f7edbd4 100644 --- a/Shared/Models/Properties/IMapping.cs +++ b/Shared/Models/Properties/IMapping.cs @@ -9,5 +9,6 @@ public interface IMapping public bool Forced { get; } public int? NormalizedPixelPercentage { get; } public PersonBirthday PersonBirthday { get; } + public string SegmentB { get; } } \ No newline at end of file diff --git a/Shared/Models/Properties/ISorting.cs b/Shared/Models/Properties/ISorting.cs new file mode 100644 index 0000000..264997f --- /dev/null +++ b/Shared/Models/Properties/ISorting.cs @@ -0,0 +1,12 @@ +namespace View_by_Distance.Shared.Models.Properties; + +public interface ISorting +{ + + public double? Confidence { init; get; } + public double? Distance { init; get; } + public int? FaceEncoding { init; get; } + public int Id { init; get; } + public int? NormalizedPixelPercentage { init; get; } + +} \ No newline at end of file diff --git a/Shared/Models/Sorting.cs b/Shared/Models/Sorting.cs new file mode 100644 index 0000000..68a0d75 --- /dev/null +++ b/Shared/Models/Sorting.cs @@ -0,0 +1,49 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace View_by_Distance.Shared.Models; + +public class Sorting : Properties.ISorting +{ + + public double? Confidence { init; get; } + public double? Distance { init; get; } + public int? FaceEncoding { init; get; } + public int Id { init; get; } + public int? NormalizedPixelPercentage { init; get; } + + [JsonConstructor] + public Sorting(double? confidence, double? distance, int? faceEncoding, int id, int? normalizedPixelPercentage) + { + Confidence = confidence; + Distance = distance; + FaceEncoding = faceEncoding; + Id = id; + NormalizedPixelPercentage = normalizedPixelPercentage; + } + + public Sorting(double? confidence, int id, int? normalizedPixelPercentage) + { + Confidence = confidence; + Distance = null; + FaceEncoding = int.MaxValue; + Id = id; + NormalizedPixelPercentage = normalizedPixelPercentage; + } + + public override string ToString() + { + string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + return result; + } + + public Sorting(Sorting sorting, double? distance) + { + Confidence = sorting.Confidence; + Distance = distance; + FaceEncoding = null; + Id = sorting.Id; + NormalizedPixelPercentage = sorting.NormalizedPixelPercentage; + } + +} \ No newline at end of file diff --git a/Shared/Models/Stateless/Methods/Face.cs b/Shared/Models/Stateless/Methods/Face.cs index 6958b7d..7345348 100644 --- a/Shared/Models/Stateless/Methods/Face.cs +++ b/Shared/Models/Stateless/Methods/Face.cs @@ -37,6 +37,21 @@ internal abstract class Face return jsonElements; } + internal static List GetSortingCollection(Models.Face face) + { + List results = new(); + Models.Sorting sorting; + if (face.FaceNumbers is not null) + { + foreach (int[] numbers in face.FaceNumbers) + { + sorting = Sorting.Get(numbers); + results.Add(sorting); + } + } + return results; + } + private static List GetFaces(string jsonFileFullName, int? maximum) { List results = new(); diff --git a/Shared/Models/Stateless/Methods/IFace.cs b/Shared/Models/Stateless/Methods/IFace.cs index 50c115d..554c7d9 100644 --- a/Shared/Models/Stateless/Methods/IFace.cs +++ b/Shared/Models/Stateless/Methods/IFace.cs @@ -18,7 +18,11 @@ public interface IFace Models.Face[] TestStatic_Getα(Dictionary faceParts); static double? Getα(Dictionary faceParts) => Face.Getα(faceParts); + List TestStatic_GetSortingCollection(Models.Face face); + static List GetSortingCollection(Models.Face face) => Face.GetSortingCollection(face); + int?[] TestStatic_GetInts(List faces); - static int?[] GetInts(List faces) => (from l in faces where l.FaceEncoding is not null && l.Location?.NormalizedPixelPercentage is not null select l.Location?.NormalizedPixelPercentage).ToArray(); + static int?[] GetInts(List faces) => + (from l in faces where l.FaceEncoding is not null && l.Location?.NormalizedPixelPercentage is not null select l.Location?.NormalizedPixelPercentage).ToArray(); } \ No newline at end of file diff --git a/Shared/Models/Stateless/Methods/ISorting.cs b/Shared/Models/Stateless/Methods/ISorting.cs new file mode 100644 index 0000000..cdbc192 --- /dev/null +++ b/Shared/Models/Stateless/Methods/ISorting.cs @@ -0,0 +1,12 @@ +namespace View_by_Distance.Shared.Models.Stateless.Methods; + +public interface ISorting +{ // ... + + Models.Sorting TestStatic_GetSorting(int[] numbers); + static Models.Sorting GetSorting(int[] numbers) => Sorting.Get(numbers); + + List TestStatic_GetFaceNumbers(List collection); + static List GetFaceNumbers(List collection) => Sorting.GetFaceNumbers(collection); + +} \ No newline at end of file diff --git a/Shared/Models/Stateless/Methods/Sorting.cs b/Shared/Models/Stateless/Methods/Sorting.cs new file mode 100644 index 0000000..a30fdc4 --- /dev/null +++ b/Shared/Models/Stateless/Methods/Sorting.cs @@ -0,0 +1,50 @@ +namespace View_by_Distance.Shared.Models.Stateless.Methods; + +internal abstract class Sorting +{ + + internal static Models.Sorting Get(int[] collection) + { + if (collection.Length != 5) + throw new Exception(); + Models.Sorting result; + int id = collection[3]; + int distance = collection[1]; + int confidence = collection[2]; + int faceEncoding = collection[0]; + int normalizedPixelPercentage = collection[4]; + result = new(confidence, distance, faceEncoding, id, normalizedPixelPercentage); + return result; + } + + internal static List GetFaceNumbers(List collection) + { + List results = new(); + List faceNumbers; + collection = (from l in collection orderby l.FaceEncoding is not null, l.Distance, l.Confidence descending, l.Id, l.NormalizedPixelPercentage select l).ToList(); + foreach (Models.Sorting sorting in collection) + { + faceNumbers = new(); + if (sorting.FaceEncoding is null) + faceNumbers.Add(Stateless.ILocation.Factor); + else + faceNumbers.Add(Stateless.ILocation.Factor * 2); + if (sorting.Distance is null) + faceNumbers.Add(Stateless.ILocation.Factor); + else + faceNumbers.Add((int)(Math.Round(sorting.Distance.Value, Stateless.ILocation.Digits) * Stateless.ILocation.Factor)); + if (sorting.Confidence is null) + faceNumbers.Add(Stateless.ILocation.Factor); + else + faceNumbers.Add((int)(Math.Round(sorting.Confidence.Value, Stateless.ILocation.Digits) * Stateless.ILocation.Factor)); + faceNumbers.Add(sorting.Id); + if (sorting.NormalizedPixelPercentage is null) + faceNumbers.Add(Stateless.ILocation.Factor); + else + faceNumbers.Add(sorting.NormalizedPixelPercentage.Value); + results.Add(faceNumbers.ToArray()); + } + return results; + } + +} \ No newline at end of file