Save Grouped Face Encodings

This commit is contained in:
Mike Phares 2022-07-31 00:28:29 -07:00
parent e4140b61e3
commit 022d9904da
6 changed files with 178 additions and 54 deletions

View File

@ -48,6 +48,7 @@ public class DlibDotNet
_FileKeyValuePairs = new List<KeyValuePair<string, string>>();
_FilePropertiesKeyValuePairs = new Dictionary<string, List<Tuple<string, A_Property>>>();
Property.Models.Configuration propertyConfiguration = Property.Models.Stateless.Configuration.Get(isEnvironment, configurationRoot, workingDirectory);
_Log.Information(propertyConfiguration.RootDirectory);
Property.Models.Configuration.Verify(propertyConfiguration);
Models.Configuration configuration = Models.Stateless.Configuration.Get(isEnvironment, configurationRoot, workingDirectory, propertyConfiguration);
Verify(configuration);
@ -101,9 +102,10 @@ public class DlibDotNet
{
E2_Navigate e2Navigate = new(console, configuration, argZero);
e2Navigate.Navigate(propertyConfiguration, model, predictorModel, configuration.OutputResolutions[0]);
_Log.Information(propertyConfiguration.RootDirectory);
}
if (!configuration.SkipSearch.Value)
Search(propertyConfiguration, configuration.JuliePhares, configuration.Reverse.Value, model, predictorModel, argZero, people);
Search(propertyConfiguration, configuration.Reverse.Value, model, predictorModel, argZero, people);
if (_Exceptions.Count == 0 && _ArgZeroIsConfigurationRootDirectory)
{
long ticks = DateTime.Now.Ticks;
@ -549,7 +551,7 @@ public class DlibDotNet
}
}
private void FullDoWork(Property.Models.Configuration configuration, string[] juliePhares, Model model, PredictorModel predictorModel, string argZero, Dictionary<string, List<Person>> peopleCollection, PropertyLogic propertyLogic, List<PropertyHolder[]> propertyHolderCollections)
private void FullDoWork(Property.Models.Configuration configuration, Model model, PredictorModel predictorModel, string argZero, Dictionary<string, List<Person>> peopleCollection, PropertyLogic propertyLogic, List<PropertyHolder[]> propertyHolderCollections)
{
if (_Log is null)
throw new Exception($"{nameof(_Log)} is null!");
@ -561,12 +563,10 @@ public class DlibDotNet
object @lock = new();
string sourceDirectory;
long ticks = DateTime.Now.Ticks;
string modelName = model.ToString();
List<List<D_Face>> faceCollections = new();
List<A_Property> propertyCollection = new();
PropertyHolder[] filteredPropertyHolderCollection;
List<FileInfo?> propertyFileInfoCollection = new();
string predictorModelName = predictorModel.ToString();
List<Dictionary<string, int[]>> resizeKeyValuePairs = new();
List<Tuple<string, DateTime>> sourceDirectoryChanges = new();
List<List<KeyValuePair<string, string>>> metadataCollection = new();
@ -668,10 +668,29 @@ public class DlibDotNet
_Exceptions.Add(sourceDirectory);
if (_ArgZeroIsConfigurationRootDirectory && exceptionCount == 0)
WriteGroup(configuration, propertyLogic, propertyCollection, metadataCollection, faceCollections, resizeKeyValuePairs, sourceDirectory, outputResolution, filteredPropertyHolderCollection);
if (_ArgZeroIsConfigurationRootDirectory && outputResolution == _Configuration.OutputResolutions[0] && exceptionCount == 0)
if (_ArgZeroIsConfigurationRootDirectory && exceptionCount == 0 && outputResolution == _Configuration.OutputResolutions[0])
propertyLogic.AddToPropertyLogicAllCollection(filteredPropertyHolderCollection);
if (exceptionCount == 0 && _Configuration.SaveShortcutsForOutputResolutions.Contains(outputResolution) && propertyLogic.NamedFaceInfoDeterministicHashCodeIndices.Any())
_Faces.SaveShortcuts(configuration, juliePhares, model, predictorModel, propertyLogic, peopleCollection, outputResolution, filteredPropertyHolderCollection, propertyCollection, faceCollections);
if (_ArgZeroIsConfigurationRootDirectory && exceptionCount == 0 && propertyLogic.NamedFaceInfoDeterministicHashCodeIndices.Any())
{
if (outputResolution == _Configuration.OutputResolutions[0] || _Configuration.SaveShortcutsForOutputResolutions.Contains(outputResolution))
{
List<(PropertyHolder, (string, D_Face?, (string, string, string, string))[])> collections = D_Face.GetCollection(configuration, model, predictorModel, propertyLogic, peopleCollection, outputResolution, filteredPropertyHolderCollection, faceCollections);
if (outputResolution == _Configuration.OutputResolutions[0])
{
foreach ((PropertyHolder propertyHolder, (string, D_Face?, (string, string, string, string))[] collection) in collections)
{
foreach ((string personKey, D_Face? face, (string directory, string copyDirectory, string copyFileName, string shortcutFileName)) in collection)
{
if (string.IsNullOrEmpty(personKey) || face is null || string.IsNullOrEmpty(directory))
continue;
propertyHolder.AddNamed(directory, personKey, face);
}
}
}
if (_Configuration.SaveShortcutsForOutputResolutions.Contains(outputResolution))
D_Face.SaveShortcuts(_Configuration.JuliePhares, peopleCollection, collections);
}
}
if (exceptionCount == 0 && _Configuration.LoadOrCreateThenSaveDistanceResultsForOutputResolutions.Contains(outputResolution))
_Distance.LoadOrCreateThenSaveDistanceResultsForOutputResolutions(configuration, model, predictorModel, sourceDirectory, outputResolution, sourceDirectoryChanges, filteredPropertyHolderCollection, faceCollections);
if (_Resize.AngleBracketCollection.Any())
@ -690,14 +709,12 @@ public class DlibDotNet
}
_Log.Information(". . .");
}
}
if (_ArgZeroIsConfigurationRootDirectory && _Configuration.SaveShortcutsForOutputResolutions.Contains(outputResolution))
{
}
if (_ArgZeroIsConfigurationRootDirectory && outputResolution == _Configuration.OutputResolutions[0])
{
propertyLogic.SaveAllCollection();
if (propertyLogic.NamedFaceInfoDeterministicHashCodeIndices.Any())
E_Distance.SaveGroupedFaceEncodings(configuration, model, predictorModel, argZero, peopleCollection, outputResolution, propertyHolderCollections);
if (!_Configuration.LoadOrCreateThenSaveImageFacesResultsForOutputResolutions.Any())
break;
if (_Exceptions.Count == 0)
@ -728,12 +745,12 @@ public class DlibDotNet
return result;
}
private void Search(Property.Models.Configuration configuration, string[] juliePhares, bool reverse, Model model, PredictorModel predictorModel, string argZero, Person[] people)
private void Search(Property.Models.Configuration configuration, bool reverse, Model model, PredictorModel predictorModel, string argZero, Person[] people)
{
PropertyLogic propertyLogic = GetPropertyLogic();
Dictionary<string, List<Person>> peopleCollection = A2_People.Convert(people);
List<PropertyHolder[]> propertyHolderCollections = Property.Models.Stateless.A_Property.Get(configuration, reverse, model, predictorModel, propertyLogic);
FullDoWork(configuration, juliePhares, model, predictorModel, argZero, peopleCollection, propertyLogic, propertyHolderCollections);
FullDoWork(configuration, model, predictorModel, argZero, peopleCollection, propertyLogic, propertyHolderCollections);
}
internal void RenameQueue(Property.Models.Configuration configuration, Model model, PredictorModel predictorModel) => _Rename.RenameQueue(configuration, model, predictorModel);

