825 lines
46 KiB
C#
825 lines
46 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.Properties;
|
|
using WindowsShortcutFactory;
|
|
|
|
namespace View_by_Distance.Map.Models;
|
|
|
|
public class MapLogic
|
|
{
|
|
|
|
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, string[]> _DeterministicHashCodeKeyValuePairs;
|
|
protected readonly Dictionary<int, string[]> _DeterministicHashCodeUnknownFaceKeyValuePairs;
|
|
protected readonly Dictionary<double, string[]> _IncorrectDeterministicHashCodeKeyValuePairs;
|
|
protected readonly Dictionary<string, (string DisplayDirectoryName, int? ApproximateYears, string Key, PersonBirthday[] PersonBirthdays)> _PeopleKeyValuePairs;
|
|
|
|
public Dictionary<int, int[]> KeyValuePairs => _KeyValuePairs;
|
|
public Dictionary<int, int[]> IndicesFromNew => _IndicesFromNew;
|
|
|
|
private readonly Serilog.ILogger? _Log;
|
|
private readonly string _OutputExtension;
|
|
private readonly Configuration _Configuration;
|
|
|
|
public MapLogic(int maxDegreeOfParallelism, Configuration configuration, string outputExtension, Dictionary<string, Person> personKeyValuePairs)
|
|
{
|
|
_AllCollection = new();
|
|
_Configuration = configuration;
|
|
_Log = Serilog.Log.ForContext<MapLogic>();
|
|
Dictionary<int, string[]>? deterministicHashCodeUnknownFaceKeyValuePairs;
|
|
if (configuration.VerifyToSeason is null || !configuration.VerifyToSeason.Any())
|
|
throw new Exception();
|
|
string json;
|
|
string[] files;
|
|
string fullPath;
|
|
_OutputExtension = outputExtension;
|
|
List<double> skipCollection = new();
|
|
Dictionary<int, int[]>? keyValuePairs;
|
|
List<KeyValuePair<int, int[]>>? collection;
|
|
string deterministicHashCodeContentDirectory;
|
|
Dictionary<int, int[]> indicesFromNew = new();
|
|
Dictionary<int, string[]>? sixCharacterNamedFaceInfo;
|
|
Dictionary<double, string[]> deterministicHashCodeKeyValuePairs = new();
|
|
Dictionary<double, string[]> incorrectDeterministicHashCodeKeyValuePairs = new();
|
|
string? rootDirectoryParent = Path.GetDirectoryName(configuration.RootDirectory);
|
|
Dictionary<string, (string, int?, string, PersonBirthday[])> peopleKeyValuePairs = new();
|
|
if (string.IsNullOrEmpty(rootDirectoryParent))
|
|
throw new NullReferenceException(nameof(rootDirectoryParent));
|
|
files = Directory.GetFiles(rootDirectoryParent, "DeterministicHashCode*.json", SearchOption.TopDirectoryOnly);
|
|
if (files.Length != 1)
|
|
deterministicHashCodeUnknownFaceKeyValuePairs = new();
|
|
else
|
|
{
|
|
json = File.ReadAllText(files[0]);
|
|
deterministicHashCodeUnknownFaceKeyValuePairs = JsonSerializer.Deserialize<Dictionary<int, string[]>>(json);
|
|
if (deterministicHashCodeUnknownFaceKeyValuePairs is null)
|
|
throw new NullReferenceException(nameof(deterministicHashCodeUnknownFaceKeyValuePairs));
|
|
}
|
|
string[] directories = Directory.GetDirectories(rootDirectoryParent, "DeterministicHashCode", SearchOption.TopDirectoryOnly);
|
|
if (!directories.Any())
|
|
deterministicHashCodeContentDirectory = string.Empty;
|
|
else
|
|
deterministicHashCodeContentDirectory = Stateless.ByDeterministicHashCode.SetByRef(_OutputExtension, personKeyValuePairs, skipCollection, peopleKeyValuePairs, deterministicHashCodeUnknownFaceKeyValuePairs, deterministicHashCodeKeyValuePairs, incorrectDeterministicHashCodeKeyValuePairs, directories[0]);
|
|
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;
|
|
_PeopleKeyValuePairs = peopleKeyValuePairs;
|
|
_SixCharacterNamedFaceInfo = sixCharacterNamedFaceInfo;
|
|
_DeterministicHashCodeKeyValuePairs = deterministicHashCodeKeyValuePairs;
|
|
_DeterministicHashCodeContentDirectory = deterministicHashCodeContentDirectory;
|
|
_IncorrectDeterministicHashCodeKeyValuePairs = incorrectDeterministicHashCodeKeyValuePairs;
|
|
_DeterministicHashCodeUnknownFaceKeyValuePairs = deterministicHashCodeUnknownFaceKeyValuePairs;
|
|
}
|
|
|
|
public bool Skip(double deterministicHashCodeKey) => _SkipCollection.Contains(deterministicHashCodeKey);
|
|
|
|
public void SaveShortcuts(string[] juliePhares, string dResultsFullGroupDirectory, long ticks, List<Item> items)
|
|
{
|
|
string fileName;
|
|
string fullName;
|
|
DateTime? minimumDateTime;
|
|
WindowsShortcut windowsShortcut;
|
|
(string DisplayDirectoryName, int? ApproximateYears, string Key, PersonBirthday[] _) person;
|
|
string dFacesContentDirectory = Path.Combine(dResultsFullGroupDirectory, $"({ticks})");
|
|
List<(Item, (string, Face?, (string, string, string, string))[])> collections = GetCollection(items, dFacesContentDirectory);
|
|
foreach ((Item item, (string personKey, Face? _, (string, string, string, string))[] collection) in collections)
|
|
{
|
|
if (collection.Length != 1)
|
|
continue;
|
|
foreach ((string personKey, Face? _, (string directory, string copyDirectory, string copyFileName, string shortcutFileName)) in collection)
|
|
{
|
|
if (string.IsNullOrEmpty(personKey))
|
|
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 (minimumDateTime is null)
|
|
continue;
|
|
if (!Directory.Exists(directory))
|
|
{
|
|
_ = Directory.CreateDirectory(directory);
|
|
if (!string.IsNullOrEmpty(personKey) && _PeopleKeyValuePairs.ContainsKey(personKey))
|
|
{
|
|
person = _PeopleKeyValuePairs[personKey];
|
|
fullName = string.Concat(person.DisplayDirectoryName, ".txt");
|
|
File.WriteAllText(Path.Combine(directory, fullName), string.Empty);
|
|
}
|
|
}
|
|
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.ImageFileHolder.FullName };
|
|
windowsShortcut.Save(fileName);
|
|
windowsShortcut.Dispose();
|
|
if (!File.Exists(fileName))
|
|
continue;
|
|
File.SetLastWriteTime(fileName, minimumDateTime.Value);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UseKeyValuePairsSaveFaceEncoding(List<Container> containers)
|
|
{
|
|
if (!string.IsNullOrEmpty(_DeterministicHashCodeContentDirectory))
|
|
{
|
|
Dictionary<int, List<Face>> keyValuePairs = new();
|
|
List<(string PersonKey, double IdAndNormalizedPixelPercentage)> deterministicHashCodeCollection = new();
|
|
List<(string PersonKey, 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)
|
|
{
|
|
Mapping mapping;
|
|
string personKey;
|
|
int? approximateYears;
|
|
string displayDirectoryName;
|
|
PersonBirthday? personBirthday;
|
|
double deterministicHashCodeKey;
|
|
List<string> personKeys = new();
|
|
(string DisplayDirectoryName, int? ApproximateYears, string Key, PersonBirthday[] PersonBirthdays) 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)
|
|
{
|
|
personKeys.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;
|
|
personKeys.AddRange(_DeterministicHashCodeKeyValuePairs[deterministicHashCodeKey]);
|
|
for (int i = 0; i < personKeys.Count; i++)
|
|
{
|
|
if (_PeopleKeyValuePairs.ContainsKey(personKeys[i]))
|
|
{
|
|
person = _PeopleKeyValuePairs[personKeys[i]];
|
|
personBirthday = person.PersonBirthdays[0];
|
|
approximateYears = person.ApproximateYears;
|
|
displayDirectoryName = person.DisplayDirectoryName;
|
|
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday);
|
|
}
|
|
else
|
|
{
|
|
approximateYears = null;
|
|
personKey = personKeys[i];
|
|
displayDirectoryName = "_ _ _";
|
|
personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKey);
|
|
}
|
|
if (personBirthday is null)
|
|
continue;
|
|
mapping = new(approximateYears, displayDirectoryName, face.Location.NormalizedPixelPercentage, personBirthday, personKey);
|
|
item.Mapping.Add(mapping);
|
|
}
|
|
}
|
|
if (Shared.Models.Stateless.IMapping.UseDeterministicHashCodeUnknownFaceKeyValuePairsForAddToMapping && !personKeys.Any())
|
|
{
|
|
if (!_DeterministicHashCodeUnknownFaceKeyValuePairs.ContainsKey(item.Property.Id.Value))
|
|
continue;
|
|
personKeys.AddRange(_DeterministicHashCodeUnknownFaceKeyValuePairs[item.Property.Id.Value]);
|
|
for (int i = 0; i < personKeys.Count; i++)
|
|
{
|
|
if (_PeopleKeyValuePairs.ContainsKey(personKeys[i]))
|
|
{
|
|
person = _PeopleKeyValuePairs[personKeys[i]];
|
|
personBirthday = person.PersonBirthdays[0];
|
|
approximateYears = person.ApproximateYears;
|
|
displayDirectoryName = person.DisplayDirectoryName;
|
|
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday);
|
|
}
|
|
else
|
|
{
|
|
approximateYears = null;
|
|
personKey = personKeys[i];
|
|
displayDirectoryName = "_ _ _";
|
|
personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKey);
|
|
}
|
|
if (personBirthday is null)
|
|
continue;
|
|
mapping = new(approximateYears, displayDirectoryName, personBirthday, personKey);
|
|
item.Mapping.Add(mapping);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<(Item, (string, Face?, (string, string, string, string))[])> GetCollection(List<Item> items, string dFacesContentDirectory)
|
|
{
|
|
List<(Item, (string, Face?, (string, string, string, string))[])> results = new();
|
|
int years;
|
|
Face face;
|
|
Item item;
|
|
string[] keys;
|
|
string directory;
|
|
string personKey;
|
|
bool? isWrongYear;
|
|
const int zero = 0;
|
|
TimeSpan? timeSpan;
|
|
string copyFileName;
|
|
string copyDirectory;
|
|
string? relativePath;
|
|
string isWrongYearFlag;
|
|
string shortcutFileName;
|
|
string subDirectoryName;
|
|
List<int> indices = new();
|
|
DateTime? minimumDateTime;
|
|
DateTime dateTime = DateTime.Now;
|
|
List<Face> faceCollection;
|
|
PersonBirthday? personBirthday;
|
|
List<(string, 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();
|
|
personKey = string.Empty;
|
|
directory = Path.Combine(dFacesContentDirectory, $"Unnamed{relativePath[2..]}");
|
|
}
|
|
else
|
|
{
|
|
faceCollection = item.Faces;
|
|
keys = _DeterministicHashCodeUnknownFaceKeyValuePairs[item.Property.Id.Value];
|
|
minimumDateTime = Shared.Models.Stateless.Methods.IProperty.GetMinimumDateTime(item.Property);
|
|
if (minimumDateTime is null)
|
|
continue;
|
|
(isWrongYear, _) = item.Property.IsWrongYear(item.ImageFileHolder.FullName, minimumDateTime.Value);
|
|
isWrongYearFlag = Shared.Models.Stateless.Methods.IItem.GetWrongYearFlag(isWrongYear);
|
|
subDirectoryName = $"{isWrongYearFlag}{minimumDateTime.Value:yyyy}";
|
|
if (!faceCollection.Any())
|
|
{
|
|
personKey = string.Empty;
|
|
directory = Path.Combine(dFacesContentDirectory, $"None{relativePath[2..]}", subDirectoryName);
|
|
}
|
|
else if (keys.Length != 1)
|
|
{
|
|
personKey = string.Empty;
|
|
directory = Path.Combine(dFacesContentDirectory, $"Not Supported{relativePath[2..]}", subDirectoryName);
|
|
}
|
|
else if (faceCollection.Count != 1)
|
|
{
|
|
personKey = string.Empty;
|
|
directory = Path.Combine(dFacesContentDirectory, $"Many{relativePath[2..]}", subDirectoryName);
|
|
}
|
|
else
|
|
{
|
|
indices.Add(zero);
|
|
personKey = keys[zero];
|
|
personBirthday = Shared.Models.Stateless.Methods.IPersonBirthday.GetPersonBirthday(personKey);
|
|
if (personBirthday is null)
|
|
continue;
|
|
timeSpan = Shared.Models.Stateless.Methods.IPersonBirthday.GetTimeSpan(minimumDateTime.Value, 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];
|
|
directory = Path.Combine(dFacesContentDirectory, "Shortcuts", personKey, subDirectoryName);
|
|
if (face.FaceEncoding is not null && face.Location?.NormalizedPixelPercentage is not null)
|
|
copyDirectory = Path.Combine(dFacesContentDirectory, "Images", personKey, subDirectoryName);
|
|
else
|
|
copyDirectory = Path.Combine(dFacesContentDirectory, "ImagesBut", personKey, subDirectoryName);
|
|
copyFileName = Path.Combine(copyDirectory, $"{item.Property.Id.Value}{item.ResizedFileHolder.ExtensionLowered}");
|
|
}
|
|
}
|
|
shortcutFileName = Path.Combine(directory, $"{item.Property.Id.Value}.lnk");
|
|
if (string.IsNullOrEmpty(personKey) || !indices.Any())
|
|
collection.Add(new(personKey, null, (directory, copyDirectory, copyFileName, shortcutFileName)));
|
|
else
|
|
{
|
|
foreach (int index in indices)
|
|
collection.Add(new(personKey, faceCollection[index], (directory, copyDirectory, copyFileName, shortcutFileName)));
|
|
}
|
|
results.Add(new(item, collection.ToArray()));
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public void AddToClosest(int maxDegreeOfParallelism, string argZero, List<Container> containers)
|
|
{
|
|
string key;
|
|
string dateKey;
|
|
Closest closest;
|
|
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.PersonKey))
|
|
continue;
|
|
dateKey = Stateless.MapLogic.GetDateKey(dateTime, closest.Mapping, closest.MinimumDateTime, closest.IsWrongYear);
|
|
key = string.Concat(closest.Mapping.PersonKey, dateKey);
|
|
if (!keyValuePairs.ContainsKey(key))
|
|
keyValuePairs.Add(key, 0);
|
|
else if (keyValuePairs[key] > Shared.Models.Stateless.IClosest.MaximumPer)
|
|
continue;
|
|
keyValuePairs[key] += 1;
|
|
item.Closest.Add(closest);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private List<(IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json)> GetClosest(string argZero, List<Container> containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
|
|
{
|
|
List<(IFileHolder?, string, FileInfo?, string, string, string)> results = new();
|
|
Closest? match;
|
|
string dateKey;
|
|
string checkFile;
|
|
string directory;
|
|
string shortcutFile;
|
|
FileInfo faceFileInfo;
|
|
string? directoryName;
|
|
string facesDirectory;
|
|
string personDirectory;
|
|
List<int> used = new();
|
|
FileInfo landmarkFileInfo;
|
|
string landmarksDirectory;
|
|
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.NormalizedPixelPercentage is null || mapping.NormalizedPixelPercentage.Value != face.Location.NormalizedPixelPercentage.Value)
|
|
continue;
|
|
throw new Exception();
|
|
}
|
|
dateKey = Stateless.MapLogic.GetDateKey(dateTime, match.Mapping, match.MinimumDateTime, match.IsWrongYear);
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, facePopulatedKey, match.Mapping.PersonKey, dateKey);
|
|
personDirectory = Path.Combine(directory, match.Mapping.DisplayDirectoryName[..1], "lnk");
|
|
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
|
|
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
|
|
landmarksDirectory = 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}");
|
|
faceFileInfo = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.png"));
|
|
landmarkFileInfo = new(Path.Combine(landmarksDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.gif"));
|
|
if (string.IsNullOrEmpty(personDirectory))
|
|
shortcutFile = string.Empty;
|
|
else
|
|
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
|
|
results.Add(new(item.ResizedFileHolder, directory, faceFileInfo, checkFile, shortcutFile, string.Empty));
|
|
personDirectory = Path.Combine(directory, match.Mapping.DisplayDirectoryName[..1], "lnk", match.Mapping.DisplayDirectoryName);
|
|
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
|
|
used.Add(face.Location.NormalizedPixelPercentage.Value);
|
|
}
|
|
foreach (Closest closest in item.Closest)
|
|
{
|
|
if (used.Contains(closest.NormalizedPixelPercentage))
|
|
continue;
|
|
dateKey = Stateless.MapLogic.GetDateKey(dateTime, closest.Mapping, closest.MinimumDateTime, closest.IsWrongYear);
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, facePopulatedKey, closest.Mapping.PersonKey, dateKey);
|
|
personDirectory = Path.Combine(directory, closest.Mapping.DisplayDirectoryName, "lnk");
|
|
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
|
|
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
|
|
landmarksDirectory = 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}");
|
|
faceFileInfo = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.png"));
|
|
landmarkFileInfo = new(Path.Combine(landmarksDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.gif"));
|
|
if (string.IsNullOrEmpty(personDirectory))
|
|
shortcutFile = string.Empty;
|
|
else
|
|
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
|
|
results.Add(new(item.ResizedFileHolder, directory, faceFileInfo, checkFile, shortcutFile, string.Empty));
|
|
personDirectory = Path.Combine(directory, closest.Mapping.DisplayDirectoryName[..1], "lnk", closest.Mapping.DisplayDirectoryName);
|
|
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
|
|
used.Add(closest.NormalizedPixelPercentage);
|
|
}
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private List<(IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json)> GetMapping(string argZero, List<Container> containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
|
|
{
|
|
List<(IFileHolder?, string, FileInfo?, string, string, string)> results = new();
|
|
string key;
|
|
string json;
|
|
string dateKey;
|
|
Mapping? match;
|
|
string checkFile;
|
|
string directory;
|
|
bool? isWrongYear;
|
|
string shortcutFile;
|
|
FileInfo faceFileInfo;
|
|
string? directoryName;
|
|
string facesDirectory;
|
|
string personDirectory;
|
|
List<int> used = new();
|
|
DateTime minimumDateTime;
|
|
FileInfo landmarkFileInfo;
|
|
string landmarksDirectory;
|
|
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.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);
|
|
key = string.Concat(match.PersonKey, dateKey);
|
|
if (match.Filtered is null)
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}Null", match.PersonKey, dateKey);
|
|
else if (!match.Filtered.Value)
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}Okay", match.PersonKey, dateKey);
|
|
else
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}OutOfControl", match.PersonKey, dateKey);
|
|
personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk");
|
|
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
|
|
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
|
|
landmarksDirectory = 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}");
|
|
faceFileInfo = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.png"));
|
|
landmarkFileInfo = new(Path.Combine(landmarksDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.gif"));
|
|
if (string.IsNullOrEmpty(personDirectory))
|
|
shortcutFile = string.Empty;
|
|
else
|
|
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
|
|
results.Add(new(item.ResizedFileHolder, directory, faceFileInfo, checkFile, shortcutFile, string.Empty));
|
|
if (!string.IsNullOrEmpty(checkFile) && Shared.Models.Stateless.IMapping.SaveFaceEncoding)
|
|
{
|
|
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.json");
|
|
json = JsonSerializer.Serialize(face.FaceEncoding);
|
|
results.Add(new(null, directory, null, checkFile, string.Empty, json));
|
|
}
|
|
personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk", match.DisplayDirectoryName);
|
|
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
|
|
used.Add(face.Location.NormalizedPixelPercentage.Value);
|
|
}
|
|
if (deterministicHashCodeKeyValuePairsAny && Shared.Models.Stateless.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.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);
|
|
if (match.Filtered is null)
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}WithButNull", match.PersonKey, dateKey);
|
|
else if (!match.Filtered.Value)
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}WithAndOkay", match.PersonKey, dateKey);
|
|
else
|
|
directory = Path.Combine(zPropertyHolderContentDirectory, $"{facePopulatedKey}WithButOutOfControl", match.PersonKey, dateKey);
|
|
personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk");
|
|
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
|
|
facesDirectory = string.Concat(dFacesContentDirectory, Path.Combine(directoryName, item.ImageFileHolder.NameWithoutExtension));
|
|
landmarksDirectory = 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}");
|
|
faceFileInfo = new(Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.png"));
|
|
landmarkFileInfo = new(Path.Combine(landmarksDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.gif"));
|
|
if (string.IsNullOrEmpty(personDirectory))
|
|
shortcutFile = string.Empty;
|
|
else
|
|
shortcutFile = Path.Combine(personDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.lnk");
|
|
results.Add(new(item.ResizedFileHolder, directory, faceFileInfo, checkFile, shortcutFile, string.Empty));
|
|
if (!string.IsNullOrEmpty(checkFile) && Shared.Models.Stateless.IMapping.SaveFaceEncoding)
|
|
{
|
|
checkFile = Path.Combine(directory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}.json");
|
|
json = JsonSerializer.Serialize(face.FaceEncoding);
|
|
results.Add(new(null, directory, null, checkFile, string.Empty, json));
|
|
}
|
|
personDirectory = Path.Combine(directory, match.DisplayDirectoryName[..1], "lnk", match.DisplayDirectoryName);
|
|
results.Add(new(null, personDirectory, null, string.Empty, string.Empty, string.Empty));
|
|
used.Add(face.Location.NormalizedPixelPercentage.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private static void Save(List<(IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json)> collection)
|
|
{
|
|
WindowsShortcut windowsShortcut;
|
|
string[] directories = (from l in collection select l.directory).Distinct().ToArray();
|
|
foreach (string directory in directories)
|
|
{
|
|
if (string.IsNullOrEmpty(directory))
|
|
continue;
|
|
if (!Directory.Exists(directory))
|
|
_ = Directory.CreateDirectory(directory);
|
|
}
|
|
foreach ((IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json) in collection)
|
|
{
|
|
if (string.IsNullOrEmpty(directory) || string.IsNullOrEmpty(checkFile) || resizedFileHolder is null || faceFileInfo is null || !string.IsNullOrEmpty(json))
|
|
continue;
|
|
if (File.Exists(checkFile))
|
|
continue;
|
|
if (faceFileInfo.Directory is not null && faceFileInfo.Directory.Exists && faceFileInfo.Exists)
|
|
File.Copy(faceFileInfo.FullName, checkFile);
|
|
else
|
|
File.Copy(resizedFileHolder.FullName, checkFile);
|
|
}
|
|
foreach ((IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json) in collection)
|
|
{
|
|
if (string.IsNullOrEmpty(directory) || string.IsNullOrEmpty(checkFile) || resizedFileHolder is not null || faceFileInfo is not null || string.IsNullOrEmpty(json))
|
|
continue;
|
|
if (File.Exists(checkFile))
|
|
continue;
|
|
_ = Shared.Models.Stateless.Methods.IPath.WriteAllText(checkFile, json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null);
|
|
}
|
|
foreach ((IFileHolder? resizedFileHolder, string directory, FileInfo? _, string checkFile, string shortcutFile, string json) in collection)
|
|
{
|
|
if (string.IsNullOrEmpty(directory) || string.IsNullOrEmpty(checkFile) || resizedFileHolder is null)
|
|
continue;
|
|
if (string.IsNullOrEmpty(shortcutFile) || !resizedFileHolder.Exists)
|
|
continue;
|
|
try
|
|
{
|
|
windowsShortcut = new() { Path = resizedFileHolder.FullName };
|
|
windowsShortcut.Save(shortcutFile);
|
|
windowsShortcut.Dispose();
|
|
}
|
|
catch (Exception)
|
|
{ }
|
|
}
|
|
}
|
|
|
|
public void SaveClosest(string argZero, List<Container> containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
|
|
{
|
|
List<(IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json)> collection = GetClosest(argZero, containers, dFacesContentDirectory, d2ResultsFullGroupDirectory, zPropertyHolderContentDirectory);
|
|
Save(collection);
|
|
}
|
|
|
|
public void SaveMapping(string argZero, List<Container> containers, string dFacesContentDirectory, string d2ResultsFullGroupDirectory, string zPropertyHolderContentDirectory)
|
|
{
|
|
List<(IFileHolder? resizedFileHolder, string directory, FileInfo? faceFileInfo, string checkFile, string shortcutFile, string json)> collection = GetMapping(argZero, containers, dFacesContentDirectory, d2ResultsFullGroupDirectory, zPropertyHolderContentDirectory);
|
|
Save(collection);
|
|
}
|
|
|
|
} |