Save Grouped Face Encodings

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

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);
}
}
}
}