View File

@ -503,42 +503,47 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
SaveFaces(faceCollection, resizedFileInfo, imageFiles);
}
internal void SaveShortcuts(Property.Models.Configuration configuration, string[] juliePhares, Model model, PredictorModel predictorModel, PropertyLogic propertyLogic, Dictionary<string, List<Person>> peopleCollection, string outputResolution, PropertyHolder[] filteredPropertyHolderCollection, List<A_Property> propertyCollection, List<List<D_Face>> faceCollections)
internal static List<(PropertyHolder, (string, D_Face?, (string, string, string, string))[])> GetCollection(Property.Models.Configuration configuration, Model model, PredictorModel predictorModel, PropertyLogic propertyLogic, Dictionary<string, List<Person>> peopleCollection, string outputResolution, PropertyHolder[] filteredPropertyHolderCollection, List<List<D_Face>> faceCollections)
{
List<(PropertyHolder, (string, D_Face?, (string, string, string, string))[])> results = new();
string[] keys;
string fileName;
string fullName;
string personKey;
string directory;
bool? isWrongYear;
FileInfo fileInfo;
TimeSpan timeSpan;
string copyFileName;
DateTime? birthDate;
string copyDirectory;
string? relativePath;
string isWrongYearFlag;
string shortcutFileName;
string subDirectoryName;
DateTime minimumDateTime;
List<int> indices = new();
List<D_Face> faceCollection;
PropertyHolder propertyHolder;
WindowsShortcut windowsShortcut;
const string pattern = @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]";
List<(string, D_Face?, (string, string, string, string))> collection;
string dFacesContentDirectory = Path.Combine(Property.Models.Stateless.IResult.GetResultsFullGroupDirectory(configuration, model, predictorModel, nameof(D_Face), outputResolution, includeResizeGroup: true, includeModel: true, includePredictorModel: true), "(_)");
for (int i = 0; i < filteredPropertyHolderCollection.Length; i++)
{
indices.Clear();
personKey = string.Empty;
copyFileName = string.Empty;
copyDirectory = string.Empty;
propertyHolder = filteredPropertyHolderCollection[i];
if (propertyHolder.ImageFileInfo is null)
continue;
fileInfo = propertyHolder.ImageFileInfo;
relativePath = Path.GetDirectoryName($"C:{propertyHolder.RelativePath}");
if (string.IsNullOrEmpty(relativePath) || relativePath.Length < 3)
continue;
if (propertyHolder.Property?.Id is null || propertyHolder.MinimumDateTime is null || propertyHolder.ResizedFileInfo is null)
continue;
collection = new();
if (!propertyLogic.NamedFaceInfoDeterministicHashCodeIndices.ContainsKey(propertyHolder.Property.Id.Value))
{
faceCollection = new();
directory = Path.Combine(dFacesContentDirectory, $"Unnamed{relativePath[2..]}");
}
else
{
faceCollection = faceCollections[i];
@ -555,6 +560,7 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
directory = Path.Combine(dFacesContentDirectory, $"Many{relativePath[2..]}", subDirectoryName);
else
{
indices.Add(0);
personKey = keys[0];
if (isWrongYear is not null && !isWrongYear.Value && personKey[..2] is "19" or "20")
{
@ -570,38 +576,69 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
}
}
}
directory = Path.Combine(dFacesContentDirectory, "Named Shortcuts", personKey, subDirectoryName);
if (juliePhares.Contains(personKey))
copyDirectory = Path.Combine(dFacesContentDirectory, "Named Images", personKey, subDirectoryName);
directory = Path.Combine(dFacesContentDirectory, "Shortcuts", personKey, subDirectoryName);
copyDirectory = Path.Combine(dFacesContentDirectory, "Images", personKey, subDirectoryName);
copyFileName = Path.Combine(copyDirectory, $"{propertyHolder.Property.Id.Value}{propertyHolder.ResizedFileInfo.Extension}");
}
}
if (!Directory.Exists(directory))
shortcutFileName = Path.Combine(directory, $"{propertyHolder.Property.Id.Value}.lnk");
if (string.IsNullOrEmpty(personKey) || !indices.Any())
collection.Add(new(personKey, null, (directory, copyDirectory, copyFileName, shortcutFileName)));
else
{
_ = Directory.CreateDirectory(directory);
if (!string.IsNullOrEmpty(personKey) && peopleCollection.ContainsKey(personKey))
foreach (int index in indices)
collection.Add(new(personKey, faceCollection[index], (directory, copyDirectory, copyFileName, shortcutFileName)));
}
results.Add(new(propertyHolder, collection.ToArray()));
}
return results;
}
internal static void SaveShortcuts(string[] juliePhares, Dictionary<string, List<Person>> peopleCollection, List<(PropertyHolder, (string, D_Face?, (string Directory, string CopyDirectory, string CopyFileName, string ShortcutFileName))[])> collections)
{
Person person;
string fileName;
string fullName;
WindowsShortcut windowsShortcut;
const string pattern = @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]";
foreach ((PropertyHolder propertyHolder, (string personKey, D_Face? face, (string, string, string, string))[] collection) in collections)
{
if (collection.Length != 1)
continue;
foreach ((string personKey, D_Face? face, (string directory, string copyDirectory, string copyFileName, string shortcutFileName)) in collection)
{
if (string.IsNullOrEmpty(personKey))
continue;
if (propertyHolder.Property?.Id is null || propertyHolder.ImageFileInfo is null || propertyHolder.MinimumDateTime is null || propertyHolder.ResizedFileInfo is null)
continue;
if (!Directory.Exists(directory))
{
Person person = peopleCollection[personKey][0];
fullName = Regex.Replace($"{Shared.Models.Stateless.Methods.IPersonName.GetFullName(person.Name)}.txt", pattern, string.Empty);
File.WriteAllText(Path.Combine(directory, fullName), string.Empty);
_ = Directory.CreateDirectory(directory);
if (!string.IsNullOrEmpty(personKey) && peopleCollection.ContainsKey(personKey))
{
person = peopleCollection[personKey][0];
fullName = Regex.Replace($"{Shared.Models.Stateless.Methods.IPersonName.GetFullName(person.Name)}.txt", pattern, string.Empty);
File.WriteAllText(Path.Combine(directory, fullName), string.Empty);
}
}
}
if (!string.IsNullOrEmpty(copyDirectory))
{
if (!Directory.Exists(copyDirectory))
_ = Directory.CreateDirectory(copyDirectory);
fileName = Path.Combine(copyDirectory, $"{propertyHolder.Property.Id.Value}{propertyHolder.ResizedFileInfo.Extension}");
if (juliePhares.Contains(personKey) && !string.IsNullOrEmpty(copyDirectory))
{
if (!Directory.Exists(copyDirectory))
_ = Directory.CreateDirectory(copyDirectory);
fileName = Path.Combine(copyDirectory, $"{propertyHolder.Property.Id.Value}{propertyHolder.ResizedFileInfo.Extension}");
if (!File.Exists(fileName))
File.Copy(propertyHolder.ResizedFileInfo.FullName, fileName);
}
fileName = Path.Combine(directory, $"{propertyHolder.Property.Id.Value}.lnk");
if (File.Exists(fileName))
continue;
windowsShortcut = new() { Path = propertyHolder.ImageFileInfo.FullName };
windowsShortcut.Save(fileName);
windowsShortcut.Dispose();
if (!File.Exists(fileName))
File.Copy(propertyHolder.ResizedFileInfo.FullName, fileName);
continue;
File.SetLastWriteTime(fileName, propertyHolder.MinimumDateTime.Value);
}
fileName = Path.Combine(directory, $"{propertyHolder.Property.Id.Value}.lnk");
if (File.Exists(fileName))
continue;
windowsShortcut = new() { Path = fileInfo.FullName };
windowsShortcut.Save(fileName);
windowsShortcut.Dispose();
if (!File.Exists(fileName))
continue;
File.SetLastWriteTime(fileName, propertyHolder.MinimumDateTime.Value);
}
}

