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>>(); _FileKeyValuePairs = new List<KeyValuePair<string, string>>();
_FilePropertiesKeyValuePairs = new Dictionary<string, List<Tuple<string, A_Property>>>(); _FilePropertiesKeyValuePairs = new Dictionary<string, List<Tuple<string, A_Property>>>();
Property.Models.Configuration propertyConfiguration = Property.Models.Stateless.Configuration.Get(isEnvironment, configurationRoot, workingDirectory); Property.Models.Configuration propertyConfiguration = Property.Models.Stateless.Configuration.Get(isEnvironment, configurationRoot, workingDirectory);
_Log.Information(propertyConfiguration.RootDirectory);
Property.Models.Configuration.Verify(propertyConfiguration); Property.Models.Configuration.Verify(propertyConfiguration);
Models.Configuration configuration = Models.Stateless.Configuration.Get(isEnvironment, configurationRoot, workingDirectory, propertyConfiguration); Models.Configuration configuration = Models.Stateless.Configuration.Get(isEnvironment, configurationRoot, workingDirectory, propertyConfiguration);
Verify(configuration); Verify(configuration);
@ -101,9 +102,10 @@ public class DlibDotNet
{ {
E2_Navigate e2Navigate = new(console, configuration, argZero); E2_Navigate e2Navigate = new(console, configuration, argZero);
e2Navigate.Navigate(propertyConfiguration, model, predictorModel, configuration.OutputResolutions[0]); e2Navigate.Navigate(propertyConfiguration, model, predictorModel, configuration.OutputResolutions[0]);
_Log.Information(propertyConfiguration.RootDirectory);
} }
if (!configuration.SkipSearch.Value) 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) if (_Exceptions.Count == 0 && _ArgZeroIsConfigurationRootDirectory)
{ {
long ticks = DateTime.Now.Ticks; 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) if (_Log is null)
throw new Exception($"{nameof(_Log)} is null!"); throw new Exception($"{nameof(_Log)} is null!");
@ -561,12 +563,10 @@ public class DlibDotNet
object @lock = new(); object @lock = new();
string sourceDirectory; string sourceDirectory;
long ticks = DateTime.Now.Ticks; long ticks = DateTime.Now.Ticks;
string modelName = model.ToString();
List<List<D_Face>> faceCollections = new(); List<List<D_Face>> faceCollections = new();
List<A_Property> propertyCollection = new(); List<A_Property> propertyCollection = new();
PropertyHolder[] filteredPropertyHolderCollection; PropertyHolder[] filteredPropertyHolderCollection;
List<FileInfo?> propertyFileInfoCollection = new(); List<FileInfo?> propertyFileInfoCollection = new();
string predictorModelName = predictorModel.ToString();
List<Dictionary<string, int[]>> resizeKeyValuePairs = new(); List<Dictionary<string, int[]>> resizeKeyValuePairs = new();
List<Tuple<string, DateTime>> sourceDirectoryChanges = new(); List<Tuple<string, DateTime>> sourceDirectoryChanges = new();
List<List<KeyValuePair<string, string>>> metadataCollection = new(); List<List<KeyValuePair<string, string>>> metadataCollection = new();
@ -668,10 +668,29 @@ public class DlibDotNet
_Exceptions.Add(sourceDirectory); _Exceptions.Add(sourceDirectory);
if (_ArgZeroIsConfigurationRootDirectory && exceptionCount == 0) if (_ArgZeroIsConfigurationRootDirectory && exceptionCount == 0)
WriteGroup(configuration, propertyLogic, propertyCollection, metadataCollection, faceCollections, resizeKeyValuePairs, sourceDirectory, outputResolution, filteredPropertyHolderCollection); 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); propertyLogic.AddToPropertyLogicAllCollection(filteredPropertyHolderCollection);
if (exceptionCount == 0 && _Configuration.SaveShortcutsForOutputResolutions.Contains(outputResolution) && propertyLogic.NamedFaceInfoDeterministicHashCodeIndices.Any()) if (_ArgZeroIsConfigurationRootDirectory && exceptionCount == 0 && propertyLogic.NamedFaceInfoDeterministicHashCodeIndices.Any())
_Faces.SaveShortcuts(configuration, juliePhares, model, predictorModel, propertyLogic, peopleCollection, outputResolution, filteredPropertyHolderCollection, propertyCollection, faceCollections); {
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)) if (exceptionCount == 0 && _Configuration.LoadOrCreateThenSaveDistanceResultsForOutputResolutions.Contains(outputResolution))
_Distance.LoadOrCreateThenSaveDistanceResultsForOutputResolutions(configuration, model, predictorModel, sourceDirectory, outputResolution, sourceDirectoryChanges, filteredPropertyHolderCollection, faceCollections); _Distance.LoadOrCreateThenSaveDistanceResultsForOutputResolutions(configuration, model, predictorModel, sourceDirectory, outputResolution, sourceDirectoryChanges, filteredPropertyHolderCollection, faceCollections);
if (_Resize.AngleBracketCollection.Any()) if (_Resize.AngleBracketCollection.Any())
@ -690,14 +709,12 @@ public class DlibDotNet
} }
_Log.Information(". . ."); _Log.Information(". . .");
} }
}
if (_ArgZeroIsConfigurationRootDirectory && _Configuration.SaveShortcutsForOutputResolutions.Contains(outputResolution))
{
} }
if (_ArgZeroIsConfigurationRootDirectory && outputResolution == _Configuration.OutputResolutions[0]) if (_ArgZeroIsConfigurationRootDirectory && outputResolution == _Configuration.OutputResolutions[0])
{ {
propertyLogic.SaveAllCollection(); propertyLogic.SaveAllCollection();
if (propertyLogic.NamedFaceInfoDeterministicHashCodeIndices.Any())
E_Distance.SaveGroupedFaceEncodings(configuration, model, predictorModel, argZero, peopleCollection, outputResolution, propertyHolderCollections);
if (!_Configuration.LoadOrCreateThenSaveImageFacesResultsForOutputResolutions.Any()) if (!_Configuration.LoadOrCreateThenSaveImageFacesResultsForOutputResolutions.Any())
break; break;
if (_Exceptions.Count == 0) if (_Exceptions.Count == 0)
@ -728,12 +745,12 @@ public class DlibDotNet
return result; 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(); PropertyLogic propertyLogic = GetPropertyLogic();
Dictionary<string, List<Person>> peopleCollection = A2_People.Convert(people); Dictionary<string, List<Person>> peopleCollection = A2_People.Convert(people);
List<PropertyHolder[]> propertyHolderCollections = Property.Models.Stateless.A_Property.Get(configuration, reverse, model, predictorModel, propertyLogic); 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); 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); 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[] keys;
string fileName;
string fullName;
string personKey; string personKey;
string directory; string directory;
bool? isWrongYear; bool? isWrongYear;
FileInfo fileInfo;
TimeSpan timeSpan; TimeSpan timeSpan;
string copyFileName;
DateTime? birthDate; DateTime? birthDate;
string copyDirectory; string copyDirectory;
string? relativePath; string? relativePath;
string isWrongYearFlag; string isWrongYearFlag;
string shortcutFileName;
string subDirectoryName; string subDirectoryName;
DateTime minimumDateTime; DateTime minimumDateTime;
List<int> indices = new();
List<D_Face> faceCollection; List<D_Face> faceCollection;
PropertyHolder propertyHolder; PropertyHolder propertyHolder;
WindowsShortcut windowsShortcut; List<(string, D_Face?, (string, string, string, string))> collection;
const string pattern = @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]";
string dFacesContentDirectory = Path.Combine(Property.Models.Stateless.IResult.GetResultsFullGroupDirectory(configuration, model, predictorModel, nameof(D_Face), outputResolution, includeResizeGroup: true, includeModel: true, includePredictorModel: true), "(_)"); 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++) for (int i = 0; i < filteredPropertyHolderCollection.Length; i++)
{ {
indices.Clear();
personKey = string.Empty; personKey = string.Empty;
copyFileName = string.Empty;
copyDirectory = string.Empty; copyDirectory = string.Empty;
propertyHolder = filteredPropertyHolderCollection[i]; propertyHolder = filteredPropertyHolderCollection[i];
if (propertyHolder.ImageFileInfo is null) if (propertyHolder.ImageFileInfo is null)
continue; continue;
fileInfo = propertyHolder.ImageFileInfo;
relativePath = Path.GetDirectoryName($"C:{propertyHolder.RelativePath}"); relativePath = Path.GetDirectoryName($"C:{propertyHolder.RelativePath}");
if (string.IsNullOrEmpty(relativePath) || relativePath.Length < 3) if (string.IsNullOrEmpty(relativePath) || relativePath.Length < 3)
continue; continue;
if (propertyHolder.Property?.Id is null || propertyHolder.MinimumDateTime is null || propertyHolder.ResizedFileInfo is null) if (propertyHolder.Property?.Id is null || propertyHolder.MinimumDateTime is null || propertyHolder.ResizedFileInfo is null)
continue; continue;
collection = new();
if (!propertyLogic.NamedFaceInfoDeterministicHashCodeIndices.ContainsKey(propertyHolder.Property.Id.Value)) if (!propertyLogic.NamedFaceInfoDeterministicHashCodeIndices.ContainsKey(propertyHolder.Property.Id.Value))
{
faceCollection = new();
directory = Path.Combine(dFacesContentDirectory, $"Unnamed{relativePath[2..]}"); directory = Path.Combine(dFacesContentDirectory, $"Unnamed{relativePath[2..]}");
}
else else
{ {
faceCollection = faceCollections[i]; faceCollection = faceCollections[i];
@ -555,6 +560,7 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
directory = Path.Combine(dFacesContentDirectory, $"Many{relativePath[2..]}", subDirectoryName); directory = Path.Combine(dFacesContentDirectory, $"Many{relativePath[2..]}", subDirectoryName);
else else
{ {
indices.Add(0);
personKey = keys[0]; personKey = keys[0];
if (isWrongYear is not null && !isWrongYear.Value && personKey[..2] is "19" or "20") 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); directory = Path.Combine(dFacesContentDirectory, "Shortcuts", personKey, subDirectoryName);
if (juliePhares.Contains(personKey)) copyDirectory = Path.Combine(dFacesContentDirectory, "Images", personKey, subDirectoryName);
copyDirectory = Path.Combine(dFacesContentDirectory, "Named 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); foreach (int index in indices)
if (!string.IsNullOrEmpty(personKey) && peopleCollection.ContainsKey(personKey)) 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]; _ = Directory.CreateDirectory(directory);
fullName = Regex.Replace($"{Shared.Models.Stateless.Methods.IPersonName.GetFullName(person.Name)}.txt", pattern, string.Empty); if (!string.IsNullOrEmpty(personKey) && peopleCollection.ContainsKey(personKey))
File.WriteAllText(Path.Combine(directory, fullName), string.Empty); {
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 (juliePhares.Contains(personKey) && !string.IsNullOrEmpty(copyDirectory))
if (!string.IsNullOrEmpty(copyDirectory)) {
{ if (!Directory.Exists(copyDirectory))
if (!Directory.Exists(copyDirectory)) _ = Directory.CreateDirectory(copyDirectory);
_ = Directory.CreateDirectory(copyDirectory); fileName = Path.Combine(copyDirectory, $"{propertyHolder.Property.Id.Value}{propertyHolder.ResizedFileInfo.Extension}");
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)) 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))); string? rootResultsDirectory = Path.GetDirectoryName(Property.Models.Stateless.IResult.GetResultsGroupDirectory(configuration, nameof(B_Metadata)));
if (string.IsNullOrEmpty(rootResultsDirectory)) if (string.IsNullOrEmpty(rootResultsDirectory))
throw new Exception(); throw new Exception();
string modelName = model.ToString();
string predictorModelName = predictorModel.ToString();
string rootResultsDirectoryAbsoluteUri = new Uri(rootResultsDirectory).AbsoluteUri; 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 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), "()"); 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))); string? rootResultsDirectory = Path.GetDirectoryName(Property.Models.Stateless.IResult.GetResultsGroupDirectory(configuration, nameof(B_Metadata)));
if (string.IsNullOrEmpty(rootResultsDirectory)) if (string.IsNullOrEmpty(rootResultsDirectory))
throw new Exception(); throw new Exception();
string modelName = model.ToString();
string predictorModelName = predictorModel.ToString();
string rootResultsDirectoryAbsoluteUri = new Uri(rootResultsDirectory).AbsoluteUri; 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 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), "()"); 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 aPropertySingletonDirectory;
string bMetadataSingletonDirectory; string bMetadataSingletonDirectory;
string eDistanceCollectionDirectory; string eDistanceCollectionDirectory;
string modelName = model.ToString();
string g2IdentifyCollectionDirectory; string g2IdentifyCollectionDirectory;
string d2FaceLandmarksContentDirectory; 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)); 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), "{}"); bMetadataSingletonDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(B_Metadata), "{}");
if (Directory.Exists(bMetadataSingletonDirectory)) if (Directory.Exists(bMetadataSingletonDirectory))
@ -129,11 +127,9 @@ internal class E3_Rename
string eDistanceContentDirectory; string eDistanceContentDirectory;
string bMetadataSingletonDirectory; string bMetadataSingletonDirectory;
string aPropertySingletonDirectory; string aPropertySingletonDirectory;
string modelName = model.ToString();
string eDistanceCollectionDirectory; string eDistanceCollectionDirectory;
string g2IdentifyCollectionDirectory; string g2IdentifyCollectionDirectory;
string d2FaceLandmarksContentDirectory; string d2FaceLandmarksContentDirectory;
string predictorModelName = predictorModel.ToString();
if (!string.IsNullOrEmpty(relativePath)) if (!string.IsNullOrEmpty(relativePath))
{ {
from = string.Concat(_Configuration.PropertyConfiguration.RootDirectory, relativePath); from = string.Concat(_Configuration.PropertyConfiguration.RootDirectory, relativePath);

View File

@ -1,5 +1,6 @@
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Text.RegularExpressions;
using View_by_Distance.FaceRecognitionDotNet; using View_by_Distance.FaceRecognitionDotNet;
using View_by_Distance.Metadata.Models; using View_by_Distance.Metadata.Models;
using View_by_Distance.Property.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? _Abandoned;
protected readonly bool? _Changed; protected readonly bool? _Changed;
protected object? _Face;
protected FileInfo? _ImageFileInfo; protected FileInfo? _ImageFileInfo;
protected readonly int _G; protected readonly int _G;
protected DateTime? _MinimumDateTime; protected DateTime? _MinimumDateTime;
protected bool? _Moved; protected bool? _Moved;
protected List<(string Directory, string PersonKey, object Object)> _Named;
protected readonly bool? _NoJson; protected readonly bool? _NoJson;
protected A_Property? _Property; protected A_Property? _Property;
protected readonly int _R; protected readonly int _R;
@ -22,11 +24,13 @@ public class PropertyHolder
protected bool? _WrongYear; protected bool? _WrongYear;
public bool? Abandoned => _Abandoned; public bool? Abandoned => _Abandoned;
public bool? Changed => _Changed; public bool? Changed => _Changed;
public object? Face => _Face;
public FileInfo? ImageFileInfo => _ImageFileInfo; public FileInfo? ImageFileInfo => _ImageFileInfo;
public int G => _G; public int G => _G;
public DateTime? MinimumDateTime => _MinimumDateTime; public DateTime? MinimumDateTime => _MinimumDateTime;
public bool? Moved => _Moved; public bool? Moved => _Moved;
public bool? NoJson => _NoJson; public bool? NoJson => _NoJson;
public List<(string Directory, string PersonKey, object Object)> Named => _Named;
public A_Property? Property => _Property; public A_Property? Property => _Property;
public int R => _R; public int R => _R;
public string RelativePath => _RelativePath; public string RelativePath => _RelativePath;
@ -39,9 +43,10 @@ public class PropertyHolder
public PropertyHolder() public PropertyHolder()
{ {
_G = -1; _G = -1;
_Named = new();
_RelativePath = string.Empty;
_SourceDirectory = string.Empty; _SourceDirectory = string.Empty;
_SourceDirectoryFile = string.Empty; _SourceDirectoryFile = string.Empty;
_RelativePath = string.Empty;
_R = -1; _R = -1;
} }
@ -54,6 +59,7 @@ public class PropertyHolder
_G = g; _G = g;
_Moved = moved; _Moved = moved;
_NoJson = abandoned is null; _NoJson = abandoned is null;
_Named = new();
_Property = property; _Property = property;
_R = r; _R = r;
_RelativePath = relativePath; _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 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));
} }