LocationContainer

This commit is contained in:
2022-12-28 23:52:42 -07:00
parent 681b2fdf3c
commit 26edd826d5
14 changed files with 317 additions and 220 deletions

View File

@ -0,0 +1,6 @@
using System.Drawing;
namespace View_by_Distance.Shared.Models;
public record LocationContainer<T>(bool FromDistanceContent, string File, int NormalizedRectangle, IReadOnlyList<T> Directories, Rectangle? Rectangle, Location? Location)
{ }

View File

@ -5,6 +5,16 @@ namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface ILocation
{ // ...
Models.Location? TestStatic_GetLocation(int height, Rectangle rectangle, int width) =>
GetLocation(height, rectangle, width);
static Models.Location? GetLocation(int height, Rectangle rectangle, int width) =>
Location.GetLocation(height, rectangle, width);
List<Models.Face> TestStatic_FilterByIntersect(Models.Face[] faces, int normalizedRectangle) =>
FilterByIntersect(faces, normalizedRectangle);
static List<Models.Face> FilterByIntersect(Models.Face[] faces, int normalizedRectangle) =>
Location.FilterByIntersect(faces, normalizedRectangle);
Rectangle? TestStatic_GetRectangle(Models.OutputResolution outputResolution, DatabaseFile databaseFile, Marker marker) =>
GetRectangle(outputResolution, databaseFile, marker);
static Rectangle? GetRectangle(Models.OutputResolution outputResolution, DatabaseFile databaseFile, Marker marker) =>
@ -15,15 +25,20 @@ public interface ILocation
static Models.Location? GetLocation(Models.OutputResolution outputResolution, DatabaseFile databaseFile, Marker marker) =>
Location.GetLocation(outputResolution, databaseFile, marker);
List<Models.Location> TestStatic_GetLocations(List<MappingFromPhotoPrism> mappingFromPhotoPrismCollection, List<Models.Face> faces) =>
GetLocations(mappingFromPhotoPrismCollection, faces);
static List<Models.Location> GetLocations(List<MappingFromPhotoPrism> mappingFromPhotoPrismCollection, List<Models.Face> faces) =>
Location.GetLocations(mappingFromPhotoPrismCollection, faces);
List<Models.Location> TestStatic_GetLocations<T>(List<MappingFromPhotoPrism> mappingFromPhotoPrismCollection, List<Models.Face> faces, List<LocationContainer<T>> containers) =>
GetLocations(mappingFromPhotoPrismCollection, faces, containers);
static List<Models.Location> GetLocations<T>(List<MappingFromPhotoPrism> mappingFromPhotoPrismCollection, List<Models.Face> faces, List<LocationContainer<T>> containers) =>
Location.GetLocations(mappingFromPhotoPrismCollection, faces, containers);
Rectangle TestStatic_GetRectangle(Rectangle checkRectangle, int locationDigits, int locationFactor, int normalizedRectangle, Models.OutputResolution outputResolution, bool useOldWay) =>
Rectangle? TestStatic_GetRectangle(Rectangle checkRectangle, int locationDigits, int locationFactor, int normalizedRectangle, Models.OutputResolution outputResolution, bool useOldWay) =>
GetRectangle(checkRectangle, locationDigits, locationFactor, normalizedRectangle, outputResolution, useOldWay);
static Rectangle GetRectangle(Rectangle checkRectangle, int locationDigits, int locationFactor, int normalizedRectangle, Models.OutputResolution outputResolution, bool useOldWay) =>
Location.GetRectangle(checkRectangle, locationDigits, locationFactor, normalizedRectangle, OutputResolution.Get(outputResolution).Height, OutputResolution.Get(outputResolution).Width, useOldWay);
static Rectangle? GetRectangle(Rectangle checkRectangle, int locationDigits, int locationFactor, int normalizedRectangle, Models.OutputResolution outputResolution, bool useOldWay) =>
Location.GetRectangle(checkRectangle, OutputResolution.Get(outputResolution).Height, locationDigits, locationFactor, normalizedRectangle.ToString(), OutputResolution.Get(outputResolution).Width, useOldWay);
Rectangle? TestStatic_GetRectangle(int height, int locationDigits, int locationFactor, int normalizedRectangle, int outputResolutionHeight, int outputResolutionWidth, int width) =>
GetRectangle(height, locationDigits, locationFactor, normalizedRectangle, outputResolutionHeight, outputResolutionWidth, width);
static Rectangle? GetRectangle(int height, int locationDigits, int locationFactor, int normalizedRectangle, int outputResolutionHeight, int outputResolutionWidth, int width) =>
Location.GetRectangle(height, locationDigits, locationFactor, normalizedRectangle.ToString(), outputResolutionHeight, outputResolutionWidth, width);
string TestStatic_GetLeftPadded(int locationDigits, string value) =>
GetLeftPadded(locationDigits, value);

View File

@ -11,11 +11,6 @@ public interface IProperty
static int GetDeterministicHashCode(byte[] value) =>
Property.GetDeterministicHashCode(value);
List<DateTime> TestStatic_GetMetadataDateTimesByPattern(string dateTimeFormat, Models.FileHolder fileHolder) =>
GetMetadataDateTimesByPattern(dateTimeFormat, fileHolder);
static List<DateTime> GetMetadataDateTimesByPattern(string dateTimeFormat, Models.FileHolder fileHolder) =>
Property.GetMetadataDateTimesByPattern(dateTimeFormat, fileHolder.FullName);
int TestStatic_GetDeterministicHashCode(string value) =>
GetDeterministicHashCode(value);
static int GetDeterministicHashCode(string value) =>

View File

@ -167,26 +167,43 @@ internal abstract class Location
return result;
}
internal static Rectangle GetRectangle(Rectangle checkRectangle, int locationDigits, int locationFactor, int normalizedRectangleValue, int height, int width, bool useOldWay)
internal static Rectangle? GetRectangle(Rectangle checkRectangle, int height, int locationDigits, int locationFactor, string normalizedRectangle, int width, bool useOldWay)
{
Rectangle? result;
string normalizedRectangle = normalizedRectangleValue.ToString();
if (normalizedRectangle.Length != locationDigits)
throw new NotImplementedException();
if (!useOldWay)
{
result = GetRectangle(locationDigits, height, normalizedRectangle, width);
if (result is null)
throw new NullReferenceException(nameof(result));
}
else
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);
}
return result.Value;
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);
}
return result;
}
private static bool Matches(Models.OutputResolution outputResolution, DatabaseFile databaseFile)
@ -217,6 +234,18 @@ internal abstract class Location
return result;
}
internal static Models.Location? GetLocation(int height, Rectangle rectangle, int width)
{
Models.Location? result;
double confidence = 0;
bool verified = Check(rectangle.Bottom, height, rectangle.Left, rectangle.Right, rectangle.Top, width, zCount: 1, throwException: false);
if (!verified)
result = null;
else
result = new(rectangle.Bottom, confidence, rectangle.Left, rectangle.Right, rectangle.Top);
return result;
}
internal static Models.Location? GetLocation(Models.OutputResolution outputResolution, DatabaseFile databaseFile, Marker marker)
{
Models.Location? result;
@ -228,7 +257,7 @@ internal abstract class Location
return result;
}
internal static List<Models.Location> GetLocations(List<MappingFromPhotoPrism> mappingFromPhotoPrismCollection, List<Models.Face> faces)
internal static List<Models.Location> GetLocations<T>(List<MappingFromPhotoPrism> mappingFromPhotoPrismCollection, List<Models.Face> faces, List<LocationContainer<T>> containers)
{
List<Models.Location> results = new();
bool any;
@ -246,6 +275,12 @@ internal abstract class Location
outputResolution ??= face.OutputResolution;
}
int before = results.Count;
foreach (LocationContainer<T> locationContainer in containers)
{
if (locationContainer.Location is null)
continue;
results.Add(locationContainer.Location);
}
foreach (MappingFromPhotoPrism mappingFromPhotoPrism in mappingFromPhotoPrismCollection)
{
if (outputResolution is null)
@ -262,12 +297,27 @@ internal abstract class Location
location = GetLocation(mappingFromPhotoPrism.DatabaseFile, marker, prismRectangle.Value);
if (location is null)
break;
foreach (LocationContainer<T> locationContainer in containers)
{
if (any)
continue;
if (locationContainer.Location is null)
continue;
dlibRectangle = new(locationContainer.Location.Left, locationContainer.Location.Top, locationContainer.Location.Right - locationContainer.Location.Left, locationContainer.Location.Bottom - locationContainer.Location.Top);
intersectRectangle = Rectangle.Intersect(prismRectangle.Value, dlibRectangle);
if (intersectRectangle.Width == 0 && intersectRectangle.Height == 0)
continue;
any = true;
break;
}
foreach (Models.Face face in faces)
{
if (any)
continue;
if (face.Location is null || face.OutputResolution is null)
continue;
dlibRectangle = new(face.Location.Left, face.Location.Top, face.Location.Right - face.Location.Left, face.Location.Bottom - face.Location.Top);
intersectRectangle = Rectangle.Intersect(dlibRectangle, prismRectangle.Value);
intersectRectangle = Rectangle.Intersect(prismRectangle.Value, dlibRectangle);
if (intersectRectangle.Width == 0 && intersectRectangle.Height == 0)
continue;
any = true;
@ -282,4 +332,35 @@ internal abstract class Location
return results;
}
internal static List<Models.Face> FilterByIntersect(Models.Face[] faces, int normalizedRectangle)
{
List<Models.Face> results = new();
bool useOldWay;
double? percent;
Rectangle checkRectangle;
Rectangle? sourceRectangle;
Rectangle intersectRectangle;
foreach (Models.Face face in faces)
{
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);
}
}
return results;
}
}

View File

@ -343,32 +343,6 @@ internal abstract class Property
return result;
}
internal static List<DateTime> GetMetadataDateTimesByPattern(string dateTimeFormat, string sourceDirectoryFile)
{
List<DateTime> results = new();
try
{
DateTime checkDateTime;
DateTime kristy = new(1976, 3, 8);
IReadOnlyList<MetadataExtractor.Directory> directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(sourceDirectoryFile);
foreach (MetadataExtractor.Directory directory in directories)
{
foreach (MetadataExtractor.Tag tag in directory.Tags)
{
if (string.IsNullOrEmpty(tag.Description) || tag.Description.Length != dateTimeFormat.Length)
continue;
if (!DateTime.TryParseExact(tag.Description, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
continue;
if (checkDateTime < kristy)
continue;
results.Add(checkDateTime);
}
}
}
catch (Exception) { }
return results;
}
#pragma warning disable CA1416
internal static (DateTime?, int?, string?) Get(Models.FileHolder fileHolder)