664 lines
39 KiB
C#
664 lines
39 KiB
C#
using ShellProgressBar;
|
|
using System.Text.Json;
|
|
using View_by_Distance.Shared.Models;
|
|
using View_by_Distance.Shared.Models.Stateless.Methods;
|
|
|
|
namespace View_by_Distance.Map.Models.Stateless;
|
|
|
|
internal abstract class MapLogic
|
|
{
|
|
|
|
private static List<PersonContainer> AddToPersonKeysThenGetNonSpecificPeopleCollection(Property.Models.Configuration propertyConfiguration, Configuration configuration, List<long> personKeys)
|
|
{
|
|
List<PersonContainer> results = new();
|
|
Person person;
|
|
long personKey;
|
|
int? approximateYears = null;
|
|
PersonBirthday personBirthday;
|
|
PersonContainer personContainer;
|
|
string[] personDisplayDirectoryAllFiles = Array.Empty<string>();
|
|
DateTime incrementDate = new(configuration.PersonBirthdayFirstYear, 1, 1);
|
|
for (int i = 0; i < 500; i++)
|
|
{
|
|
personKey = incrementDate.Ticks;
|
|
incrementDate = incrementDate.AddDays(1);
|
|
if (personKeys.Contains(personKey))
|
|
continue;
|
|
personKeys.Add(personKey);
|
|
personBirthday = IPersonBirthday.GetPersonBirthday(personKey);
|
|
person = IPerson.GetPerson(propertyConfiguration.ResultAllInOne, personKey, personBirthday);
|
|
personContainer = new(approximateYears, person, new PersonBirthday[] { personBirthday }, personDisplayDirectoryAllFiles, propertyConfiguration.ResultAllInOne, personKey);
|
|
results.Add(personContainer);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private static void SetPersonCollections(Configuration configuration, string resizeFilenameExtension, List<PersonContainer> personContainers, List<long> personKeys, Dictionary<string, string> personKeyFormattedToNewestPersonKeyFormatted, Dictionary<int, List<int>> skipCollection)
|
|
{
|
|
int? id;
|
|
long personKey;
|
|
string personKeyFormatted;
|
|
int? normalizedPixelPercentage;
|
|
string newestPersonKeyFormatted;
|
|
foreach (PersonContainer personContainer in personContainers)
|
|
{
|
|
foreach (string personDisplayDirectoryAllFile in personContainer.PersonDisplayDirectoryAllFiles)
|
|
{
|
|
if (Path.GetExtension(personDisplayDirectoryAllFile) != resizeFilenameExtension)
|
|
continue;
|
|
(id, normalizedPixelPercentage, _) = IMapping.GetReversedDeterministicHashCodeKey(configuration.LocationDigits, personDisplayDirectoryAllFile);
|
|
if (id is null || normalizedPixelPercentage is null)
|
|
continue;
|
|
if (!skipCollection.ContainsKey(id.Value))
|
|
skipCollection.Add(id.Value, new());
|
|
skipCollection[id.Value].Add(normalizedPixelPercentage.Value);
|
|
}
|
|
if (personContainer.Person is null || personContainer.PersonKey is null || personContainer.PersonBirthdays is null || !personContainer.PersonBirthdays.Any())
|
|
continue;
|
|
foreach (PersonBirthday personBirthday in personContainer.PersonBirthdays)
|
|
{
|
|
personKey = personBirthday.Value.Ticks;
|
|
personKeys.Add(personKey);
|
|
}
|
|
if (personContainer.PersonBirthdays.Length > 1)
|
|
{
|
|
foreach (PersonBirthday personBirthday in personContainer.PersonBirthdays)
|
|
{
|
|
personKeyFormatted = IPersonBirthday.GetFormatted(configuration.PersonBirthdayFormat, personBirthday);
|
|
newestPersonKeyFormatted = IPersonBirthday.GetFormatted(configuration.PersonBirthdayFormat, personContainer.PersonKey.Value);
|
|
if (!personKeyFormattedToNewestPersonKeyFormatted.ContainsKey(personKeyFormatted))
|
|
personKeyFormattedToNewestPersonKeyFormatted.Add(personKeyFormatted, newestPersonKeyFormatted);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
internal static List<(string, char, string, int?, int?, List<Face>?)> DeleteEmptyDirectoriesAndGetCollection(Configuration configuration, long ticks, string eDistanceContentDirectory, Dictionary<int, List<Face>> keyValuePairs)
|
|
{
|
|
List<(string, char, string, int?, int?, List<Face>?)> results = new();
|
|
int? id;
|
|
bool check;
|
|
string[] files;
|
|
List<Face>? faces;
|
|
const int zero = 0;
|
|
List<int> checks = new();
|
|
string[] yearDirectories;
|
|
string personKeyFormatted;
|
|
string ticksDirectoryName;
|
|
string? personFirstInitial;
|
|
DirectoryInfo directoryInfo;
|
|
string[] personKeyDirectories;
|
|
int? normalizedPixelPercentage;
|
|
string[] personNameDirectories;
|
|
string[] personNameLinkDirectories;
|
|
string? personFirstInitialDirectory;
|
|
bool keyValuePairsAny = keyValuePairs.Any();
|
|
int totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
|
|
string[] ticksDirectories = Directory.GetDirectories(eDistanceContentDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
string message = $") {ticksDirectories.Length:000} compile from and clean ticks Director(ies) - A - {totalSeconds} total second(s)";
|
|
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
|
using ProgressBar progressBar = new(ticksDirectories.Length, message, options);
|
|
foreach (string ticksDirectory in ticksDirectories)
|
|
{
|
|
progressBar.Tick();
|
|
ticksDirectoryName = Path.GetFileName(ticksDirectory);
|
|
if (ticksDirectoryName.Length < 3 || ticksDirectoryName[zero] != '(' || ticksDirectoryName[^1] != ')')
|
|
continue;
|
|
if (!long.TryParse(ticksDirectoryName[1..^1], out long directoryTicks))
|
|
{
|
|
if (!long.TryParse(ticksDirectoryName[1..^4], out directoryTicks))
|
|
continue;
|
|
}
|
|
directoryInfo = new(ticksDirectory);
|
|
if (directoryInfo.CreationTime.Ticks != directoryTicks)
|
|
Directory.SetCreationTime(ticksDirectory, new DateTime(directoryTicks));
|
|
if (directoryInfo.LastWriteTime.Ticks != directoryTicks)
|
|
Directory.SetLastWriteTime(ticksDirectory, new DateTime(directoryTicks));
|
|
personKeyDirectories = Directory.GetDirectories(ticksDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
foreach (string personKeyDirectory in personKeyDirectories)
|
|
{
|
|
personKeyFormatted = Path.GetFileName(personKeyDirectory);
|
|
if (personKeyFormatted.Length != configuration.PersonBirthdayFormat.Length)
|
|
continue;
|
|
yearDirectories = Directory.GetDirectories(personKeyDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
foreach (string yearDirectory in yearDirectories)
|
|
{
|
|
files = Directory.GetFiles(yearDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
personNameDirectories = Directory.GetDirectories(yearDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
foreach (string file in files)
|
|
File.Delete(file);
|
|
foreach (string personNameDirectory in personNameDirectories)
|
|
{
|
|
personFirstInitial = Path.GetFileName(personNameDirectory)[..1];
|
|
if (personFirstInitial is null)
|
|
continue;
|
|
personFirstInitialDirectory = Path.Combine(yearDirectory, personFirstInitial.ToString());
|
|
if (personNameDirectory != personFirstInitialDirectory)
|
|
Directory.Move(personNameDirectory, personFirstInitialDirectory);
|
|
files = Directory.GetFiles(personFirstInitialDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
if (personKeyFormatted == nameof(Models.MapLogic.ForceSingleImage) && files.Any())
|
|
throw new Exception($"Move personKey directories up one from {nameof(Models.MapLogic.ForceSingleImage)} and delete {nameof(Models.MapLogic.ForceSingleImage)} directory!");
|
|
if (personKeyFormatted == nameof(Sorting) && files.Any())
|
|
throw new Exception($"Move personKey directories up one from {nameof(Sorting)} and delete {nameof(Sorting)} directory!");
|
|
foreach (string file in files)
|
|
{
|
|
if (file.EndsWith(".lnk") || file.EndsWith(".json"))
|
|
continue;
|
|
(id, normalizedPixelPercentage, faces) = IMapping.GetReversedDeterministicHashCodeKey(configuration.LocationDigits, keyValuePairsAny, keyValuePairs, file);
|
|
if (id is null || normalizedPixelPercentage is null)
|
|
continue;
|
|
if (configuration.MappingMoveUnableToMatch)
|
|
{
|
|
if (faces is null)
|
|
check = false;
|
|
else
|
|
{
|
|
check = false;
|
|
checks.Clear();
|
|
foreach (Face face in faces)
|
|
{
|
|
if (face.Mapping is null)
|
|
throw new NotSupportedException();
|
|
checks.Add(face.Mapping.MappingFromLocation.NormalizedPixelPercentage);
|
|
if (normalizedPixelPercentage.Value != face.Mapping.MappingFromLocation.NormalizedPixelPercentage)
|
|
continue;
|
|
check = true;
|
|
}
|
|
if (!check)
|
|
checks.Add(normalizedPixelPercentage.Value);
|
|
}
|
|
}
|
|
results.Add(new(personKeyFormatted, personFirstInitial[0], file, id, normalizedPixelPercentage, faces));
|
|
}
|
|
personNameLinkDirectories = Directory.GetDirectories(personFirstInitialDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
foreach (string personNameLinkDirectory in personNameLinkDirectories)
|
|
{
|
|
files = Directory.GetFiles(personNameLinkDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
foreach (string file in files)
|
|
{
|
|
if (!file.EndsWith(".lnk"))
|
|
continue;
|
|
File.Delete(file);
|
|
}
|
|
_ = IPath.DeleteEmptyDirectories(personNameLinkDirectory);
|
|
}
|
|
_ = IPath.DeleteEmptyDirectories(personFirstInitialDirectory);
|
|
}
|
|
_ = IPath.DeleteEmptyDirectories(yearDirectory);
|
|
}
|
|
_ = IPath.DeleteEmptyDirectories(personKeyDirectory);
|
|
}
|
|
_ = IPath.DeleteEmptyDirectories(ticksDirectory);
|
|
_ = IPath.DeleteEmptyDirectories(ticksDirectory);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private static PersonContainer[] GetDistinctPersonContainers(List<PersonContainer> personContainers)
|
|
{
|
|
List<PersonContainer> results = new();
|
|
List<long> distinctCheck = new();
|
|
foreach (PersonContainer personContainer in personContainers)
|
|
{
|
|
if (personContainer.PersonKey is null || distinctCheck.Contains(personContainer.PersonKey.Value))
|
|
continue;
|
|
results.Add(personContainer);
|
|
}
|
|
return results.ToArray();
|
|
}
|
|
|
|
private static void SetKeyValuePairs(Configuration configuration, long ticks, List<PersonContainer> personContainers, List<Face> distinctFilteredFaces, List<(string, int, int)> personKeyFormattedIdThenNormalizedPixelPercentageCollection, List<(string, int, int)> incorrectPersonKeyFormattedIdThenNormalizedPixelPercentageCollection, Dictionary<long, PersonContainer> personKeyToPersonContainer, Dictionary<int, Dictionary<int, PersonContainer[]>> idThenNormalizedPixelPercentageToPersonContainers, Dictionary<int, Dictionary<int, PersonContainer[]>> incorrectIdThenNormalizedPixelPercentageToPersonContainers, Dictionary<long, (long LCL, long Minimum, long Maximum, long UCL)> personKeyToRanges)
|
|
{
|
|
string check;
|
|
string rightPadded;
|
|
PersonBirthday? personBirthday;
|
|
string newestPersonKeyFormatted;
|
|
PersonContainer[] distinctPersonContainers;
|
|
int normalizedPixelPercentageInDecimalForm;
|
|
Dictionary<string, PersonContainer> personKeyFormattedToPersonContainer = new();
|
|
Dictionary<int, Dictionary<int, List<PersonContainer>>> idThenNormalizedPixelPercentageToPersonContainerCollection = new();
|
|
Dictionary<int, Dictionary<int, List<PersonContainer>>> incorrectIdThenNormalizedPixelPercentageToPersonContainerCollection = new();
|
|
foreach (PersonContainer personContainer in personContainers)
|
|
{
|
|
if (personContainer.PersonKey is null)
|
|
continue;
|
|
if (!personKeyToPersonContainer.ContainsKey(personContainer.PersonKey.Value))
|
|
personKeyToPersonContainer.Add(personContainer.PersonKey.Value, personContainer);
|
|
newestPersonKeyFormatted = IPersonBirthday.GetFormatted(configuration.PersonBirthdayFormat, personContainer.PersonKey.Value);
|
|
if (!personKeyFormattedToPersonContainer.ContainsKey(newestPersonKeyFormatted))
|
|
personKeyFormattedToPersonContainer.Add(newestPersonKeyFormatted, personContainer);
|
|
}
|
|
foreach ((string personKeyFormatted, int id, int normalizedPixelPercentage) in personKeyFormattedIdThenNormalizedPixelPercentageCollection)
|
|
{
|
|
personBirthday = IPersonBirthday.GetPersonBirthday(configuration.PersonBirthdayFormat, personKeyFormatted);
|
|
if (personBirthday is null)
|
|
continue;
|
|
if (!idThenNormalizedPixelPercentageToPersonContainerCollection.ContainsKey(id))
|
|
idThenNormalizedPixelPercentageToPersonContainerCollection.Add(id, new());
|
|
check = normalizedPixelPercentage.ToString();
|
|
if (check.Length == configuration.LocationDigits)
|
|
{
|
|
if (!idThenNormalizedPixelPercentageToPersonContainerCollection[id].ContainsKey(normalizedPixelPercentage))
|
|
idThenNormalizedPixelPercentageToPersonContainerCollection[id].Add(normalizedPixelPercentage, new());
|
|
if (!personKeyFormattedToPersonContainer.ContainsKey(personKeyFormatted))
|
|
throw new NotSupportedException();
|
|
idThenNormalizedPixelPercentageToPersonContainerCollection[id][normalizedPixelPercentage].Add(personKeyFormattedToPersonContainer[personKeyFormatted]);
|
|
}
|
|
else
|
|
{
|
|
rightPadded = ILocation.GetRightPadded(configuration.LocationDigits, check);
|
|
normalizedPixelPercentageInDecimalForm = int.Parse(rightPadded);
|
|
if (!idThenNormalizedPixelPercentageToPersonContainerCollection[id].ContainsKey(normalizedPixelPercentageInDecimalForm))
|
|
idThenNormalizedPixelPercentageToPersonContainerCollection[id].Add(normalizedPixelPercentageInDecimalForm, new());
|
|
if (!personKeyFormattedToPersonContainer.ContainsKey(personKeyFormatted))
|
|
throw new NotSupportedException();
|
|
idThenNormalizedPixelPercentageToPersonContainerCollection[id][normalizedPixelPercentage].Add(personKeyFormattedToPersonContainer[personKeyFormatted]);
|
|
}
|
|
}
|
|
foreach (KeyValuePair<int, Dictionary<int, List<PersonContainer>>> keyValuePair in idThenNormalizedPixelPercentageToPersonContainerCollection)
|
|
{
|
|
idThenNormalizedPixelPercentageToPersonContainers.Add(keyValuePair.Key, new());
|
|
foreach (KeyValuePair<int, List<PersonContainer>> innerKeyValuePair in keyValuePair.Value)
|
|
{
|
|
distinctPersonContainers = GetDistinctPersonContainers(innerKeyValuePair.Value);
|
|
idThenNormalizedPixelPercentageToPersonContainers[keyValuePair.Key].Add(innerKeyValuePair.Key, distinctPersonContainers);
|
|
}
|
|
};
|
|
SetPersonTicks(ticks, distinctFilteredFaces, personKeyToRanges, idThenNormalizedPixelPercentageToPersonContainers);
|
|
foreach ((string personKeyFormatted, int id, int normalizedPixelPercentage) in incorrectPersonKeyFormattedIdThenNormalizedPixelPercentageCollection)
|
|
{
|
|
personBirthday = IPersonBirthday.GetPersonBirthday(configuration.PersonBirthdayFormat, personKeyFormatted);
|
|
if (personBirthday is null)
|
|
continue;
|
|
if (!incorrectIdThenNormalizedPixelPercentageToPersonContainerCollection.ContainsKey(id))
|
|
incorrectIdThenNormalizedPixelPercentageToPersonContainerCollection.Add(id, new());
|
|
check = normalizedPixelPercentage.ToString();
|
|
if (check.Length == configuration.LocationDigits)
|
|
{
|
|
if (!incorrectIdThenNormalizedPixelPercentageToPersonContainerCollection[id].ContainsKey(normalizedPixelPercentage))
|
|
incorrectIdThenNormalizedPixelPercentageToPersonContainerCollection[id].Add(normalizedPixelPercentage, new());
|
|
if (!personKeyFormattedToPersonContainer.ContainsKey(personKeyFormatted))
|
|
throw new NotSupportedException();
|
|
incorrectIdThenNormalizedPixelPercentageToPersonContainerCollection[id][normalizedPixelPercentage].Add(personKeyFormattedToPersonContainer[personKeyFormatted]);
|
|
}
|
|
else
|
|
{
|
|
rightPadded = ILocation.GetRightPadded(configuration.LocationDigits, check);
|
|
normalizedPixelPercentageInDecimalForm = int.Parse(rightPadded);
|
|
if (!incorrectIdThenNormalizedPixelPercentageToPersonContainerCollection[id].ContainsKey(normalizedPixelPercentageInDecimalForm))
|
|
incorrectIdThenNormalizedPixelPercentageToPersonContainerCollection[id].Add(normalizedPixelPercentageInDecimalForm, new());
|
|
if (!personKeyFormattedToPersonContainer.ContainsKey(personKeyFormatted))
|
|
throw new NotSupportedException();
|
|
incorrectIdThenNormalizedPixelPercentageToPersonContainerCollection[id][normalizedPixelPercentage].Add(personKeyFormattedToPersonContainer[personKeyFormatted]);
|
|
}
|
|
}
|
|
foreach (KeyValuePair<int, Dictionary<int, List<PersonContainer>>> keyValuePair in incorrectIdThenNormalizedPixelPercentageToPersonContainerCollection)
|
|
{
|
|
incorrectIdThenNormalizedPixelPercentageToPersonContainers.Add(keyValuePair.Key, new());
|
|
foreach (KeyValuePair<int, List<PersonContainer>> innerKeyValuePair in keyValuePair.Value)
|
|
{
|
|
distinctPersonContainers = GetDistinctPersonContainers(innerKeyValuePair.Value);
|
|
incorrectIdThenNormalizedPixelPercentageToPersonContainers[keyValuePair.Key].Add(innerKeyValuePair.Key, distinctPersonContainers);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static string? GetCheckFile(string file, int id, int normalizedPixelPercentage)
|
|
{
|
|
string? result;
|
|
string? fileName = Path.GetFileName(file);
|
|
if (fileName is null)
|
|
result = null;
|
|
else
|
|
{
|
|
string[] segments = fileName.Split('.');
|
|
if (segments.Length != 3)
|
|
result = null;
|
|
else
|
|
{
|
|
string extensionLowered = $".{segments[2]}";
|
|
string? directoryName = Path.GetDirectoryName(file);
|
|
if (string.IsNullOrEmpty(directoryName))
|
|
result = null;
|
|
else
|
|
result = Path.Combine(directoryName, $"{IMapping.GetDeterministicHashCodeKey(id, normalizedPixelPercentage)}{extensionLowered}.json");
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static bool Valid(string checkFile, List<Face> faces)
|
|
{
|
|
bool result = false;
|
|
string json;
|
|
foreach (Face face in faces)
|
|
{
|
|
if (face.FaceEncoding is null)
|
|
throw new NotSupportedException();
|
|
if (faces.Count != 1)
|
|
break;
|
|
result = true;
|
|
if (File.Exists(checkFile))
|
|
continue;
|
|
json = JsonSerializer.Serialize(face.FaceEncoding);
|
|
if (IPath.WriteAllText(checkFile, json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null))
|
|
continue;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static void MoveUnableToMatch(Configuration configuration, string eDistanceContentDirectory, string file, string jsonFile)
|
|
{
|
|
bool result;
|
|
string? fileName = Path.GetFileName(file);
|
|
string? jsonFileName = Path.GetFileName(jsonFile);
|
|
if (fileName is null || jsonFileName is null)
|
|
result = false;
|
|
else
|
|
{
|
|
string? directoryName = Path.GetDirectoryName(jsonFile);
|
|
string? jsonDirectoryName = Path.GetDirectoryName(jsonFile);
|
|
if (string.IsNullOrEmpty(directoryName) || string.IsNullOrEmpty(directoryName) || directoryName != jsonDirectoryName || !directoryName.Contains(eDistanceContentDirectory))
|
|
result = false;
|
|
else
|
|
{
|
|
List<string> directoryNames = new();
|
|
string? checkDirectoryName = directoryName;
|
|
for (int i = 0; i < int.MaxValue; i++)
|
|
{
|
|
if (string.IsNullOrEmpty(checkDirectoryName))
|
|
continue;
|
|
directoryNames.Add(Path.GetFileName(checkDirectoryName));
|
|
checkDirectoryName = Path.GetDirectoryName(checkDirectoryName);
|
|
if (string.IsNullOrEmpty(checkDirectoryName))
|
|
continue;
|
|
if (checkDirectoryName == eDistanceContentDirectory)
|
|
break;
|
|
}
|
|
if (string.IsNullOrEmpty(checkDirectoryName) || !directoryNames.Any() || !long.TryParse(directoryNames[^1][1..^1], out long directoryTicks))
|
|
result = false;
|
|
else
|
|
{
|
|
bool jsonFileExists = File.Exists(jsonFile);
|
|
if (!jsonFileExists)
|
|
checkDirectoryName = Path.Combine(checkDirectoryName, $"({directoryTicks}.00)");
|
|
else
|
|
checkDirectoryName = Path.Combine(checkDirectoryName, $"({directoryTicks}{configuration.FaceDistanceTolerance.ToString()[1..]})");
|
|
for (int i = directoryNames.Count - 1 - 1; i > -1; i--)
|
|
checkDirectoryName = Path.Combine(checkDirectoryName, directoryNames[i]);
|
|
if (!Directory.Exists(checkDirectoryName))
|
|
_ = Directory.CreateDirectory(checkDirectoryName);
|
|
File.Move(file, Path.Combine(checkDirectoryName, fileName));
|
|
if (jsonFileExists)
|
|
File.Move(jsonFile, Path.Combine(checkDirectoryName, jsonFileName));
|
|
result = true;
|
|
}
|
|
}
|
|
}
|
|
if (result)
|
|
{ }
|
|
}
|
|
|
|
private static int SetCollectionsAndGetUnableToMatchCount(Configuration configuration, long ticks, string eDistanceContentDirectory, Shared.Models.Methods.IFaceDistance? distance, Dictionary<string, string> personKeyFormattedToNewestPersonKeyFormatted, List<(string, int, int)> personKeyFormattedIdThenNormalizedPixelPercentageCollection, List<(string, int, int)> incorrectPersonKeyFormattedIdThenNormalizedPixelPercentageCollection, List<(string, char, string, int?, int?, List<Face>?)> collection)
|
|
{
|
|
int result = 0;
|
|
string? checkFile;
|
|
List<Face> checkFaces = new();
|
|
string newestPersonKeyFormatted;
|
|
int totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
|
|
string message = $") {collection.Count:000} join from ticks Director(ies) - B - {totalSeconds} total second(s)";
|
|
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
|
using ProgressBar progressBar = new(collection.Count, message, options);
|
|
foreach ((string personKeyFormatted, char personFirstInitial, string file, int? id, int? normalizedPixelPercentage, List<Face>? faces) in collection)
|
|
{
|
|
progressBar.Tick();
|
|
if (id is null || normalizedPixelPercentage is null)
|
|
continue;
|
|
if (faces is null)
|
|
result++;
|
|
else
|
|
{
|
|
checkFaces.Clear();
|
|
foreach (Face face in faces)
|
|
{
|
|
if (face.Mapping is null)
|
|
throw new NotSupportedException();
|
|
if (normalizedPixelPercentage.Value != face.Mapping.MappingFromLocation.NormalizedPixelPercentage)
|
|
continue;
|
|
checkFaces.Add(face);
|
|
}
|
|
checkFile = GetCheckFile(file, id.Value, normalizedPixelPercentage.Value);
|
|
if (string.IsNullOrEmpty(checkFile))
|
|
{
|
|
result++;
|
|
continue;
|
|
}
|
|
if (checkFaces.Count != 1 && distance is not null && File.Exists(checkFile))
|
|
{
|
|
checkFaces.Clear();
|
|
checkFaces.AddRange(distance.GetMatchingFaces(configuration.FaceDistanceTolerance, checkFile, faces));
|
|
}
|
|
if (!checkFaces.Any())
|
|
{
|
|
result++;
|
|
if (configuration.MappingMoveUnableToMatch)
|
|
MoveUnableToMatch(configuration, eDistanceContentDirectory, file, checkFile);
|
|
continue;
|
|
}
|
|
if (checkFaces.Count != 1)
|
|
{
|
|
result++;
|
|
if (configuration.MappingMoveUnableToMatch)
|
|
MoveUnableToMatch(configuration, eDistanceContentDirectory, file, checkFile);
|
|
continue;
|
|
}
|
|
if (!Valid(checkFile, checkFaces))
|
|
{
|
|
result++;
|
|
if (configuration.MappingMoveUnableToMatch)
|
|
MoveUnableToMatch(configuration, eDistanceContentDirectory, file, checkFile);
|
|
continue;
|
|
}
|
|
}
|
|
if (!personKeyFormattedToNewestPersonKeyFormatted.ContainsKey(personKeyFormatted))
|
|
newestPersonKeyFormatted = personKeyFormatted;
|
|
else
|
|
newestPersonKeyFormatted = personKeyFormattedToNewestPersonKeyFormatted[personKeyFormatted];
|
|
if (personFirstInitial != '!')
|
|
personKeyFormattedIdThenNormalizedPixelPercentageCollection.Add(new(newestPersonKeyFormatted, id.Value, normalizedPixelPercentage.Value));
|
|
else
|
|
incorrectPersonKeyFormattedIdThenNormalizedPixelPercentageCollection.Add(new(newestPersonKeyFormatted, id.Value, normalizedPixelPercentage.Value));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static double GetStandardDeviation(IEnumerable<long> 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 static void SetPersonKeysRanges(long ticks, Dictionary<long, List<long>> personTicks, Dictionary<long, (long LCL, long Minimum, long Maximum, long UCL)> personKeyToRanges)
|
|
{
|
|
long lcl;
|
|
long ucl;
|
|
long maximum;
|
|
long minimum;
|
|
double average;
|
|
long[] collection;
|
|
double standardDeviation;
|
|
foreach (KeyValuePair<long, List<long>> keyValuePair in personTicks)
|
|
{
|
|
minimum = keyValuePair.Value.Min();
|
|
if (keyValuePair.Value.Count < 3)
|
|
{
|
|
maximum = keyValuePair.Value.Max();
|
|
personKeyToRanges.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 * IMapLogic.Sigma));
|
|
lcl = (long)(average - (standardDeviation * IMapLogic.Sigma));
|
|
if (lcl < 0)
|
|
lcl = 0;
|
|
if (ucl > 0)
|
|
ucl = ticks;
|
|
personKeyToRanges.Add(keyValuePair.Key, new(lcl, minimum, maximum, ucl));
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void SetPersonTicks(long ticks, List<Face> distinctFilteredFaces, Dictionary<long, (long LCL, long Minimum, long Maximum, long UCL)> personKeyToRanges, Dictionary<int, Dictionary<int, PersonContainer[]>> idThenNormalizedPixelPercentageToPersonContainers)
|
|
{
|
|
PersonContainer[] personContainers;
|
|
Dictionary<int, PersonContainer[]> keyValuePairs;
|
|
Dictionary<long, List<long>> personTicks = new();
|
|
int totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
|
|
string message = $") {distinctFilteredFaces.Count:000} Set Person Ticks - {totalSeconds} total second(s)";
|
|
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
|
using ProgressBar progressBar = new(distinctFilteredFaces.Count, message, options);
|
|
foreach (Face face in distinctFilteredFaces)
|
|
{
|
|
progressBar.Tick();
|
|
if (face.Mapping is null)
|
|
throw new NotSupportedException();
|
|
if (!idThenNormalizedPixelPercentageToPersonContainers.ContainsKey(face.Mapping.MappingFromItem.Id))
|
|
continue;
|
|
keyValuePairs = idThenNormalizedPixelPercentageToPersonContainers[face.Mapping.MappingFromItem.Id];
|
|
if (!keyValuePairs.ContainsKey(face.Mapping.MappingFromLocation.NormalizedPixelPercentage))
|
|
continue;
|
|
personContainers = keyValuePairs[face.Mapping.MappingFromLocation.NormalizedPixelPercentage];
|
|
foreach (PersonContainer personContainer in personContainers)
|
|
{
|
|
if (personContainer.PersonKey is null)
|
|
continue;
|
|
if (!personTicks.ContainsKey(personContainer.PersonKey.Value))
|
|
personTicks.Add(personContainer.PersonKey.Value, new());
|
|
personTicks[personContainer.PersonKey.Value].Add(face.Mapping.MappingFromItem.MinimumDateTime.Ticks);
|
|
}
|
|
}
|
|
SetPersonKeysRanges(ticks, personTicks, personKeyToRanges);
|
|
}
|
|
|
|
private static List<long> GetNotMappedPersonKeys(Property.Models.Configuration propertyConfiguration, List<PersonContainer> personContainers, long[] personKeyCollection)
|
|
{
|
|
List<long> results = new();
|
|
List<long> notMappedAndNotNamedPersonKeys = new();
|
|
List<long> notMappedAndWithNamedPersonKeys = new();
|
|
foreach (PersonContainer personContainer in personContainers)
|
|
{
|
|
if (personContainer.Person is null || personContainer.PersonKey is null || personContainer.PersonBirthdays is null || !personContainer.PersonBirthdays.Any())
|
|
continue;
|
|
if (personKeyCollection.Contains(personContainer.PersonKey.Value))
|
|
continue;
|
|
else if (string.IsNullOrEmpty(personContainer.PersonDisplayDirectoryName) || personContainer.PersonDisplayDirectoryName == propertyConfiguration.ResultAllInOne)
|
|
notMappedAndNotNamedPersonKeys.Add(personContainer.PersonKey.Value);
|
|
else
|
|
notMappedAndWithNamedPersonKeys.Add(personContainer.PersonKey.Value);
|
|
}
|
|
notMappedAndNotNamedPersonKeys.Sort();
|
|
notMappedAndWithNamedPersonKeys.Sort();
|
|
results.AddRange(notMappedAndNotNamedPersonKeys);
|
|
results.AddRange(notMappedAndWithNamedPersonKeys);
|
|
return results;
|
|
}
|
|
|
|
private static void AppendToSkipCollection(Dictionary<int, List<int>> skipCollection, Dictionary<int, Dictionary<int, PersonContainer[]>> idThenNormalizedPixelPercentageToPersonContainers, Dictionary<int, Dictionary<int, PersonContainer[]>> incorrectIdThenNormalizedPixelPercentageToPersonContainers)
|
|
{
|
|
foreach (KeyValuePair<int, Dictionary<int, PersonContainer[]>> keyValuePair in incorrectIdThenNormalizedPixelPercentageToPersonContainers)
|
|
{
|
|
if (!skipCollection.ContainsKey(keyValuePair.Key))
|
|
skipCollection.Add(keyValuePair.Key, new());
|
|
if (idThenNormalizedPixelPercentageToPersonContainers.ContainsKey(keyValuePair.Key))
|
|
{
|
|
if (idThenNormalizedPixelPercentageToPersonContainers[keyValuePair.Key].ContainsKey(keyValuePair.Value.ElementAt(0).Key))
|
|
continue;
|
|
}
|
|
skipCollection[keyValuePair.Key].AddRange(from l in keyValuePair.Value.Keys select l);
|
|
}
|
|
}
|
|
|
|
private static void SetPersonKeyToPersonContainer(Property.Models.Configuration propertyConfiguration, List<PersonContainer> personContainers, long[] personKeyCollection, Dictionary<long, PersonContainer> personKeyToPersonContainer)
|
|
{
|
|
foreach (PersonContainer personContainer in personContainers)
|
|
{
|
|
if (personContainer.PersonKey is null || !personKeyCollection.Contains(personContainer.PersonKey.Value))
|
|
continue;
|
|
if (personKeyToPersonContainer.ContainsKey(personContainer.PersonKey.Value))
|
|
{
|
|
if (personKeyToPersonContainer[personContainer.PersonKey.Value].PersonDisplayDirectoryName == personContainer.PersonDisplayDirectoryName)
|
|
continue;
|
|
throw new NotImplementedException();
|
|
}
|
|
personKeyToPersonContainer.Add(personContainer.PersonKey.Value, personContainer);
|
|
}
|
|
if (personKeyCollection.Any())
|
|
{
|
|
int? approximateYears = null;
|
|
PersonBirthday? personBirthday;
|
|
PersonContainer personContainer;
|
|
string displayDirectoryName = propertyConfiguration.ResultAllInOne;
|
|
foreach (long personKey in personKeyCollection)
|
|
{
|
|
if (personKeyToPersonContainer.ContainsKey(personKey))
|
|
continue;
|
|
personBirthday = IPersonBirthday.GetPersonBirthday(personKey);
|
|
personContainer = new(approximateYears, personBirthday, displayDirectoryName, personKey);
|
|
personKeyToPersonContainer.Add(personKey, personContainer);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal static void Set(Property.Models.Configuration propertyConfiguration, Configuration? configuration, string resizeFilenameExtension, long ticks, List<PersonContainer> personContainers, string eDistanceContentDirectory, List<Face> distinctFilteredFaces, Shared.Models.Methods.IFaceDistance? distance, Dictionary<long, PersonContainer> personKeyToPersonContainer, Dictionary<long, (long LCL, long Minimum, long Maximum, long UCL)> personKeyToRanges, List<long> notMappedPersonKeys, Dictionary<int, List<int>> skipCollection, Dictionary<int, Dictionary<int, PersonContainer[]>> idThenNormalizedPixelPercentageToPersonContainers)
|
|
{
|
|
if (configuration is null)
|
|
throw new NullReferenceException(nameof(configuration));
|
|
List<long> personKeys = new();
|
|
List<long?> nullablePersonKeyCollection = new();
|
|
Dictionary<int, List<Face>> keyValuePairs = new();
|
|
Dictionary<string, string> personKeyFormattedToNewestPersonKeyFormatted = new();
|
|
List<(string, int, int)> personKeyFormattedIdThenNormalizedPixelPercentageCollection = new();
|
|
List<(string, int, int)> incorrectPersonKeyFormattedIdThenNormalizedPixelPercentageCollection = new();
|
|
Dictionary<int, Dictionary<int, PersonContainer[]>> incorrectIdThenNormalizedPixelPercentageToPersonContainers = new();
|
|
SetPersonCollections(configuration, resizeFilenameExtension, personContainers, personKeys, personKeyFormattedToNewestPersonKeyFormatted, skipCollection);
|
|
personContainers.AddRange(AddToPersonKeysThenGetNonSpecificPeopleCollection(propertyConfiguration, configuration, personKeys));
|
|
foreach (Face face in distinctFilteredFaces)
|
|
{
|
|
if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null)
|
|
throw new NotSupportedException();
|
|
if (face.Mapping is null)
|
|
throw new NotSupportedException();
|
|
if (!keyValuePairs.ContainsKey(face.Mapping.MappingFromItem.Id))
|
|
keyValuePairs.Add(face.Mapping.MappingFromItem.Id, new());
|
|
keyValuePairs[face.Mapping.MappingFromItem.Id].Add(face);
|
|
}
|
|
List<(string, char, string, int?, int?, List<Face>?)> collection = DeleteEmptyDirectoriesAndGetCollection(configuration, ticks, eDistanceContentDirectory, keyValuePairs);
|
|
int unableToMatchCount = SetCollectionsAndGetUnableToMatchCount(configuration, ticks, eDistanceContentDirectory, distance, personKeyFormattedToNewestPersonKeyFormatted, personKeyFormattedIdThenNormalizedPixelPercentageCollection, incorrectPersonKeyFormattedIdThenNormalizedPixelPercentageCollection, collection);
|
|
SetKeyValuePairs(configuration, ticks, personContainers, distinctFilteredFaces, personKeyFormattedIdThenNormalizedPixelPercentageCollection, incorrectPersonKeyFormattedIdThenNormalizedPixelPercentageCollection, personKeyToPersonContainer, idThenNormalizedPixelPercentageToPersonContainers, incorrectIdThenNormalizedPixelPercentageToPersonContainers, personKeyToRanges);
|
|
int totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
|
|
string message = $") {collection.Count:000} message from ticks Director(ies) - C - {unableToMatchCount} Unable To Match Count / {collection.Count} Collection - {totalSeconds} total second(s)";
|
|
ProgressBarOptions options = new() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true };
|
|
using (ProgressBar progressBar = new(collection.Count, message, options))
|
|
{
|
|
foreach (KeyValuePair<int, Dictionary<int, PersonContainer[]>> keyValuePair in idThenNormalizedPixelPercentageToPersonContainers)
|
|
{
|
|
progressBar.Tick();
|
|
foreach (KeyValuePair<int, PersonContainer[]> keyValue in keyValuePair.Value)
|
|
nullablePersonKeyCollection.AddRange(from l in keyValue.Value select l.PersonKey);
|
|
}
|
|
}
|
|
long[] personKeyCollection = (from l in nullablePersonKeyCollection where l is not null select l.Value).Distinct().ToArray();
|
|
notMappedPersonKeys.AddRange(GetNotMappedPersonKeys(propertyConfiguration, personContainers, personKeyCollection));
|
|
SetPersonKeyToPersonContainer(propertyConfiguration, personContainers, personKeyCollection, personKeyToPersonContainer);
|
|
AppendToSkipCollection(skipCollection, idThenNormalizedPixelPercentageToPersonContainers, incorrectIdThenNormalizedPixelPercentageToPersonContainers);
|
|
}
|
|
|
|
} |