View File

@ -45,8 +45,6 @@ internal class E2_Navigate
string? rootResultsDirectory = Path.GetDirectoryName(Property.Models.Stateless.IResult.GetResultsGroupDirectory(configuration, nameof(B_Metadata)));
if (string.IsNullOrEmpty(rootResultsDirectory))
throw new Exception();
string modelName = model.ToString();
string predictorModelName = predictorModel.ToString();
string rootResultsDirectoryAbsoluteUri = new Uri(rootResultsDirectory).AbsoluteUri;
string dFacesContentDirectory = Path.Combine(Property.Models.Stateless.IResult.GetResultsFullGroupDirectory(configuration, model, predictorModel, nameof(D_Face), outputResolution, includeResizeGroup: true, includeModel: true, includePredictorModel: true), "()");
string cResizeContentDirectory = Path.Combine(Property.Models.Stateless.IResult.GetResultsFullGroupDirectory(configuration, model, predictorModel, nameof(C_Resize), outputResolution, includeResizeGroup: true, includeModel: false, includePredictorModel: false), "()");
@ -78,8 +76,6 @@ internal class E2_Navigate
string? rootResultsDirectory = Path.GetDirectoryName(Property.Models.Stateless.IResult.GetResultsGroupDirectory(configuration, nameof(B_Metadata)));
if (string.IsNullOrEmpty(rootResultsDirectory))
throw new Exception();
string modelName = model.ToString();
string predictorModelName = predictorModel.ToString();
string rootResultsDirectoryAbsoluteUri = new Uri(rootResultsDirectory).AbsoluteUri;
string dFacesContentDirectory = Path.Combine(Property.Models.Stateless.IResult.GetResultsFullGroupDirectory(configuration, model, predictorModel, nameof(D_Face), outputResolution, includeResizeGroup: true, includeModel: true, includePredictorModel: true), "()");
string cResizeContentDirectory = Path.Combine(Property.Models.Stateless.IResult.GetResultsFullGroupDirectory(configuration, model, predictorModel, nameof(C_Resize), outputResolution, includeResizeGroup: true, includeModel: false, includePredictorModel: false), "()");

