using ShellProgressBar; using System.Text.Json; using View_by_Distance.Property.Models; using View_by_Distance.Shared.Models; using View_by_Distance.Shared.Models.Stateless.Methods; using WindowsShortcutFactory; namespace View_by_Distance.Map.Models; public class MapLogic { protected readonly List _SkipCollection; protected readonly List _NotMappedPersonKeys; protected readonly Dictionary _KeyValuePairs; protected readonly Dictionary _IndicesFromNew; protected readonly Dictionary _SixCharacterNamedFaceInfo; protected readonly Dictionary _DeterministicHashCodeUnknownFaceKeyValuePairs; protected readonly Dictionary _IncorrectDeterministicHashCodeKeyValuePairs; protected readonly Dictionary _PersonKeysRanges; protected readonly Dictionary> _IdThenNormalizedPixelPercentageKeyValuePairs; protected readonly Dictionary _PeopleKeyValuePairs; public Dictionary KeyValuePairs => _KeyValuePairs; public Dictionary IndicesFromNew => _IndicesFromNew; private readonly long _Ticks; private readonly Serilog.ILogger? _Log; private readonly Configuration _Configuration; private readonly string _FacesFilenameExtension; private readonly string _ResizeFilenameExtension; private readonly string _FacePartsFilenameExtension; private readonly string _FacesHiddenFilenameExtension; private readonly string _ZPropertyHolderContentDirectory; private readonly string _ZPropertyHolderContentTicksDirectory; private readonly string _ZPropertyHolderSingletonTicksDirectory; public MapLogic(int maxDegreeOfParallelism, Configuration configuration, string resizeFilenameExtension, string facesFilenameExtension, string facesHiddenFilenameExtension, string facePartsFilenameExtension, long ticks, Person[] people, string peopleDateGroupDirectory, string zResultsFullGroupDirectory) { _Ticks = ticks; _PersonKeysRanges = new(); _Configuration = configuration; _Log = Serilog.Log.ForContext(); _FacesFilenameExtension = facesFilenameExtension; _ResizeFilenameExtension = resizeFilenameExtension; _FacePartsFilenameExtension = facePartsFilenameExtension; _FacesHiddenFilenameExtension = facesHiddenFilenameExtension; if (configuration.VerifyToSeason is null || !configuration.VerifyToSeason.Any()) throw new Exception(); string json; string[] files; string fullPath; const int zero = 0; List notMappedTicks = new(); List skipCollection = new(); Dictionary? keyValuePairs; List>? collection; Dictionary indicesFromNew = new(); Dictionary? sixCharacterNamedFaceInfo; string? rootDirectoryParent = Path.GetDirectoryName(configuration.RootDirectory); Dictionary? deterministicHashCodeUnknownFaceKeyValuePairs; Dictionary peopleKeyValuePairs = new(); string zPropertyHolderContentDirectory = Path.Combine(zResultsFullGroupDirectory, "()"); Dictionary incorrectDeterministicHashCodeKeyValuePairs = new(); string zPropertyHolderSingletonDirectory = Path.Combine(zResultsFullGroupDirectory, "{}"); Dictionary> idThenNormalizedPixelPercentageKeyValuePairs = new(); string zPropertyHolderContentTicksDirectory = Path.Combine(zPropertyHolderContentDirectory, $"({ticks})"); string zPropertyHolderPeopleContentDirectory = Path.Combine(peopleDateGroupDirectory, "()", "(KnownPeople)"); string zPropertyHolderSingletonTicksDirectory = Path.Combine(zPropertyHolderSingletonDirectory, string.Concat('{', _Ticks, '}')); if (string.IsNullOrEmpty(rootDirectoryParent)) throw new NullReferenceException(nameof(rootDirectoryParent)); if (!Directory.Exists(zPropertyHolderContentDirectory)) _ = Directory.CreateDirectory(zPropertyHolderContentDirectory); if (!Directory.Exists(zPropertyHolderPeopleContentDirectory)) _ = Directory.CreateDirectory(zPropertyHolderPeopleContentDirectory); files = Directory.GetFiles(rootDirectoryParent, "DeterministicHashCode*.json", SearchOption.TopDirectoryOnly); if (files.Length != 1) deterministicHashCodeUnknownFaceKeyValuePairs = new(); else { json = File.ReadAllText(files[zero]); deterministicHashCodeUnknownFaceKeyValuePairs = GetKeyValuePairs(json); } Stateless.ByDeterministicHashCode.SetByRef(maxDegreeOfParallelism, ticks, _ResizeFilenameExtension, people, skipCollection, peopleKeyValuePairs, notMappedTicks, deterministicHashCodeUnknownFaceKeyValuePairs, idThenNormalizedPixelPercentageKeyValuePairs, incorrectDeterministicHashCodeKeyValuePairs, zPropertyHolderContentDirectory, zPropertyHolderPeopleContentDirectory); if (!Directory.Exists(zPropertyHolderContentTicksDirectory)) _ = Directory.CreateDirectory(zPropertyHolderContentTicksDirectory); if (!deterministicHashCodeUnknownFaceKeyValuePairs.Any()) sixCharacterNamedFaceInfo = new(); else { files = Directory.GetFiles(rootDirectoryParent, "*SixCharacter*.json", SearchOption.TopDirectoryOnly); if (files.Length != 1) sixCharacterNamedFaceInfo = new(); else { json = File.ReadAllText(files[zero]); sixCharacterNamedFaceInfo = JsonSerializer.Deserialize>(json); if (sixCharacterNamedFaceInfo is null) throw new NullReferenceException(nameof(sixCharacterNamedFaceInfo)); } } files = Directory.GetFiles(rootDirectoryParent, "*keyValuePairs-6*.json", SearchOption.TopDirectoryOnly); if (files.Length != 1) keyValuePairs = new(); else { json = File.ReadAllText(files[zero]); keyValuePairs = JsonSerializer.Deserialize>(json); if (keyValuePairs is null) throw new NullReferenceException(nameof(keyValuePairs)); } foreach (string propertyContentCollectionFile in configuration.PropertyContentCollectionFiles) { fullPath = Path.GetFullPath(string.Concat(rootDirectoryParent, propertyContentCollectionFile)); if (fullPath.Contains(configuration.RootDirectory)) continue; if (!File.Exists(fullPath)) continue; json = File.ReadAllText(fullPath); collection = JsonSerializer.Deserialize>>(json); if (collection is null) throw new NullReferenceException(nameof(collection)); foreach (KeyValuePair keyValuePair in collection) { if (indicesFromNew.ContainsKey(keyValuePair.Key)) continue; indicesFromNew.Add(keyValuePair.Key, keyValuePair.Value); } } _KeyValuePairs = keyValuePairs; _IndicesFromNew = indicesFromNew; _SkipCollection = skipCollection; _NotMappedPersonKeys = notMappedTicks; _PeopleKeyValuePairs = peopleKeyValuePairs; _SixCharacterNamedFaceInfo = sixCharacterNamedFaceInfo; _ZPropertyHolderContentDirectory = zPropertyHolderContentDirectory; _ZPropertyHolderContentTicksDirectory = zPropertyHolderContentTicksDirectory; _ZPropertyHolderSingletonTicksDirectory = zPropertyHolderSingletonTicksDirectory; _IncorrectDeterministicHashCodeKeyValuePairs = incorrectDeterministicHashCodeKeyValuePairs; _IdThenNormalizedPixelPercentageKeyValuePairs = idThenNormalizedPixelPercentageKeyValuePairs; _DeterministicHashCodeUnknownFaceKeyValuePairs = deterministicHashCodeUnknownFaceKeyValuePairs; } public MapLogic(int maxDegreeOfParallelism, Configuration configuration, string outputExtension, long ticks, Person[] people, string peopleDateGroupDirectory, string zResultsFullGroupDirectory) : this(maxDegreeOfParallelism, configuration, outputExtension, outputExtension, outputExtension, outputExtension, ticks, people, peopleDateGroupDirectory, zResultsFullGroupDirectory) { } public override string ToString() { string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); return result; } public bool Skip(double deterministicHashCodeKey) => _SkipCollection.Contains(deterministicHashCodeKey); private long LogDelta(long ticks, string? methodName) { long result; if (_Log is null) throw new NullReferenceException(nameof(_Log)); double delta = new TimeSpan(DateTime.Now.Ticks - ticks).TotalMilliseconds; _Log.Debug($"{methodName} took {Math.Floor(delta)} millisecond(s)"); result = DateTime.Now.Ticks; return result; } private List<(long, long, long, long)> GetPersonKeysRangesCollection(PersonBirthday[] personBirthdays) { List<(long, long, long, long)> results = new(); long personKey; foreach (PersonBirthday personBirthday in personBirthdays) { personKey = personBirthday.Value.Ticks; if (!_PersonKeysRanges.ContainsKey(personKey)) continue; results.Add(_PersonKeysRanges[personKey]); } return results; } public List GetSortingCollection(int i, FaceDistance faceDistanceEncoding, List faceDistanceLengths, bool anyLowerThanTolerance) { List results = new(); Sorting sorting; FaceDistance faceDistanceLength; Dictionary keyValuePairs; List<(long lcl, long minimum, long maximum, long ucl)> personKeysRangesCollection; for (int j = 0; j < faceDistanceLengths.Count; j++) { if (j == i) continue; faceDistanceLength = faceDistanceLengths[j]; if (faceDistanceEncoding.NormalizedPixelPercentage is null || faceDistanceLength.NormalizedPixelPercentage is null || faceDistanceLength.Length is null) throw new NotSupportedException(); if (faceDistanceLength.Length == 0) continue; if (_IdThenNormalizedPixelPercentageKeyValuePairs.ContainsKey(faceDistanceEncoding.Id)) { keyValuePairs = _IdThenNormalizedPixelPercentageKeyValuePairs[faceDistanceEncoding.Id]; if (keyValuePairs.ContainsKey(faceDistanceEncoding.NormalizedPixelPercentage.Value)) continue; } if (!_IdThenNormalizedPixelPercentageKeyValuePairs.ContainsKey(faceDistanceLength.Id)) continue; keyValuePairs = _IdThenNormalizedPixelPercentageKeyValuePairs[faceDistanceLength.Id]; if (!keyValuePairs.ContainsKey(faceDistanceLength.NormalizedPixelPercentage.Value)) continue; personKeysRangesCollection = GetPersonKeysRangesCollection(keyValuePairs[faceDistanceLength.NormalizedPixelPercentage.Value]); sorting = ISorting.Get(faceDistanceEncoding, faceDistanceLength, anyLowerThanTolerance, personKeysRangesCollection); if (sorting.DistancePermyriad == 0) continue; if (sorting.Id == faceDistanceEncoding.Id) { if (sorting.NormalizedPixelPercentage == faceDistanceEncoding.NormalizedPixelPercentage.Value) throw new NotSupportedException(); continue; } results.Add(sorting); } return results; } public void SaveShortcuts(string[] juliePhares, Item[] filteredItems) { string fileName; string fullName; DateTime minimumDateTime; string personKeyFormatted; PersonBirthday personBirthday; WindowsShortcut windowsShortcut; (string DisplayDirectoryName, int? ApproximateYears, PersonBirthday[] PersonBirthdays, long PersonKey) person; List<(Item, (long?, Face?, (string, string, string, string))[])> collections = GetCollection(filteredItems); foreach ((Item item, (long? personKey, Face? _, (string, string, string, string))[] collection) in collections) { if (collection.Length != 1) continue; foreach ((long? personKey, Face? _, (string directory, string copyDirectory, string copyFileName, string shortcutFileName)) in collection) { if (personKey is null) continue; if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) continue; minimumDateTime = IProperty.GetMinimumDateTime(item.Property); if (!Directory.Exists(directory)) { _ = Directory.CreateDirectory(directory); if (personKey is not null && _PeopleKeyValuePairs.ContainsKey(personKey.Value)) { person = _PeopleKeyValuePairs[personKey.Value]; fullName = string.Concat(person.DisplayDirectoryName, ".txt"); File.WriteAllText(Path.Combine(directory, fullName), string.Empty); } } if (personKey is null) continue; personBirthday = IPersonBirthday.GetPersonBirthday(personKey.Value); personKeyFormatted = IPersonBirthday.GetFormatted(personBirthday); if (juliePhares.Contains(personKeyFormatted) && !string.IsNullOrEmpty(copyDirectory)) { if (!Directory.Exists(copyDirectory)) _ = Directory.CreateDirectory(copyDirectory); fileName = Path.Combine(copyDirectory, $"{item.Property.Id.Value}{item.ResizedFileHolder.ExtensionLowered}"); if (!File.Exists(fileName)) File.Copy(item.ResizedFileHolder.FullName, fileName); } fileName = Path.Combine(directory, $"{item.Property.Id.Value}.lnk"); if (File.Exists(fileName)) continue; windowsShortcut = new() { Path = item.ResizedFileHolder.FullName }; windowsShortcut.Save(fileName); windowsShortcut.Dispose(); if (!File.Exists(fileName)) continue; File.SetLastWriteTime(fileName, minimumDateTime); } } } private void UseKeyValuePairsSaveFaceEncoding(int maxDegreeOfParallelism, SortingContainer[] sortingContainers) { Dictionary personKeyAliases = new(); Dictionary> keyValuePairs = new(); List<(PersonBirthday, int, int)> idThenNormalizedPixelPercentageCollection = new(); List<(PersonBirthday PersonBirthday, double IdAndNormalizedPixelPercentage)> incorrectDeterministicHashCodeCollection = new(); foreach (SortingContainer sortingContainer in sortingContainers) { if (sortingContainer.Face.FaceEncoding is null || sortingContainer.Face.Location?.NormalizedPixelPercentage is null) throw new NotSupportedException(); if (sortingContainer.Face.Mapping is null) throw new NotSupportedException(); if (!keyValuePairs.ContainsKey(sortingContainer.Face.Mapping.MappingFromItem.Id)) keyValuePairs.Add(sortingContainer.Face.Mapping.MappingFromItem.Id, new()); keyValuePairs[sortingContainer.Face.Mapping.MappingFromItem.Id].Add(sortingContainer.Face); } Stateless.ByDeterministicHashCode.SetCollections(maxDegreeOfParallelism, _Ticks, _ZPropertyHolderContentDirectory, personKeyAliases, idThenNormalizedPixelPercentageCollection, incorrectDeterministicHashCodeCollection, keyValuePairs); } private static string GetMappingSegmentB(long ticks, PersonBirthday personBirthday, int? approximateYears, long minimumDateTimeTicks, bool? isWrongYear) { int years; string result; TimeSpan? timeSpan = IPersonBirthday.GetTimeSpan(minimumDateTimeTicks, isWrongYear, personBirthday); if (timeSpan.HasValue && timeSpan.Value.Ticks < 0) result = "!---"; else if (timeSpan.HasValue) { (years, _) = IPersonBirthday.GetAge(minimumDateTimeTicks, personBirthday); result = $"^{years:000}"; } else if (approximateYears.HasValue) { DateTime dateTime = new(ticks); (years, _) = IAge.GetAge(minimumDateTimeTicks, dateTime.AddYears(-approximateYears.Value)); result = $"~{years:000}"; } else { string isWrongYearFlag = IItem.GetWrongYearFlag(isWrongYear); result = $"{isWrongYearFlag}{new DateTime(minimumDateTimeTicks):yyyy}"; } return result; } private static string GetMappingSegmentB(long ticks, PersonBirthday personBirthday, int? approximateYears, DateTime minimumDateTime, bool? isWrongYear) { string result = GetMappingSegmentB(ticks, personBirthday, approximateYears, minimumDateTime.Ticks, isWrongYear); return result; } private static string GetMappingSegmentB(long ticks, PersonBirthday personBirthday, int? approximateYears, MappingFromItem mappingFromItem) { string result = GetMappingSegmentB(ticks, personBirthday, approximateYears, mappingFromItem.MinimumDateTime, mappingFromItem.IsWrongYear); return result; } private static Dictionary GetKeyValuePairs(string json) { Dictionary results = new(); PersonBirthday? personBirthday; List personBirthdays; Dictionary? keyValuePairs = JsonSerializer.Deserialize>(json); if (keyValuePairs is null) throw new NullReferenceException(nameof(keyValuePairs)); foreach (KeyValuePair keyValuePair in keyValuePairs) { personBirthdays = new(); foreach (string personKey in keyValuePair.Value) { personBirthday = IPersonBirthday.GetPersonBirthday(personKey); if (personBirthday is null) continue; } if (!personBirthdays.Any()) continue; results.Add(keyValuePair.Key, personBirthdays.ToArray()); } return results; } private int AddToMapping(List distinctFilteredFaces) { int by; int result = 0; long personKey; const int zero = 0; int? approximateYears; string mappingSegmentB; string personKeyFormatted; string displayDirectoryName; PersonBirthday personBirthday; List personBirthdays = new(); Dictionary keyValuePairs; (string DisplayDirectoryName, int? ApproximateYears, PersonBirthday[] PersonBirthdays, long PersonKey) person; foreach (Face face in distinctFilteredFaces) { by = 2; personBirthdays.Clear(); if (face.Mapping is null) throw new NotSupportedException(); if (face.Mapping is null) throw new NotSupportedException(); if (!_IdThenNormalizedPixelPercentageKeyValuePairs.ContainsKey(face.Mapping.MappingFromItem.Id)) result += 1; else { keyValuePairs = _IdThenNormalizedPixelPercentageKeyValuePairs[face.Mapping.MappingFromItem.Id]; if (!keyValuePairs.ContainsKey(face.Mapping.MappingFromLocation.NormalizedPixelPercentage)) result += 1; else personBirthdays.AddRange(keyValuePairs[face.Mapping.MappingFromLocation.NormalizedPixelPercentage]); } for (int i = 0; i < personBirthdays.Count; i++) { personKey = personBirthdays[i].Value.Ticks; if (!_PeopleKeyValuePairs.ContainsKey(personKey)) continue; person = _PeopleKeyValuePairs[personKey]; approximateYears = person.ApproximateYears; personBirthday = person.PersonBirthdays[zero]; displayDirectoryName = person.DisplayDirectoryName; personKeyFormatted = IPersonBirthday.GetFormatted(personBirthday); mappingSegmentB = GetMappingSegmentB(_Ticks, personBirthday, approximateYears, face.Mapping.MappingFromItem); face.Mapping.UpdateMappingFromPerson(approximateYears, by, displayDirectoryName, personBirthday, mappingSegmentB); } if (!Shared.Models.Stateless.IMapping.UseDeterministicHashCodeUnknownFaceKeyValuePairsForAddToMapping || !_DeterministicHashCodeUnknownFaceKeyValuePairs.ContainsKey(face.Mapping.MappingFromItem.Id)) continue; by = 1; personBirthdays.AddRange(_DeterministicHashCodeUnknownFaceKeyValuePairs[face.Mapping.MappingFromItem.Id]); for (int i = 0; i < personBirthdays.Count; i++) { personKey = personBirthdays[i].Value.Ticks; if (!_PeopleKeyValuePairs.ContainsKey(personKey)) continue; person = _PeopleKeyValuePairs[personKey]; approximateYears = person.ApproximateYears; personBirthday = person.PersonBirthdays[zero]; displayDirectoryName = person.DisplayDirectoryName; personKeyFormatted = IPersonBirthday.GetFormatted(personBirthday); mappingSegmentB = GetMappingSegmentB(_Ticks, personBirthday, approximateYears, face.Mapping.MappingFromItem); face.Mapping.UpdateMappingFromPerson(approximateYears, by, displayDirectoryName, personBirthday, mappingSegmentB); } } return result; } private void SaveContainers(List saveContainers) { WindowsShortcut windowsShortcut; string[] directories = (from l in saveContainers select l.Directory).Distinct().ToArray(); int totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - _Ticks).TotalSeconds); string message = $") {saveContainers.Count:000} save(s) - {totalSeconds} total second(s)"; ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = false }; foreach (string directory in directories) { if (string.IsNullOrEmpty(directory)) continue; if (!Directory.Exists(directory)) _ = Directory.CreateDirectory(directory); } using ProgressBar progressBar = new(saveContainers.Count, message, options); foreach (SaveContainer saveContainer in saveContainers) { progressBar.Tick(); if (string.IsNullOrEmpty(saveContainer.Directory) || string.IsNullOrEmpty(saveContainer.CheckFile) || saveContainer.ResizedFileHolder is null || saveContainer.FaceFileHolder is null || !string.IsNullOrEmpty(saveContainer.Json)) continue; if (File.Exists(saveContainer.CheckFile)) continue; if (saveContainer.FaceFileHolder.Exists) File.Copy(saveContainer.FaceFileHolder.FullName, saveContainer.CheckFile); else File.Copy(saveContainer.ResizedFileHolder.FullName, saveContainer.CheckFile); if (saveContainer.HiddenFaceFileHolder is not null && saveContainer.HiddenFaceFileHolder.Exists) File.Copy(saveContainer.HiddenFaceFileHolder.FullName, Path.ChangeExtension(saveContainer.CheckFile, _FacesHiddenFilenameExtension)); else if (saveContainer.FacePartsFileHolder is not null && saveContainer.FacePartsFileHolder.Exists) File.Copy(saveContainer.FacePartsFileHolder.FullName, Path.ChangeExtension(saveContainer.CheckFile, _FacePartsFilenameExtension)); } foreach (SaveContainer saveContainer in saveContainers) { if (string.IsNullOrEmpty(saveContainer.Directory) || string.IsNullOrEmpty(saveContainer.CheckFile) || saveContainer.ResizedFileHolder is not null || saveContainer.FaceFileHolder is not null || string.IsNullOrEmpty(saveContainer.Json)) continue; if (File.Exists(saveContainer.CheckFile)) continue; _ = IPath.WriteAllText(saveContainer.CheckFile, saveContainer.Json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null); } foreach (SaveContainer saveContainer in saveContainers) { if (string.IsNullOrEmpty(saveContainer.Directory) || string.IsNullOrEmpty(saveContainer.CheckFile) || saveContainer.ResizedFileHolder is null) continue; if (string.IsNullOrEmpty(saveContainer.ShortcutFile) || !saveContainer.ResizedFileHolder.Exists) continue; try { windowsShortcut = new() { Path = saveContainer.ResizedFileHolder.FullName }; windowsShortcut.Save(saveContainer.ShortcutFile); windowsShortcut.Dispose(); } catch (Exception) { } } } private void SaveNotMappedTicks() { string directory; string personKeyFormatted; SaveContainer saveContainer; PersonBirthday personBirthday; List saveContainers = new(); const string facePopulatedKey = nameof(Sorting); foreach (long personKey in _NotMappedPersonKeys) { personBirthday = IPersonBirthday.GetPersonBirthday(personKey); personKeyFormatted = IPersonBirthday.GetFormatted(personBirthday); directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"{facePopulatedKey}NotMapped", personKeyFormatted, Property.Models.Stateless.IResult.AllInOne); saveContainer = new(directory); saveContainers.Add(saveContainer); } SaveContainers(saveContainers); } public List<(Item, (long?, Face?, (string, string, string, string))[])> GetCollection(Item[] filteredItems) { List<(Item, (long?, Face?, (string, string, string, string))[])> results = new(); int years; Face face; Item item; long? personKey; string directory; bool? isWrongYear; const int zero = 0; TimeSpan? timeSpan; string copyFileName; string copyDirectory; string? relativePath; string isWrongYearFlag; string shortcutFileName; string subDirectoryName; DateTime minimumDateTime; List indices = new(); List faceCollection; string personKeyFormatted; PersonBirthday personBirthday; PersonBirthday[] personBirthdays; DateTime dateTime = DateTime.Now; List<(long?, Face?, (string, string, string, string))> collection; for (int i = 0; i < filteredItems.Length; i++) { indices.Clear(); copyFileName = string.Empty; copyDirectory = string.Empty; item = filteredItems[i]; if (item.ImageFileHolder is null) continue; relativePath = Path.GetDirectoryName($"C:{item.RelativePath}"); if (string.IsNullOrEmpty(relativePath) || relativePath.Length < 3) continue; 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(); personKey = null; directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"Unnamed{relativePath[2..]}"); } else { faceCollection = item.Faces; personBirthdays = _DeterministicHashCodeUnknownFaceKeyValuePairs[item.Property.Id.Value]; minimumDateTime = IProperty.GetMinimumDateTime(item.Property); (isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime); isWrongYearFlag = IItem.GetWrongYearFlag(isWrongYear); subDirectoryName = $"{isWrongYearFlag}{minimumDateTime:yyyy}"; if (!faceCollection.Any()) { personKey = null; directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"None{relativePath[2..]}", subDirectoryName); } else if (personBirthdays.Length != 1) { personKey = null; directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"Not Supported{relativePath[2..]}", subDirectoryName); } else if (faceCollection.Count != 1) { personKey = null; directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"Many{relativePath[2..]}", subDirectoryName); } else { indices.Add(zero); personBirthday = personBirthdays[zero]; personKey = personBirthday.Value.Ticks; timeSpan = IPersonBirthday.GetTimeSpan(minimumDateTime, isWrongYear, personBirthday); if (timeSpan.HasValue) { if (timeSpan.Value.Ticks < 0) subDirectoryName = "!---"; else { (years, _) = IPersonBirthday.GetAge(dateTime, personBirthday); subDirectoryName = $"^{years:000}"; } } face = faceCollection[zero]; personKeyFormatted = 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", personKeyFormatted, subDirectoryName); else 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 (personKey is null || !indices.Any()) collection.Add(new(personKey, null, (directory, copyDirectory, copyFileName, shortcutFileName))); else { foreach (int index in indices) collection.Add(new(personKey, faceCollection[index], (directory, copyDirectory, copyFileName, shortcutFileName))); } results.Add(new(item, collection.ToArray())); } return results; } public void SavePropertyHolders(Item[] items) { string json; FileInfo fileInfo; bool updateDateWhenMatches = false; JsonSerializerOptions jsonSerializerOptions = new() { WriteIndented = true }; foreach (Item item in items) { if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null) continue; if (!item.Faces.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; _ = IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches, compareBeforeWrite: true); } } private static Dictionary> Convert(List<(int Id, Face Face)> collection) { Dictionary> results = new(); Dictionary keyValuePairs; foreach ((int id, Face face) in collection) { if (!results.ContainsKey(id)) results.Add(id, new()); if (face.Location?.NormalizedPixelPercentage is null) throw new NotSupportedException(); keyValuePairs = results[id]; if (keyValuePairs.ContainsKey(face.Location.NormalizedPixelPercentage.Value)) continue; results[id].Add(face.Location.NormalizedPixelPercentage.Value, face); } return results; } private Dictionary> GetUsedIdThenNormalizedPixelPercentageKeyValuePairs() { Dictionary> results = new(); foreach (KeyValuePair> keyValuePair in _IdThenNormalizedPixelPercentageKeyValuePairs) results.Add(keyValuePair.Key, (from l in keyValuePair.Value select l.Key).ToList()); return results; } private int UpdateFromSortingContainers(SortingContainer[] sortingContainers) { int result = 0; Dictionary> results = new(); string key; long personKey; const int by = 3; const int zero = 0; HashSet hashSet; string mappingSegmentB; string personKeyFormatted; List checkCollection; PersonBirthday personBirthday; PersonBirthday[] personBirthdays; Dictionary keyValuePairs; Dictionary> checkKeyValuePairs = new(); int totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - _Ticks).TotalSeconds); (string DisplayDirectoryName, int? ApproximateYears, PersonBirthday[] PersonBirthdays, long PersonKey) person; string message = $") {sortingContainers.Length:000} Update From Sorting Container(s) - {totalSeconds} total second(s)"; ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = false }; foreach (KeyValuePair> keyValuePair in _IdThenNormalizedPixelPercentageKeyValuePairs) results.Add(keyValuePair.Key, (from l in keyValuePair.Value select l.Key).ToHashSet()); using ProgressBar progressBar = new(sortingContainers.Length, message, options); foreach (SortingContainer sortingContainer in sortingContainers) { if (sortingContainer.Face.Mapping is null) throw new NotSupportedException(); if (!_IdThenNormalizedPixelPercentageKeyValuePairs.ContainsKey(sortingContainer.Sorting.Id)) throw new NotSupportedException(); if (!results.ContainsKey(sortingContainer.Face.Mapping.MappingFromItem.Id)) results.Add(sortingContainer.Face.Mapping.MappingFromItem.Id, new()); hashSet = results[sortingContainer.Face.Mapping.MappingFromItem.Id]; if (hashSet.Contains(sortingContainer.Face.Mapping.MappingFromLocation.NormalizedPixelPercentage)) continue; keyValuePairs = _IdThenNormalizedPixelPercentageKeyValuePairs[sortingContainer.Sorting.Id]; if (!keyValuePairs.ContainsKey(sortingContainer.Sorting.NormalizedPixelPercentage)) throw new NotSupportedException(); personBirthdays = keyValuePairs[sortingContainer.Sorting.NormalizedPixelPercentage]; if (sortingContainer.Sorting.DistancePermyriad > Shared.Models.Stateless.IFaceDistance.Permyriad || sortingContainer.Sorting.DaysDelta > Shared.Models.Stateless.ISorting.DaysDeltaTolerance) continue; for (int i = 0; i < personBirthdays.Length; i++) { personKey = personBirthdays[i].Value.Ticks; if (!_PeopleKeyValuePairs.ContainsKey(personKey)) continue; person = _PeopleKeyValuePairs[personKey]; personBirthday = person.PersonBirthdays[zero]; personKeyFormatted = IPersonBirthday.GetFormatted(personBirthday); mappingSegmentB = GetMappingSegmentB(_Ticks, personBirthday, person.ApproximateYears, sortingContainer.Face.Mapping.MappingFromItem); key = string.Concat(personKeyFormatted, '\t', mappingSegmentB); if (!checkKeyValuePairs.ContainsKey(key)) checkKeyValuePairs.Add(key, new()); checkCollection = checkKeyValuePairs[key]; if (checkCollection.Count > Shared.Models.Stateless.ISorting.MaximumPerKey) continue; _ = hashSet.Add(sortingContainer.Sorting.NormalizedPixelPercentage); sortingContainer.Face.Mapping.UpdateMappingFromPerson(person.ApproximateYears, by, person.DisplayDirectoryName, personBirthday, mappingSegmentB); checkCollection.Add(sortingContainer.Face.Mapping); result += 1; break; } } return result; } internal void ForceSingleImage(IEnumerable distinctFilteredFaces) { int by = 4; long? personKey; const int zero = 0; string mappingSegmentB; int? approximateYears = null; PersonBirthday personBirthday; Face[] orderedDistinctFilteredFaces = (from l in distinctFilteredFaces orderby l.Mapping is not null, l.Mapping?.MappingFromLocation.Confidence descending select l).ToArray(); const string displayDirectoryName = Property.Models.Stateless.IResult.AllInOne; foreach (Face face in orderedDistinctFilteredFaces) { if (face.Mapping is null) throw new NotSupportedException(); if (face.Mapping.MappingFromPerson.PersonBirthday is not null) continue; personKey = _NotMappedPersonKeys[zero]; personBirthday = IPersonBirthday.GetPersonBirthday(personKey.Value); mappingSegmentB = GetMappingSegmentB(_Ticks, personBirthday, approximateYears, face.Mapping.MappingFromItem); face.Mapping.UpdateMappingFromPerson(approximateYears, by, displayDirectoryName, personBirthday, mappingSegmentB); break; } } private List GetMappingSaveContainers(string dFacesContentDirectory, string d2ResultsFullGroupDirectory, List filteredFaces) { List results = new(); string by; string json; string checkFile; string directory; string shortcutFile; string facesDirectory; string? directoryName; string personDirectory; FileHolder faceFileHolder; string facePartsDirectory; string personKeyFormatted; SaveContainer saveContainer; FileHolder facePartsFileHolder; FileHolder hiddenFaceFileHolder; foreach (Face face in filteredFaces) { if (face.Mapping is null) throw new NotSupportedException(); directoryName = Path.GetDirectoryName(face.RelativePath); if (directoryName is null) throw new NotSupportedException(); if (face.Mapping.MappingFromPerson.PersonBirthday is null) continue; if (string.IsNullOrEmpty(face.Mapping.MappingFromPerson.SegmentB)) throw new NotSupportedException(); personKeyFormatted = IPersonBirthday.GetFormatted(face.Mapping.MappingFromPerson.PersonBirthday); if (face.Mapping.MappingFromPerson.By is null) by = $"{nameof(Mapping)}Null"; else by = face.Mapping.MappingFromPerson.By.Value switch { 1 => "ByUnknownFaceKey", 2 => nameof(Mapping), 3 => nameof(Sorting), 4 => nameof(MapLogic.ForceSingleImage), _ => throw new NotImplementedException() }; if (face.Mapping.MappingFromPerson.By is not null and not 2) { directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, by, personKeyFormatted, face.Mapping.MappingFromPerson.SegmentB); personDirectory = Path.Combine(directory, face.Mapping.MappingFromPerson.DisplayDirectoryName, "lnk"); } else { directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, by, personKeyFormatted, face.Mapping.MappingFromPerson.SegmentB); personDirectory = Path.Combine(directory, face.Mapping.MappingFromPerson.DisplayDirectoryName[..1], "lnk"); if (face.Mapping.MappingFromPerson.By == 2 && !Shared.Models.Stateless.IMapping.SaveMapped) continue; } saveContainer = new(personDirectory); results.Add(saveContainer); facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, face.Mapping.MappingFromItem.ImageFileHolder.NameWithoutExtension)); facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, face.Mapping.MappingFromItem.ImageFileHolder.NameWithoutExtension)); checkFile = Path.Combine(directory, $"{face.Mapping.MappingFromLocation.DeterministicHashCodeKeyDisplay}{face.Mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}"); faceFileHolder = new(Path.Combine(facesDirectory, $"{face.Mapping.MappingFromLocation.DeterministicHashCodeKeyDisplay}{face.Mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}")); hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{face.Mapping.MappingFromLocation.DeterministicHashCodeKeyDisplay}{face.Mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}")); facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{face.Mapping.MappingFromLocation.DeterministicHashCodeKeyDisplay}{face.Mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}")); if (string.IsNullOrEmpty(personDirectory)) shortcutFile = string.Empty; else shortcutFile = Path.Combine(personDirectory, $"{face.Mapping.MappingFromLocation.DeterministicHashCodeKeyDisplay}{face.Mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}.lnk"); saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, face.Mapping.MappingFromItem.ResizedFileHolder, shortcutFile); results.Add(saveContainer); if (!string.IsNullOrEmpty(checkFile) && Shared.Models.Stateless.IMapping.SaveFaceEncoding) { checkFile = Path.Combine(directory, $"{face.Mapping.MappingFromLocation.DeterministicHashCodeKeyDisplay}{face.Mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}.json"); json = JsonSerializer.Serialize(face.FaceEncoding); saveContainer = new(checkFile, directory, json); results.Add(saveContainer); } results.Add(saveContainer); } return results; } public void CommonWork(int maxDegreeOfParallelism, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, List distinctFilteredFaces, SortingContainer[] sortingContainers) { List saveContainers; if (_DeterministicHashCodeUnknownFaceKeyValuePairs.Any()) UseKeyValuePairsSaveFaceEncoding(maxDegreeOfParallelism, sortingContainers); int totalNotMapped = AddToMapping(distinctFilteredFaces); if (totalNotMapped == 0) saveContainers = new(); else if (!sortingContainers.Any()) { ForceSingleImage(distinctFilteredFaces); saveContainers = GetMappingSaveContainers(dFacesContentDirectory, d2ResultsFullGroupDirectory, distinctFilteredFaces); } else { int updated = UpdateFromSortingContainers(sortingContainers); if (totalNotMapped - updated > 0) ForceSingleImage(distinctFilteredFaces); saveContainers = GetMappingSaveContainers(dFacesContentDirectory, d2ResultsFullGroupDirectory, distinctFilteredFaces); } SaveContainers(saveContainers); for (int i = 1; i < 5; i++) _ = IPath.DeleteEmptyDirectories(_ZPropertyHolderContentTicksDirectory); _ = IPath.DeleteEmptyDirectories(_ZPropertyHolderContentTicksDirectory); SaveNotMappedTicks(); } private static double GetStandardDeviation(IEnumerable values, double average) { double result = 0; if (!values.Any()) throw new Exception("Collection must have at least one value!"); double sum = values.Sum(l => (l - average) * (l - average)); result = Math.Sqrt(sum / values.Count()); return result; } private void SetPersonKeysRanges(Dictionary> personTicks) { long lcl; long ucl; long maximum; long minimum; double average; long[] collection; double standardDeviation; foreach (KeyValuePair> keyValuePair in personTicks) { minimum = keyValuePair.Value.Min(); if (keyValuePair.Value.Count < 3) { maximum = keyValuePair.Value.Max(); _PersonKeysRanges.Add(keyValuePair.Key, new(new DateTime(minimum).AddYears(-1).Ticks, minimum, maximum, new DateTime(maximum).AddYears(1).Ticks)); } else { collection = (from l in keyValuePair.Value select l - minimum).ToArray(); maximum = collection.Max() + minimum; average = (collection.Sum() / collection.Length) + minimum; standardDeviation = GetStandardDeviation(collection, average); ucl = (long)(average + (standardDeviation * Stateless.IMapLogic.Sigma)); lcl = (long)(average - (standardDeviation * Stateless.IMapLogic.Sigma)); if (lcl < 0) lcl = 0; if (ucl > 0) ucl = _Ticks; _PersonKeysRanges.Add(keyValuePair.Key, new(lcl, minimum, maximum, ucl)); } } } public void SetPersonTicks(string outputResolution, Face[] filteredFaces) { PersonBirthday[] personBirthdays; Dictionary keyValuePairs; Dictionary> personTicks = new(); int totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - _Ticks).TotalSeconds); string message = $") {filteredFaces.Length:000} Set Person Ticks - {totalSeconds} total second(s) - {outputResolution}"; ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true }; using ProgressBar progressBar = new(filteredFaces.Length, message, options); foreach (Face face in filteredFaces) { progressBar.Tick(); if (face.Mapping is null) throw new NotSupportedException(); if (!_IdThenNormalizedPixelPercentageKeyValuePairs.ContainsKey(face.Mapping.MappingFromItem.Id)) continue; keyValuePairs = _IdThenNormalizedPixelPercentageKeyValuePairs[face.Mapping.MappingFromItem.Id]; if (!keyValuePairs.ContainsKey(face.Mapping.MappingFromLocation.NormalizedPixelPercentage)) continue; personBirthdays = keyValuePairs[face.Mapping.MappingFromLocation.NormalizedPixelPercentage]; foreach (PersonBirthday personBirthday in personBirthdays) { if (!personTicks.ContainsKey(personBirthday.Value.Ticks)) personTicks.Add(personBirthday.Value.Ticks, new()); personTicks[personBirthday.Value.Ticks].Add(face.Mapping.MappingFromItem.MinimumDateTime.Ticks); } } SetPersonKeysRanges(personTicks); } }