Tested first run
This commit is contained in:
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user