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

@ -55,8 +55,8 @@ public class Face : Properties.IFace
_DateTime = (from l in dateTimes where l.HasValue select l.Value).Min();
}
public Face(Face face, Location location, int locationDigits, int locationFactor, int zCount) :
this(face.DateTime, face.FaceDistance, face.FaceEncoding, face.FaceParts, new(location, locationDigits, locationFactor, zCount), face.LocationIndex, face.Mapping, face.OutputResolution, face.RelativePath)
public Face(Face face, int height, Location location, int locationDigits, int locationFactor, int width, int zCount) :
this(face.DateTime, face.FaceDistance, face.FaceEncoding, face.FaceParts, new(height, location, locationDigits, locationFactor, width, zCount), face.LocationIndex, face.Mapping, face.OutputResolution, face.RelativePath)
{ }
public override string ToString()

View File

@ -6,42 +6,54 @@ namespace View_by_Distance.Shared.Models;
public class Location : Properties.ILocation, IEquatable<Location>
{
protected int _Bottom;
protected double _Confidence;
protected int _Left;
protected readonly int? _NormalizedPixelPercentage;
protected int _Right;
protected int _Top;
public double Confidence => _Confidence;
public int Bottom => _Bottom;
public int Left => _Left;
public int? NormalizedPixelPercentage => _NormalizedPixelPercentage;
public int Right => _Right;
public int Top => _Top;
public int Bottom { init; get; }
public double Confidence { init; get; }
public int Left { init; get; }
public int? NormalizedPixelPercentage { init; get; }
public int Right { init; get; }
public int Top { init; get; }
[JsonConstructor]
public Location(int bottom, double confidence, int left, int? normalizedPixelPercentage, int right, int top)
{
_Confidence = confidence;
_Bottom = bottom;
_Left = left;
_NormalizedPixelPercentage = normalizedPixelPercentage;
_Right = right;
_Top = top;
Confidence = confidence;
Bottom = bottom;
Left = left;
NormalizedPixelPercentage = normalizedPixelPercentage;
Right = right;
Top = top;
Stateless.Methods.Location.Check(bottom, left, normalizedPixelPercentage, right, top, zCount: 1);
}
public Location(double confidence, int height, Location location, int locationDigits, int locationFactor, int width, int zCount) :
this(location.Bottom, confidence, location.Left, Stateless.Methods.Location.GetNormalizedPixelPercentage(location.Bottom, height, location.Left, locationDigits, locationFactor, location.Right, location.Top, width, zCount), location.Right, location.Top) =>
Stateless.Methods.Location.Check(_Bottom, _Left, _NormalizedPixelPercentage, _Right, _Top, zCount);
this(
location.Bottom,
confidence,
location.Left,
Stateless.Methods.Location.GetNormalizedPixelPercentage(location.Bottom, height, location.Left, locationDigits, locationFactor, location.Right, location.Top, width, zCount),
location.Right,
location.Top) =>
Stateless.Methods.Location.Check(Bottom, Left, NormalizedPixelPercentage, Right, Top, zCount);
public Location(int bottom, double confidence, int height, int left, int locationDigits, int locationFactor, int right, int top, int width, int zCount) :
this(bottom, confidence, left, Stateless.Methods.Location.GetNormalizedPixelPercentage(bottom, height, left, locationDigits, locationFactor, right, top, width, zCount), right, top) =>
Stateless.Methods.Location.Check(_Bottom, height, _Left, _NormalizedPixelPercentage, _Right, _Top, width, zCount);
this(
bottom,
confidence,
left,
Stateless.Methods.Location.GetNormalizedPixelPercentage(bottom, height, left, locationDigits, locationFactor, right, top, width, zCount),
right,
top) =>
Stateless.Methods.Location.Check(Bottom, height, Left, NormalizedPixelPercentage, Right, Top, width, zCount);
public Location(Location location, int locationDigits, int locationFactor, int zCount) :
this(location.Bottom, location.Confidence, location.Left, Stateless.Methods.Location.GetNormalizedPixelPercentage(location.Bottom, height: location.Bottom - location.Top, location.Left, locationDigits, locationFactor, location.Right, location.Top, width: location.Right - location.Left, zCount), location.Right, location.Top) =>
Stateless.Methods.Location.Check(_Bottom, _Left, _NormalizedPixelPercentage, _Right, _Top, zCount);
public Location(int height, Location location, int locationDigits, int locationFactor, int width, int zCount) :
this(
location.Bottom,
location.Confidence,
location.Left,
Stateless.Methods.Location.GetNormalizedPixelPercentage(location.Bottom, height, location.Left, locationDigits, locationFactor, location.Right, location.Top, width, zCount),
location.Right,
location.Top) =>
Stateless.Methods.Location.Check(Bottom, Left, NormalizedPixelPercentage, Right, Top, zCount);
public Location(double confidence, int factor, int height, Location location, int locationDigits, int locationFactor, int width, int zCount)
{
@ -52,13 +64,15 @@ public class Location : Properties.ILocation, IEquatable<Location>
int right = Math.Min(location.Right + x, width);
int top = Math.Max(location.Top - y, 0);
int normalizedPixelPercentage = Stateless.Methods.Location.GetNormalizedPixelPercentage(location.Bottom, height, location.Left, locationDigits, locationFactor, location.Right, location.Top, width, zCount);
Stateless.Methods.Location.Check(bottom, left, _NormalizedPixelPercentage, right, top, zCount);
_Confidence = confidence;
_Bottom = bottom;
_Left = left;
_NormalizedPixelPercentage = normalizedPixelPercentage;
_Right = right;
_Top = top;
if (location.NormalizedPixelPercentage is null || normalizedPixelPercentage != location.NormalizedPixelPercentage.Value)
throw new Exception();
Stateless.Methods.Location.Check(bottom, left, NormalizedPixelPercentage, right, top, zCount);
Confidence = confidence;
Bottom = bottom;
Left = left;
NormalizedPixelPercentage = normalizedPixelPercentage;
Right = right;
Top = top;
}
public override bool Equals(object? obj) => Equals(obj as Location);
@ -72,20 +86,20 @@ public class Location : Properties.ILocation, IEquatable<Location>
public override int GetHashCode()
{
int hashCode = -773114317;
hashCode = hashCode * -1521134295 + _Bottom.GetHashCode();
hashCode = hashCode * -1521134295 + _Left.GetHashCode();
hashCode = hashCode * -1521134295 + _Right.GetHashCode();
hashCode = hashCode * -1521134295 + _Top.GetHashCode();
hashCode = hashCode * -1521134295 + Bottom.GetHashCode();
hashCode = hashCode * -1521134295 + Left.GetHashCode();
hashCode = hashCode * -1521134295 + Right.GetHashCode();
hashCode = hashCode * -1521134295 + Top.GetHashCode();
return hashCode;
}
public bool Equals(Location? location)
{
return location is not null
&& _Bottom == location.Bottom
&& _Left == location.Left
&& _Right == location.Right
&& _Top == location.Top;
&& Bottom == location.Bottom
&& Left == location.Left
&& Right == location.Right
&& Top == location.Top;
}
public static bool operator ==(Location location1, Location location2) => EqualityComparer<Location>.Default.Equals(location1, location2);

View File

@ -3,11 +3,11 @@ namespace View_by_Distance.Shared.Models.Properties;
public interface ILocation
{
public int Bottom { get; }
public double Confidence { get; }
public int Left { get; }
public int? NormalizedPixelPercentage { get; }
public int Right { get; }
public int Top { get; }
public int Bottom { init; get; }
public double Confidence { init; get; }
public int Left { init; get; }
public int? NormalizedPixelPercentage { init; get; }
public int Right { init; get; }
public int Top { init; get; }
}

View File

@ -3,7 +3,7 @@
public interface ILocation
{
const int Digits = 6;
const int Factor = 1000000;
const int Digits = 9;
const int Factor = 10000;
}

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);
}