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

@ -305,10 +305,10 @@ public class DlibDotNet
throw new Exception("Input directory should be the source and not a resized directory!");
}
private void FullParallelForWork(PropertyLogic propertyLogic, object @lock, string outputResolution, List<Tuple<string, DateTime>> sourceDirectoryChanges, List<FileInfo?> propertyFileInfoCollection, List<A_Property> propertyCollection, List<List<KeyValuePair<string, string>>> metadataCollections, List<Dictionary<string, int[]>> resizeKeyValuePairs, List<List<D_Face>> imageFaceCollections, string sourceDirectory, int index, PropertyHolder propertyHolder)
private void FullParallelForWork(PropertyLogic propertyLogic, string outputResolution, List<Tuple<string, DateTime>> sourceDirectoryChanges, List<FileHolder?> propertyFileHolderCollection, List<A_Property> propertyCollection, List<List<KeyValuePair<string, string>>> metadataCollections, List<Dictionary<string, int[]>> resizeKeyValuePairs, List<List<D_Face>> imageFaceCollections, string sourceDirectory, int index, PropertyHolder propertyHolder)
{
if (propertyHolder.ImageFileInfo is null)
throw new ArgumentNullException(nameof(propertyHolder.ImageFileInfo));
if (propertyHolder.ImageFileHolder is null)
throw new ArgumentNullException(nameof(propertyHolder.ImageFileHolder));
if (_AppSettings.MaxDegreeOfParallelism is null)
throw new ArgumentNullException(nameof(_AppSettings.MaxDegreeOfParallelism));
if (_Configuration.SaveResizedSubfiles is null)
@ -328,7 +328,8 @@ public class DlibDotNet
List<KeyValuePair<string, string>> metadataCollection;
if (propertyHolder.Property is null)
{
sourceDirectoryChanges.Add(new Tuple<string, DateTime>(nameof(A_Property), propertyHolder.ImageFileInfo.LastWriteTime));
lock (sourceDirectoryChanges)
sourceDirectoryChanges.Add(new Tuple<string, DateTime>(nameof(A_Property), DateTime.Now));
property = propertyLogic.GetProperty(propertyLogic.AngleBracketCollection[0], propertyHolder, subFileTuples, parseExceptions);
propertyHolder.Update(property);
}
@ -336,13 +337,16 @@ public class DlibDotNet
{
property = propertyHolder.Property;
if (propertyHolder.Changed.HasValue && propertyHolder.Changed.Value)
sourceDirectoryChanges.Add(new Tuple<string, DateTime>(nameof(A_Property), propertyHolder.ImageFileInfo.LastWriteTime));
{
lock (sourceDirectoryChanges)
sourceDirectoryChanges.Add(new Tuple<string, DateTime>(nameof(A_Property), DateTime.Now));
}
}
(int metadataGroups, metadataCollection) = _Metadata.GetMetadataCollection(subFileTuples, parseExceptions, propertyHolder);
if (_AppSettings.MaxDegreeOfParallelism.Value < 2)
ticks = LogDelta(ticks, nameof(B_Metadata.GetMetadataCollection));
FileInfo resizedFileInfo = new(Path.Combine(_Resize.AngleBracketCollection[0].Replace("<>", "()"), Path.GetFileName(propertyHolder.ImageFileInfo.FullName)));
propertyHolder.SetResizedFileInfo(resizedFileInfo);
FileHolder resizedFileHolder = new(Path.Combine(_Resize.AngleBracketCollection[0].Replace("<>", "()"), Path.GetFileName(propertyHolder.ImageFileHolder.FullName)));
propertyHolder.SetResizedFileHolder(resizedFileHolder);
imageResizeKeyValuePairs = _Resize.GetResizeKeyValuePairs(subFileTuples, parseExceptions, original, metadataCollection, property, propertyHolder);
if (_AppSettings.MaxDegreeOfParallelism.Value < 2)
ticks = LogDelta(ticks, nameof(C_Resize.GetResizeKeyValuePairs));
@ -351,14 +355,14 @@ public class DlibDotNet
_Resize.SaveResizedSubfile(outputResolution, subFileTuples, propertyHolder, original, property, imageResizeKeyValuePairs);
if (_AppSettings.MaxDegreeOfParallelism.Value < 2)
ticks = LogDelta(ticks, nameof(C_Resize.SaveResizedSubfile));
resizedFileInfo.Refresh();
propertyHolder.SetResizedFileHolder(FileHolder.Refresh(resizedFileHolder));
}
else if (outputResolution == _Configuration.OutputResolutions[0] && false)
{
byte[] bytes = _Resize.GetResizedBytes(outputResolution, subFileTuples, propertyHolder, property, imageResizeKeyValuePairs);
if (_AppSettings.MaxDegreeOfParallelism.Value < 2)
ticks = LogDelta(ticks, nameof(C_Resize.GetResizedBytes));
string path = Path.Combine(resizedFileInfo.DirectoryName, Path.GetFileNameWithoutExtension(resizedFileInfo.Name));
string path = Path.Combine(resizedFileHolder.DirectoryName, resizedFileHolder.NameWithoutExtension);
File.WriteAllBytes(path, bytes);
}
if (!_Configuration.LoadOrCreateThenSaveImageFacesResultsForOutputResolutions.Contains(outputResolution))
@ -369,7 +373,7 @@ public class DlibDotNet
int outputResolutionWidth = outputResolutionCollection[0];
int outputResolutionHeight = outputResolutionCollection[1];
int outputResolutionOrientation = outputResolutionCollection[2];
faceCollection = _Faces.GetFaces(_Configuration.PropertyConfiguration, outputResolution, subFileTuples, parseExceptions, propertyHolder, property, resizedFileInfo, outputResolutionWidth, outputResolutionHeight, outputResolutionOrientation);
faceCollection = _Faces.GetFaces(_Configuration.PropertyConfiguration, outputResolution, subFileTuples, parseExceptions, propertyHolder, property, resizedFileHolder, outputResolutionWidth, outputResolutionHeight, outputResolutionOrientation);
if (_AppSettings.MaxDegreeOfParallelism.Value < 2)
ticks = LogDelta(ticks, nameof(D_Face.GetFaces));
_Faces.SaveFaces(_Configuration.PropertyConfiguration, subFileTuples, parseExceptions, propertyHolder, faceCollection);
@ -382,18 +386,18 @@ public class DlibDotNet
ticks = LogDelta(ticks, nameof(D2_FaceLandmarks.SaveFaceLandmarkImages));
}
}
lock (@lock)
lock (sourceDirectoryChanges)
{
propertyCollection[index] = property;
imageFaceCollections[index] = faceCollection;
metadataCollections[index] = metadataCollection;
resizeKeyValuePairs[index] = imageResizeKeyValuePairs;
propertyFileInfoCollection[index] = propertyHolder.ImageFileInfo;
propertyFileHolderCollection[index] = propertyHolder.ImageFileHolder;
sourceDirectoryChanges.AddRange(from l in subFileTuples where l.Item2 > dateTime select l);
}
}
private int FullParallelWork(object @lock, long ticks, PropertyLogic propertyLogic, string outputResolution, List<Tuple<string, DateTime>> sourceDirectoryChanges, List<FileInfo?> propertyFileInfoCollection, List<A_Property> propertyCollection, List<List<KeyValuePair<string, string>>> metadataCollection, List<Dictionary<string, int[]>> resizeKeyValuePairs, List<List<D_Face>> faceCollections, int propertyHolderCollectionsCount, int g, string sourceDirectory, int r, PropertyHolder[] filteredPropertyHolderCollection)
private int FullParallelWork(long ticks, PropertyLogic propertyLogic, string outputResolution, List<Tuple<string, DateTime>> sourceDirectoryChanges, List<FileHolder?> propertyFileHolderCollection, List<A_Property> propertyCollection, List<List<KeyValuePair<string, string>>> metadataCollection, List<Dictionary<string, int[]>> resizeKeyValuePairs, List<List<D_Face>> faceCollections, int propertyHolderCollectionsCount, int g, string sourceDirectory, int r, PropertyHolder[] filteredPropertyHolderCollection)
{
int result = 0;
if (_Log is null)
@ -410,18 +414,19 @@ public class DlibDotNet
metadataCollection.Add(new());
resizeKeyValuePairs.Add(new());
propertyCollection.Add(new());
propertyFileInfoCollection.Add(null);
propertyFileHolderCollection.Add(null);
}
}
int totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
using (ProgressBar progressBar = new(filteredPropertyHolderCollection.Length, $"{r + 1:000}.{g} / {propertyHolderCollectionsCount:000}) {filteredPropertyHolderCollection.Length:000} file(s) - {totalSeconds} total second(s) - {outputResolution} - {sourceDirectory}", options))
string message = $"{r + 1:000}.{g} / {propertyHolderCollectionsCount:000}) {filteredPropertyHolderCollection.Length:000} file(s) - {totalSeconds} total second(s) - {outputResolution} - {sourceDirectory}";
using (ProgressBar progressBar = new(filteredPropertyHolderCollection.Length, message, options))
{
_ = Parallel.For(0, filteredPropertyHolderCollection.Length, parallelOptions, i =>
{
try
{
FullParallelForWork(propertyLogic, @lock, outputResolution, sourceDirectoryChanges, propertyFileInfoCollection, propertyCollection, metadataCollection, resizeKeyValuePairs, faceCollections, sourceDirectory, index: i, filteredPropertyHolderCollection[i]);
if (sourceDirectoryChanges.Any())
FullParallelForWork(propertyLogic, outputResolution, sourceDirectoryChanges, propertyFileHolderCollection, propertyCollection, metadataCollection, resizeKeyValuePairs, faceCollections, sourceDirectory, index: i, filteredPropertyHolderCollection[i]);
if (i == 0 || sourceDirectoryChanges.Any())
progressBar.Tick();
}
catch (Exception ex)
@ -502,9 +507,9 @@ public class DlibDotNet
propertyHolder = filteredPropertyHolderCollection[i];
if (propertyHolder.Property is null)
continue;
if (propertyHolder.ImageFileInfo is null)
if (propertyHolder.ImageFileHolder is null)
continue;
key = Property.Models.Stateless.IPath.GetRelativePath(propertyHolder.ImageFileInfo.FullName, sourceDirectoryLength);
key = Property.Models.Stateless.IPath.GetRelativePath(propertyHolder.ImageFileHolder.FullName, sourceDirectoryLength);
_FileKeyValuePairs.Add(new KeyValuePair<string, string>(sourceDirectory, key));
_FilePropertiesKeyValuePairs[sourceDirectory].Add(new Tuple<string, A_Property>(key, propertyCollection[i]));
faceCollectionsKeyValuePairs.Add(new KeyValuePair<string, List<D_Face>>(key, faceCollections[i]));
@ -564,29 +569,31 @@ public class DlibDotNet
int g;
int r;
int exceptionCount;
object @lock = new();
string sourceDirectory;
long ticks = DateTime.Now.Ticks;
string eDistanceCollectionDirectory;
List<List<D_Face>> faceCollections = new();
List<A_Property> propertyCollection = new();
PropertyHolder[] filteredPropertyHolderCollection;
List<FileInfo?> propertyFileInfoCollection = new();
List<FileHolder?> propertyFileHolderCollection = new();
List<Dictionary<string, int[]>> resizeKeyValuePairs = new();
List<Tuple<string, DateTime>> sourceDirectoryChanges = new();
List<List<KeyValuePair<string, string>>> metadataCollection = new();
List<(string[], PersonBirthday, FaceRecognitionDotNet.FaceEncoding[])> collection;
string aPropertySingletonDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(A_Property), "{}");
string propertyRoot = Property.Models.Stateless.IResult.GetResultsGroupDirectory(configuration, nameof(A_Property));
foreach (string outputResolution in _Configuration.OutputResolutions)
{
_FileKeyValuePairs.Clear();
_FilePropertiesKeyValuePairs.Clear();
eDistanceCollectionDirectory = Path.Combine(Property.Models.Stateless.IResult.GetResultsFullGroupDirectory(configuration, model, predictorModel, nameof(E_Distance), outputResolution, includeResizeGroup: true, includeModel: true, includePredictorModel: true), $"[{ticks}]");
foreach (PropertyHolder[] propertyHolderCollection in propertyHolderCollections)
{
if (!propertyHolderCollection.Any())
continue;
if (!_ArgZeroIsConfigurationRootDirectory && !propertyHolderCollection[0].SourceDirectory.StartsWith(argZero))
continue;
filteredPropertyHolderCollection = (from l in propertyHolderCollection where l.ImageFileInfo is not null && l.Property is not null && l.ValidImageFormatExtension.HasValue && l.ValidImageFormatExtension.Value && !_Configuration.IgnoreExtensions.Contains(l.ImageFileInfo.Extension) select l).ToArray();
filteredPropertyHolderCollection = (from l in propertyHolderCollection where l.ImageFileHolder is not null && l.Property is not null && l.ValidImageFormatExtension.HasValue && l.ValidImageFormatExtension.Value && !_Configuration.IgnoreExtensions.Contains(l.ImageFileHolder.Extension) select l).ToArray();
if (!filteredPropertyHolderCollection.Any())
continue;
faceCollections.Clear();
@ -594,7 +601,7 @@ public class DlibDotNet
propertyCollection.Clear();
resizeKeyValuePairs.Clear();
sourceDirectoryChanges.Clear();
propertyFileInfoCollection.Clear();
propertyFileHolderCollection.Clear();
_Faces.AngleBracketCollection.Clear();
_Resize.AngleBracketCollection.Clear();
_Metadata.AngleBracketCollection.Clear();
@ -665,7 +672,7 @@ public class DlibDotNet
contentDescription: "n x 2 png file(s) for each face found",
singletonDescription: string.Empty,
collectionDescription: string.Empty));
exceptionCount = FullParallelWork(@lock, ticks, propertyLogic, outputResolution, sourceDirectoryChanges, propertyFileInfoCollection, propertyCollection, metadataCollection, resizeKeyValuePairs, faceCollections, propertyHolderCollections.Count, g, sourceDirectory, r, filteredPropertyHolderCollection);
exceptionCount = FullParallelWork(ticks, propertyLogic, outputResolution, sourceDirectoryChanges, propertyFileHolderCollection, propertyCollection, metadataCollection, resizeKeyValuePairs, faceCollections, propertyHolderCollections.Count, g, sourceDirectory, r, filteredPropertyHolderCollection);
if (metadataCollection.Count != filteredPropertyHolderCollection.Length || propertyCollection.Count != filteredPropertyHolderCollection.Length || resizeKeyValuePairs.Count != filteredPropertyHolderCollection.Length || faceCollections.Count != filteredPropertyHolderCollection.Length)
throw new Exception("Counts don't match!");
if (exceptionCount != 0)
@ -708,7 +715,11 @@ public class DlibDotNet
{
propertyLogic.SaveAllCollection();
if (propertyLogic.NamedFaceInfoDeterministicHashCodeIndices.Any())
E_Distance.SaveGroupedFaceEncodings(configuration, model, predictorModel, argZero, ticks, peopleCollection, outputResolution, propertyHolderCollections);
{
collection = E_Distance.GetGroupedFaceEncodings(argZero, propertyHolderCollections);
E_Distance.SaveGroupedFaceEncodings(collection, peopleCollection, eDistanceCollectionDirectory);
E_Distance.GetClosest(argZero, propertyHolderCollections, collection, peopleCollection, eDistanceCollectionDirectory);
}
if (!_Configuration.LoadOrCreateThenSaveImageFacesResultsForOutputResolutions.Any())
break;
if (_Exceptions.Count == 0)

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