GetLocations update and

NameWithoutExtensionIsIdFormat
This commit is contained in:
Mike Phares 2022-12-27 20:49:11 -07:00
parent b5ff1b202c
commit facafec0e6
8 changed files with 78 additions and 41 deletions

View File

@ -70,8 +70,8 @@ public class DeleteByDistinct
// if (!fileTicksToNames.TryGetValue(fileInfo.LastWriteTime.Ticks, out fileNames)) // if (!fileTicksToNames.TryGetValue(fileInfo.LastWriteTime.Ticks, out fileNames))
// throw new Exception(); // throw new Exception();
// } // }
checkDate = new DateTime(ticks); // checkDate = new DateTime(ticks);
// checkDate = new DateTime(fileInfo.LastWriteTime.Year, fileInfo.LastWriteTime.Month, fileInfo.LastWriteTime.Day); checkDate = new DateTime(fileInfo.LastWriteTime.Year, fileInfo.LastWriteTime.Month, fileInfo.LastWriteTime.Day);
if (!fileTicksToNames.TryGetValue(checkDate.Ticks, out fileNames)) if (!fileTicksToNames.TryGetValue(checkDate.Ticks, out fileNames))
{ {
fileTicksToNames.Add(checkDate.Ticks, new()); fileTicksToNames.Add(checkDate.Ticks, new());

View File

@ -181,7 +181,6 @@ public partial class Form : System.Windows.Forms.Form
FileHolder fileHolder; FileHolder fileHolder;
bool isIgnoreExtension; bool isIgnoreExtension;
string checkFileExtension; string checkFileExtension;
bool skipOneAllAreNumbers;
DateTime? minimumDateTime; DateTime? minimumDateTime;
const string jpg = ".jpg"; const string jpg = ".jpg";
_InnerProgressBar.Step = 1; _InnerProgressBar.Step = 1;
@ -208,10 +207,7 @@ public partial class Form : System.Windows.Forms.Form
continue; continue;
isValidImageFormatExtension = _Configuration.PropertyConfiguration.ValidImageFormatExtensions.Contains(fileHolder.ExtensionLowered); isValidImageFormatExtension = _Configuration.PropertyConfiguration.ValidImageFormatExtensions.Contains(fileHolder.ExtensionLowered);
isIgnoreExtension = isValidImageFormatExtension && _Configuration.IgnoreExtensions.Contains(fileHolder.ExtensionLowered); isIgnoreExtension = isValidImageFormatExtension && _Configuration.IgnoreExtensions.Contains(fileHolder.ExtensionLowered);
if (fileHolder.NameWithoutExtension.Length > 4) if (IProperty.NameWithoutExtensionIsIdFormat(fileHolder))
{
skipOneAllAreNumbers = fileHolder.NameWithoutExtension[1..].All(l => char.IsNumber(l));
if ((skipOneAllAreNumbers && fileHolder.NameWithoutExtension[0] == '-') || (skipOneAllAreNumbers && char.IsNumber(fileHolder.NameWithoutExtension[0])))
{ {
if (fileHolder.ExtensionLowered == jpeg) if (fileHolder.ExtensionLowered == jpeg)
{ {
@ -221,7 +217,6 @@ public partial class Form : System.Windows.Forms.Form
} }
continue; continue;
} }
}
if (!isIgnoreExtension && isValidImageFormatExtension) if (!isIgnoreExtension && isValidImageFormatExtension)
extraLargeBitmapThumbnail = null; extraLargeBitmapThumbnail = null;
else else

View File

@ -212,7 +212,7 @@ public class D_Face
} }
} }
private List<Shared.Models.Face> GetFaces(Shared.Models.Property property, MappingFromItem mappingFromItem, int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation, List<Location> locations) private List<Shared.Models.Face> GetFaces(Shared.Models.Property property, MappingFromItem mappingFromItem, int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation, List<Location>? locations)
{ {
if (_Log is null) if (_Log is null)
throw new NullReferenceException(nameof(_Log)); throw new NullReferenceException(nameof(_Log));
@ -297,7 +297,7 @@ public class D_Face
if (string.IsNullOrEmpty(dResultsFullGroupDirectory)) if (string.IsNullOrEmpty(dResultsFullGroupDirectory))
throw new NullReferenceException(nameof(dResultsFullGroupDirectory)); throw new NullReferenceException(nameof(dResultsFullGroupDirectory));
string json; string json;
List<Location> locations = new(); List<Location>? locations;
string[] changesFrom = new string[] { nameof(A_Property), nameof(B_Metadata), nameof(C_Resize) }; string[] changesFrom = new string[] { nameof(A_Property), nameof(B_Metadata), nameof(C_Resize) };
List<DateTime> dateTimes = (from l in subFileTuples where changesFrom.Contains(l.Item1) select l.Item2).ToList(); List<DateTime> dateTimes = (from l in subFileTuples where changesFrom.Contains(l.Item1) select l.Item2).ToList();
string dCollectionFile = Path.Combine(dResultsFullGroupDirectory, "[]", _Configuration.ResultAllInOne, $"{mappingFromItem.Id}{mappingFromItem.ImageFileHolder.ExtensionLowered}.json"); string dCollectionFile = Path.Combine(dResultsFullGroupDirectory, "[]", _Configuration.ResultAllInOne, $"{mappingFromItem.Id}{mappingFromItem.ImageFileHolder.ExtensionLowered}.json");
@ -334,18 +334,20 @@ public class D_Face
parseExceptions.Add(nameof(D_Face)); parseExceptions.Add(nameof(D_Face));
} }
} }
if (mappingFromPhotoPrismCollection is not null && results is not null) if (mappingFromPhotoPrismCollection is null || results is null)
locations.AddRange(Shared.Models.Stateless.Methods.ILocation.GetLocations(mappingFromPhotoPrismCollection, results)); locations = null;
if (results is null || locations.Any()) else
locations = Shared.Models.Stateless.Methods.ILocation.GetLocations(mappingFromPhotoPrismCollection, results);
if (results is null || (locations is not null && locations.Any()))
{ {
results = GetFaces(property, mappingFromItem, outputResolutionWidth, outputResolutionHeight, outputResolutionOrientation, locations); results = GetFaces(property, mappingFromItem, outputResolutionWidth, outputResolutionHeight, outputResolutionOrientation, locations);
if (!results.Any()) if (!results.Any())
File.Move(mappingFromItem.ResizedFileHolder.FullName, $"{mappingFromItem.ResizedFileHolder.FullName}.err"); File.Move(mappingFromItem.ResizedFileHolder.FullName, $"{mappingFromItem.ResizedFileHolder.FullName}.err");
else else
{ {
json = JsonSerializer.Serialize(results, _WriteIndentedAndWhenWritingNull);
bool updateDateWhenMatches = dateTimes.Any() && fileInfo.Exists && dateTimes.Max() > fileInfo.LastWriteTime; bool updateDateWhenMatches = dateTimes.Any() && fileInfo.Exists && dateTimes.Max() > fileInfo.LastWriteTime;
DateTime? dateTime = !updateDateWhenMatches ? null : dateTimes.Max(); DateTime? dateTime = !updateDateWhenMatches ? null : dateTimes.Max();
json = JsonSerializer.Serialize(results, _WriteIndentedAndWhenWritingNull);
if (Shared.Models.Stateless.Methods.IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches, compareBeforeWrite: true, updateToWhenMatches: dateTime)) if (Shared.Models.Stateless.Methods.IPath.WriteAllText(fileInfo.FullName, json, updateDateWhenMatches, compareBeforeWrite: true, updateToWhenMatches: dateTime))
{ {
if (!_ForceFaceLastWriteTimeToCreationTime) if (!_ForceFaceLastWriteTimeToCreationTime)

View File

@ -198,7 +198,7 @@ public class FaceRecognition : DisposableObject
return results; return results;
} }
public List<(Location, FaceEncoding?, Dictionary<FacePart, FacePoint[]>?)> GetCollection(Image image, List<Location> locations, bool includeFaceEncoding, bool includeFaceParts) public List<(Location, FaceEncoding?, Dictionary<FacePart, FacePoint[]>?)> GetCollection(Image image, List<Location>? locations, bool includeFaceEncoding, bool includeFaceParts)
{ {
List<(Location, FaceEncoding?, Dictionary<FacePart, FacePoint[]>?)> results = new(); List<(Location, FaceEncoding?, Dictionary<FacePart, FacePoint[]>?)> results = new();
if (image is null) if (image is null)
@ -207,6 +207,9 @@ public class FaceRecognition : DisposableObject
ThrowIfDisposed(); ThrowIfDisposed();
if (_PredictorModel == PredictorModel.Custom) if (_PredictorModel == PredictorModel.Custom)
throw new NotSupportedException("FaceRecognition.PredictorModel.Custom is not supported."); throw new NotSupportedException("FaceRecognition.PredictorModel.Custom is not supported.");
if (locations is null)
locations = GetLocations(image);
else if (!locations.Any())
locations.AddRange(GetLocations(image)); locations.AddRange(GetLocations(image));
List<FullObjectDetection> fullObjectDetections = GetFullObjectDetections(image, locations); List<FullObjectDetection> fullObjectDetections = GetFullObjectDetections(image, locations);
if (fullObjectDetections.Count != locations.Count) if (fullObjectDetections.Count != locations.Count)

View File

@ -302,6 +302,9 @@ public partial class DlibDotNet
subFileTuples.Add(new Tuple<string, DateTime>(nameof(A_Property), item.SourceDirectoryFileHolder.LastWriteTime.Value)); subFileTuples.Add(new Tuple<string, DateTime>(nameof(A_Property), item.SourceDirectoryFileHolder.LastWriteTime.Value));
else else
subFileTuples.Add(new Tuple<string, DateTime>(nameof(A_Property), new FileInfo(item.SourceDirectoryFileHolder.FullName).LastWriteTime)); subFileTuples.Add(new Tuple<string, DateTime>(nameof(A_Property), new FileInfo(item.SourceDirectoryFileHolder.FullName).LastWriteTime));
bool nameWithoutExtensionIsIdFormat = Shared.Models.Stateless.Methods.IProperty.NameWithoutExtensionIsIdFormat(item.ImageFileHolder);
if (nameWithoutExtensionIsIdFormat && item.ImageFileHolder.NameWithoutExtension != item.Property.Id.ToString())
_Log.Information($"Name without extension is Id format but doesn't match id <{item.ImageFileHolder.FullName}>");
} }
else else
{ {
@ -1100,7 +1103,7 @@ public partial class DlibDotNet
MapLogic? mapLogic = _Configuration.DistanceMoveUnableToMatch ? null : new(_AppSettings.MaxDegreeOfParallelism, _Configuration.PropertyConfiguration, _MapConfiguration, ticks, _PersonContainers, a2PeopleSingletonDirectory, eDistanceContentDirectory, mapLogicSupport); MapLogic? mapLogic = _Configuration.DistanceMoveUnableToMatch ? null : new(_AppSettings.MaxDegreeOfParallelism, _Configuration.PropertyConfiguration, _MapConfiguration, ticks, _PersonContainers, a2PeopleSingletonDirectory, eDistanceContentDirectory, mapLogicSupport);
if (Directory.Exists(fPhotoPrismSingletonDirectory)) if (Directory.Exists(fPhotoPrismSingletonDirectory))
{ {
Dictionary<string, List<MappingFromPhotoPrism>> fileNameToCollection = F_PhotoPrism.GetFileNameToCollection(fPhotoPrismContentDirectory); Dictionary<string, List<MappingFromPhotoPrism>> fileNameToCollection = F_PhotoPrism.GetFileNameToCollection(fPhotoPrismSingletonDirectory);
foreach (KeyValuePair<string, List<MappingFromPhotoPrism>> keyValuePair in fileNameToCollection) foreach (KeyValuePair<string, List<MappingFromPhotoPrism>> keyValuePair in fileNameToCollection)
_FileNameToCollection.Add(keyValuePair.Key, keyValuePair.Value); _FileNameToCollection.Add(keyValuePair.Key, keyValuePair.Value);
} }

View File

@ -61,6 +61,11 @@ public interface IProperty
static DateTime? GetDateTimeFromName(Models.FileHolder fileHolder) => static DateTime? GetDateTimeFromName(Models.FileHolder fileHolder) =>
Property.GetDateTimeFromName(fileHolder); Property.GetDateTimeFromName(fileHolder);
bool TestStatic_NameWithoutExtensionIsIdFormat(Models.FileHolder fileHolder) =>
NameWithoutExtensionIsIdFormat(fileHolder);
static bool NameWithoutExtensionIsIdFormat(Models.FileHolder fileHolder) =>
Property.NameWithoutExtensionIsIdFormat(fileHolder);
List<DateTime> TestStatic_GetDateTimes(Models.Property property) => List<DateTime> TestStatic_GetDateTimes(Models.Property property) =>
GetDateTimes(property); GetDateTimes(property);
static List<DateTime> GetDateTimes(Models.Property property) => static List<DateTime> GetDateTimes(Models.Property property) =>

View File

@ -206,10 +206,10 @@ internal abstract class Location
return result; return result;
} }
internal static Models.Location? GetLocation(Marker marker, Rectangle rectangle) private static Models.Location? GetLocation(DatabaseFile databaseFile, Marker marker, Rectangle rectangle)
{ {
Models.Location? result; Models.Location? result;
bool verified = Check(rectangle.Bottom, rectangle.Height, rectangle.Left, rectangle.Right, rectangle.Top, rectangle.Width, zCount: 1, throwException: false); bool verified = Check(rectangle.Bottom, databaseFile.FileHeight, rectangle.Left, rectangle.Right, rectangle.Top, databaseFile.FileWidth, zCount: 1, throwException: false);
if (!verified) if (!verified)
result = null; result = null;
else else
@ -224,45 +224,61 @@ internal abstract class Location
if (rectangle is null) if (rectangle is null)
result = null; result = null;
else else
result = GetLocation(marker, rectangle.Value); result = GetLocation(databaseFile, marker, rectangle.Value);
return result; return result;
} }
internal static List<Models.Location> GetLocations(List<MappingFromPhotoPrism> mappingFromPhotoPrismCollection, List<Models.Face> faces) internal static List<Models.Location> GetLocations(List<MappingFromPhotoPrism> mappingFromPhotoPrismCollection, List<Models.Face> faces)
{ {
List<Models.Location> results = new(); List<Models.Location> results = new();
double percent; bool any;
bool matches;
Rectangle dlibRectangle; Rectangle dlibRectangle;
Rectangle? prismRectangle; Rectangle? prismRectangle;
Models.Location? location; Models.Location? location;
Rectangle intersectRectangle; Rectangle intersectRectangle;
Models.OutputResolution? outputResolution = null;
foreach (Models.Face face in faces) foreach (Models.Face face in faces)
{ {
if (face.Location is null || face.OutputResolution is null) if (face.Location is null || face.OutputResolution is null)
continue; continue;
results.Add(face.Location);
outputResolution ??= face.OutputResolution;
}
int before = results.Count;
foreach (MappingFromPhotoPrism mappingFromPhotoPrism in mappingFromPhotoPrismCollection) foreach (MappingFromPhotoPrism mappingFromPhotoPrism in mappingFromPhotoPrismCollection)
{ {
dlibRectangle = new(face.Location.Left, face.Location.Top, face.Location.Right - face.Location.Left, face.Location.Bottom - face.Location.Top); if (outputResolution is null)
break;
matches = Matches(outputResolution, mappingFromPhotoPrism.DatabaseFile);
if (!matches)
break;
foreach (Marker marker in mappingFromPhotoPrism.Markers) foreach (Marker marker in mappingFromPhotoPrism.Markers)
{ {
prismRectangle = GetRectangle(face.OutputResolution, mappingFromPhotoPrism.DatabaseFile, marker); any = false;
prismRectangle = GetRectangle(outputResolution, mappingFromPhotoPrism.DatabaseFile, marker);
if (prismRectangle is null) if (prismRectangle is null)
{ break;
location = new(1, 0, 0, 1, 0); location = GetLocation(mappingFromPhotoPrism.DatabaseFile, marker, prismRectangle.Value);
results.Add(location);
continue;
}
location = GetLocation(marker, prismRectangle.Value);
if (location is null) if (location is null)
break;
foreach (Models.Face face in faces)
{
if (face.Location is null || face.OutputResolution is null)
continue; 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(dlibRectangle, prismRectangle.Value);
percent = (double)intersectRectangle.Width * intersectRectangle.Height / (dlibRectangle.Width * dlibRectangle.Height); if (intersectRectangle.Width == 0 && intersectRectangle.Height == 0)
if (percent > 0.000001)
continue; continue;
any = true;
break;
}
if (!any)
results.Add(location); results.Add(location);
} }
} }
} if (before == results.Count)
results.Clear();
return results; return results;
} }

View File

@ -330,6 +330,19 @@ internal abstract class Property
return result; return result;
} }
internal static bool NameWithoutExtensionIsIdFormat(Models.FileHolder fileHolder)
{
bool result;
if (fileHolder.NameWithoutExtension.Length < 5)
result = false;
else
{
bool skipOneAllAreNumbers = fileHolder.NameWithoutExtension[1..].All(l => char.IsNumber(l));
result = (skipOneAllAreNumbers && fileHolder.NameWithoutExtension[0] == '-') || (skipOneAllAreNumbers && char.IsNumber(fileHolder.NameWithoutExtension[0]));
}
return result;
}
internal static List<DateTime> GetMetadataDateTimesByPattern(string dateTimeFormat, string sourceDirectoryFile) internal static List<DateTime> GetMetadataDateTimesByPattern(string dateTimeFormat, string sourceDirectoryFile)
{ {
List<DateTime> results = new(); List<DateTime> results = new();