Added FileHolder

This commit is contained in:
2022-08-08 19:23:48 -07:00
parent daf5f428b9
commit 3aeab88384
15 changed files with 448 additions and 288 deletions

View File

@ -43,7 +43,7 @@ internal class D2_FaceLandmarks
return result;
}
private static void SaveFaceLandmarkImages(List<D_Face> faceCollections, List<string[]> imageFiles, int pointSize, FileInfo resizedFileInfo)
private static void SaveFaceLandmarkImages(List<D_Face> faceCollections, List<string[]> imageFiles, int pointSize, FileHolder resizedFileHolder)
{
int x;
int y;
@ -63,7 +63,7 @@ internal class D2_FaceLandmarks
rotatedImageFileFullName = imageFiles[i][1];
try
{
using (Image image = Image.FromFile(resizedFileInfo.FullName))
using (Image image = Image.FromFile(resizedFileHolder.FullName))
{
using Graphics graphic = Graphics.FromImage(image);
if (face.FaceLandmarks is null || !face.FaceLandmarks.Any())
@ -92,7 +92,7 @@ internal class D2_FaceLandmarks
}
if (face.α.HasValue)
{
using Image image = Image.FromFile(resizedFileInfo.FullName);
using Image image = Image.FromFile(resizedFileHolder.FullName);
rotated = RotateBitmap(image, (float)face.α.Value);
if (rotated is not null)
{
@ -109,8 +109,8 @@ internal class D2_FaceLandmarks
internal void SaveFaceLandmarkImages(List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, PropertyHolder propertyHolder, List<D_Face> faceCollections)
{
if (propertyHolder.ResizedFileInfo is null)
throw new Exception($"{propertyHolder.ResizedFileInfo} is null!");
if (propertyHolder.ResizedFileHolder is null)
throw new Exception($"{propertyHolder.ResizedFileHolder} is null!");
FileInfo fileInfo;
bool check = false;
string parentCheck;
@ -164,7 +164,7 @@ internal class D2_FaceLandmarks
}
}
if (check)
SaveFaceLandmarkImages(faceCollections, imageFiles, pointSize, propertyHolder.ResizedFileInfo);
SaveFaceLandmarkImages(faceCollections, imageFiles, pointSize, propertyHolder.ResizedFileHolder);
}
}

View File

@ -219,7 +219,7 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
return result;
}
private static void SaveFaces(List<D_Face> faceCollection, FileInfo resizedFileInfo, List<string> imageFiles)
private static void SaveFaces(List<D_Face> faceCollection, FileHolder resizedFileHolder, List<string> imageFiles)
{
int width;
int height;
@ -227,7 +227,7 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
Location location;
Bitmap preRotated;
Rectangle rectangle;
using Bitmap source = new(resizedFileInfo.FullName);
using Bitmap source = new(resizedFileHolder.FullName);
for (int i = 0; i < faceCollection.Count; i++)
{
if (!faceCollection[i].Populated || faceCollection[i]?.Location is null)
@ -251,7 +251,7 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
}
}
private List<D_Face> GetFaces(FileInfo resizedFileInfo, PropertyHolder propertyHolder, A_Property property, int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation, string facesDirectory)
private List<D_Face> GetFaces(FileHolder resizedFileHolder, PropertyHolder propertyHolder, A_Property property, int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation, string facesDirectory)
{
List<D_Face> results = new();
if (_Configuration.PaddingLoops is null)
@ -262,10 +262,10 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
throw new ArgumentNullException(nameof(_Configuration.NumberOfTimesToUpsample));
List<Location> locations;
FaceRecognitionDotNet.Image? unknownImage = null;
if (resizedFileInfo.Exists)
if (resizedFileHolder.Exists)
{
try
{ unknownImage = FaceRecognition.LoadImageFile(resizedFileInfo.FullName); }
{ unknownImage = FaceRecognition.LoadImageFile(resizedFileHolder.FullName); }
catch (Exception) { }
}
if (unknownImage is null)
@ -410,7 +410,7 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
_Populated = populated;
}
internal List<D_Face> GetFaces(Property.Models.Configuration configuration, string outputResolution, List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, PropertyHolder propertyHolder, A_Property property, FileInfo resizedFileInfo, int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation)
internal List<D_Face> GetFaces(Property.Models.Configuration configuration, string outputResolution, List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, PropertyHolder propertyHolder, A_Property property, FileHolder resizedFileHolder, int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation)
{
List<D_Face>? results;
if (_Configuration.PropertiesChangedForFaces is null)
@ -419,8 +419,8 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
D_Face face;
bool checkForOutputResolutionChange = false;
string[] changesFrom = new string[] { nameof(A_Property), nameof(B_Metadata), nameof(C_Resize) };
string facesDirectory = Path.Combine(AngleBracketCollection[0].Replace("<>", "()"), propertyHolder.ImageFileNameWithoutExtension);
List<DateTime> dateTimes = (from l in subFileTuples where changesFrom.Contains(l.Item1) select l.Item2).ToList();
string facesDirectory = Path.Combine(AngleBracketCollection[0].Replace("<>", "()"), propertyHolder.ImageFileNameWithoutExtension);
FileInfo fileInfo = new(Path.Combine(AngleBracketCollection[0].Replace("<>", "[]"), $"{propertyHolder.ImageFileNameWithoutExtension}.json"));
if (!fileInfo.Exists)
{
@ -473,7 +473,7 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
}
else if (results is null)
{
results = GetFaces(resizedFileInfo, propertyHolder, property, outputResolutionWidth, outputResolutionHeight, outputResolutionOrientation, facesDirectory);
results = GetFaces(resizedFileHolder, propertyHolder, property, outputResolutionWidth, outputResolutionHeight, outputResolutionOrientation, facesDirectory);
json = JsonSerializer.Serialize(results, _WriteIndentedJsonSerializerOptions);
bool updateDateWhenMatches = dateTimes.Any() && fileInfo.Exists && dateTimes.Max() > fileInfo.LastWriteTime;
DateTime? dateTime = !updateDateWhenMatches ? null : dateTimes.Max();
@ -487,8 +487,8 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
{
if (_Configuration.OverrideForFaceImages is null)
throw new Exception();
if (propertyHolder.ResizedFileInfo is null)
throw new Exception($"{propertyHolder.ResizedFileInfo} is null!");
if (propertyHolder.ResizedFileHolder is null)
throw new Exception($"{propertyHolder.ResizedFileHolder} is null!");
FileInfo fileInfo;
bool check = false;
string parentCheck;
@ -524,7 +524,7 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
check = true;
}
if (check)
SaveFaces(faceCollection, propertyHolder.ResizedFileInfo, imageFiles);
SaveFaces(faceCollection, propertyHolder.ResizedFileHolder, imageFiles);
}
internal static void SaveShortcuts(Property.Models.Configuration configuration, Model? model, PredictorModel? predictorModel, string[] juliePhares, long ticks, Dictionary<string, List<Person>> peopleCollection, PropertyLogic propertyLogic, string outputResolution, PropertyHolder[] filteredPropertyHolderCollection)
@ -544,7 +544,7 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
{
if (string.IsNullOrEmpty(personKey))
continue;
if (propertyHolder.Property?.Id is null || propertyHolder.ImageFileInfo is null || propertyHolder.MinimumDateTime is null || propertyHolder.ResizedFileInfo is null)
if (propertyHolder.Property?.Id is null || propertyHolder.ImageFileHolder is null || propertyHolder.MinimumDateTime is null || propertyHolder.ResizedFileHolder is null)
continue;
if (!Directory.Exists(directory))
{
@ -560,14 +560,14 @@ public class D_Face : Shared.Models.Properties.IFace, IFace
{
if (!Directory.Exists(copyDirectory))
_ = Directory.CreateDirectory(copyDirectory);
fileName = Path.Combine(copyDirectory, $"{propertyHolder.Property.Id.Value}{propertyHolder.ResizedFileInfo.Extension}");
fileName = Path.Combine(copyDirectory, $"{propertyHolder.Property.Id.Value}{propertyHolder.ResizedFileHolder.Extension}");
if (!File.Exists(fileName))
File.Copy(propertyHolder.ResizedFileInfo.FullName, fileName);
File.Copy(propertyHolder.ResizedFileHolder.FullName, fileName);
}
fileName = Path.Combine(directory, $"{propertyHolder.Property.Id.Value}.lnk");
if (File.Exists(fileName))
continue;
windowsShortcut = new() { Path = propertyHolder.ImageFileInfo.FullName };
windowsShortcut = new() { Path = propertyHolder.ImageFileHolder.FullName };
windowsShortcut.Save(fileName);
windowsShortcut.Dispose();
if (!File.Exists(fileName))

View File

@ -173,8 +173,7 @@ internal class E_Distance
private void LoadOrCreateThenSaveDistanceResultsForOutputResolutions(Property.Models.Configuration configuration, PropertyHolder[] filteredPropertyHolderCollection, List<List<D_Face>> faceCollections, List<string[]> directories, bool updateDateWhenMatches, DateTime? updateToWhenMatches)
{
FileInfo? fileInfo;
string fileNameWithoutExtension;
FileHolder? fileHolder;
List<int[]> locationIndicesCollection = new();
List<Tuple<string, DateTime>> subFileTuples = new();
List<FaceEncoding> faceEncodingCollection = new();
@ -186,11 +185,10 @@ internal class E_Distance
throw new Exception();
for (int i = 0; i < filteredPropertyHolderCollection.Length; i++)
{
fileInfo = filteredPropertyHolderCollection[i].ImageFileInfo;
if (fileInfo is null)
fileHolder = filteredPropertyHolderCollection[i].ImageFileHolder;
if (fileHolder is null)
continue;
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileInfo.FullName);
LoadOrCreateThenSaveDistanceResultsForOutputResolutionsLoop(configuration, faceCollections, filteredPropertyHolderCollection.Length, i, faceCollections[i], locationIndicesCollection, subFileTuples, faceEncodingCollection, faceEncodingCollections[i], fileNameWithoutExtension, directories[i][0], directories[i][1], updateDateWhenMatches, updateToWhenMatches);
LoadOrCreateThenSaveDistanceResultsForOutputResolutionsLoop(configuration, faceCollections, filteredPropertyHolderCollection.Length, i, faceCollections[i], locationIndicesCollection, subFileTuples, faceEncodingCollection, faceEncodingCollections[i], fileHolder.NameWithoutExtension, directories[i][0], directories[i][1], updateDateWhenMatches, updateToWhenMatches);
}
}
@ -201,12 +199,11 @@ internal class E_Distance
if (_Configuration.PropertiesChangedForDistance is null)
throw new Exception();
string json;
FileInfo? fileInfo;
bool check = false;
string parentCheck;
FileHolder? fileHolder;
DateTime? dateTime = null;
FileInfo[] fileInfoCollection;
string fileNameWithoutExtension;
bool updateDateWhenMatches = false;
List<string[]> directories = new();
System.IO.DirectoryInfo directoryInfo;
@ -227,11 +224,10 @@ internal class E_Distance
collectionDescription: "n json file(s) for each face found (one to many)");
for (int i = 0; i < filteredPropertyHolderCollection.Length; i++)
{
fileInfo = filteredPropertyHolderCollection[i].ImageFileInfo;
if (fileInfo is null)
fileHolder = filteredPropertyHolderCollection[i].ImageFileHolder;
if (fileHolder is null)
continue;
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileInfo.FullName);
directoryInfo = new System.IO.DirectoryInfo(Path.Combine(directoryInfoCollection[0].Replace("<>", "[]"), fileNameWithoutExtension));
directoryInfo = new System.IO.DirectoryInfo(Path.Combine(directoryInfoCollection[0].Replace("<>", "[]"), fileHolder.NameWithoutExtension));
if (!directoryInfo.Exists)
{
if (directoryInfo.Parent?.Parent is null)
@ -244,7 +240,7 @@ internal class E_Distance
Directory.Delete(parentCheck);
}
}
tvsDirectoryInfo = new System.IO.DirectoryInfo(Path.Combine(directoryInfoCollection[0].Replace("<>", "()"), fileNameWithoutExtension));
tvsDirectoryInfo = new System.IO.DirectoryInfo(Path.Combine(directoryInfoCollection[0].Replace("<>", "()"), fileHolder.NameWithoutExtension));
directories.Add(new string[] { directoryInfo.FullName, tvsDirectoryInfo.FullName });
if (_Configuration.CheckJsonForDistanceResults.Value && directoryInfo.Exists)
{
@ -418,80 +414,168 @@ internal class E_Distance
return result;
}
internal static void SaveGroupedFaceEncodings(Property.Models.Configuration configuration, Model? model, PredictorModel? predictorModel, string argZero, long ticks, Dictionary<string, List<Shared.Models.Person>> peopleCollection, string outputResolution, List<PropertyHolder[]> propertyHolderCollections)
internal static List<(string[], Shared.Models.PersonBirthday, FaceEncoding[])> GetGroupedFaceEncodings(string argZero, List<PropertyHolder[]> propertyHolderCollections)
{
List<(string[], Shared.Models.PersonBirthday, FaceEncoding[])> results = new();
double lcl;
double ucl;
string json;
double sum;
double average;
int lowestIndex;
string checkFile;
string personKey;
double lowestAverage;
double lowestSum;
const int zero = 0;
double standardDeviation;
FaceEncoding faceEncoding;
List<double> faceDistances;
List<double[]> rawEncodings;
Shared.Models.Person person;
List<FaceEncoding> faceEncodings;
List<string> checkDirectories = new();
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), $"[{ticks}]");
List<(string, Shared.Models.PersonBirthday, Shared.Models.Properties.IFace)[]> collection = PropertyHolder.GetCollection(argZero, propertyHolderCollections, eDistanceCollectionDirectory);
foreach ((string, Shared.Models.PersonBirthday, Shared.Models.Properties.IFace)[] group in collection)
Dictionary<string, List<(string[], Shared.Models.PersonBirthday, Shared.Models.Properties.IFace)>> keyValuePairs = PropertyHolder.GetKeyValuePairs(argZero, propertyHolderCollections);
foreach (KeyValuePair<string, List<(string[] Directories, Shared.Models.PersonBirthday PersonBirthday, Shared.Models.Properties.IFace _)>> keyValuePair in keyValuePairs)
{
lowestIndex = 0;
rawEncodings = new();
faceEncodings = new();
checkDirectories.Clear();
checkFile = string.Empty;
lowestAverage = double.MaxValue;
foreach ((string directory, Shared.Models.PersonBirthday personBirthday, Shared.Models.Properties.IFace @interface) in group)
lowestSum = double.MaxValue;
foreach ((string[] directories, Shared.Models.PersonBirthday personBirthday, Shared.Models.Properties.IFace face) in keyValuePair.Value)
{
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday);
if (string.IsNullOrEmpty(personKey) || !peopleCollection.ContainsKey(personKey))
if (!face.Populated)
continue;
if (@interface is not D_Face face || !face.Populated)
continue;
person = peopleCollection[personKey][0];
faceEncoding = FaceRecognition.LoadFaceEncoding(face.FaceEncoding.RawEncoding);
checkFile = string.Concat(directory, " - ", Regex.Replace(Shared.Models.Stateless.Methods.IPersonName.GetFullName(person.Name), pattern, string.Empty), ".json");
checkDirectories.Add(directory);
faceEncodings.Add(faceEncoding);
}
if (string.IsNullOrEmpty(checkFile) || !checkDirectories.Any() || faceEncodings.Count < 2)
if (keyValuePair.Value.Count != faceEncodings.Count)
continue;
if (faceEncodings.Count < 2)
continue;
foreach (string checkDirectory in checkDirectories.Distinct())
{
if (!Directory.Exists(checkDirectory))
_ = Directory.CreateDirectory(checkDirectory);
}
for (int i = 0; i < faceEncodings.Count; i++)
{
faceDistances = FaceRecognition.FaceDistances(faceEncodings, faceEncodings[i]);
average = faceDistances.Average();
if (average > lowestAverage)
sum = faceDistances.Sum();
if (sum > lowestSum)
continue;
lowestIndex = i;
lowestAverage = average;
lowestSum = sum;
}
faceDistances = FaceRecognition.FaceDistances(faceEncodings, faceEncodings[lowestIndex]);
average = faceDistances.Average();
if (average != lowestAverage)
sum = faceDistances.Sum();
if (sum != lowestSum)
continue;
average = faceDistances.Average();
standardDeviation = GetStandardDeviation(faceDistances, average);
lcl = average - (standardDeviation * 3);
ucl = average + (standardDeviation * 3);
for (int i = 0; i < faceEncodings.Count; i++)
for (int i = faceEncodings.Count; i > -1; i--)
{
if (faceDistances[i] < lcl || faceDistances[i] > ucl)
continue;
rawEncodings.Add(faceEncodings[i].GetRawEncoding());
faceEncodings.RemoveAt(i);
}
// outOfControl = faceEncodings.Count - rawEncodings.Count;
results.Add(new(keyValuePair.Value[zero].Directories, keyValuePair.Value[zero].PersonBirthday, faceEncodings.ToArray()));
}
return results;
}
internal static void SaveGroupedFaceEncodings(List<(string[], Shared.Models.PersonBirthday, FaceEncoding[])> collection, Dictionary<string, List<Shared.Models.Person>> peopleCollection, string eDistanceCollectionDirectory)
{
string json;
string checkFile;
string personKey;
string directory;
List<double[]> rawEncodings;
Shared.Models.Person person;
const string pattern = @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]";
foreach ((string[] directories, Shared.Models.PersonBirthday personBirthday, FaceEncoding[] faceEncodings) in collection)
{
directories[0] = eDistanceCollectionDirectory;
rawEncodings = new();
checkFile = string.Empty;
directory = Path.Combine(directories);
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(personBirthday);
person = peopleCollection[personKey][0];
checkFile = string.Concat(directory, " - ", Regex.Replace(Shared.Models.Stateless.Methods.IPersonName.GetFullName(person.Name), pattern, string.Empty), ".json");
if (string.IsNullOrEmpty(checkFile))
continue;
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
for (int i = 0; i < faceEncodings.Length; i++)
rawEncodings.Add(faceEncodings[i].GetRawEncoding());
json = JsonSerializer.Serialize(rawEncodings, new JsonSerializerOptions { WriteIndented = true });
_ = Property.Models.Stateless.IPath.WriteAllText(checkFile, json, updateDateWhenMatches: true, compareBeforeWrite: true);
}
}
public static void GetKeyValuePairs(string argZero, List<PropertyHolder[]> propertyHolderCollections, List<(string[], Shared.Models.PersonBirthday, FaceEncoding[])> collection)
{
double average;
string personKey;
bool? isWrongYear;
TimeSpan? timeSpan;
double lowestAverage;
string[] directories;
string isWrongYearFlag;
FaceEncoding faceEncoding;
List<double> faceDistances;
const string facePopulatedKey = "MatchImages";
Shared.Models.PersonBirthday? lowestPersonBirthday;
foreach (PropertyHolder[] propertyHolderCollection in propertyHolderCollections)
{
lowestPersonBirthday = null;
lowestAverage = double.MaxValue;
if (!propertyHolderCollection.Any())
continue;
if (!propertyHolderCollection[0].SourceDirectory.StartsWith(argZero))
continue;
foreach (PropertyHolder propertyHolder in propertyHolderCollection)
{
if (propertyHolder.ImageFileHolder is null || propertyHolder.Property is null || propertyHolder.Named.Any())
continue;
if (propertyHolder.MinimumDateTime is null)
continue;
(isWrongYear, _) = propertyHolder.IsWrongYear();
foreach (Shared.Models.Properties.IFace face in propertyHolder.Faces)
{
if (!face.Populated)
continue;
faceEncoding = FaceRecognition.LoadFaceEncoding(face.FaceEncoding.RawEncoding);
foreach ((string[] _, Shared.Models.PersonBirthday personBirthday, FaceEncoding[] faceEncodings) in collection)
{
faceDistances = FaceRecognition.FaceDistances(faceEncodings, faceEncoding);
average = faceDistances.Average();
if (average < lowestAverage)
continue;
lowestPersonBirthday = personBirthday;
}
if (lowestPersonBirthday is null)
continue;
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(lowestPersonBirthday);
timeSpan = Shared.Models.Stateless.Methods.IPersonBirthday.GetTimeSpan(propertyHolder.MinimumDateTime.Value, isWrongYear, lowestPersonBirthday);
if (timeSpan.HasValue && timeSpan.Value.Ticks < 0)
directories = new string[] { string.Empty, facePopulatedKey, personKey, "!---" };
else if (timeSpan.HasValue)
directories = new string[] { string.Empty, facePopulatedKey, personKey, $"^{Math.Floor(timeSpan.Value.TotalDays / 365):000}" };
else
{
isWrongYearFlag = PropertyHolder.GetWrongYearFlag(isWrongYear);
directories = new string[] { string.Empty, facePopulatedKey, personKey, $"{isWrongYearFlag}{propertyHolder.MinimumDateTime.Value:yyyy}" };
}
}
}
}
}
internal static void GetClosest(string argZero, List<PropertyHolder[]> propertyHolderCollections, List<(string[], Shared.Models.PersonBirthday, FaceEncoding[])> collection, Dictionary<string, List<Shared.Models.Person>> peopleCollection, string eDistanceCollectionDirectory)
{
GetKeyValuePairs(argZero, propertyHolderCollections, collection);
if (peopleCollection is null)
{ }
if (string.IsNullOrEmpty(eDistanceCollectionDirectory))
{ }
// foreach (KeyValuePair<string, List<(string[] Directories, Shared.Models.PersonBirthday PersonBirthday, Shared.Models.Properties.IFace _)>> keyValuePair in keyValuePairs)
// {
// foreach ((string[] directories, Shared.Models.PersonBirthday personBirthday, Shared.Models.Properties.IFace face) in keyValuePair.Value)
// {
// if (face.Populated)
// continue;
// }
// }
}
}