Tested first run
This commit is contained in:
@ -73,18 +73,4 @@ internal class A2_People
|
||||
return results.ToArray();
|
||||
}
|
||||
|
||||
internal static Dictionary<string, Person> Convert(Person[] people)
|
||||
{
|
||||
Dictionary<string, Person> results = new();
|
||||
string personKey;
|
||||
foreach (Person person in people)
|
||||
{
|
||||
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(person.Birthday);
|
||||
if (results.ContainsKey(personKey))
|
||||
break;
|
||||
results.Add(personKey, person);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
@ -15,8 +15,10 @@ namespace View_by_Distance.Instance.Models;
|
||||
internal class D2_FaceParts
|
||||
{
|
||||
|
||||
protected readonly string _FilenameExtension;
|
||||
public string FilenameExtension => _FilenameExtension;
|
||||
|
||||
private readonly Serilog.ILogger? _Log;
|
||||
private readonly string _FilenameExtension;
|
||||
private readonly Configuration _Configuration;
|
||||
private readonly ImageCodecInfo _ImageCodecInfo;
|
||||
private readonly EncoderParameters _EncoderParameters;
|
||||
|
@ -19,29 +19,39 @@ public class D_Face
|
||||
|
||||
internal List<string> AngleBracketCollection { get; }
|
||||
|
||||
protected readonly string _FilenameExtension;
|
||||
public string FilenameExtension => _FilenameExtension;
|
||||
|
||||
protected readonly string _HiddenFilenameExtension;
|
||||
public string HiddenFilenameExtension => _HiddenFilenameExtension;
|
||||
|
||||
private readonly Model _Model;
|
||||
private readonly string _ArgZero;
|
||||
private readonly Serilog.ILogger? _Log;
|
||||
private readonly string _FilenameExtension;
|
||||
private readonly Configuration _Configuration;
|
||||
private readonly ImageCodecInfo _ImageCodecInfo;
|
||||
private readonly ModelParameter _ModelParameter;
|
||||
private readonly PredictorModel _PredictorModel;
|
||||
private readonly ImageCodecInfo _ImageCodecInfo;
|
||||
private readonly EncoderParameters _EncoderParameters;
|
||||
private readonly ImageCodecInfo _HiddenImageCodecInfo;
|
||||
private readonly EncoderParameters _HiddenEncoderParameters;
|
||||
private readonly JsonSerializerOptions _WriteIndentedJsonSerializerOptions;
|
||||
|
||||
internal D_Face(Configuration configuration, string argZero, Model model, ModelParameter modelParameter, PredictorModel predictorModel, ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters, string filenameExtension)
|
||||
internal D_Face(Configuration configuration, string argZero, Model model, ModelParameter modelParameter, PredictorModel predictorModel, ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters, string filenameExtension, ImageCodecInfo hiddenImageCodecInfo, EncoderParameters hiddenEncoderParameters, string hiddenFilenameExtension)
|
||||
{
|
||||
_Model = model;
|
||||
_ArgZero = argZero;
|
||||
_Configuration = configuration;
|
||||
_ImageCodecInfo = imageCodecInfo;
|
||||
_ModelParameter = modelParameter;
|
||||
_PredictorModel = predictorModel;
|
||||
_ImageCodecInfo = imageCodecInfo;
|
||||
_EncoderParameters = encoderParameters;
|
||||
_FilenameExtension = filenameExtension;
|
||||
AngleBracketCollection = new List<string>();
|
||||
_Log = Serilog.Log.ForContext<D_Face>();
|
||||
AngleBracketCollection = new List<string>();
|
||||
_HiddenImageCodecInfo = hiddenImageCodecInfo;
|
||||
_HiddenEncoderParameters = hiddenEncoderParameters;
|
||||
_HiddenFilenameExtension = hiddenFilenameExtension;
|
||||
_WriteIndentedJsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true };
|
||||
}
|
||||
|
||||
@ -133,7 +143,7 @@ public class D_Face
|
||||
return result;
|
||||
}
|
||||
|
||||
private void SaveFaces(FileHolder resizedFileHolder, List<(Face, string)> collection)
|
||||
private void SaveFaces(FileHolder resizedFileHolder, List<(Face, FileInfo?, string)> collection)
|
||||
{
|
||||
int width;
|
||||
int height;
|
||||
@ -142,8 +152,10 @@ public class D_Face
|
||||
Bitmap preRotated;
|
||||
Rectangle rectangle;
|
||||
using Bitmap source = new(resizedFileHolder.FullName);
|
||||
foreach ((Face face, string fileName) in collection)
|
||||
foreach ((Face face, FileInfo? fileInfo, string fileName) in collection)
|
||||
{
|
||||
if (fileInfo is null)
|
||||
continue;
|
||||
if (face.FaceEncoding is null || face?.Location is null)
|
||||
continue;
|
||||
location = Shared.Models.Stateless.Methods.ILocation.GetLocation(face.Location, source.Height, source.Width, collection.Count);
|
||||
@ -156,23 +168,40 @@ public class D_Face
|
||||
{
|
||||
using (graphics = Graphics.FromImage(preRotated))
|
||||
graphics.DrawImage(source, new Rectangle(0, 0, width, height), rectangle, GraphicsUnit.Pixel);
|
||||
preRotated.Save(fileName, _ImageCodecInfo, _EncoderParameters);
|
||||
preRotated.Save(fileInfo.FullName, _ImageCodecInfo, _EncoderParameters);
|
||||
}
|
||||
if (File.Exists(fileName))
|
||||
File.Delete(fileName);
|
||||
location = Shared.Models.Stateless.Methods.ILocation.GetLocation(IFaceDistance.HiddenImageFactor, face.Location, source.Height, source.Width, collection.Count);
|
||||
if (location is null)
|
||||
continue;
|
||||
width = location.Right - location.Left;
|
||||
height = location.Bottom - location.Top;
|
||||
rectangle = new Rectangle(location.Left, location.Top, width, height);
|
||||
using (preRotated = new(width, height))
|
||||
{
|
||||
using (graphics = Graphics.FromImage(preRotated))
|
||||
graphics.DrawImage(source, new Rectangle(0, 0, width, height), rectangle, GraphicsUnit.Pixel);
|
||||
preRotated.Save(fileName, _HiddenImageCodecInfo, _HiddenEncoderParameters);
|
||||
}
|
||||
File.SetAttributes(fileName, FileAttributes.Hidden);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Face> GetFaces(FileHolder resizedFileHolder, Item item, Shared.Models.Property property, int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation)
|
||||
private List<Face> GetFaces(Item item, Shared.Models.Property property, int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation)
|
||||
{
|
||||
List<Face> results = new();
|
||||
if (item.ImageFileHolder is null)
|
||||
throw new NullReferenceException(nameof(item.ImageFileHolder));
|
||||
if (item.ResizedFileHolder is null)
|
||||
throw new NullReferenceException(nameof(item.ResizedFileHolder));
|
||||
FaceRecognitionDotNet.Image? unknownImage;
|
||||
if (!resizedFileHolder.Exists)
|
||||
if (!item.ResizedFileHolder.Exists)
|
||||
unknownImage = null;
|
||||
else
|
||||
{
|
||||
try
|
||||
{ unknownImage = FaceRecognition.LoadImageFile(resizedFileHolder.FullName); }
|
||||
{ unknownImage = FaceRecognition.LoadImageFile(item.ResizedFileHolder.FullName); }
|
||||
catch (Exception)
|
||||
{ unknownImage = null; }
|
||||
}
|
||||
@ -214,7 +243,7 @@ public class D_Face
|
||||
|
||||
#pragma warning restore CA1416
|
||||
|
||||
internal List<Face> GetFaces(string dResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, Item item, Shared.Models.Property property, FileHolder resizedFileHolder, int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation)
|
||||
internal List<Face> GetFaces(string dResultsFullGroupDirectory, List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, Item item, Shared.Models.Property property, int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation)
|
||||
{
|
||||
List<Face>? results;
|
||||
if (item.Property?.Id is null)
|
||||
@ -277,8 +306,6 @@ public class D_Face
|
||||
if (!_Configuration.ForceFaceLastWriteTimeToCreationTime)
|
||||
{
|
||||
normalizedPixelPercentageCollection = Shared.Models.Stateless.Methods.IFace.GetInts(results);
|
||||
if (normalizedPixelPercentageCollection.Contains(3))
|
||||
throw new Exception($"Not allowed! <{fileInfo.FullName}>");
|
||||
normalizedPixelPercentageDistinctCount = normalizedPixelPercentageCollection.Distinct().Count();
|
||||
if (normalizedPixelPercentageDistinctCount != normalizedPixelPercentageCollection.Length)
|
||||
throw new Exception($"Not distinct! <{fileInfo.FullName}>");
|
||||
@ -293,7 +320,7 @@ public class D_Face
|
||||
}
|
||||
if (results is null)
|
||||
{
|
||||
results = GetFaces(resizedFileHolder, item, property, outputResolutionWidth, outputResolutionHeight, outputResolutionOrientation);
|
||||
results = GetFaces(item, property, outputResolutionWidth, outputResolutionHeight, outputResolutionOrientation);
|
||||
json = JsonSerializer.Serialize(results, _WriteIndentedJsonSerializerOptions);
|
||||
bool updateDateWhenMatches = dateTimes.Any() && fileInfo.Exists && dateTimes.Max() > fileInfo.LastWriteTime;
|
||||
DateTime? dateTime = !updateDateWhenMatches ? null : dateTimes.Max();
|
||||
@ -330,7 +357,7 @@ public class D_Face
|
||||
bool check = false;
|
||||
string parentCheck;
|
||||
double deterministicHashCodeKey;
|
||||
List<(Face, string)> collection = new();
|
||||
List<(Face, FileInfo?, string)> collection = new();
|
||||
string[] changesFrom = new string[] { nameof(A_Property), nameof(B_Metadata), nameof(C_Resize) };
|
||||
string facesDirectory = Path.Combine(AngleBracketCollection[0].Replace("<>", "()"), item.ImageFileHolder.NameWithoutExtension);
|
||||
List<DateTime> dateTimes = (from l in subFileTuples where changesFrom.Contains(l.Item1) select l.Item2).ToList();
|
||||
@ -341,7 +368,7 @@ public class D_Face
|
||||
{
|
||||
if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null)
|
||||
{
|
||||
collection.Add(new(face, string.Empty));
|
||||
collection.Add(new(face, null, string.Empty));
|
||||
continue;
|
||||
}
|
||||
deterministicHashCodeKey = Shared.Models.Stateless.Methods.IMapping.GetDeterministicHashCodeKey(item, face);
|
||||
@ -354,7 +381,7 @@ public class D_Face
|
||||
if (File.Exists(parentCheck))
|
||||
File.Delete(parentCheck);
|
||||
}
|
||||
collection.Add(new(face, fileInfo.FullName));
|
||||
collection.Add(new(face, fileInfo, Path.Combine(facesDirectory, $"{deterministicHashCodeKey}{item.ImageFileHolder.ExtensionLowered}{_HiddenFilenameExtension}")));
|
||||
if (_Configuration.OverrideForFaceImages)
|
||||
check = true;
|
||||
else if (!fileInfo.Exists)
|
||||
|
@ -612,6 +612,15 @@ internal class E_Distance
|
||||
return results;
|
||||
}
|
||||
|
||||
private static void SetNonFiltered(List<(FaceRecognitionDotNet.FaceEncoding, MappingContainer)> collection)
|
||||
{
|
||||
foreach ((FaceRecognitionDotNet.FaceEncoding _, MappingContainer mappingContainer) in collection)
|
||||
{
|
||||
if (mappingContainer.Mapping.Filtered is null)
|
||||
mappingContainer.Mapping.SetFiltered(value: false);
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<string, List<(FaceRecognitionDotNet.FaceEncoding, MappingContainer)>> GetThreeSigmaFaceEncodings(int maxDegreeOfParallelism, long ticks, Dictionary<string, List<MappingContainer>> keyValuePairs)
|
||||
{
|
||||
if (_Log is null)
|
||||
@ -644,8 +653,9 @@ internal class E_Distance
|
||||
faceDistances = FaceRecognition.FaceDistances(faceEncodings, faceEncodings[selectedIndex]);
|
||||
for (int i = 0; i < faceEncodings.Count; i++)
|
||||
collection[i].MappingContainer.SetDistance(faceDistances[i]);
|
||||
if (collection.Count > 1)
|
||||
if (collection.Count > 3)
|
||||
SetFiltered(collection);
|
||||
SetNonFiltered(collection);
|
||||
totalSeconds = (int)Math.Floor(new TimeSpan(DateTime.Now.Ticks - ticks).TotalSeconds);
|
||||
_Log.Information($"{keyValuePairsCount:0000}) {totalSeconds} total second(s) - {keyValuePair.Key} - {collection[selectedIndex].MappingContainer.Mapping.DisplayDirectoryName}");
|
||||
}
|
||||
@ -656,6 +666,11 @@ internal class E_Distance
|
||||
{
|
||||
Dictionary<string, List<(FaceRecognitionDotNet.FaceEncoding, MappingContainer)>> results;
|
||||
Dictionary<string, List<MappingContainer>> keyValuePairs = Map.Models.Stateless.IMapLogic.GetKeyValuePairs(ignoreRelativePaths, argZero, containers);
|
||||
if (!keyValuePairs.Any())
|
||||
{
|
||||
Map.Models.Stateless.IMapLogic.SetSingleImage(ignoreRelativePaths, argZero, containers);
|
||||
keyValuePairs = Map.Models.Stateless.IMapLogic.GetKeyValuePairs(ignoreRelativePaths, argZero, containers);
|
||||
}
|
||||
results = GetThreeSigmaFaceEncodings(maxDegreeOfParallelism, ticks, keyValuePairs);
|
||||
return results;
|
||||
}
|
||||
@ -719,11 +734,11 @@ internal class E_Distance
|
||||
if (item.ImageFileHolder is null || item.Property is null || !item.Faces.Any() || !item.Closest.Any())
|
||||
continue;
|
||||
json = JsonSerializer.Serialize(item, jsonSerializerOptions);
|
||||
fileInfo = new(string.Concat(zPropertyHolderSingletonDirectory, item.RelativePath, ".json"));
|
||||
fileInfo = new(Path.GetFullPath(string.Concat(zPropertyHolderSingletonDirectory, item.RelativePath, ".json")));
|
||||
if (fileInfo.Directory is null)
|
||||
continue;
|
||||
if (!fileInfo.Directory.Exists)
|
||||
fileInfo.Directory.Create();
|
||||
continue;
|
||||
_ = Shared.Models.Stateless.Methods.IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches, compareBeforeWrite: true);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user