Change to the Normalized Pixel Percentage Formula

This commit is contained in:
2022-09-22 23:39:58 -07:00
parent 192d2ad776
commit fb1c68e1f5
27 changed files with 426 additions and 451 deletions

View File

@ -28,21 +28,6 @@ internal abstract class Face
return result;
}
internal static List<Models.Face> GetVerifiedFaces(int locationDigits, int locationFactor, List<Models.Face> faces)
{
List<Models.Face> results = new();
foreach (Models.Face face in faces)
{
if (face.Location?.NormalizedPixelPercentage is null)
results.Add(face);
else if (face.Location.NormalizedPixelPercentage.ToString() == ILocation.GetRightPadded(locationDigits, face.Location.NormalizedPixelPercentage.Value))
results.Add(face);
else
results.Add(new(face, face.Location, locationDigits, locationFactor, faces.Count));
}
return results;
}
private static JsonElement[] GetJsonElements(string jsonFileFullName)
{
string json = GetJson(jsonFileFullName);

View File

@ -18,11 +18,6 @@ public interface IFace
static Models.Face GetFace(string jsonFileFullName) =>
Face.GetFace(jsonFileFullName);
List<Models.Face> TestStatic_GetVerifiedFaces(int locationDigits, int locationFactor, List<Models.Face> faces) =>
GetVerifiedFaces(locationDigits, locationFactor, faces);
static List<Models.Face> GetVerifiedFaces(int locationDigits, int locationFactor, List<Models.Face> faces) =>
Face.GetVerifiedFaces(locationDigits, locationFactor, faces);
Models.Face[] TestStatic_GetFaces(string jsonFileFullName) =>
GetFaces(jsonFileFullName);
static Models.Face[] GetFaces(string jsonFileFullName) =>

View File

@ -3,15 +3,15 @@ namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface ILocation
{ // ...
string TestStatic_GetRightPadded(int locationDigits, string value) =>
GetRightPadded(locationDigits, value);
static string GetRightPadded(int locationDigits, string value) =>
value.Length == locationDigits ? value : value.Length > locationDigits ? value[..locationDigits] : value.PadRight(locationDigits, '0');
string TestStatic_GetLeftPadded(int locationDigits, string value) =>
GetLeftPadded(locationDigits, value);
static string GetLeftPadded(int locationDigits, string value) =>
value.Length == locationDigits ? value : value.Length > locationDigits ? value[..locationDigits] : value.PadLeft(locationDigits, '0');
string TestStatic_GetRightPadded(int locationDigits, int value) =>
GetRightPadded(locationDigits, value);
static string GetRightPadded(int locationDigits, int value) =>
GetRightPadded(locationDigits, value.ToString());
string TestStatic_GetLeftPadded(int locationDigits, int value) =>
GetLeftPadded(locationDigits, value);
static string GetLeftPadded(int locationDigits, int value) =>
GetLeftPadded(locationDigits, value.ToString());
Models.Location? TestStatic_GetLocation(Models.Location? location, int locationDigits, int locationFactor, int height, int width, int zCount) =>
GetLocation(location, locationDigits, locationFactor, height, width, zCount);
@ -19,7 +19,7 @@ public interface ILocation
location is null ? null : new(location.Confidence, height, location, locationDigits, locationFactor, width, zCount);
Models.Location? TestStatic_GetLocation(int factor, Models.Location? location, int locationDigits, int locationFactor, int height, int width, int zCount) =>
GetLocation(location, locationDigits, locationFactor, height, width, zCount);
GetLocation(factor, location, locationDigits, locationFactor, height, width, zCount);
static Models.Location? GetLocation(int factor, Models.Location? location, int locationDigits, int locationFactor, int height, int width, int zCount) =>
location is null ? null : new(location.Confidence, factor, height, location, locationDigits, locationFactor, width, zCount);
@ -28,10 +28,15 @@ public interface ILocation
static int?[] GetInts(List<Models.Location> locations) =>
(from l in locations where l.NormalizedPixelPercentage is not null select l.NormalizedPixelPercentage).ToArray();
int TestStatic_GetNormalizedPixelPercentage(int bottom, int height, int left, int locationDigits, int locationFactor, int right, int top, int width, int zCount) =>
GetNormalizedPixelPercentage(bottom, height, left, locationDigits, locationFactor, right, top, width, zCount);
static int GetNormalizedPixelPercentage(int bottom, int height, int left, int locationDigits, int locationFactor, int right, int top, int width, int zCount) =>
Location.GetNormalizedPixelPercentage(bottom, height, left, locationDigits, locationFactor, right, top, width, zCount);
int TestStatic_GetNormalizedPixelPercentage(Models.Location location, int locationDigits, int locationFactor, OutputResolution outputResolution) =>
GetNormalizedPixelPercentage(location, locationDigits, locationFactor, outputResolution);
static int GetNormalizedPixelPercentage(Models.Location location, int locationDigits, int locationFactor, OutputResolution outputResolution) =>
Location.GetNormalizedPixelPercentage(location.Bottom, outputResolution.Height, location.Left, locationDigits, locationFactor, location.Right, location.Top, outputResolution.Width, zCount: 1);
int TestStatic_GetNormalizedPixelPercentage(int bottom, int height, int left, int locationDigits, int locationFactor, int right, int top, int width) =>
GetNormalizedPixelPercentage(bottom, height, left, locationDigits, locationFactor, right, top, width);
static int GetNormalizedPixelPercentage(int bottom, int height, int left, int locationDigits, int locationFactor, int right, int top, int width) =>
Location.GetNormalizedPixelPercentage(bottom, height, left, locationDigits, locationFactor, right, top, width, zCount: 1);
Models.Location TestStatic_GetTrimBound(double detectionConfidence, System.Drawing.Rectangle rectangle, int width, int height, int facesCount) =>
TrimBound(detectionConfidence, rectangle, width, height, facesCount);

View File

@ -3,8 +3,8 @@ namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IMapping
{ // ...
static string GetDeterministicHashCodeKey(int id, int normalizedPixelPercentage)
=> $"{id}.{normalizedPixelPercentage}";
static string GetDeterministicHashCodeKey(int id, int pixel)
=> $"{id}.{pixel}";
(string?, string?, string?, bool?) TestStatic_GetSegments(int locationDigits, string facesFileNameExtension, string fileName)
=> GetSegments(locationDigits, facesFileNameExtension, fileName);
@ -16,9 +16,9 @@ public interface IMapping
static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKey(int locationDigits, string facesFileNameExtension, string file) =>
Mapping.GetReversedDeterministicHashCodeKey(locationDigits, facesFileNameExtension, false, new(), file);
(int?, int?, List<Models.Face>?) TestStatic_GetReversedDeterministicHashCodeKey(int locationDigits, string facesFileNameExtension, bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file) =>
GetReversedDeterministicHashCodeKey(locationDigits, facesFileNameExtension, keyValuePairsAny, keyValuePairs, file);
static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKey(int locationDigits, string facesFileNameExtension, bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file) =>
Mapping.GetReversedDeterministicHashCodeKey(locationDigits, facesFileNameExtension, keyValuePairsAny, keyValuePairs, file);
(int?, int?, List<Models.Face>?) TestStatic_GetReversedDeterministicHashCodeKey(int locationDigits, string facesFileNameExtension, bool idToFacesAny, Dictionary<int, List<Models.Face>> idToFaces, string file) =>
GetReversedDeterministicHashCodeKey(locationDigits, facesFileNameExtension, idToFacesAny, idToFaces, file);
static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKey(int locationDigits, string facesFileNameExtension, bool idToFacesAny, Dictionary<int, List<Models.Face>> idToFaces, string file) =>
Mapping.GetReversedDeterministicHashCodeKey(locationDigits, facesFileNameExtension, idToFacesAny, idToFaces, file);
}

View File

@ -3,25 +3,6 @@ namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Location
{
internal static int GetNormalizedPixelPercentage(int bottom, int height, int left, int locationDigits, int locationFactor, int right, int top, int width, int zCount)
{
int result;
double value;
double total = width * height;
Check(bottom, left, right, top, zCount);
double xCenter = left + ((right - left) / 2);
double yCenter = top + ((bottom - top) / 2);
double at = ((yCenter - 1) * width) + xCenter;
value = at / total;
if (value < 0)
value = 3;
result = (int)(Math.Round(value, locationDigits) * locationFactor);
string rightPadded = ILocation.GetRightPadded(locationDigits, result);
if (result.ToString() != rightPadded)
result = int.Parse(rightPadded);
return result;
}
internal static void Check(int bottom, int left, int right, int top, int zCount)
{
if (left < 0)
@ -69,4 +50,41 @@ internal abstract class Location
throw new Exception();
}
internal static int GetNormalizedPixelPercentage(int bottom, int height, int left, int locationDigits, int locationFactor, int right, int top, int width, int zCount)
{
int result;
int checksum;
decimal center = 2m;
string xCenterPadded;
string yCenterPadded;
decimal factor = locationFactor;
// int.MaxPercentage = 21 4748 3647;
int length = (locationDigits - 1) / 2;
Check(bottom, left, right, top, zCount);
Check(bottom, height, left, right, top, width, zCount);
decimal xCenterValue = left + ((right - left) / center);
decimal yCenterValue = top + ((bottom - top) / center);
if (xCenterValue > yCenterValue)
checksum = 1;
else
checksum = 2;
decimal xCenterPercentageFactored = xCenterValue / width * factor;
decimal yCenterPercentageFactored = yCenterValue / height * factor;
int xCenterRounded = (int)Math.Round(xCenterPercentageFactored, 0);
int yCenterRounded = (int)Math.Round(yCenterPercentageFactored, 0);
if (xCenterRounded != factor)
xCenterPadded = ILocation.GetLeftPadded(length, xCenterRounded);
else
xCenterPadded = ILocation.GetLeftPadded(length, xCenterRounded - 1);
if (yCenterRounded != factor)
yCenterPadded = ILocation.GetLeftPadded(length, yCenterRounded);
else
yCenterPadded = ILocation.GetLeftPadded(length, yCenterRounded - 1);
long check = long.Parse(string.Concat(xCenterPadded, yCenterPadded, checksum));
if (check > int.MaxValue)
throw new Exception();
result = (int)check;
return result;
}
}

View File

@ -3,48 +3,6 @@ namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Mapping
{
private static void IfNotAlreadyFileMove(string facesFileNameExtension, string file, int idValue, int normalizedPixelPercentageValue, string extensionLowered)
{
string? directoryName = Path.GetDirectoryName(file);
if (string.IsNullOrEmpty(directoryName))
throw new Exception();
string checkFile = Path.Combine(directoryName, $"{IMapping.GetDeterministicHashCodeKey(idValue, normalizedPixelPercentageValue)}{extensionLowered}{facesFileNameExtension}");
if (!File.Exists(checkFile))
File.Move(file, checkFile);
}
private static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKeysFromSegments(int locationDigits, string facesFileNameExtension, bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file, string fileName)
{
int? id;
List<Models.Face>? faces;
int? normalizedPixelPercentage;
(string? Id, string? NormalizedPixelPercentage, string? ExtensionLowered, bool? Check) segments = GetSegments(locationDigits, facesFileNameExtension, fileName);
if (string.IsNullOrEmpty(segments.Id) || string.IsNullOrEmpty(segments.NormalizedPixelPercentage) || string.IsNullOrEmpty(segments.ExtensionLowered) || segments.Check is null)
{
id = null;
faces = null;
normalizedPixelPercentage = null;
}
else if (!int.TryParse(segments.Id, out int idValue) || !int.TryParse(ILocation.GetRightPadded(locationDigits, segments.NormalizedPixelPercentage), out int normalizedPixelPercentageValue))
{
id = null;
faces = null;
normalizedPixelPercentage = null;
}
else
{
id = idValue;
normalizedPixelPercentage = normalizedPixelPercentageValue;
if (segments.Check.Value || segments.NormalizedPixelPercentage.Length != locationDigits)
IfNotAlreadyFileMove(facesFileNameExtension, file, idValue, normalizedPixelPercentageValue, segments.ExtensionLowered);
if (!keyValuePairsAny || !keyValuePairs.ContainsKey(idValue))
faces = null;
else
faces = keyValuePairs[idValue];
}
return new(id, normalizedPixelPercentage, faces);
}
internal static (string?, string?, string?, bool?) GetSegments(int locationDigits, string facesFileNameExtension, string fileName)
{
string[] segments = fileName.Split('.');
@ -52,7 +10,7 @@ internal abstract class Mapping
string? extensionLowered;
bool? needsFacesFileNameExtension;
string? normalizedPixelPercentage;
if (segments.Length < 3 || (segments.Length == 4 && $".{segments[3]}" != facesFileNameExtension))
if (segments.Length < 4 || $".{segments[3]}" != facesFileNameExtension)
{
id = null;
extensionLowered = null;
@ -64,12 +22,42 @@ internal abstract class Mapping
id = segments[0];
extensionLowered = $".{segments[2]}";
needsFacesFileNameExtension = segments.Length == 3;
normalizedPixelPercentage = ILocation.GetRightPadded(locationDigits, segments[1]);
normalizedPixelPercentage = segments[1].Length == locationDigits ? segments[1] : null;
}
return new(id, normalizedPixelPercentage, extensionLowered, needsFacesFileNameExtension);
}
internal static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKey(int locationDigits, string facesFileNameExtension, bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file)
private static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKeysFromSegments(int locationDigits, string facesFileNameExtension, bool idToFacesAny, Dictionary<int, List<Models.Face>> idToFaces, string fileName)
{
int? id;
List<Models.Face>? faces;
int? normalizedPixelPercentage;
(string? Id, string? NormalizedPixelPercentage, string? ExtensionLowered, bool? Check) segments = GetSegments(locationDigits, facesFileNameExtension, fileName);
if (string.IsNullOrEmpty(segments.Id) || string.IsNullOrEmpty(segments.NormalizedPixelPercentage) || string.IsNullOrEmpty(segments.ExtensionLowered) || segments.Check is null)
{
id = null;
faces = null;
normalizedPixelPercentage = null;
}
else if (!int.TryParse(segments.Id, out int idValue) || !int.TryParse(segments.NormalizedPixelPercentage, out int normalizedPixelPercentageValue))
{
id = null;
faces = null;
normalizedPixelPercentage = null;
}
else
{
id = idValue;
normalizedPixelPercentage = segments.NormalizedPixelPercentage.Length == locationDigits ? normalizedPixelPercentageValue : null;
if (!idToFacesAny || !idToFaces.ContainsKey(idValue))
faces = null;
else
faces = idToFaces[idValue];
}
return new(id, normalizedPixelPercentage, faces);
}
internal static (int?, int?, List<Models.Face>?) GetReversedDeterministicHashCodeKey(int locationDigits, string facesFileNameExtension, bool idToFacesAny, Dictionary<int, List<Models.Face>> idToFaces, string file)
{
int? id;
List<Models.Face>? faces;
@ -85,9 +73,8 @@ internal abstract class Mapping
(id, normalizedPixelPercentage, faces) = GetReversedDeterministicHashCodeKeysFromSegments(
locationDigits,
facesFileNameExtension,
keyValuePairsAny,
keyValuePairs,
file,
idToFacesAny,
idToFaces,
fileName);
return new(id, normalizedPixelPercentage, faces);
}