Removed old GetRectangle

RectangleIntersectMinimums
AddUserSecrets
This commit is contained in:
2023-06-19 21:33:04 -07:00
parent 2ff966d6b0
commit 8863fd763f
20 changed files with 328 additions and 498 deletions

View File

@ -108,30 +108,6 @@ internal abstract class Location
return result;
}
internal static (int?, int?) GetXY(int locationDigits, int locationFactor, int width, int height, string normalizedRectangle)
{
int? x;
int? y;
int center = 2;
decimal factor = locationFactor;
int each = (locationDigits - 1) / center;
string segmentA = normalizedRectangle[..each];
string segmentB = normalizedRectangle[each..^1];
if (!int.TryParse(segmentA, out int xNormalized) || !int.TryParse(segmentB, out int yNormalized))
{
x = null;
y = null;
}
else
{
decimal xValue = xNormalized / factor * width;
decimal yValue = yNormalized / factor * height;
x = (int)Math.Round(xValue, 0);
y = (int)Math.Round(yValue, 0);
}
return new(x, y);
}
internal static int GetConfidencePercent(int faceConfidencePercent, float[] rangeFaceConfidence, double confidence)
{
int result = (int)(confidence / rangeFaceConfidence[1] * faceConfidencePercent);
@ -162,7 +138,7 @@ internal abstract class Location
return result;
}
private static Rectangle? GetRectangle(int locationDigits, int height, string normalizedRectangle, int width)
internal static Rectangle? GetRectangle(int height, int locationDigits, string normalizedRectangle, int width)
{
Rectangle? result;
Rectangle? rectangle = GetNormalizedRectangle(locationDigits, normalizedRectangle);
@ -176,42 +152,15 @@ internal abstract class Location
return result;
}
internal static Rectangle? GetRectangle(Rectangle checkRectangle, int height, int locationDigits, int locationFactor, string normalizedRectangle, int width, bool useOldWay)
internal static Rectangle? GetRectangle(int locationDigits, string normalizedRectangle, Models.OutputResolution outputResolution)
{
Rectangle? result;
if (useOldWay)
{
(int? x, int? y) = GetXY(locationDigits, locationFactor, width, height, normalizedRectangle);
if (x is null || y is null)
throw new Exception();
result = new(x.Value - (checkRectangle.Width / 2), y.Value - (checkRectangle.Height / 2), checkRectangle.Width, checkRectangle.Height);
}
else
{
if (normalizedRectangle.Length != locationDigits)
result = null;
else
{
result = GetRectangle(locationDigits, height, normalizedRectangle, width);
if (result is null)
throw new NullReferenceException(nameof(result));
}
}
return result;
}
internal static Rectangle? GetRectangle(int height, int locationDigits, int locationFactor, string normalizedRectangle, int outputResolutionHeight, int outputResolutionWidth, int width)
{
Rectangle? result;
if (normalizedRectangle.Length == locationDigits && normalizedRectangle[0] is '4' or '8')
result = GetRectangle(locationDigits, outputResolutionHeight, normalizedRectangle, outputResolutionWidth);
else
{
(int? x, int? y) = GetXY(locationDigits, locationFactor, outputResolutionWidth, outputResolutionHeight, normalizedRectangle);
if (x is null || y is null)
throw new Exception();
result = new(x.Value - (width / 2), y.Value - (height / 2), width, height);
}
if (normalizedRectangle.Length != locationDigits || normalizedRectangle[0] is not '4' and not '8')
throw new NotSupportedException("Old way has been removed!");
(int width, int height) = OutputResolution.Get(outputResolution);
result = GetRectangle(height, locationDigits, normalizedRectangle, width);
if (result is null)
throw new NullReferenceException(nameof(result));
return result;
}
@ -221,7 +170,7 @@ internal abstract class Location
return result;
}
internal static Rectangle? GetRectangle(Models.OutputResolution outputResolution, DatabaseFile databaseFile, Marker marker)
internal static Rectangle? GetRectangle(DatabaseFile databaseFile, Marker marker, Models.OutputResolution outputResolution)
{
Rectangle? result;
bool matches = Matches(outputResolution, databaseFile);
@ -255,10 +204,10 @@ internal abstract class Location
return result;
}
internal static Models.Location? GetLocation(Models.OutputResolution outputResolution, DatabaseFile databaseFile, Marker marker)
internal static Models.Location? GetLocation(DatabaseFile databaseFile, Marker marker, Models.OutputResolution outputResolution)
{
Models.Location? result;
Rectangle? rectangle = GetRectangle(outputResolution, databaseFile, marker);
Rectangle? rectangle = GetRectangle(databaseFile, marker, outputResolution);
if (rectangle is null)
result = null;
else
@ -300,7 +249,7 @@ internal abstract class Location
foreach (Marker marker in mappingFromPhotoPrism.Markers)
{
any = false;
prismRectangle = GetRectangle(outputResolution, mappingFromPhotoPrism.DatabaseFile, marker);
prismRectangle = GetRectangle(mappingFromPhotoPrism.DatabaseFile, marker, outputResolution);
if (prismRectangle is null)
break;
location = GetLocation(mappingFromPhotoPrism.DatabaseFile, marker, prismRectangle.Value);
@ -341,10 +290,9 @@ internal abstract class Location
return results;
}
internal static List<Models.Face> FilterByIntersect(Models.Face[] faces, int normalizedRectangle)
internal static List<Models.Face> FilterByIntersect(Models.Face[] faces, int normalizedRectangle, float rectangleIntersectMinimum)
{
List<Models.Face> results = new();
bool useOldWay;
double? percent;
Rectangle checkRectangle;
Rectangle? sourceRectangle;
@ -354,20 +302,16 @@ internal abstract class Location
if (face.Location is null || face.OutputResolution is null)
continue;
checkRectangle = new(face.Location.Left, face.Location.Top, face.Location.Right - face.Location.Left, face.Location.Bottom - face.Location.Top);
for (int i = 1; i < 3; i++)
{
useOldWay = i == 1;
sourceRectangle = ILocation.GetRectangle(checkRectangle, Stateless.ILocation.Digits, Stateless.ILocation.Factor, normalizedRectangle, face.OutputResolution, useOldWay);
if (sourceRectangle is null)
continue;
intersectRectangle = Rectangle.Intersect(checkRectangle, sourceRectangle.Value);
if (intersectRectangle.Width == 0 || intersectRectangle.Height == 0)
continue;
percent = (double)intersectRectangle.Width * intersectRectangle.Height / (checkRectangle.Width * checkRectangle.Height);
if (percent < 0.000001)
continue;
results.Add(face);
}
sourceRectangle = GetRectangle(Stateless.ILocation.Digits, normalizedRectangle.ToString(), face.OutputResolution);
if (sourceRectangle is null)
continue;
intersectRectangle = Rectangle.Intersect(checkRectangle, sourceRectangle.Value);
if (intersectRectangle.Width == 0 || intersectRectangle.Height == 0)
continue;
percent = (double)intersectRectangle.Width * intersectRectangle.Height / (checkRectangle.Width * checkRectangle.Height);
if (percent < rectangleIntersectMinimum)
continue;
results.Add(face);
}
return results;
}