Model changes for Unit Test Face

This commit is contained in:
2022-08-01 20:06:07 -07:00
parent 022d9904da
commit 62bdc17f7a
20 changed files with 194 additions and 158 deletions

View File

@ -52,7 +52,7 @@ public class DlibDotNet
Property.Models.Configuration.Verify(propertyConfiguration);
Models.Configuration configuration = Models.Stateless.Configuration.Get(isEnvironment, configurationRoot, workingDirectory, propertyConfiguration);
Verify(configuration);
(Model model, PredictorModel predictorModel) = GetTuple(args, propertyConfiguration, configuration);
VerifyExtra(args, propertyConfiguration, configuration);
_Configuration = configuration;
_Index = new G_Index(configuration);
_Random = new F_Random(configuration);
@ -84,13 +84,8 @@ public class DlibDotNet
_ArgZeroIsConfigurationRootDirectory = propertyConfiguration.RootDirectory == argZero;
(ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters) = C_Resize.GetTuple(configuration.OutputExtension, configuration.OutputQuality.Value);
_Resize = new C_Resize(configuration.ForceResizeLastWriteTimeToCreationTime.Value, configuration.OverrideForResizeImages.Value, configuration.PropertiesChangedForResize.Value, configuration.ValidResolutions, imageCodecInfo, encoderParameters);
ModelParameter modelParameter = new()
{
CnnFaceDetectorModel = File.ReadAllBytes(Path.Combine(configuration.ModelDirectory, "mmod_human_face_detector.dat")),
FaceRecognitionModel = File.ReadAllBytes(Path.Combine(configuration.ModelDirectory, "dlib_face_recognition_resnet_model_v1.dat")),
PosePredictor5FaceLandmarksModel = File.ReadAllBytes(Path.Combine(configuration.ModelDirectory, "shape_predictor_5_face_landmarks.dat")),
PosePredictor68FaceLandmarksModel = File.ReadAllBytes(Path.Combine(configuration.ModelDirectory, "shape_predictor_68_face_landmarks.dat"))
};
_Log.Information(configuration.ModelDirectory);
(Model model, PredictorModel predictorModel, ModelParameter modelParameter) = GetModel(configuration);
_Faces = new D_Face(configuration, argZero, model, modelParameter, predictorModel);
if (configuration.SkipSearch is null)
throw new Exception($"{nameof(configuration.SkipSearch)} is null!");
@ -149,37 +144,12 @@ public class DlibDotNet
#pragma warning disable CA1416
#pragma warning restore CA1416
private (Model Model, PredictorModel PredictorModel) GetTuple(List<string> args, Property.Models.Configuration propertyConfiguration, Models.Configuration configuration)
private static (Model model, PredictorModel predictorModel, ModelParameter modelParameter) GetModel(Models.Configuration configuration)
{
if (_Log is null)
throw new Exception($"{nameof(_Log)} is null!");
(Model Model, PredictorModel PredictorModel) result;
(Model, PredictorModel, ModelParameter) result;
Array array;
Model? model = null;
string[] sourceDirectoryNames;
PredictorModel? predictorModel = null;
if (!args.Any())
sourceDirectoryNames = Array.Empty<string>();
else
{
string argZero = Path.GetFullPath(args[0]);
sourceDirectoryNames = argZero.Split(Path.DirectorySeparatorChar);
if (!argZero.StartsWith(propertyConfiguration.RootDirectory))
throw new Exception($"Source directory must be inside root directory! <{argZero}> <{propertyConfiguration.RootDirectory}>");
if (_ArgZeroIsConfigurationRootDirectory && propertyConfiguration.RootDirectory != argZero)
{
if (!configuration.MixedYearRelativePaths.Contains(sourceDirectoryNames[0]))
{
string[] segments = sourceDirectoryNames[0].Split(' ');
if (segments.Length < 2 || segments[^1].Length != 4 || (segments[^1][..2] != "19" && segments[^1][..2] != "20"))
throw new Exception("root subdirectory must have a year at the end or directory name needs to be added to the exclude list!");
}
}
}
_Log.Information(configuration.ModelDirectory);
string[] resizeMatch = (from l in sourceDirectoryNames where configuration.ValidResolutions.Contains(l) select l).ToArray();
if (resizeMatch.Any())
throw new Exception("Input directory should be the source and not a resized directory!");
array = Enum.GetValues(typeof(Model));
foreach (Model check in array)
{
@ -204,7 +174,14 @@ public class DlibDotNet
if (predictorModel is null)
throw new Exception("Destination directory must have Predictor Model name!");
predictorModel = predictorModel.Value;
result = new(model.Value, predictorModel.Value);
ModelParameter modelParameter = new()
{
CnnFaceDetectorModel = File.ReadAllBytes(Path.Combine(configuration.ModelDirectory, "mmod_human_face_detector.dat")),
FaceRecognitionModel = File.ReadAllBytes(Path.Combine(configuration.ModelDirectory, "dlib_face_recognition_resnet_model_v1.dat")),
PosePredictor5FaceLandmarksModel = File.ReadAllBytes(Path.Combine(configuration.ModelDirectory, "shape_predictor_5_face_landmarks.dat")),
PosePredictor68FaceLandmarksModel = File.ReadAllBytes(Path.Combine(configuration.ModelDirectory, "shape_predictor_68_face_landmarks.dat"))
};
result = new(model.Value, predictorModel.Value, modelParameter);
return result;
}
@ -300,6 +277,32 @@ public class DlibDotNet
throw new Exception($"{nameof(configuration.DistanceFactor)} and {nameof(configuration.LocationConfidenceFactor)} must add up to 10!");
}
private void VerifyExtra(List<string> args, Property.Models.Configuration propertyConfiguration, Models.Configuration configuration)
{
string[] sourceDirectoryNames;
if (!args.Any())
sourceDirectoryNames = Array.Empty<string>();
else
{
string argZero = Path.GetFullPath(args[0]);
sourceDirectoryNames = argZero.Split(Path.DirectorySeparatorChar);
if (!argZero.StartsWith(propertyConfiguration.RootDirectory))
throw new Exception($"Source directory must be inside root directory! <{argZero}> <{propertyConfiguration.RootDirectory}>");
if (_ArgZeroIsConfigurationRootDirectory && propertyConfiguration.RootDirectory != argZero)
{
if (!configuration.MixedYearRelativePaths.Contains(sourceDirectoryNames[0]))
{
string[] segments = sourceDirectoryNames[0].Split(' ');
if (segments.Length < 2 || segments[^1].Length != 4 || (segments[^1][..2] != "19" && segments[^1][..2] != "20"))
throw new Exception("root subdirectory must have a year at the end or directory name needs to be added to the exclude list!");
}
}
}
string[] resizeMatch = (from l in sourceDirectoryNames where configuration.ValidResolutions.Contains(l) select l).ToArray();
if (resizeMatch.Any())
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)
{
if (propertyHolder.ImageFileInfo is null)
@ -325,6 +328,7 @@ public class DlibDotNet
{
sourceDirectoryChanges.Add(new Tuple<string, DateTime>(nameof(A_Property), propertyHolder.ImageFileInfo.LastWriteTime));
property = propertyLogic.GetProperty(propertyLogic.AngleBracketCollection[0], propertyHolder, subFileTuples, parseExceptions);
propertyHolder.Update(property);
}
else
{
@ -332,25 +336,24 @@ public class DlibDotNet
if (propertyHolder.Changed.HasValue && propertyHolder.Changed.Value)
sourceDirectoryChanges.Add(new Tuple<string, DateTime>(nameof(A_Property), propertyHolder.ImageFileInfo.LastWriteTime));
}
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(propertyHolder.ImageFileInfo.FullName);
(int metadataGroups, metadataCollection) = _Metadata.GetMetadataCollection(subFileTuples, parseExceptions, propertyHolder.ImageFileInfo.FullName, propertyHolder.RelativePath, fileNameWithoutExtension);
(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);
imageResizeKeyValuePairs = _Resize.GetResizeKeyValuePairs(subFileTuples, parseExceptions, original, metadataCollection, property, propertyHolder.ImageFileInfo.FullName, propertyHolder.RelativePath, fileNameWithoutExtension);
imageResizeKeyValuePairs = _Resize.GetResizeKeyValuePairs(subFileTuples, parseExceptions, original, metadataCollection, property, propertyHolder);
if (_AppSettings.MaxDegreeOfParallelism.Value < 2)
ticks = LogDelta(ticks, nameof(C_Resize.GetResizeKeyValuePairs));
if (_Configuration.SaveResizedSubfiles.Value)
{
_Resize.SaveResizedSubfile(outputResolution, subFileTuples, propertyHolder.ImageFileInfo.FullName, original, property, imageResizeKeyValuePairs, resizedFileInfo);
_Resize.SaveResizedSubfile(outputResolution, subFileTuples, propertyHolder, original, property, imageResizeKeyValuePairs);
if (_AppSettings.MaxDegreeOfParallelism.Value < 2)
ticks = LogDelta(ticks, nameof(C_Resize.SaveResizedSubfile));
resizedFileInfo.Refresh();
}
else if (outputResolution == _Configuration.OutputResolutions[0] && false)
{
byte[] bytes = _Resize.GetResizedBytes(outputResolution, subFileTuples, propertyHolder.ImageFileInfo.FullName, property, imageResizeKeyValuePairs);
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));
@ -364,15 +367,15 @@ public class DlibDotNet
int outputResolutionWidth = outputResolutionCollection[0];
int outputResolutionHeight = outputResolutionCollection[1];
int outputResolutionOrientation = outputResolutionCollection[2];
faceCollection = _Faces.GetFaces(_Configuration.PropertyConfiguration, outputResolution, subFileTuples, parseExceptions, propertyHolder.RelativePath, fileNameWithoutExtension, property, resizedFileInfo, outputResolutionWidth, outputResolutionHeight, outputResolutionOrientation);
faceCollection = _Faces.GetFaces(_Configuration.PropertyConfiguration, outputResolution, subFileTuples, parseExceptions, propertyHolder, property, resizedFileInfo, outputResolutionWidth, outputResolutionHeight, outputResolutionOrientation);
if (_AppSettings.MaxDegreeOfParallelism.Value < 2)
ticks = LogDelta(ticks, nameof(D_Face.GetFaces));
_Faces.SaveFaces(_Configuration.PropertyConfiguration, subFileTuples, parseExceptions, propertyHolder.RelativePath, fileNameWithoutExtension, resizedFileInfo, faceCollection);
_Faces.SaveFaces(_Configuration.PropertyConfiguration, subFileTuples, parseExceptions, propertyHolder, faceCollection);
if (_AppSettings.MaxDegreeOfParallelism.Value < 2)
ticks = LogDelta(ticks, nameof(D_Face.SaveFaces));
if (_Configuration.SaveFaceLandmarkForOutputResolutions.Contains(outputResolution))
{
_FaceLandmarks.SaveFaceLandmarkImages(subFileTuples, parseExceptions, propertyHolder.RelativePath, fileNameWithoutExtension, resizedFileInfo, faceCollection);
_FaceLandmarks.SaveFaceLandmarkImages(subFileTuples, parseExceptions, propertyHolder, faceCollection);
if (_AppSettings.MaxDegreeOfParallelism.Value < 2)
ticks = LogDelta(ticks, nameof(D2_FaceLandmarks.SaveFaceLandmarkImages));
}
@ -551,7 +554,7 @@ public class DlibDotNet
}
}
private void FullDoWork(Property.Models.Configuration configuration, Model model, PredictorModel predictorModel, string argZero, Dictionary<string, List<Person>> peopleCollection, PropertyLogic propertyLogic, List<PropertyHolder[]> propertyHolderCollections)
private void FullDoWork(Property.Models.Configuration configuration, Model? model, PredictorModel? predictorModel, string argZero, Dictionary<string, List<Person>> peopleCollection, PropertyLogic propertyLogic, List<PropertyHolder[]> propertyHolderCollections)
{
if (_Log is null)
throw new Exception($"{nameof(_Log)} is null!");
@ -745,7 +748,7 @@ public class DlibDotNet
return result;
}
private void Search(Property.Models.Configuration configuration, bool reverse, Model model, PredictorModel predictorModel, string argZero, Person[] people)
private void Search(Property.Models.Configuration configuration, bool reverse, Model? model, PredictorModel? predictorModel, string argZero, Person[] people)
{
PropertyLogic propertyLogic = GetPropertyLogic();
Dictionary<string, List<Person>> peopleCollection = A2_People.Convert(people);
@ -753,6 +756,6 @@ public class DlibDotNet
FullDoWork(configuration, model, predictorModel, argZero, peopleCollection, propertyLogic, propertyHolderCollections);
}
internal void RenameQueue(Property.Models.Configuration configuration, Model model, PredictorModel predictorModel) => _Rename.RenameQueue(configuration, model, predictorModel);
internal void RenameQueue(Property.Models.Configuration configuration, Model? model, PredictorModel? predictorModel) => _Rename.RenameQueue(configuration, model, predictorModel);
}