967 lines
54 KiB
C#
967 lines
54 KiB
C#
using System.Diagnostics;
|
|
using System.Text.Json;
|
|
using View_by_Distance.Property.Models;
|
|
using View_by_Distance.Shared.Models;
|
|
using View_by_Distance.Shared.Models.Stateless;
|
|
using WindowsShortcutFactory;
|
|
|
|
namespace View_by_Distance.Map.Models;
|
|
|
|
public class MapLogic
|
|
{
|
|
|
|
protected readonly List<long> _NotMappedTicks;
|
|
protected readonly List<double> _SkipCollection;
|
|
protected readonly List<(int, string[])> _AllCollection;
|
|
protected readonly Dictionary<int, int[]> _KeyValuePairs;
|
|
protected readonly Dictionary<int, int[]> _IndicesFromNew;
|
|
protected readonly string _DeterministicHashCodeContentDirectory;
|
|
protected readonly Dictionary<int, string[]> _SixCharacterNamedFaceInfo;
|
|
protected readonly Dictionary<double, PersonBirthday[]> _DeterministicHashCodeKeyValuePairs;
|
|
protected readonly Dictionary<int, PersonBirthday[]> _DeterministicHashCodeUnknownFaceKeyValuePairs;
|
|
protected readonly Dictionary<double, PersonBirthday[]> _IncorrectDeterministicHashCodeKeyValuePairs;
|
|
protected readonly Dictionary<long, (string DisplayDirectoryName, int? ApproximateYears, PersonBirthday[] PersonBirthdays, long Ticks)> _PeopleKeyValuePairs;
|
|
|
|
public Dictionary<int, int[]> KeyValuePairs => _KeyValuePairs;
|
|
public Dictionary<int, int[]> IndicesFromNew => _IndicesFromNew;
|
|
|
|
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;
|
|
|
|
public MapLogic(int maxDegreeOfParallelism, Configuration configuration, string resizeFilenameExtension, string facesFilenameExtension, string facesHiddenFilenameExtension, string facePartsFilenameExtension, Person[] people)
|
|
{
|
|
_AllCollection = new();
|
|
_Configuration = configuration;
|
|
_Log = Serilog.Log.ForContext<MapLogic>();
|
|
_FacesFilenameExtension = facesFilenameExtension;
|
|
_ResizeFilenameExtension = resizeFilenameExtension;
|
|
_FacePartsFilenameExtension = facePartsFilenameExtension;
|
|
_FacesHiddenFilenameExtension = facesHiddenFilenameExtension;
|
|
Dictionary<int, PersonBirthday[]>? deterministicHashCodeUnknownFaceKeyValuePairs;
|
|
if (configuration.VerifyToSeason is null || !configuration.VerifyToSeason.Any())
|
|
throw new Exception();
|
|
string json;
|
|
string[] files;
|
|
string fullPath;
|
|
List<double> skipCollection = new();
|
|
Dictionary<int, int[]>? keyValuePairs;
|
|
List<long> notMappedTicks = new();
|
|
List<KeyValuePair<int, int[]>>? collection;
|
|
string deterministicHashCodeContentDirectory;
|
|
Dictionary<int, int[]> indicesFromNew = new();
|
|
Dictionary<int, string[]>? sixCharacterNamedFaceInfo;
|
|
Dictionary<double, PersonBirthday[]> deterministicHashCodeKeyValuePairs = new();
|
|
string? rootDirectoryParent = Path.GetDirectoryName(configuration.RootDirectory);
|
|
Dictionary<long, (string, int?, PersonBirthday[], long)> peopleKeyValuePairs = new();
|
|
Dictionary<double, PersonBirthday[]> incorrectDeterministicHashCodeKeyValuePairs = new();
|
|
if (string.IsNullOrEmpty(rootDirectoryParent))
|
|
throw new NullReferenceException(nameof(rootDirectoryParent));
|
|
string deterministicHashCodeRootDirectory = Path.Combine(rootDirectoryParent, "DeterministicHashCode");
|
|
files = Directory.GetFiles(rootDirectoryParent, "DeterministicHashCode*.json", SearchOption.TopDirectoryOnly);
|
|
if (files.Length != 1)
|
|
deterministicHashCodeUnknownFaceKeyValuePairs = new();
|
|
else
|
|
{
|
|
json = File.ReadAllText(files[0]);
|
|
deterministicHashCodeUnknownFaceKeyValuePairs = Get(json);
|
|
}
|
|
if (!Directory.Exists(deterministicHashCodeRootDirectory))
|
|
_ = Directory.CreateDirectory(deterministicHashCodeRootDirectory);
|
|
deterministicHashCodeContentDirectory = Path.Combine(deterministicHashCodeRootDirectory, "()");
|
|
Stateless.ByDeterministicHashCode.SetByRef(_ResizeFilenameExtension, people, skipCollection, peopleKeyValuePairs, notMappedTicks, deterministicHashCodeUnknownFaceKeyValuePairs, deterministicHashCodeKeyValuePairs, incorrectDeterministicHashCodeKeyValuePairs, deterministicHashCodeRootDirectory, deterministicHashCodeContentDirectory);
|
|
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[0]);
|
|
sixCharacterNamedFaceInfo = JsonSerializer.Deserialize<Dictionary<int, string[]>>(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[0]);
|
|
keyValuePairs = JsonSerializer.Deserialize<Dictionary<int, int[]>>(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<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;
|
|
_NotMappedTicks = notMappedTicks;
|
|
_PeopleKeyValuePairs = peopleKeyValuePairs;
|
|
_SixCharacterNamedFaceInfo = sixCharacterNamedFaceInfo;
|
|
_DeterministicHashCodeKeyValuePairs = deterministicHashCodeKeyValuePairs;
|
|
_DeterministicHashCodeContentDirectory = deterministicHashCodeContentDirectory;
|
|
_IncorrectDeterministicHashCodeKeyValuePairs = incorrectDeterministicHashCodeKeyValuePairs;
|
|
_DeterministicHashCodeUnknownFaceKeyValuePairs = deterministicHashCodeUnknownFaceKeyValuePairs;
|
|
}
|
|
|
|
public MapLogic(int maxDegreeOfParallelism, Configuration configuration, string outputExtension, Person[] people) :
|
|
this(maxDegreeOfParallelism, configuration, outputExtension, outputExtension, outputExtension, outputExtension, people)
|
|
{ }
|
|
|
|
public bool Skip(double deterministicHashCodeKey) => _SkipCollection.Contains(deterministicHashCodeKey);
|
|
|
|
private static Dictionary<int, PersonBirthday[]> Get(string json)
|
|
{
|
|
Dictionary<int, PersonBirthday[]> results = new();
|
|
PersonBirthday? personBirthday;
|
|
List<PersonBirthday> personBirthdays;
|
|
Dictionary<int, string[]>? keyValuePairs = JsonSerializer.Deserialize<Dictionary<int, string[]>>(json);
|
|
if (keyValuePairs is null)
|
|
throw new NullReferenceException(nameof(keyValuePairs));
|
|
foreach (KeyValuePair<int, string[]> keyValuePair in keyValuePairs)
|
|
{
|
|
personBirthdays = new();
|
|
foreach (string personKey in keyValuePair.Value)
|
|
{
|
|
personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKey);
|
|
if (personBirthday is null)
|
|
continue;
|
|
}
|
|
if (!personBirthdays.Any())
|
|
continue;
|
|
results.Add(keyValuePair.Key, personBirthdays.ToArray());
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public void SaveShortcuts(string[] juliePhares, string zPropertyHolderContentDirectory, List<Item> items)
|
|
{
|
|
string fileName;
|
|
string fullName;
|
|
string personKey;
|
|
DateTime minimumDateTime;
|
|
PersonBirthday personBirthday;
|
|
WindowsShortcut windowsShortcut;
|
|
(string DisplayDirectoryName, int? ApproximateYears, PersonBirthday[] _, long Ticks) person;
|
|
List<(Item, (long?, Face?, (string, string, string, string))[])> collections = GetCollection(items, zPropertyHolderContentDirectory);
|
|
foreach ((Item item, (long? ticks, Face? _, (string, string, string, string))[] collection) in collections)
|
|
{
|
|
if (collection.Length != 1)
|
|
continue;
|
|
foreach ((long? ticks, Face? _, (string directory, string copyDirectory, string copyFileName, string shortcutFileName)) in collection)
|
|
{
|
|
if (ticks is null)
|
|
continue;
|
|
if (item.Property?.Id is null || item.ImageFileHolder is null || item.ResizedFileHolder is null)
|
|
continue;
|
|
minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property);
|
|
if (!Directory.Exists(directory))
|
|
{
|
|
_ = Directory.CreateDirectory(directory);
|
|
if (ticks is not null && _PeopleKeyValuePairs.ContainsKey(ticks.Value))
|
|
{
|
|
person = _PeopleKeyValuePairs[ticks.Value];
|
|
fullName = string.Concat(person.DisplayDirectoryName, ".txt");
|
|
File.WriteAllText(Path.Combine(directory, fullName), string.Empty);
|
|
}
|
|
}
|
|
if (ticks is null)
|
|
continue;
|
|
personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(ticks.Value);
|
|
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday);
|
|
if (juliePhares.Contains(personKey) && !string.IsNullOrEmpty(copyDirectory))
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UseKeyValuePairsSaveFaceEncoding(Container[] containers)
|
|
{
|
|
if (!string.IsNullOrEmpty(_DeterministicHashCodeContentDirectory))
|
|
{
|
|
Dictionary<int, List<Face>> keyValuePairs = new();
|
|
List<(PersonBirthday PersonBirthday, double IdAndNormalizedPixelPercentage)> deterministicHashCodeCollection = new();
|
|
List<(PersonBirthday PersonBirthday, double IdAndNormalizedPixelPercentage)> incorrectDeterministicHashCodeCollection = new();
|
|
foreach (Container container in containers)
|
|
{
|
|
foreach (Item item in container.Items)
|
|
{
|
|
if (item.ImageFileHolder is null || item.Property?.Id is null || !item.Faces.Any())
|
|
continue;
|
|
if (keyValuePairs.ContainsKey(item.Property.Id.Value))
|
|
{
|
|
if (keyValuePairs[item.Property.Id.Value].Count != item.Faces.Count)
|
|
throw new Exception();
|
|
continue;
|
|
}
|
|
keyValuePairs.Add(item.Property.Id.Value, item.Faces);
|
|
}
|
|
}
|
|
Stateless.ByDeterministicHashCode.SetKeyValuePairs(_DeterministicHashCodeContentDirectory, deterministicHashCodeCollection, incorrectDeterministicHashCodeCollection, keyValuePairs);
|
|
}
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
|
|
return result;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public void AddToMapLogicAllCollection(Item[] filteredItems)
|
|
{
|
|
if (_SixCharacterNamedFaceInfo.Any())
|
|
{
|
|
string[] keys;
|
|
Item item;
|
|
for (int i = 0; i < filteredItems.Length; i++)
|
|
{
|
|
item = filteredItems[i];
|
|
if (item.Property?.Id is null)
|
|
continue;
|
|
foreach (int sixCharacterIndex in item.Property.Indices)
|
|
{
|
|
if (!_SixCharacterNamedFaceInfo.ContainsKey(sixCharacterIndex))
|
|
continue;
|
|
keys = _SixCharacterNamedFaceInfo[sixCharacterIndex];
|
|
_AllCollection.Add(new(item.Property.Id.Value, keys));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SaveAllCollection()
|
|
{
|
|
if (_AllCollection.Any())
|
|
{
|
|
string[] keys;
|
|
long ticks = DateTime.Now.Ticks;
|
|
string? rootDirectoryParent = Path.GetDirectoryName(_Configuration.RootDirectory);
|
|
if (string.IsNullOrEmpty(rootDirectoryParent))
|
|
throw new NullReferenceException(nameof(rootDirectoryParent));
|
|
Dictionary<int, string[]> namedFaceInfoDeterministicHashCodeIndices = new();
|
|
List<(int, string[])> allCollection = _AllCollection.OrderBy(l => l.Item1).ToList();
|
|
foreach ((int deterministicHashCode, string[] values) in allCollection)
|
|
{
|
|
if (namedFaceInfoDeterministicHashCodeIndices.ContainsKey(deterministicHashCode))
|
|
{
|
|
keys = namedFaceInfoDeterministicHashCodeIndices[deterministicHashCode];
|
|
if (JsonSerializer.Serialize(values) == JsonSerializer.Serialize(keys))
|
|
continue;
|
|
throw new Exception();
|
|
}
|
|
namedFaceInfoDeterministicHashCodeIndices.Add(deterministicHashCode, values);
|
|
}
|
|
string json = JsonSerializer.Serialize(namedFaceInfoDeterministicHashCodeIndices, new JsonSerializerOptions { WriteIndented = true });
|
|
string checkFile = Path.Combine(rootDirectoryParent, "NamedFaceInfoDeterministicHashCodeIndices.json");
|
|
_ = Shared.Models.Stateless.Methods.IPath.WriteAllText(checkFile, json, updateDateWhenMatches: true, compareBeforeWrite: true);
|
|
_ = LogDelta(ticks, new StackFrame().GetMethod()?.Name);
|
|
}
|
|
}
|
|
|
|
public void AddToMapping(List<Item> items)
|
|
{
|
|
long ticks;
|
|
Mapping mapping;
|
|
bool forced = false;
|
|
int? approximateYears;
|
|
string displayDirectoryName;
|
|
PersonBirthday personBirthday;
|
|
double deterministicHashCodeKey;
|
|
List<PersonBirthday> personBirthdays = new();
|
|
(string DisplayDirectoryName, int? ApproximateYears, PersonBirthday[] PersonBirthdays, long Ticks) person;
|
|
foreach (Item item in items)
|
|
{
|
|
if (item.ImageFileHolder is null)
|
|
continue;
|
|
if (item.Property?.Id is null || item.ResizedFileHolder is null)
|
|
continue;
|
|
foreach (Face face in item.Faces)
|
|
{
|
|
personBirthdays.Clear();
|
|
if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null)
|
|
continue;
|
|
deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face);
|
|
if (!_DeterministicHashCodeKeyValuePairs.ContainsKey(deterministicHashCodeKey))
|
|
continue;
|
|
personBirthdays.AddRange(_DeterministicHashCodeKeyValuePairs[deterministicHashCodeKey]);
|
|
for (int i = 0; i < personBirthdays.Count; i++)
|
|
{
|
|
ticks = personBirthdays[i].Value.Ticks;
|
|
if (!_PeopleKeyValuePairs.ContainsKey(ticks))
|
|
continue;
|
|
person = _PeopleKeyValuePairs[ticks];
|
|
personBirthday = person.PersonBirthdays[0];
|
|
approximateYears = person.ApproximateYears;
|
|
displayDirectoryName = person.DisplayDirectoryName;
|
|
mapping = new(approximateYears, displayDirectoryName, forced, face.Location.NormalizedPixelPercentage, personBirthday);
|
|
item.Mapping.Add(mapping);
|
|
}
|
|
}
|
|
if (IMapping.UseDeterministicHashCodeUnknownFaceKeyValuePairsForAddToMapping && !personBirthdays.Any())
|
|
{
|
|
if (!_DeterministicHashCodeUnknownFaceKeyValuePairs.ContainsKey(item.Property.Id.Value))
|
|
continue;
|
|
personBirthdays.AddRange(_DeterministicHashCodeUnknownFaceKeyValuePairs[item.Property.Id.Value]);
|
|
for (int i = 0; i < personBirthdays.Count; i++)
|
|
{
|
|
ticks = personBirthdays[i].Value.Ticks;
|
|
if (!_PeopleKeyValuePairs.ContainsKey(ticks))
|
|
continue;
|
|
person = _PeopleKeyValuePairs[ticks];
|
|
personBirthday = person.PersonBirthdays[0];
|
|
approximateYears = person.ApproximateYears;
|
|
displayDirectoryName = person.DisplayDirectoryName;
|
|
mapping = new(approximateYears, displayDirectoryName, forced, personBirthday);
|
|
item.Mapping.Add(mapping);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SaveContainers(List<SaveContainer> saveContainers)
|
|
{
|
|
WindowsShortcut windowsShortcut;
|
|
string[] directories = (from l in saveContainers select l.Directory).Distinct().ToArray();
|
|
foreach (string directory in directories)
|
|
{
|
|
if (string.IsNullOrEmpty(directory))
|
|
continue;
|
|
if (!Directory.Exists(directory))
|
|
_ = Directory.CreateDirectory(directory);
|
|
}
|
|
foreach (SaveContainer saveContainer in saveContainers)
|
|
{
|
|
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;
|
|
_ = Shared.Models.Stateless.Methods.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(string zPropertyHolderContentDirectory)
|
|
{
|
|
string directory;
|
|
string personKey;
|
|
SaveContainer saveContainer;
|
|
PersonBirthday personBirthday;
|
|
List<SaveContainer> saveContainers = new();
|
|
const string facePopulatedKey = nameof(Closest);
|
|
foreach (long ticks in _NotMappedTicks)
|
|
{
|
|
personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(ticks);
|
|
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday);
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}NotMapped", personKey, Property.Models.Stateless.IResult.AllInOne);
|
|
saveContainer = new(directory);
|
|
saveContainers.Add(saveContainer);
|
|
}
|
|
SaveContainers(saveContainers);
|
|
}
|
|
|
|
public List<(Item, (long?, Face?, (string, string, string, string))[])> GetCollection(List<Item> items, string zPropertyHolderContentDirectory)
|
|
{
|
|
List<(Item, (long?, Face?, (string, string, string, string))[])> results = new();
|
|
int years;
|
|
Face face;
|
|
Item item;
|
|
long? ticks;
|
|
string directory;
|
|
string personKey;
|
|
bool? isWrongYear;
|
|
const int zero = 0;
|
|
TimeSpan? timeSpan;
|
|
string copyFileName;
|
|
string copyDirectory;
|
|
string? relativePath;
|
|
string isWrongYearFlag;
|
|
string shortcutFileName;
|
|
string subDirectoryName;
|
|
DateTime minimumDateTime;
|
|
List<int> indices = new();
|
|
List<Face> faceCollection;
|
|
PersonBirthday personBirthday;
|
|
PersonBirthday[] personBirthdays;
|
|
DateTime dateTime = DateTime.Now;
|
|
List<(long?, Face?, (string, string, string, string))> collection;
|
|
for (int i = 0; i < items.Count; i++)
|
|
{
|
|
indices.Clear();
|
|
copyFileName = string.Empty;
|
|
copyDirectory = string.Empty;
|
|
item = items[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.ResizedFileHolder is null)
|
|
continue;
|
|
collection = new();
|
|
if (!_DeterministicHashCodeUnknownFaceKeyValuePairs.ContainsKey(item.Property.Id.Value))
|
|
{
|
|
faceCollection = new();
|
|
ticks = null;
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"Unnamed{relativePath[2..]}");
|
|
}
|
|
else
|
|
{
|
|
faceCollection = item.Faces;
|
|
personBirthdays = _DeterministicHashCodeUnknownFaceKeyValuePairs[item.Property.Id.Value];
|
|
minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property);
|
|
(isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime);
|
|
isWrongYearFlag = Shared.Models.Stateless.Methods.IItem.GetWrongYearFlag(isWrongYear);
|
|
subDirectoryName = $"{isWrongYearFlag}{minimumDateTime:yyyy}";
|
|
if (!faceCollection.Any())
|
|
{
|
|
ticks = null;
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"None{relativePath[2..]}", subDirectoryName);
|
|
}
|
|
else if (personBirthdays.Length != 1)
|
|
{
|
|
ticks = null;
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"Not Supported{relativePath[2..]}", subDirectoryName);
|
|
}
|
|
else if (faceCollection.Count != 1)
|
|
{
|
|
ticks = null;
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"Many{relativePath[2..]}", subDirectoryName);
|
|
}
|
|
else
|
|
{
|
|
indices.Add(zero);
|
|
personBirthday = personBirthdays[zero];
|
|
ticks = personBirthday.Value.Ticks;
|
|
timeSpan = Shared.Models.Stateless.Methods.IPersonBirthday.GetTimeSpan(minimumDateTime, isWrongYear, personBirthday);
|
|
if (timeSpan.HasValue)
|
|
{
|
|
if (timeSpan.Value.Ticks < 0)
|
|
subDirectoryName = "!---";
|
|
else
|
|
{
|
|
(years, _) = Shared.Models.Stateless.Methods.IPersonBirthday.GetAge(dateTime, personBirthday);
|
|
subDirectoryName = $"^{years:000}";
|
|
}
|
|
}
|
|
face = faceCollection[zero];
|
|
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday);
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, "Shortcuts", personKey, subDirectoryName);
|
|
if (face.FaceEncoding is not null && face.Location?.NormalizedPixelPercentage is not null)
|
|
copyDirectory = Path.Combine(zPropertyHolderContentDirectory, "Images", personKey, subDirectoryName);
|
|
else
|
|
copyDirectory = Path.Combine(zPropertyHolderContentDirectory, "ImagesBut", personKey, subDirectoryName);
|
|
copyFileName = Path.Combine(copyDirectory, $"{item.Property.Id.Value}{item.ResizedFileHolder.ExtensionLowered}");
|
|
}
|
|
}
|
|
shortcutFileName = Path.Combine(directory, $"{item.Property.Id.Value}.lnk");
|
|
if (ticks is null || !indices.Any())
|
|
collection.Add(new(ticks, null, (directory, copyDirectory, copyFileName, shortcutFileName)));
|
|
else
|
|
{
|
|
foreach (int index in indices)
|
|
collection.Add(new(ticks, faceCollection[index], (directory, copyDirectory, copyFileName, shortcutFileName)));
|
|
}
|
|
results.Add(new(item, collection.ToArray()));
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public void AddToClosest(int maxDegreeOfParallelism, string argZero, Container[] containers)
|
|
{
|
|
string key;
|
|
string dateKey;
|
|
Closest closest;
|
|
string personKey;
|
|
DateTime minimumDateTime;
|
|
Closest[] closestCollection;
|
|
double deterministicHashCodeKey;
|
|
DateTime dateTime = DateTime.Now;
|
|
Dictionary<string, int> keyValuePairs = new();
|
|
foreach (Container container in containers)
|
|
{
|
|
if (!container.Items.Any())
|
|
continue;
|
|
if (!container.SourceDirectory.StartsWith(argZero))
|
|
continue;
|
|
foreach (Item item in container.Items)
|
|
{
|
|
if (item.ImageFileHolder is null || item.Property is null)
|
|
continue;
|
|
foreach (Face face in item.Faces)
|
|
{
|
|
if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null)
|
|
continue;
|
|
deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face);
|
|
if (_DeterministicHashCodeKeyValuePairs.ContainsKey(deterministicHashCodeKey))
|
|
continue;
|
|
minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property);
|
|
closestCollection = Shared.Models.Stateless.Methods.IClosest.GetCollection(face, minimumDateTime, face.FaceDistances);
|
|
face.FaceDistances.Clear();
|
|
for (int j = 0; j < closestCollection.Length; j++)
|
|
{
|
|
closest = closestCollection[j];
|
|
if (_IncorrectDeterministicHashCodeKeyValuePairs.ContainsKey(deterministicHashCodeKey) && _IncorrectDeterministicHashCodeKeyValuePairs[deterministicHashCodeKey].Contains(closest.Mapping.PersonBirthday))
|
|
continue;
|
|
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(closest.Mapping.PersonBirthday);
|
|
dateKey = Stateless.MapLogic.GetDateKey(dateTime, closest.Mapping, closest.MinimumDateTime, closest.IsWrongYear);
|
|
key = string.Concat(personKey, dateKey);
|
|
if (!keyValuePairs.ContainsKey(key))
|
|
keyValuePairs.Add(key, 0);
|
|
else if (keyValuePairs[key] > IClosest.MaximumPer)
|
|
continue;
|
|
keyValuePairs[key] += 1;
|
|
item.Closest.Add(closest);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private List<SaveContainer> GetMappingSaveContainers(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
|
|
{
|
|
List<SaveContainer> results = new();
|
|
string key;
|
|
string json;
|
|
string dateKey;
|
|
Mapping? match;
|
|
string personKey;
|
|
string checkFile;
|
|
string directory;
|
|
bool? isWrongYear;
|
|
string shortcutFile;
|
|
string? directoryName;
|
|
string facesDirectory;
|
|
string personDirectory;
|
|
List<int> used = new();
|
|
DateTime minimumDateTime;
|
|
FileHolder faceFileHolder;
|
|
string facePartsDirectory;
|
|
SaveContainer saveContainer;
|
|
FileHolder facePartsFileHolder;
|
|
FileHolder hiddenFaceFileHolder;
|
|
double deterministicHashCodeKey;
|
|
DateTime dateTime = DateTime.Now;
|
|
const string facePopulatedKey = nameof(Mapping);
|
|
bool deterministicHashCodeKeyValuePairsAny = _DeterministicHashCodeKeyValuePairs.Any();
|
|
foreach (Container container in containers)
|
|
{
|
|
if (!container.Items.Any())
|
|
continue;
|
|
if (!container.SourceDirectory.StartsWith(argZero))
|
|
continue;
|
|
foreach (Item item in container.Items)
|
|
{
|
|
used.Clear();
|
|
if (item.ImageFileHolder is null || item.Property?.Id is null || item.ResizedFileHolder is null)
|
|
continue;
|
|
directoryName = Path.GetDirectoryName(item.RelativePath);
|
|
if (directoryName is null)
|
|
throw new Exception();
|
|
foreach (Face face in item.Faces)
|
|
{
|
|
match = null;
|
|
if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null)
|
|
continue;
|
|
foreach (Mapping mapping in item.Mapping)
|
|
{
|
|
if (mapping.NormalizedPixelPercentage is null || mapping.NormalizedPixelPercentage.Value != face.Location.NormalizedPixelPercentage.Value)
|
|
continue;
|
|
match = mapping;
|
|
break;
|
|
}
|
|
if (match is null)
|
|
continue;
|
|
foreach (Closest closest in item.Closest)
|
|
{
|
|
if (closest.Mapping.Forced)
|
|
continue;
|
|
if (closest.NormalizedPixelPercentage != face.Location.NormalizedPixelPercentage.Value)
|
|
continue;
|
|
throw new Exception();
|
|
}
|
|
minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property);
|
|
(isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime);
|
|
dateKey = Stateless.MapLogic.GetDateKey(dateTime, match, minimumDateTime, isWrongYear);
|
|
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(match.PersonBirthday);
|
|
key = string.Concat(personKey, dateKey);
|
|
if (match.Filtered is null)
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}Null", personKey, dateKey);
|
|
else if (!match.Filtered.Value)
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}Okay", personKey, dateKey);
|
|
else
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}OutOfControl", personKey, dateKey);
|
|
personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk");
|
|
saveContainer = new(personDirectory);
|
|
results.Add(saveContainer);
|
|
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
|
|
facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
|
|
deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face);
|
|
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}");
|
|
faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}"));
|
|
hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}"));
|
|
facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}"));
|
|
if (string.IsNullOrEmpty(personDirectory))
|
|
shortcutFile = string.Empty;
|
|
else
|
|
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
|
|
saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile);
|
|
results.Add(saveContainer);
|
|
if (!string.IsNullOrEmpty(checkFile) && IMapping.SaveFaceEncoding)
|
|
{
|
|
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.json");
|
|
json = JsonSerializer.Serialize(face.FaceEncoding);
|
|
saveContainer = new(checkFile, directory, json);
|
|
results.Add(saveContainer);
|
|
}
|
|
personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk", match.DisplayDirectoryName);
|
|
saveContainer = new(personDirectory);
|
|
results.Add(saveContainer);
|
|
used.Add(face.Location.NormalizedPixelPercentage.Value);
|
|
}
|
|
if (deterministicHashCodeKeyValuePairsAny && IMapping.UseDeterministicHashCodeUnknownFaceKeyValuePairsForSaveMapping)
|
|
{
|
|
foreach (Face face in item.Faces)
|
|
{
|
|
match = null;
|
|
if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null)
|
|
continue;
|
|
if (used.Contains(face.Location.NormalizedPixelPercentage.Value))
|
|
continue;
|
|
// if (item.Faces.Count != 1 || item.Mapping.Count != 1)
|
|
// continue;
|
|
foreach (Mapping mapping in item.Mapping)
|
|
{
|
|
if (mapping.NormalizedPixelPercentage is not null)
|
|
continue;
|
|
match = mapping;
|
|
break;
|
|
}
|
|
if (match is null)
|
|
continue;
|
|
foreach (Closest closest in item.Closest)
|
|
{
|
|
if (closest.Mapping.Forced)
|
|
continue;
|
|
if (closest.NormalizedPixelPercentage != face.Location.NormalizedPixelPercentage.Value)
|
|
continue;
|
|
throw new Exception();
|
|
}
|
|
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(match.PersonBirthday);
|
|
minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property);
|
|
(isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime);
|
|
dateKey = Stateless.MapLogic.GetDateKey(dateTime, match, minimumDateTime, isWrongYear);
|
|
if (match.Filtered is null)
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}WithButNull", personKey, dateKey);
|
|
else if (!match.Filtered.Value)
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}WithAndOkay", personKey, dateKey);
|
|
else
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}WithButOutOfControl", personKey, dateKey);
|
|
personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk");
|
|
saveContainer = new(personDirectory);
|
|
results.Add(saveContainer);
|
|
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
|
|
facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
|
|
deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face);
|
|
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}");
|
|
faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}"));
|
|
facePartsFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}"));
|
|
facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}"));
|
|
if (string.IsNullOrEmpty(personDirectory))
|
|
shortcutFile = string.Empty;
|
|
else
|
|
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
|
|
saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile);
|
|
results.Add(saveContainer);
|
|
if (!string.IsNullOrEmpty(checkFile) && IMapping.SaveFaceEncoding)
|
|
{
|
|
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.json");
|
|
json = JsonSerializer.Serialize(face.FaceEncoding);
|
|
saveContainer = new(checkFile, directory, json);
|
|
results.Add(saveContainer);
|
|
}
|
|
personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk", match.DisplayDirectoryName);
|
|
saveContainer = new(personDirectory);
|
|
results.Add(saveContainer);
|
|
used.Add(face.Location.NormalizedPixelPercentage.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private List<SaveContainer> GetClosestSaveContainers(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
|
|
{
|
|
List<SaveContainer> results = new();
|
|
Closest? match;
|
|
string dateKey;
|
|
string checkFile;
|
|
string directory;
|
|
string personKey;
|
|
string shortcutFile;
|
|
string? directoryName;
|
|
string facesDirectory;
|
|
string personDirectory;
|
|
List<int> used = new();
|
|
FileHolder faceFileHolder;
|
|
string facePartsDirectory;
|
|
SaveContainer saveContainer;
|
|
PersonBirthday personBirthday;
|
|
FileHolder facePartsFileHolder;
|
|
FileHolder hiddenFaceFileHolder;
|
|
double deterministicHashCodeKey;
|
|
DateTime dateTime = DateTime.Now;
|
|
const string facePopulatedKey = nameof(Closest);
|
|
foreach (Container container in containers)
|
|
{
|
|
if (!container.Items.Any())
|
|
continue;
|
|
if (!container.SourceDirectory.StartsWith(argZero))
|
|
continue;
|
|
foreach (Item item in container.Items)
|
|
{
|
|
used.Clear();
|
|
if (item.ImageFileHolder is null || item.Property?.Id is null || item.ResizedFileHolder is null)
|
|
continue;
|
|
if (!item.Closest.Any())
|
|
continue;
|
|
directoryName = Path.GetDirectoryName(item.RelativePath);
|
|
if (directoryName is null)
|
|
throw new Exception();
|
|
foreach (Face face in item.Faces)
|
|
{
|
|
match = null;
|
|
if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null)
|
|
continue;
|
|
foreach (Closest closest in item.Closest)
|
|
{
|
|
if (closest.NormalizedPixelPercentage != face.Location.NormalizedPixelPercentage.Value)
|
|
continue;
|
|
match = closest;
|
|
break;
|
|
}
|
|
if (match is null)
|
|
continue;
|
|
foreach (Mapping mapping in item.Mapping)
|
|
{
|
|
if (mapping.Forced)
|
|
continue;
|
|
if (mapping.NormalizedPixelPercentage is null || mapping.NormalizedPixelPercentage.Value != face.Location.NormalizedPixelPercentage.Value)
|
|
continue;
|
|
throw new Exception();
|
|
}
|
|
if (!match.AboveTolerance)
|
|
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(match.Mapping.PersonBirthday);
|
|
else
|
|
{
|
|
personKey = string.Empty;
|
|
foreach (long ticks in _NotMappedTicks)
|
|
{
|
|
if (ticks == match.Mapping.PersonBirthday.Value.Ticks)
|
|
continue;
|
|
personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(ticks);
|
|
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday);
|
|
break;
|
|
}
|
|
if (string.IsNullOrEmpty(personKey))
|
|
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(match.Mapping.PersonBirthday);
|
|
}
|
|
dateKey = Stateless.MapLogic.GetDateKey(dateTime, match.Mapping, match.MinimumDateTime, match.IsWrongYear);
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, facePopulatedKey, personKey, dateKey);
|
|
personDirectory = Path.Combine(directory, match.Mapping.DisplayDirectoryName[..1], "lnk");
|
|
saveContainer = new(personDirectory);
|
|
results.Add(saveContainer);
|
|
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
|
|
facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
|
|
deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face);
|
|
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}");
|
|
faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}"));
|
|
hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}"));
|
|
facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}"));
|
|
if (string.IsNullOrEmpty(personDirectory))
|
|
shortcutFile = string.Empty;
|
|
else
|
|
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
|
|
saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile);
|
|
results.Add(saveContainer);
|
|
personDirectory = Path.Combine(directory, match.Mapping.DisplayDirectoryName[..1], "lnk", match.Mapping.DisplayDirectoryName);
|
|
saveContainer = new(personDirectory);
|
|
results.Add(saveContainer);
|
|
used.Add(face.Location.NormalizedPixelPercentage.Value);
|
|
}
|
|
foreach (Closest closest in item.Closest)
|
|
{
|
|
if (used.Contains(closest.NormalizedPixelPercentage))
|
|
continue;
|
|
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(closest.Mapping.PersonBirthday);
|
|
dateKey = Stateless.MapLogic.GetDateKey(dateTime, closest.Mapping, closest.MinimumDateTime, closest.IsWrongYear);
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, facePopulatedKey, personKey, dateKey);
|
|
personDirectory = Path.Combine(directory, closest.Mapping.DisplayDirectoryName, "lnk");
|
|
saveContainer = new(personDirectory);
|
|
results.Add(saveContainer);
|
|
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
|
|
facePartsDirectory = string.Concat(d2ResultsFullGroupDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
|
|
deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, closest);
|
|
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}");
|
|
faceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesFilenameExtension}"));
|
|
hiddenFaceFileHolder = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacesHiddenFilenameExtension}"));
|
|
facePartsFileHolder = new(Path.Combine(facePartsDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_FacePartsFilenameExtension}"));
|
|
if (string.IsNullOrEmpty(personDirectory))
|
|
shortcutFile = string.Empty;
|
|
else
|
|
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
|
|
saveContainer = new(checkFile, directory, faceFileHolder, hiddenFaceFileHolder, string.Empty, facePartsFileHolder, item.ResizedFileHolder, shortcutFile);
|
|
results.Add(saveContainer);
|
|
personDirectory = Path.Combine(directory, closest.Mapping.DisplayDirectoryName[..1], "lnk", closest.Mapping.DisplayDirectoryName);
|
|
saveContainer = new(personDirectory);
|
|
results.Add(saveContainer);
|
|
used.Add(closest.NormalizedPixelPercentage);
|
|
}
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public void SaveMapping(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
|
|
{
|
|
List<SaveContainer> saveContainers = GetMappingSaveContainers(argZero, containers, dFacesContentDirectory, d2ResultsFullGroupDirectory, zPropertyHolderContentDirectory);
|
|
SaveContainers(saveContainers);
|
|
}
|
|
|
|
public void SaveClosest(string argZero, Container[] containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
|
|
{
|
|
List<SaveContainer> saveContainers = GetClosestSaveContainers(argZero, containers, dFacesContentDirectory, d2ResultsFullGroupDirectory, zPropertyHolderContentDirectory);
|
|
SaveContainers(saveContainers);
|
|
}
|
|
|
|
public void ForceSingleImage(string[] ignoreRelativePaths, string argZero, Container[] containers)
|
|
{
|
|
Closest closest;
|
|
Mapping mapping;
|
|
bool? isWrongYear;
|
|
long? ticks = null;
|
|
const int zero = 0;
|
|
bool forced = true;
|
|
DateTime minimumDateTime;
|
|
bool aboveTolerance = false;
|
|
int? approximateYears = null;
|
|
PersonBirthday personBirthday;
|
|
const string displayDirectoryName = Property.Models.Stateless.IResult.AllInOne;
|
|
foreach (Container container in containers)
|
|
{
|
|
if (!_NotMappedTicks.Any())
|
|
break;
|
|
if (!container.Items.Any())
|
|
continue;
|
|
if (!container.SourceDirectory.StartsWith(argZero))
|
|
continue;
|
|
if (ignoreRelativePaths.Contains(Path.GetFileName(container.SourceDirectory)))
|
|
continue;
|
|
foreach (Item item in container.Items)
|
|
{
|
|
if (item.ImageFileHolder is null || item.Property?.Id is null || item.Mapping.Any())
|
|
continue;
|
|
foreach (Face face in item.Faces)
|
|
{
|
|
if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null)
|
|
continue;
|
|
ticks = _NotMappedTicks[zero];
|
|
minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property);
|
|
(isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime);
|
|
personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(ticks.Value);
|
|
mapping = new(approximateYears, displayDirectoryName, forced, face.Location.NormalizedPixelPercentage, personBirthday);
|
|
closest = new(aboveTolerance, zero, face.Location.NormalizedPixelPercentage.Value, isWrongYear, mapping, zero, minimumDateTime, null);
|
|
item.Closest.Add(closest);
|
|
item.Mapping.Add(mapping);
|
|
if (ticks is not null)
|
|
break;
|
|
}
|
|
if (ticks is not null)
|
|
break;
|
|
}
|
|
if (ticks is not null)
|
|
break;
|
|
}
|
|
}
|
|
|
|
} |