Added Class Library FaceRecognitionDotNet

This commit is contained in:
2022-07-30 16:43:23 -07:00
parent f642c5669a
commit 2ebec0b7a9
45 changed files with 2398 additions and 149 deletions

View File

@ -95,7 +95,7 @@ public class A_Property : Shared.Models.Properties.IProperty, IProperty
string year;
string directoryName;
string[] directorySegments;
string? check = Path.GetPathRoot(filteredSourceDirectoryFile);
string? check = Path.GetFullPath(filteredSourceDirectoryFile);
string? pathRoot = Path.GetPathRoot(filteredSourceDirectoryFile);
if (string.IsNullOrEmpty(pathRoot))
throw new Exception();

View File

@ -14,16 +14,18 @@ namespace View_by_Distance.Property.Models;
public class PropertyLogic
{
protected readonly List<(int, string[])> _AllCollection;
protected readonly List<string> _ExceptionsDirectories;
protected readonly Dictionary<int, int[]> _KeyValuePairs;
protected readonly Dictionary<int, int[]> _IndicesFromNew;
protected readonly Dictionary<int, int[]> _IndicesFromOld;
protected readonly Dictionary<int, string[]> _NamedFaceInfo;
protected readonly Dictionary<int, string[]> _SixCharacterNamedFaceInfo;
protected readonly Dictionary<int, string[]> _NamedFaceInfoDeterministicHashCodeIndices;
public List<string> AngleBracketCollection { get; }
public Dictionary<int, int[]> KeyValuePairs => _KeyValuePairs;
public Dictionary<int, int[]> IndicesFromNew => _IndicesFromNew;
public Dictionary<int, int[]> IndicesFromOld => _IndicesFromOld;
public Dictionary<int, string[]> NamedFaceInfo => _NamedFaceInfo;
public List<string> ExceptionsDirectories => _ExceptionsDirectories;
public Dictionary<int, string[]> NamedFaceInfoDeterministicHashCodeIndices => _NamedFaceInfoDeterministicHashCodeIndices;
private readonly Serilog.ILogger? _Log;
private readonly string[] _VerifyToSeason;
@ -34,12 +36,14 @@ public class PropertyLogic
public PropertyLogic(int maxDegreeOfParallelism, Configuration configuration)
{
_AllCollection = new();
_Configuration = configuration;
_ExceptionsDirectories = new();
_ASCIIEncoding = new ASCIIEncoding();
AngleBracketCollection = new List<string>();
_Log = Serilog.Log.ForContext<A_Property>();
_MaxDegreeOfParallelism = maxDegreeOfParallelism;
Dictionary<int, string[]>? namedFaceInfoDeterministicHashCodeIndices;
_WriteIndentedJsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true };
if (configuration.VerifyToSeason is null || !configuration.VerifyToSeason.Any())
throw new Exception();
@ -47,32 +51,47 @@ public class PropertyLogic
string json;
string[] files;
string fullPath;
Dictionary<int, int[]>? indicesFromOld;
Dictionary<int, string[]>? namedFaceInfo;
Dictionary<int, int[]>? keyValuePairs;
List<KeyValuePair<int, int[]>>? collection;
Dictionary<int, int[]> indicesFromNew = new();
Dictionary<int, string[]>? sixCharacterNamedFaceInfo;
string? rootDirectoryParent = Path.GetDirectoryName(configuration.RootDirectory);
if (string.IsNullOrEmpty(rootDirectoryParent))
throw new Exception($"{nameof(rootDirectoryParent)} is null!");
files = Directory.GetFiles(rootDirectoryParent, "*Named*.json", SearchOption.TopDirectoryOnly);
files = Directory.GetFiles(rootDirectoryParent, "*DeterministicHashCode*.json", SearchOption.TopDirectoryOnly);
if (files.Length != 1)
namedFaceInfo = new();
namedFaceInfoDeterministicHashCodeIndices = new();
else
{
json = File.ReadAllText(files[0]);
namedFaceInfo = JsonSerializer.Deserialize<Dictionary<int, string[]>>(json);
if (namedFaceInfo is null)
throw new Exception($"{nameof(namedFaceInfo)} is null!");
namedFaceInfoDeterministicHashCodeIndices = JsonSerializer.Deserialize<Dictionary<int, string[]>>(json);
if (namedFaceInfoDeterministicHashCodeIndices is null)
throw new Exception($"{nameof(namedFaceInfoDeterministicHashCodeIndices)} is null!");
}
if (namedFaceInfoDeterministicHashCodeIndices.Any())
sixCharacterNamedFaceInfo = new();
else
{
files = Directory.GetFiles(rootDirectoryParent, "*SixCharacter*.json", SearchOption.TopDirectoryOnly);
if (files.Length != 1)
sixCharacterNamedFaceInfo = new();
else
{
json = File.ReadAllText(files[0]);
sixCharacterNamedFaceInfo = JsonSerializer.Deserialize<Dictionary<int, string[]>>(json);
if (sixCharacterNamedFaceInfo is null)
throw new Exception($"{nameof(sixCharacterNamedFaceInfo)} is null!");
}
}
files = Directory.GetFiles(rootDirectoryParent, "*keyValuePairs*.json", SearchOption.TopDirectoryOnly);
if (files.Length != 1)
indicesFromOld = new();
keyValuePairs = new();
else
{
json = File.ReadAllText(files[0]);
indicesFromOld = JsonSerializer.Deserialize<Dictionary<int, int[]>>(json);
if (indicesFromOld is null)
throw new Exception($"{nameof(indicesFromOld)} is null!");
keyValuePairs = JsonSerializer.Deserialize<Dictionary<int, int[]>>(json);
if (keyValuePairs is null)
throw new Exception($"{nameof(keyValuePairs)} is null!");
}
foreach (string propertyContentCollectionFile in configuration.PropertyContentCollectionFiles)
{
@ -92,9 +111,10 @@ public class PropertyLogic
indicesFromNew.Add(keyValuePair.Key, keyValuePair.Value);
}
}
_NamedFaceInfo = namedFaceInfo;
_KeyValuePairs = keyValuePairs;
_IndicesFromNew = indicesFromNew;
_IndicesFromOld = indicesFromOld;
_SixCharacterNamedFaceInfo = sixCharacterNamedFaceInfo;
_NamedFaceInfoDeterministicHashCodeIndices = namedFaceInfoDeterministicHashCodeIndices;
}
public override string ToString()
@ -180,6 +200,8 @@ public class PropertyLogic
}
else if (!isIgnoreExtension && isValidImageFormatExtension)
{
if (!_IndicesFromNew.Any() && !_KeyValuePairs.Any())
throw new Exception("In order to keep six character indices at least one need to have an item!");
try
{
using Image image = Image.FromFile(filteredSourceDirectoryFileInfo.FullName);
@ -216,10 +238,10 @@ public class PropertyLogic
encodingHash = Stateless.A_Property.GetDeterministicHashCode(encoding);
if (_MaxDegreeOfParallelism < 2)
ticks = LogDelta(ticks, nameof(Stateless.A_Property.GetDeterministicHashCode));
if (!_IndicesFromOld.ContainsKey(encodingHash))
if (!_KeyValuePairs.ContainsKey(encodingHash))
indices.Add(encodingHash);
else
indices.AddRange(_IndicesFromOld[encodingHash]);
indices.AddRange(_KeyValuePairs[encodingHash]);
}
}
width = image.Width;
@ -635,7 +657,7 @@ public class PropertyLogic
File.Move(propertyHolder.ImageFileInfo.FullName, filteredSourceDirectoryFileExtensionLowered);
if (propertyHolder.Changed is null || propertyHolder.Changed.Value || propertyHolder.Property is null)
{
property = GetPropertyOfPrivate(angleBracket, propertyHolder, firstPass, filteredSourceDirectoryFileTuples, parseExceptions, isIgnoreExtension, isValidImageFormatExtension, isValidMetadataExtensions, extensionLowered,fileNameWithoutExtension);
property = GetPropertyOfPrivate(angleBracket, propertyHolder, firstPass, filteredSourceDirectoryFileTuples, parseExceptions, isIgnoreExtension, isValidImageFormatExtension, isValidMetadataExtensions, extensionLowered, fileNameWithoutExtension);
lock (propertyHolder)
propertyHolder.Update(property);
}
@ -799,4 +821,53 @@ public class PropertyLogic
return results.OrderBy(l => l.Ticks).ToArray();
}
public void AddToPropertyLogicAllCollection(PropertyHolder[] filteredPropertyHolderCollection)
{
if (_SixCharacterNamedFaceInfo.Any())
{
string[] keys;
PropertyHolder propertyHolder;
for (int i = 0; i < filteredPropertyHolderCollection.Length; i++)
{
propertyHolder = filteredPropertyHolderCollection[i];
if (propertyHolder.Property?.Id is null)
continue;
foreach (int sixCharacterIndex in propertyHolder.Property.Indices)
{
if (!_SixCharacterNamedFaceInfo.ContainsKey(sixCharacterIndex))
continue;
keys = _SixCharacterNamedFaceInfo[sixCharacterIndex];
_AllCollection.Add(new(propertyHolder.Property.Id.Value, keys));
}
}
}
}
public void SaveAllCollection()
{
if (_AllCollection.Any())
{
string[] keys;
string? rootDirectoryParent = Path.GetDirectoryName(_Configuration.RootDirectory);
if (string.IsNullOrEmpty(rootDirectoryParent))
throw new Exception($"{nameof(rootDirectoryParent)} is null!");
Dictionary<int, string[]> namedFaceInfoDeterministicHashCodeIndices = new();
List<(int, string[])> allCollection = _AllCollection.OrderBy(l => l.Item1).ToList();
foreach ((int deterministicHashCode, string[] values) in allCollection)
{
if (namedFaceInfoDeterministicHashCodeIndices.ContainsKey(deterministicHashCode))
{
keys = namedFaceInfoDeterministicHashCodeIndices[deterministicHashCode];
if (JsonSerializer.Serialize(values) == JsonSerializer.Serialize(keys))
continue;
throw new Exception();
}
namedFaceInfoDeterministicHashCodeIndices.Add(deterministicHashCode, values);
}
string json = JsonSerializer.Serialize(namedFaceInfoDeterministicHashCodeIndices, new JsonSerializerOptions { WriteIndented = true });
string checkFile = Path.Combine(rootDirectoryParent, "NamedFaceInfoDeterministicHashCodeIndices.json");
_ = IPath.WriteAllText(checkFile, json, compareBeforeWrite: true);
}
}
}