View File

@ -42,10 +42,8 @@ internal class E3_Rename
string aPropertySingletonDirectory;
string bMetadataSingletonDirectory;
string eDistanceCollectionDirectory;
string modelName = model.ToString();
string g2IdentifyCollectionDirectory;
string d2FaceLandmarksContentDirectory;
string predictorModelName = predictorModel.ToString();
add = Directory.Exists(string.Concat(Path.Combine(Property.Models.Stateless.IResult.GetResultsFullGroupDirectory(configuration, model, predictorModel, nameof(C_Resize), _Configuration.ValidResolutions[0], includeResizeGroup: true, includeModel: false, includePredictorModel: false), "()"), relativePath));
bMetadataSingletonDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(B_Metadata), "{}");
if (Directory.Exists(bMetadataSingletonDirectory))
@ -129,11 +127,9 @@ internal class E3_Rename
string eDistanceContentDirectory;
string bMetadataSingletonDirectory;
string aPropertySingletonDirectory;
string modelName = model.ToString();
string eDistanceCollectionDirectory;
string g2IdentifyCollectionDirectory;
string d2FaceLandmarksContentDirectory;
string predictorModelName = predictorModel.ToString();
if (!string.IsNullOrEmpty(relativePath))
{
from = string.Concat(_Configuration.PropertyConfiguration.RootDirectory, relativePath);

View File

@ -1,5 +1,6 @@
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using View_by_Distance.FaceRecognitionDotNet;
using View_by_Distance.Metadata.Models;
using View_by_Distance.Property.Models;
@ -400,4 +401,73 @@ internal class E_Distance
}
}
private static Dictionary<string, List<(string directory, string personKey, D_Face face)>> Convert(string argZero, List<PropertyHolder[]> propertyHolderCollections)
{
Dictionary<string, List<(string directory, string personKey, D_Face face)>> results = new();
string key;
foreach (PropertyHolder[] propertyHolderCollection in propertyHolderCollections)
{
if (!propertyHolderCollection.Any())
continue;
if (!propertyHolderCollection[0].SourceDirectory.StartsWith(argZero))
continue;
foreach (PropertyHolder propertyHolder in propertyHolderCollection)
{
if (propertyHolder.ImageFileInfo is null || propertyHolder.Property is null || !propertyHolder.Named.Any())
continue;
foreach ((string directory, string personKey, object @object) in propertyHolder.Named)
{
if (string.IsNullOrEmpty(directory) || string.IsNullOrEmpty(personKey) || !directory.Contains(personKey))
continue;
if (@object is not D_Face face || face.FaceEncoding is null)
continue;
key = directory.Split(personKey)[1];
if (!results.ContainsKey(key))
results.Add(key, new());
results[key].Add(new(directory, personKey, face));
}
}
}
return results;
}
internal static void SaveGroupedFaceEncodings(Property.Models.Configuration configuration, Model model, PredictorModel predictorModel, string argZero, Dictionary<string, List<Shared.Models.Person>> peopleCollection, string outputResolution, List<PropertyHolder[]> propertyHolderCollections)
{
string json;
string checkFile;
string directory;
Shared.Models.Person person;
List<string> checkDirectories = new();
List<Shared.Models.Properties.IFace> collection;
const string pattern = @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]";
string eDistanceCollectionDirectory = Path.Combine(Property.Models.Stateless.IResult.GetResultsFullGroupDirectory(configuration, model, predictorModel, nameof(E_Distance), outputResolution, includeResizeGroup: true, includeModel: true, includePredictorModel: true), "[_]");
Dictionary<string, List<(string directory, string personKey, D_Face face)>> keyValuePairs = Convert(argZero, propertyHolderCollections);
foreach (KeyValuePair<string, List<(string directory, string personKey, D_Face face)>> keyValuePair in keyValuePairs)
{
collection = new();
checkDirectories.Clear();
checkFile = string.Empty;
foreach ((string _, string personKey, D_Face face) in keyValuePair.Value)
{
if (string.IsNullOrEmpty(personKey) || !peopleCollection.ContainsKey(personKey))
continue;
person = peopleCollection[personKey][0];
directory = Path.Combine(eDistanceCollectionDirectory, $"{personKey}{keyValuePair.Key}");
checkFile = Path.Combine(directory, Regex.Replace($"{Shared.Models.Stateless.Methods.IPersonName.GetFullName(person.Name)}.json", pattern, string.Empty));
checkDirectories.Add(directory);
collection.Add(face);
}
if (!string.IsNullOrEmpty(checkFile) && checkDirectories.Any())
{
foreach (string checkDirectory in checkDirectories)
{
if (!Directory.Exists(checkDirectory))
_ = Directory.CreateDirectory(checkDirectory);
}
json = JsonSerializer.Serialize(collection, new JsonSerializerOptions { WriteIndented = true });
_ = Property.Models.Stateless.IPath.WriteAllText(checkFile, json, compareBeforeWrite: true);
}
}
}
}

