CopyFacesAndSaveFaceLandmarkImage
This commit is contained in:
@ -4,6 +4,7 @@ using System.Drawing.Imaging;
|
||||
using System.Text.Json;
|
||||
using View_by_Distance.Face.Models;
|
||||
using View_by_Distance.Metadata.Models;
|
||||
using View_by_Distance.Property.Models;
|
||||
using View_by_Distance.Property.Models.Stateless;
|
||||
using View_by_Distance.Resize.Models;
|
||||
using View_by_Distance.Shared.Models;
|
||||
@ -45,7 +46,7 @@ public class D2_FaceParts
|
||||
return result;
|
||||
}
|
||||
|
||||
public void SetAngleBracketCollection(Property.Models.Configuration configuration, string d2ResultsFullGroupDirectory, string sourceDirectory)
|
||||
public void SetAngleBracketCollection(Configuration configuration, string d2ResultsFullGroupDirectory, string sourceDirectory)
|
||||
{
|
||||
_AngleBracketCollection.Clear();
|
||||
_AngleBracketCollection.AddRange(IResult.GetDirectoryInfoCollection(configuration,
|
||||
@ -57,7 +58,7 @@ public class D2_FaceParts
|
||||
converted: true));
|
||||
}
|
||||
|
||||
public string GetFacePartsDirectory(Property.Models.Configuration configuration, string dResultsFullGroupDirectory, Item item, bool includeNameWithoutExtension)
|
||||
public string GetFacePartsDirectory(Configuration configuration, string dResultsFullGroupDirectory, Item item, bool includeNameWithoutExtension)
|
||||
{
|
||||
string result;
|
||||
bool angleBracketCollectionAny = _AngleBracketCollection.Any();
|
||||
@ -67,10 +68,14 @@ public class D2_FaceParts
|
||||
throw new NullReferenceException(nameof(item.ImageFileHolder.DirectoryName));
|
||||
SetAngleBracketCollection(configuration, dResultsFullGroupDirectory, item.ImageFileHolder.DirectoryName);
|
||||
}
|
||||
if (!includeNameWithoutExtension)
|
||||
result = _AngleBracketCollection[0].Replace("<>", "[()]");
|
||||
else
|
||||
if (includeNameWithoutExtension)
|
||||
result = Path.Combine(_AngleBracketCollection[0].Replace("<>", "()"), item.ImageFileHolder.NameWithoutExtension);
|
||||
else
|
||||
{
|
||||
result = _AngleBracketCollection[0].Replace("<>", "[()]");
|
||||
if (!Directory.Exists(result))
|
||||
_ = Directory.CreateDirectory(result);
|
||||
}
|
||||
if (!angleBracketCollectionAny)
|
||||
_AngleBracketCollection.Clear();
|
||||
return result;
|
||||
@ -105,7 +110,7 @@ public class D2_FaceParts
|
||||
#elif OSX
|
||||
throw new Exception("Built on macOS!");
|
||||
#elif Windows
|
||||
// Make a Matrix to represent rotation
|
||||
// Make save Matrix to represent rotation
|
||||
// by this angle.
|
||||
Matrix rotate_at_origin = new();
|
||||
rotate_at_origin.Rotate(angle);
|
||||
@ -123,7 +128,7 @@ public class D2_FaceParts
|
||||
float xMinimum, xMaximum, yMinimum, yMaximum;
|
||||
GetPointBounds(points, out xMinimum, out xMaximum, out yMinimum, out yMaximum);
|
||||
|
||||
// Make a bitmap to hold the rotated result.
|
||||
// Make save bitmap to hold the rotated result.
|
||||
int wid = (int)Math.Round(xMaximum - xMinimum);
|
||||
int hgt = (int)Math.Round(yMaximum - yMinimum);
|
||||
result = new Bitmap(wid, hgt);
|
||||
@ -177,7 +182,7 @@ public class D2_FaceParts
|
||||
Bitmap rotated;
|
||||
foreach ((Shared.Models.Face face, string fileName, string rotatedFileName) in collection)
|
||||
{
|
||||
if (face.FaceEncoding is null || face.Location is null)
|
||||
if (face.FaceEncoding is null)
|
||||
continue;
|
||||
try
|
||||
{
|
||||
@ -197,19 +202,13 @@ public class D2_FaceParts
|
||||
foreach ((FacePart facePart, FacePoint[] facePoints) in face.FaceParts)
|
||||
{
|
||||
foreach (FacePoint facePoint in facePoints)
|
||||
{
|
||||
if (face.Location is null)
|
||||
continue;
|
||||
graphic.DrawEllipse(Pens.GreenYellow, facePoint.X - pointSize, facePoint.Y - pointSize, pointSize * 2, pointSize * 2);
|
||||
}
|
||||
if (facePart == FacePart.Chin)
|
||||
continue;
|
||||
if (facePoints.Length < 3)
|
||||
continue;
|
||||
x = (int)(from l in facePoints select l.X).Average();
|
||||
y = (int)(from l in facePoints select l.Y).Average();
|
||||
if (face.Location is null)
|
||||
continue;
|
||||
graphic.DrawEllipse(Pens.Purple, x - pointSize, y - pointSize, pointSize * 2, pointSize * 2);
|
||||
}
|
||||
}
|
||||
@ -233,53 +232,12 @@ public class D2_FaceParts
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveFaceParts(int pointSize, IFileHolder resizedFileHolder, List<(Shared.Models.Face, string, string)> collection)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
Pen pen;
|
||||
string? firstFileName = null;
|
||||
using Image image = Image.FromFile(resizedFileHolder.FullName);
|
||||
using Graphics graphic = Graphics.FromImage(image);
|
||||
foreach ((Shared.Models.Face face, string fileName, string _) in collection)
|
||||
{
|
||||
firstFileName ??= fileName;
|
||||
try
|
||||
{
|
||||
if (face.FaceParts is null || !face.FaceParts.Any())
|
||||
continue;
|
||||
foreach ((FacePart facePart, FacePoint[] facePoints) in face.FaceParts)
|
||||
{
|
||||
foreach (FacePoint facePoint in facePoints)
|
||||
{
|
||||
if (face.Location is null)
|
||||
continue;
|
||||
pen = face.Mapping?.MappingFromPerson is null ? Pens.Red : Pens.GreenYellow;
|
||||
graphic.DrawEllipse(pen, facePoint.X - pointSize, facePoint.Y - pointSize, pointSize * 2, pointSize * 2);
|
||||
}
|
||||
if (facePart == FacePart.Chin)
|
||||
continue;
|
||||
if (facePoints.Length < 3)
|
||||
continue;
|
||||
x = (int)(from l in facePoints select l.X).Average();
|
||||
y = (int)(from l in facePoints select l.Y).Average();
|
||||
if (face.Location is null)
|
||||
continue;
|
||||
graphic.DrawEllipse(Pens.Purple, x - pointSize, y - pointSize, pointSize * 2, pointSize * 2);
|
||||
}
|
||||
}
|
||||
catch (Exception) { }
|
||||
}
|
||||
if (!string.IsNullOrEmpty(firstFileName))
|
||||
image.Save(firstFileName, _ImageCodecInfo, _EncoderParameters);
|
||||
}
|
||||
|
||||
#pragma warning restore CA1416
|
||||
|
||||
public void SaveFaceLandmarkImages(Property.Models.Configuration configuration, string facePartsDirectory, string facePartsCollectionDirectory, List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, MappingFromItem mappingFromItem, List<Shared.Models.Face> faces, bool saveRotated)
|
||||
public void SaveFaceLandmarkImages(Configuration configuration, string facePartsDirectory, List<Tuple<string, DateTime>> subFileTuples, List<string> parseExceptions, MappingFromItem mappingFromItem, List<Shared.Models.Face> faces, bool saveRotated)
|
||||
{
|
||||
bool check;
|
||||
FileInfo fileInfo;
|
||||
bool check = false;
|
||||
const int pointSize = 2;
|
||||
FileInfo rotatedFileInfo;
|
||||
DateTime? dateTime = null;
|
||||
@ -287,41 +245,10 @@ public class D2_FaceParts
|
||||
string deterministicHashCodeKey;
|
||||
bool updateDateWhenMatches = false;
|
||||
List<(Shared.Models.Face, string, string)> collection = new();
|
||||
string[] changesFrom = new string[] { nameof(Property.Models.A_Property), nameof(B_Metadata), nameof(C_Resize), nameof(D_Face) };
|
||||
string[] changesFrom = new string[] { nameof(A_Property), nameof(B_Metadata), nameof(C_Resize), nameof(D_Face) };
|
||||
List<DateTime> dateTimes = (from l in subFileTuples where changesFrom.Contains(l.Item1) select l.Item2).ToList();
|
||||
if (!Directory.Exists(facePartsDirectory))
|
||||
_ = Directory.CreateDirectory(facePartsDirectory);
|
||||
if (!Directory.Exists(facePartsCollectionDirectory))
|
||||
_ = Directory.CreateDirectory(facePartsCollectionDirectory);
|
||||
fileInfo = new FileInfo(Path.Combine(facePartsCollectionDirectory, $"{mappingFromItem.ImageFileHolder.Name}{_FileNameExtension}"));
|
||||
if (_OverrideForFaceLandmarkImages)
|
||||
check = true;
|
||||
else if (!fileInfo.Exists)
|
||||
check = true;
|
||||
else if (_CheckDFaceAndUpWriteDates && dateTimes.Any() && dateTimes.Max() > fileInfo.LastWriteTime)
|
||||
check = true;
|
||||
else
|
||||
check = false;
|
||||
if (check && !updateDateWhenMatches)
|
||||
{
|
||||
updateDateWhenMatches = dateTimes.Any() && fileInfo.Exists && dateTimes.Max() > fileInfo.LastWriteTime;
|
||||
dateTime = !updateDateWhenMatches ? null : dateTimes.Max();
|
||||
}
|
||||
if (check)
|
||||
{
|
||||
foreach (Shared.Models.Face face in faces)
|
||||
{
|
||||
if (face.FaceEncoding is null || face.Location is null || face.OutputResolution is null)
|
||||
continue;
|
||||
if (face.FaceParts is null || !face.FaceParts.Any())
|
||||
continue;
|
||||
collection.Add(new(face, fileInfo.FullName, string.Empty));
|
||||
}
|
||||
}
|
||||
if (check && collection.Any())
|
||||
SaveFaceParts(pointSize, mappingFromItem.ResizedFileHolder, collection);
|
||||
check = false;
|
||||
collection.Clear();
|
||||
foreach (Shared.Models.Face face in faces)
|
||||
{
|
||||
if (face.FaceEncoding is null || face.Location is null || face.OutputResolution is null)
|
||||
@ -355,4 +282,77 @@ public class D2_FaceParts
|
||||
SaveFaceParts(pointSize, mappingFromItem.ResizedFileHolder, saveRotated, collection);
|
||||
}
|
||||
|
||||
#pragma warning disable CA1416
|
||||
|
||||
private void SaveFaceLandmarkImage(MappingFromItem mappingFromItem, List<(Shared.Models.Face, FileInfo?, string, bool)> faceCollection, string fileName)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
Pen pen;
|
||||
const int pointSize = 2;
|
||||
using Image image = Image.FromFile(mappingFromItem.ResizedFileHolder.FullName);
|
||||
using Graphics graphic = Graphics.FromImage(image);
|
||||
foreach ((Shared.Models.Face face, FileInfo? fileInfo, string _, bool _) in faceCollection)
|
||||
{
|
||||
if (face.FaceEncoding is null || face.Location is null || face.OutputResolution is null || face.FaceParts is null || !face.FaceParts.Any())
|
||||
continue;
|
||||
try
|
||||
{
|
||||
foreach ((FacePart facePart, FacePoint[] facePoints) in face.FaceParts)
|
||||
{
|
||||
foreach (FacePoint facePoint in facePoints)
|
||||
{
|
||||
pen = face.Mapping?.MappingFromPerson is null ? Pens.Red : Pens.GreenYellow;
|
||||
graphic.DrawEllipse(pen, facePoint.X - pointSize, facePoint.Y - pointSize, pointSize * 2, pointSize * 2);
|
||||
}
|
||||
if (facePart == FacePart.Chin)
|
||||
continue;
|
||||
if (facePoints.Length < 3)
|
||||
continue;
|
||||
x = (int)(from l in facePoints select l.X).Average();
|
||||
y = (int)(from l in facePoints select l.Y).Average();
|
||||
graphic.DrawEllipse(Pens.Purple, x - pointSize, y - pointSize, pointSize * 2, pointSize * 2);
|
||||
}
|
||||
}
|
||||
catch (Exception) { }
|
||||
}
|
||||
image.Save(fileName, _ImageCodecInfo, _EncoderParameters);
|
||||
}
|
||||
|
||||
#pragma warning restore CA1416
|
||||
|
||||
public void CopyFacesAndSaveFaceLandmarkImage(string facePartsCollectionDirectory, MappingFromItem mappingFromItem, List<(Shared.Models.Face Face, FileInfo?, string, bool)> faceCollection)
|
||||
{
|
||||
string checkFile;
|
||||
bool hasNotMapped = false;
|
||||
string fileName = Path.Combine(facePartsCollectionDirectory, $"{mappingFromItem.ImageFileHolder.Name}{_FileNameExtension}");
|
||||
bool save = faceCollection.Any(l => l.Face.FaceEncoding is not null && l.Face.Location is not null && l.Face.OutputResolution is not null && l.Face.FaceParts is not null && l.Face.FaceParts.Any());
|
||||
FileInfo newFileInfo = new(fileName);
|
||||
foreach ((Shared.Models.Face face, FileInfo? fileInfo, string _, bool _) in faceCollection)
|
||||
{
|
||||
if (face.FaceEncoding is null || face.Location is null || face.OutputResolution is null)
|
||||
continue;
|
||||
if (fileInfo is not null)
|
||||
{
|
||||
checkFile = Path.Combine(facePartsCollectionDirectory, fileInfo.Name);
|
||||
if (face.Mapping?.MappingFromPerson is not null)
|
||||
{
|
||||
if (File.Exists(checkFile))
|
||||
File.Delete(checkFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!hasNotMapped)
|
||||
hasNotMapped = true;
|
||||
if (!File.Exists(checkFile))
|
||||
File.Copy(fileInfo.FullName, checkFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (save && !newFileInfo.Exists)
|
||||
SaveFaceLandmarkImage(mappingFromItem, faceCollection, fileName);
|
||||
if (!hasNotMapped && !newFileInfo.Attributes.HasFlag(FileAttributes.Hidden) && (newFileInfo.Exists || save))
|
||||
File.SetAttributes(fileName, FileAttributes.Hidden);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user