Change to support 7680 x 4320 and
Configuration additions
This commit is contained in:
@ -1,80 +1,68 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace View_by_Distance.Shared.Models.Stateless.Methods;
|
||||
|
||||
internal abstract class Mapping
|
||||
{
|
||||
|
||||
private static void UseKeyValuePairsSaveFaceEncoding(int locationDigits, Dictionary<int, List<Models.Face>> keyValuePairs, string file, int id, int normalizedPixelPercentageValue, double deterministicHashCodeKey, string extensionLowered)
|
||||
private static void IfNotAlreadyFileMove(string file, int idValue, int normalizedPixelPercentageValue, string extensionLowered)
|
||||
{
|
||||
string json;
|
||||
string checkFile;
|
||||
string? directoryName;
|
||||
List<Models.Face> collection = new();
|
||||
List<Models.Face> faces = keyValuePairs[id];
|
||||
foreach (Models.Face face in faces)
|
||||
{
|
||||
if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null)
|
||||
continue;
|
||||
if (normalizedPixelPercentageValue != face.Location.NormalizedPixelPercentage.Value && deterministicHashCodeKey != IMapping.GetDeterministicHashCodeKeyValue(locationDigits, id, face.Location.NormalizedPixelPercentage.Value))
|
||||
continue;
|
||||
collection.Add(face);
|
||||
}
|
||||
if (collection.Count != 1)
|
||||
string? directoryName = Path.GetDirectoryName(file);
|
||||
if (string.IsNullOrEmpty(directoryName))
|
||||
throw new Exception();
|
||||
foreach (Models.Face face in collection)
|
||||
{
|
||||
directoryName = Path.GetDirectoryName(file);
|
||||
if (string.IsNullOrEmpty(directoryName))
|
||||
continue;
|
||||
checkFile = Path.Combine(directoryName, $"{deterministicHashCodeKey}{extensionLowered}.json");
|
||||
if (File.Exists(checkFile))
|
||||
continue;
|
||||
json = JsonSerializer.Serialize(face.FaceEncoding);
|
||||
_ = IPath.WriteAllText(checkFile, json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null);
|
||||
}
|
||||
string checkFile = Path.Combine(directoryName, $"{IMapping.GetDeterministicHashCodeKey(idValue, normalizedPixelPercentageValue)}{extensionLowered}");
|
||||
if (!File.Exists(checkFile))
|
||||
File.Move(file, checkFile);
|
||||
}
|
||||
|
||||
private static (int?, int?, double?) GetReversedDeterministicHashCodeKeysFromSegments(int locationDigits, bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file, string[] segments)
|
||||
{
|
||||
double? result;
|
||||
if (segments.Length != 3)
|
||||
throw new Exception();
|
||||
string id = segments[0];
|
||||
int normalizedPixelPercentageValue;
|
||||
string normalizedPixelPercentage = segments[1];
|
||||
if (!int.TryParse(id, out int idValue) | !int.TryParse(normalizedPixelPercentage, out normalizedPixelPercentageValue))
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
result = IMapping.GetDeterministicHashCodeKeyValue(locationDigits, idValue, normalizedPixelPercentageValue);
|
||||
if (keyValuePairsAny && keyValuePairs.ContainsKey(idValue))
|
||||
UseKeyValuePairsSaveFaceEncoding(locationDigits, keyValuePairs, file, idValue, normalizedPixelPercentageValue, result.Value, $".{segments[2]}");
|
||||
}
|
||||
return new(idValue, normalizedPixelPercentageValue, result);
|
||||
}
|
||||
|
||||
internal static (int?, int?) GetReversedDeterministicHashCodeKey(int locationDigits, bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file)
|
||||
private static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKeysFromSegments(int locationDigits, bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file, string[] segments)
|
||||
{
|
||||
int? id;
|
||||
List<Models.Face>? faces;
|
||||
int? normalizedPixelPercentage;
|
||||
if (segments.Length != 3)
|
||||
{
|
||||
id = null;
|
||||
faces = null;
|
||||
normalizedPixelPercentage = null;
|
||||
}
|
||||
else if (!int.TryParse(segments[0], out int idValue) || !int.TryParse(segments[1].PadRight(locationDigits, '0'), out int normalizedPixelPercentageValue))
|
||||
{
|
||||
id = null;
|
||||
faces = null;
|
||||
normalizedPixelPercentage = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
id = idValue;
|
||||
string extensionLowered = $".{segments[2]}";
|
||||
normalizedPixelPercentage = normalizedPixelPercentageValue;
|
||||
if (segments[1].Length != locationDigits)
|
||||
IfNotAlreadyFileMove(file, idValue, normalizedPixelPercentageValue, extensionLowered);
|
||||
if (!keyValuePairsAny || !keyValuePairs.ContainsKey(idValue))
|
||||
faces = null;
|
||||
else
|
||||
faces = keyValuePairs[idValue];
|
||||
}
|
||||
return new(id, normalizedPixelPercentage, faces);
|
||||
}
|
||||
|
||||
internal static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKey(int locationDigits, bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file)
|
||||
{
|
||||
int? id;
|
||||
List<Models.Face>? faces;
|
||||
int? normalizedPixelPercentage;
|
||||
string fileName = Path.GetFileName(file);
|
||||
if (fileName.Length < 2 || fileName[1..].Contains('-'))
|
||||
{
|
||||
id = null;
|
||||
faces = null;
|
||||
normalizedPixelPercentage = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
string[] segments = fileName.Split('.');
|
||||
(id, normalizedPixelPercentage, double? result) = GetReversedDeterministicHashCodeKeysFromSegments(locationDigits, keyValuePairsAny, keyValuePairs, file, segments);
|
||||
if (result is null)
|
||||
{
|
||||
id = null;
|
||||
normalizedPixelPercentage = null;
|
||||
}
|
||||
(id, normalizedPixelPercentage, faces) = GetReversedDeterministicHashCodeKeysFromSegments(locationDigits, keyValuePairsAny, keyValuePairs, file, segments);
|
||||
}
|
||||
return new(id, normalizedPixelPercentage);
|
||||
return new(id, normalizedPixelPercentage, faces);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user