View File

@ -7,10 +7,12 @@ public class PropertyHolder
protected readonly bool? _Abandoned;
protected readonly bool? _Changed;
protected object? _Face;
protected FileInfo? _ImageFileInfo;
protected readonly int _G;
protected DateTime? _MinimumDateTime;
protected bool? _Moved;
protected List<(string Directory, string PersonKey, object Object)> _Named;
protected readonly bool? _NoJson;
protected A_Property? _Property;
protected readonly int _R;
@ -22,11 +24,13 @@ public class PropertyHolder
protected bool? _WrongYear;
public bool? Abandoned => _Abandoned;
public bool? Changed => _Changed;
public object? Face => _Face;
public FileInfo? ImageFileInfo => _ImageFileInfo;
public int G => _G;
public DateTime? MinimumDateTime => _MinimumDateTime;
public bool? Moved => _Moved;
public bool? NoJson => _NoJson;
public List<(string Directory, string PersonKey, object Object)> Named => _Named;
public A_Property? Property => _Property;
public int R => _R;
public string RelativePath => _RelativePath;
@ -39,9 +43,10 @@ public class PropertyHolder
public PropertyHolder()
{
_G = -1;
_Named = new();
_RelativePath = string.Empty;
_SourceDirectory = string.Empty;
_SourceDirectoryFile = string.Empty;
_RelativePath = string.Empty;
_R = -1;
}
@ -54,6 +59,7 @@ public class PropertyHolder
_G = g;
_Moved = moved;
_NoJson = abandoned is null;
_Named = new();
_Property = property;
_R = r;
_RelativePath = relativePath;
@ -80,4 +86,6 @@ public class PropertyHolder
public bool Any() => (_Abandoned.HasValue && _Abandoned.Value) || (_Changed.HasValue && _Changed.Value) || (_Moved.HasValue && _Moved.Value) || (_NoJson.HasValue && _NoJson.Value);
public void AddNamed(string directory, string personKey, object face) => _Named.Add(new(directory, personKey, face));
}