756 lines
42 KiB
C#
756 lines
42 KiB
C#
using ShellProgressBar;
|
|
using System.Text.Json;
|
|
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<double> _SkipCollection;
|
|
protected readonly List<long> _NotMappedPersonKeys;
|
|
protected readonly Dictionary<int, int[]> _KeyValuePairs;
|
|
protected readonly Dictionary<int, int[]> _IndicesFromNew;
|
|
protected readonly Dictionary<long, (long LCL, long Minimum, long Maximum, long UCL)> _PersonKeysRanges;
|
|
protected readonly Dictionary<int, Dictionary<int, PersonBirthday[]>> _IdThenNormalizedPixelPercentageKeyValuePairs;
|
|
protected readonly Dictionary<int, Dictionary<int, PersonBirthday[]>> _IncorrectIdThenNormalizedPixelPercentageKeyValuePairs;
|
|
protected readonly Dictionary<long, (string DisplayDirectoryName, int? ApproximateYears, PersonBirthday[] PersonBirthdays, long PersonKey)> _PeopleKeyValuePairs;
|
|
|
|
public Dictionary<int, int[]> KeyValuePairs => _KeyValuePairs;
|
|
public Dictionary<int, int[]> IndicesFromNew => _IndicesFromNew;
|
|
|
|
private readonly long _Ticks;
|
|
private const int _Mapping = 1;
|
|
private const int _Sorting = 2;
|
|
private Configuration? _Configuration;
|
|
private readonly Serilog.ILogger? _Log;
|
|
private const int _ForceSingleImage = 3;
|
|
private readonly int _MaxDegreeOfParallelism;
|
|
private readonly string _FacesFilenameExtension;
|
|
private readonly string _ResizeFilenameExtension;
|
|
private readonly string _FacePartsFilenameExtension;
|
|
private readonly string _FacesHiddenFilenameExtension;
|
|
private readonly string _ZPropertyHolderContentTicksDirectory;
|
|
|
|
public MapLogic(int maxDegreeOfParallelism, Property.Models.Configuration propertyConfiguration, string resizeFilenameExtension, string facesFilenameExtension, string facesHiddenFilenameExtension, string facePartsFilenameExtension, long ticks, Person[] people, string peopleDateGroupDirectory, string zResultsFullGroupDirectory, Configuration? configuration, List<Face> distinctFilteredFaces, Shared.Models.Methods.IFaceDistance? distance)
|
|
{
|
|
_Ticks = ticks;
|
|
_PersonKeysRanges = new();
|
|
_Configuration = configuration;
|
|
_Log = Serilog.Log.ForContext<MapLogic>();
|
|
_FacesFilenameExtension = facesFilenameExtension;
|
|
_MaxDegreeOfParallelism = maxDegreeOfParallelism;
|
|
_ResizeFilenameExtension = resizeFilenameExtension;
|
|
_FacePartsFilenameExtension = facePartsFilenameExtension;
|
|
_FacesHiddenFilenameExtension = facesHiddenFilenameExtension;
|
|
if (_Log is null)
|
|
{ }
|
|
if (propertyConfiguration.VerifyToSeason is null || !propertyConfiguration.VerifyToSeason.Any())
|
|
throw new Exception();
|
|
if (_MaxDegreeOfParallelism == 0)
|
|
{ }
|
|
string json;
|
|
string[] files;
|
|
string fullPath;
|
|
const int zero = 0;
|
|
List<long> notMappedTicks = new();
|
|
List<double> skipCollection = new();
|
|
Dictionary<int, int[]>? keyValuePairs;
|
|
List<KeyValuePair<int, int[]>>? collection;
|
|
Dictionary<int, int[]> indicesFromNew = new();
|
|
Dictionary<long, (string, int?, PersonBirthday[], long)> peopleKeyValuePairs = new();
|
|
string zPropertyHolderContentDirectory = Path.Combine(zResultsFullGroupDirectory, "()");
|
|
string? rootDirectoryParent = Path.GetDirectoryName(propertyConfiguration.RootDirectory);
|
|
Dictionary<int, Dictionary<int, PersonBirthday[]>> idThenNormalizedPixelPercentageKeyValuePairs = new();
|
|
string zPropertyHolderContentTicksDirectory = Path.Combine(zPropertyHolderContentDirectory, $"({ticks})");
|
|
string zPropertyHolderPeopleContentDirectory = Path.Combine(peopleDateGroupDirectory, "()", "(KnownPeople)");
|
|
Dictionary<int, Dictionary<int, PersonBirthday[]>> incorrectIdThenNormalizedPixelPercentageKeyValuePairs = new();
|
|
for (int i = 1; i < 5; i++)
|
|
_ = IPath.DeleteEmptyDirectories(zPropertyHolderContentDirectory);
|
|
_ = IPath.DeleteEmptyDirectories(zPropertyHolderContentDirectory);
|
|
if (string.IsNullOrEmpty(rootDirectoryParent))
|
|
throw new NullReferenceException(nameof(rootDirectoryParent));
|
|
if (!Directory.Exists(zPropertyHolderContentDirectory))
|
|
_ = Directory.CreateDirectory(zPropertyHolderContentDirectory);
|
|
if (!Directory.Exists(zPropertyHolderPeopleContentDirectory))
|
|
_ = Directory.CreateDirectory(zPropertyHolderPeopleContentDirectory);
|
|
Stateless.ByRef.Set(propertyConfiguration, configuration, ticks, _ResizeFilenameExtension, people, zPropertyHolderContentDirectory, zPropertyHolderPeopleContentDirectory, distinctFilteredFaces, distance, skipCollection, peopleKeyValuePairs, notMappedTicks, idThenNormalizedPixelPercentageKeyValuePairs, incorrectIdThenNormalizedPixelPercentageKeyValuePairs);
|
|
if (!Directory.Exists(zPropertyHolderContentTicksDirectory))
|
|
_ = Directory.CreateDirectory(zPropertyHolderContentTicksDirectory);
|
|
files = Directory.GetFiles(rootDirectoryParent, "*keyValuePairs-6*.json", SearchOption.TopDirectoryOnly);
|
|
if (files.Length != 1)
|
|
keyValuePairs = new();
|
|
else
|
|
{
|
|
json = File.ReadAllText(files[zero]);
|
|
keyValuePairs = JsonSerializer.Deserialize<Dictionary<int, int[]>>(json);
|
|
if (keyValuePairs is null)
|
|
throw new NullReferenceException(nameof(keyValuePairs));
|
|
}
|
|
foreach (string propertyContentCollectionFile in propertyConfiguration.PropertyContentCollectionFiles)
|
|
{
|
|
fullPath = Path.GetFullPath(string.Concat(rootDirectoryParent, propertyContentCollectionFile));
|
|
if (fullPath.Contains(propertyConfiguration.RootDirectory))
|
|
continue;
|
|
if (!File.Exists(fullPath))
|
|
continue;
|
|
json = File.ReadAllText(fullPath);
|
|
collection = JsonSerializer.Deserialize<List<KeyValuePair<int, int[]>>>(json);
|
|
if (collection is null)
|
|
throw new NullReferenceException(nameof(collection));
|
|
foreach (KeyValuePair<int, int[]> 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;
|
|
_ZPropertyHolderContentTicksDirectory = zPropertyHolderContentTicksDirectory;
|
|
_IdThenNormalizedPixelPercentageKeyValuePairs = idThenNormalizedPixelPercentageKeyValuePairs;
|
|
_IncorrectIdThenNormalizedPixelPercentageKeyValuePairs = incorrectIdThenNormalizedPixelPercentageKeyValuePairs;
|
|
}
|
|
|
|
public MapLogic(int maxDegreeOfParallelism, Property.Models.Configuration propertyConfiguration, string resizeFilenameExtension, string facesFilenameExtension, string facesHiddenFilenameExtension, string facePartsFilenameExtension, long ticks, Person[] people, string peopleDateGroupDirectory, string zResultsFullGroupDirectory) :
|
|
this(maxDegreeOfParallelism, propertyConfiguration, resizeFilenameExtension, facesFilenameExtension, facesHiddenFilenameExtension, facePartsFilenameExtension, ticks, people, peopleDateGroupDirectory, zResultsFullGroupDirectory, null, new(), null)
|
|
{ }
|
|
|
|
public MapLogic(int maxDegreeOfParallelism, Property.Models.Configuration propertyConfiguration, string outputExtension, long ticks, Person[] people, string peopleDateGroupDirectory, string zResultsFullGroupDirectory) :
|
|
this(maxDegreeOfParallelism, propertyConfiguration, outputExtension, outputExtension, outputExtension, outputExtension, ticks, people, peopleDateGroupDirectory, zResultsFullGroupDirectory, null, new(), null)
|
|
{ }
|
|
|
|
public override string ToString()
|
|
{
|
|
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
|
|
return result;
|
|
}
|
|
|
|
public void Update(Configuration configuration) => _Configuration = configuration;
|
|
|
|
public bool Skip(double deterministicHashCodeKey) => _SkipCollection.Contains(deterministicHashCodeKey);
|
|
|
|
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<Sorting> GetSortingCollection(int i, FaceDistance faceDistanceEncoding, List<FaceDistance> faceDistanceLengths, bool anyLowerThanTolerance)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
List<Sorting> results = new();
|
|
Sorting sorting;
|
|
FaceDistance faceDistanceLength;
|
|
Dictionary<int, PersonBirthday[]> 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(_Configuration.FaceDistancePermyriad, _Configuration.FaceDistanceTolerance, 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, List<Face> distinctFilteredFaces)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
string fileName;
|
|
string fullName;
|
|
string personKeyFormatted;
|
|
PersonBirthday personBirthday;
|
|
WindowsShortcut windowsShortcut;
|
|
(string DisplayDirectoryName, int? ApproximateYears, PersonBirthday[] PersonBirthdays, long PersonKey) person;
|
|
List<(Face, long?, (string, string, string, string))> collection = GetCollection(distinctFilteredFaces);
|
|
foreach ((Face face, long? personKey, (string directory, string copyDirectory, string copyFileName, string shortcutFileName)) in collection)
|
|
{
|
|
if (personKey is null)
|
|
continue;
|
|
if (face.Mapping is null)
|
|
throw new NotSupportedException();
|
|
personBirthday = IPersonBirthday.GetPersonBirthday(personKey.Value);
|
|
personKeyFormatted = IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, personBirthday);
|
|
if (juliePhares.Contains(personKeyFormatted) && !string.IsNullOrEmpty(copyDirectory))
|
|
{
|
|
if (!Directory.Exists(copyDirectory))
|
|
_ = Directory.CreateDirectory(copyDirectory);
|
|
fileName = Path.Combine(copyDirectory, $"{face.Mapping.MappingFromItem.Id}{face.Mapping.MappingFromItem.ResizedFileHolder.ExtensionLowered}");
|
|
if (!File.Exists(fileName))
|
|
File.Copy(face.Mapping.MappingFromItem.ResizedFileHolder.FullName, fileName);
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
fileName = Path.Combine(directory, $"{face.Mapping.MappingFromItem.Id}.lnk");
|
|
if (File.Exists(fileName))
|
|
continue;
|
|
windowsShortcut = new() { Path = face.Mapping.MappingFromItem.ResizedFileHolder.FullName };
|
|
windowsShortcut.Save(fileName);
|
|
windowsShortcut.Dispose();
|
|
if (!File.Exists(fileName))
|
|
continue;
|
|
File.SetLastWriteTime(fileName, face.Mapping.MappingFromItem.MinimumDateTime);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public int AddToMapping(List<Face> distinctFilteredFaces)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
int result = 0;
|
|
long personKey;
|
|
int by = _Mapping;
|
|
const int zero = 0;
|
|
int? approximateYears;
|
|
string mappingSegmentB;
|
|
string personKeyFormatted;
|
|
string displayDirectoryName;
|
|
PersonBirthday personBirthday;
|
|
List<PersonBirthday> personBirthdays = new();
|
|
Dictionary<int, PersonBirthday[]> keyValuePairs;
|
|
(string DisplayDirectoryName, int? ApproximateYears, PersonBirthday[] PersonBirthdays, long PersonKey) person;
|
|
foreach (Face face in distinctFilteredFaces)
|
|
{
|
|
personBirthdays.Clear();
|
|
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(_Configuration.PersonBirthdayFormat, personBirthday);
|
|
mappingSegmentB = GetMappingSegmentB(_Ticks, personBirthday, approximateYears, face.Mapping.MappingFromItem);
|
|
face.Mapping.UpdateMappingFromPerson(approximateYears, by, displayDirectoryName, personBirthday, mappingSegmentB);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private void SaveContainers(List<SaveContainer> 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 = true };
|
|
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)
|
|
{ }
|
|
}
|
|
}
|
|
|
|
public void SaveNotMappedTicks(Property.Models.Configuration propertyConfiguration)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
string directory;
|
|
string personKeyFormatted;
|
|
SaveContainer saveContainer;
|
|
PersonBirthday personBirthday;
|
|
List<SaveContainer> saveContainers = new();
|
|
const string facePopulatedKey = nameof(_Sorting);
|
|
foreach (long personKey in _NotMappedPersonKeys)
|
|
{
|
|
personBirthday = IPersonBirthday.GetPersonBirthday(personKey);
|
|
personKeyFormatted = IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, personBirthday);
|
|
directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"{facePopulatedKey}NotMapped", personKeyFormatted, propertyConfiguration.ResultAllInOne);
|
|
saveContainer = new(directory);
|
|
saveContainers.Add(saveContainer);
|
|
}
|
|
SaveContainers(saveContainers);
|
|
}
|
|
|
|
public List<(Face, long?, (string, string, string, string))> GetCollection(List<Face> distinctFilteredFaces)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
List<(Face, long?, (string, string, string, string))> results = new();
|
|
int years;
|
|
long? personKey;
|
|
string directory;
|
|
const int zero = 0;
|
|
TimeSpan? timeSpan;
|
|
string copyFileName;
|
|
string copyDirectory;
|
|
string? relativePath;
|
|
string isWrongYearFlag;
|
|
string shortcutFileName;
|
|
string subDirectoryName;
|
|
string personKeyFormatted;
|
|
PersonBirthday personBirthday;
|
|
PersonBirthday[] personBirthdays;
|
|
DateTime dateTime = DateTime.Now;
|
|
Dictionary<int, PersonBirthday[]> keyValuePairs;
|
|
foreach (Face face in distinctFilteredFaces)
|
|
{
|
|
if (face.Mapping is null)
|
|
throw new NotSupportedException();
|
|
copyFileName = string.Empty;
|
|
copyDirectory = string.Empty;
|
|
relativePath = Path.GetDirectoryName($"C:{face.RelativePath}");
|
|
if (string.IsNullOrEmpty(relativePath) || relativePath.Length < 3)
|
|
continue;
|
|
if (!_IdThenNormalizedPixelPercentageKeyValuePairs.ContainsKey(face.Mapping.MappingFromItem.Id))
|
|
{
|
|
personKey = null;
|
|
directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"Unnamed{relativePath[2..]}");
|
|
}
|
|
else
|
|
{
|
|
keyValuePairs = _IdThenNormalizedPixelPercentageKeyValuePairs[face.Mapping.MappingFromItem.Id];
|
|
if (!keyValuePairs.ContainsKey(face.Mapping.MappingFromLocation.NormalizedPixelPercentage))
|
|
throw new NotSupportedException();
|
|
personBirthdays = keyValuePairs[face.Mapping.MappingFromLocation.NormalizedPixelPercentage];
|
|
isWrongYearFlag = IItem.GetWrongYearFlag(face.Mapping.MappingFromItem.IsWrongYear);
|
|
subDirectoryName = $"{isWrongYearFlag}{face.Mapping.MappingFromItem.MinimumDateTime:yyyy}";
|
|
if (personBirthdays.Length != 1)
|
|
{
|
|
personKey = null;
|
|
directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, $"Not Supported{relativePath[2..]}", subDirectoryName);
|
|
}
|
|
else
|
|
{
|
|
personBirthday = personBirthdays[zero];
|
|
personKey = personBirthday.Value.Ticks;
|
|
timeSpan = IPersonBirthday.GetTimeSpan(face.Mapping.MappingFromItem.MinimumDateTime, face.Mapping.MappingFromItem.IsWrongYear, personBirthday);
|
|
if (timeSpan.HasValue)
|
|
{
|
|
if (timeSpan.Value.Ticks < 0)
|
|
subDirectoryName = "!---";
|
|
else
|
|
{
|
|
(years, _) = IPersonBirthday.GetAge(dateTime, personBirthday);
|
|
subDirectoryName = $"^{years:000}";
|
|
}
|
|
}
|
|
personKeyFormatted = IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, 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, $"{face.Mapping.MappingFromItem.Id}{face.Mapping.MappingFromItem.ResizedFileHolder.ExtensionLowered}");
|
|
}
|
|
}
|
|
shortcutFileName = Path.Combine(directory, $"{face.Mapping.MappingFromItem.Id}.lnk");
|
|
if (personKey is null)
|
|
results.Add(new(face, null, (directory, copyDirectory, copyFileName, shortcutFileName)));
|
|
else
|
|
results.Add(new(face, personKey, (directory, copyDirectory, copyFileName, shortcutFileName)));
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private int UpdateFromSortingContainers(SortingContainer[] sortingContainers)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
int result = 0;
|
|
Dictionary<int, HashSet<int>> results = new();
|
|
string key;
|
|
long personKey;
|
|
const int zero = 0;
|
|
HashSet<int> hashSet;
|
|
string mappingSegmentB;
|
|
const int by = _Sorting;
|
|
string personKeyFormatted;
|
|
List<Mapping> checkCollection;
|
|
PersonBirthday personBirthday;
|
|
PersonBirthday[] personBirthdays;
|
|
Dictionary<int, PersonBirthday[]> keyValuePairs;
|
|
Dictionary<string, List<Mapping>> 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 = true };
|
|
foreach (KeyValuePair<int, Dictionary<int, PersonBirthday[]>> 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)
|
|
{
|
|
progressBar.Tick();
|
|
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.Face.Mapping.MappingFromLocation.Confidence < _Configuration.FaceDistanceMinimumConfidence || sortingContainer.Sorting.DistancePermyriad > _Configuration.FaceDistancePermyriad || sortingContainer.Sorting.DaysDelta > _Configuration.SortingDaysDeltaTolerance)
|
|
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(_Configuration.PersonBirthdayFormat, 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 > _Configuration.SortingMaximumPerKey)
|
|
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(Property.Models.Configuration propertyConfiguration, IEnumerable<Face> distinctFilteredFaces)
|
|
{
|
|
long? personKey;
|
|
const int zero = 0;
|
|
string mappingSegmentB;
|
|
int by = _ForceSingleImage;
|
|
int? approximateYears = null;
|
|
PersonBirthday personBirthday;
|
|
string displayDirectoryName = propertyConfiguration.ResultAllInOne;
|
|
Face[] orderedDistinctFilteredFaces = (from l in distinctFilteredFaces orderby l.Mapping is not null, l.Mapping?.MappingFromLocation.Confidence descending select l).ToArray();
|
|
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<SaveContainer> GetMappingSaveContainers(string dFacesContentDirectory, string d2ResultsFullGroupDirectory, List<Face> filteredFaces)
|
|
{
|
|
if (_Configuration is null)
|
|
throw new NullReferenceException(nameof(_Configuration));
|
|
List<SaveContainer> 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;
|
|
Dictionary<string, int> keyValuePairs = new();
|
|
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(_Configuration.PersonBirthdayFormat, face.Mapping.MappingFromPerson.PersonBirthday);
|
|
if (face.Mapping.MappingFromPerson.By is null)
|
|
by = $"{nameof(_Mapping)}Null";
|
|
else
|
|
{
|
|
if (face.Mapping.MappingFromPerson.By == _Mapping && !_Configuration.MappingSaveMapped)
|
|
continue;
|
|
by = face.Mapping.MappingFromPerson.By.Value switch
|
|
{
|
|
_Mapping => nameof(_Mapping),
|
|
_Sorting => nameof(_Sorting),
|
|
_ForceSingleImage => nameof(_ForceSingleImage),
|
|
_ => throw new NotImplementedException()
|
|
};
|
|
}
|
|
directory = Path.Combine(_ZPropertyHolderContentTicksDirectory, by[1..], personKeyFormatted, face.Mapping.MappingFromPerson.SegmentB);
|
|
if (!keyValuePairs.ContainsKey(directory))
|
|
keyValuePairs.Add(directory, 0);
|
|
keyValuePairs[directory]++;
|
|
saveContainer = new(Path.Combine(directory, "!"));
|
|
if (face.Mapping.MappingFromPerson.By.HasValue && face.Mapping.MappingFromPerson.By == _Sorting && keyValuePairs[directory] > 3)
|
|
results.Add(saveContainer);
|
|
if (face.Mapping.MappingFromPerson.By is not null)
|
|
personDirectory = Path.Combine(directory, face.Mapping.MappingFromPerson.DisplayDirectoryName, "lnk");
|
|
else
|
|
personDirectory = Path.Combine(directory, face.Mapping.MappingFromPerson.DisplayDirectoryName[..1], "lnk");
|
|
saveContainer = new(personDirectory);
|
|
results.Add(saveContainer);
|
|
facesDirectory = Path.Combine($"{dFacesContentDirectory}{directoryName}", face.Mapping.MappingFromItem.ImageFileHolder.NameWithoutExtension);
|
|
facePartsDirectory = Path.Combine($"{d2ResultsFullGroupDirectory}{directoryName}", face.Mapping.MappingFromItem.ImageFileHolder.NameWithoutExtension);
|
|
checkFile = Path.Combine(directory, $"{face.Mapping.MappingFromLocation.DeterministicHashCodeKey}{face.Mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}");
|
|
faceFileHolder = new(Path.Combine(facesDirectory, $"{face.Mapping.MappingFromLocation.DeterministicHashCodeKey}{face.Mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}"));
|
|
hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{face.Mapping.MappingFromLocation.DeterministicHashCodeKey}{face.Mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}"));
|
|
facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{face.Mapping.MappingFromLocation.DeterministicHashCodeKey}{face.Mapping.MappingFromItem.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}"));
|
|
if (string.IsNullOrEmpty(personDirectory))
|
|
shortcutFile = string.Empty;
|
|
else
|
|
shortcutFile = Path.Combine(personDirectory, $"{face.Mapping.MappingFromLocation.DeterministicHashCodeKey}{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) && _Configuration.MappingSaveFaceEncoding)
|
|
{
|
|
checkFile = Path.Combine(directory, $"{face.Mapping.MappingFromLocation.DeterministicHashCodeKey}{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 ForceSingleImageThenSaveMapping(int maxDegreeOfParallelism, Property.Models.Configuration propertyConfiguration, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, List<Face> distinctFilteredFaces, SortingContainer[] sortingContainers, int totalNotMapped)
|
|
{
|
|
List<SaveContainer> saveContainers;
|
|
if (!sortingContainers.Any())
|
|
{
|
|
ForceSingleImage(propertyConfiguration, distinctFilteredFaces);
|
|
saveContainers = GetMappingSaveContainers(dFacesContentDirectory, d2ResultsFullGroupDirectory, distinctFilteredFaces);
|
|
}
|
|
else
|
|
{
|
|
int updated = UpdateFromSortingContainers(sortingContainers);
|
|
if (totalNotMapped - updated > 0)
|
|
ForceSingleImage(propertyConfiguration, distinctFilteredFaces);
|
|
saveContainers = GetMappingSaveContainers(dFacesContentDirectory, d2ResultsFullGroupDirectory, distinctFilteredFaces);
|
|
}
|
|
SaveContainers(saveContainers);
|
|
}
|
|
|
|
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 void SetPersonKeysRanges(Dictionary<long, List<long>> personTicks)
|
|
{
|
|
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();
|
|
_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(List<Face> distinctFilteredFaces)
|
|
{
|
|
PersonBirthday[] personBirthdays;
|
|
Dictionary<int, PersonBirthday[]> 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 (!_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);
|
|
}
|
|
|
|
} |