mirror of
https://github.com/n00mkrad/flowframes.git
synced 2025-12-16 16:37:48 +01:00
Merge pull request #181 from Hacktank/main
Massively improved the efficiency of Magick based frame de-deuplicati…
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using Flowframes.IO;
|
||||
using ImageMagick;
|
||||
using Flowframes.Os;
|
||||
@@ -37,23 +38,9 @@ namespace Flowframes.Magick
|
||||
await RemoveDupeFrames(path, currentThreshold, "*", testRun, false, (currentMode == Mode.Auto));
|
||||
}
|
||||
|
||||
public static Dictionary<string, MagickImage> imageCache = new Dictionary<string, MagickImage>();
|
||||
static MagickImage GetImage(string path)
|
||||
{
|
||||
bool allowCaching = true;
|
||||
|
||||
if (!allowCaching)
|
||||
return new MagickImage(path);
|
||||
|
||||
if (!imageCache.ContainsKey(path))
|
||||
imageCache.Add(path, new MagickImage(path));
|
||||
|
||||
return imageCache[path];
|
||||
}
|
||||
|
||||
public static void ClearCache ()
|
||||
{
|
||||
imageCache.Clear();
|
||||
}
|
||||
|
||||
public static async Task RemoveDupeFrames(string path, float threshold, string ext, bool testRun = false, bool debugLog = false, bool skipIfNoDupes = false)
|
||||
@@ -65,78 +52,138 @@ namespace Flowframes.Magick
|
||||
FileInfo[] framePaths = IoUtils.GetFileInfosSorted(path, false, "*." + ext);
|
||||
List<string> framesToDelete = new List<string>();
|
||||
|
||||
int bufferSize = await GetBufferSize();
|
||||
|
||||
int currentOutFrame = 1;
|
||||
int currentDupeCount = 0;
|
||||
|
||||
int statsFramesKept = 0;
|
||||
int statsFramesKept = framePaths.Length > 0 ? 1 : 0; // always keep at least one frame
|
||||
int statsFramesDeleted = 0;
|
||||
|
||||
bool hasReachedEnd = false;
|
||||
Mutex mtx_framesToDelete = new Mutex();
|
||||
Mutex mtx_debugLog = new Mutex();
|
||||
Task[] workTasks = new Task[Environment.ProcessorCount];
|
||||
|
||||
string fileContent = "";
|
||||
bool threadAbort = false;
|
||||
|
||||
for (int i = 0; i < framePaths.Length; i++) // Loop through frames
|
||||
Action<int, int> lamProcessFrames = (indStart, indEnd) =>
|
||||
{
|
||||
if (hasReachedEnd)
|
||||
break;
|
||||
MagickImage img1 = null;
|
||||
MagickImage img2 = null;
|
||||
|
||||
string frame1 = framePaths[i].FullName;
|
||||
|
||||
int compareWithIndex = i + 1;
|
||||
|
||||
while (true) // Loop dupes
|
||||
for (int i = indStart; i < indEnd; i++) // Loop through frames
|
||||
{
|
||||
//compareWithIndex++;
|
||||
if (compareWithIndex >= framePaths.Length)
|
||||
string frame1_name = framePaths[i].FullName;
|
||||
|
||||
// its likely we carried over an already loaded image from a previous iteration
|
||||
if(!(img1 != null && img1.FileName == frame1_name))
|
||||
img1 = GetImage(framePaths[i].FullName);
|
||||
|
||||
if (img1 == null) continue;
|
||||
|
||||
for (int j = i + 1; j < framePaths.Length; j++)
|
||||
{
|
||||
hasReachedEnd = true;
|
||||
break;
|
||||
if (threadAbort || Interpolate.canceled) return;
|
||||
|
||||
//if (j % 3 == 0)
|
||||
//await Task.Delay(1);
|
||||
|
||||
string frame2_name = framePaths[j].FullName;
|
||||
|
||||
if(j>=indEnd)
|
||||
{
|
||||
// if we are already extending outside of this thread's range and j is already flagged, then we need to abort
|
||||
bool isFlaggedForDeletion = false;
|
||||
mtx_framesToDelete.WaitOne();
|
||||
isFlaggedForDeletion = framesToDelete.Contains(frame2_name);
|
||||
mtx_framesToDelete.ReleaseMutex();
|
||||
if (isFlaggedForDeletion)
|
||||
return;
|
||||
}
|
||||
|
||||
if (framesToDelete.Contains(framePaths[compareWithIndex].FullName) || !File.Exists(framePaths[compareWithIndex].FullName))
|
||||
{
|
||||
//Logger.Log($"Frame {compareWithIndex} was already deleted - skipping");
|
||||
compareWithIndex++;
|
||||
}
|
||||
else
|
||||
{
|
||||
string frame2 = framePaths[compareWithIndex].FullName;
|
||||
float diff = GetDifference(frame1, frame2);
|
||||
img2 = GetImage(framePaths[j].FullName);
|
||||
if (img2 == null) continue;
|
||||
|
||||
|
||||
float diff = GetDifference(img1, img2);
|
||||
|
||||
if (diff < threshold) // Is a duped frame.
|
||||
{
|
||||
if (!testRun)
|
||||
{
|
||||
framesToDelete.Add(frame2);
|
||||
if (debugLog) Logger.Log("Deduplication: Deleted " + Path.GetFileName(frame2));
|
||||
mtx_framesToDelete.WaitOne();
|
||||
framesToDelete.Add(frame2_name);
|
||||
mtx_framesToDelete.ReleaseMutex();
|
||||
if (debugLog)
|
||||
{
|
||||
mtx_debugLog.WaitOne();
|
||||
Logger.Log("Deduplication: Deleted " + Path.GetFileName(frame2_name));
|
||||
mtx_debugLog.ReleaseMutex();
|
||||
}
|
||||
}
|
||||
|
||||
statsFramesDeleted++;
|
||||
currentDupeCount++;
|
||||
System.Threading.Interlocked.Increment(ref statsFramesDeleted);
|
||||
|
||||
if (j+1 == framePaths.Length)
|
||||
return;
|
||||
|
||||
continue; // test next frame
|
||||
}
|
||||
else
|
||||
{
|
||||
fileContent += $"{Path.GetFileNameWithoutExtension(framePaths[i].Name)}:{currentDupeCount}\n";
|
||||
statsFramesKept++;
|
||||
currentOutFrame++;
|
||||
currentDupeCount = 0;
|
||||
|
||||
|
||||
System.Threading.Interlocked.Increment(ref statsFramesKept);
|
||||
|
||||
// this frame is different, stop testing agaisnt 'i'
|
||||
// all the frames between i and j are dupes, we can skip them
|
||||
i = j - 1;
|
||||
// keep the currently loaded in img for the next iteration
|
||||
img1 = img2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Action lamUpdateInfoBox = () =>
|
||||
{
|
||||
int framesProcessed = statsFramesKept + statsFramesDeleted;
|
||||
Logger.Log($"Deduplication: Running de-duplication ({framesProcessed}/{framePaths.Length}), deleted {statsFramesDeleted} ({(((float)statsFramesDeleted / framePaths.Length) * 100f).ToString("0")}%) duplicate frames so far...", false, true);
|
||||
Program.mainForm.SetProgress((int)Math.Round(((float)framesProcessed / framePaths.Length) * 100f));
|
||||
};
|
||||
|
||||
// start the worker threads
|
||||
for (int i = 0; i < workTasks.Length; i++)
|
||||
{
|
||||
int chunkSize = framePaths.Length / workTasks.Length;
|
||||
int indStart = chunkSize * i;
|
||||
int indEnd = indStart + chunkSize;
|
||||
if (i + 1 == workTasks.Length) indEnd = framePaths.Length;
|
||||
|
||||
workTasks[i] = Task.Run(() => lamProcessFrames(indStart, indEnd));
|
||||
}
|
||||
|
||||
if (sw.ElapsedMilliseconds >= 500 || (i + 1) == framePaths.Length) // Print every 0.5s (or when done)
|
||||
// wait for all the worker threads to finish and update the info box
|
||||
while (!Interpolate.canceled)
|
||||
{
|
||||
await Task.Delay(5);
|
||||
|
||||
|
||||
bool anyThreadStillWorking = false;
|
||||
// wait for the threads to finish
|
||||
for (int i = 0; i < workTasks.Length; i++)
|
||||
{
|
||||
if (!workTasks[i].IsCompleted) anyThreadStillWorking = true;
|
||||
}
|
||||
|
||||
if (sw.ElapsedMilliseconds >= 250 || !anyThreadStillWorking) // Print every 0.25s (or when done)
|
||||
{
|
||||
sw.Restart();
|
||||
Logger.Log($"Deduplication: Running de-duplication ({i}/{framePaths.Length}), deleted {statsFramesDeleted} ({(((float)statsFramesDeleted / framePaths.Length) * 100f).ToString("0")}%) duplicate frames so far...", false, true);
|
||||
Program.mainForm.SetProgress((int)Math.Round(((float)i / framePaths.Length) * 100f));
|
||||
|
||||
if (imageCache.Count > bufferSize || (imageCache.Count > 50 && OsUtils.GetFreeRamMb() < 3500))
|
||||
ClearCache();
|
||||
lamUpdateInfoBox();
|
||||
}
|
||||
|
||||
if (!anyThreadStillWorking) break;
|
||||
}
|
||||
|
||||
threadAbort = true;
|
||||
for (int i = 0; i < workTasks.Length; i++)
|
||||
await workTasks[i];
|
||||
|
||||
lamUpdateInfoBox();
|
||||
|
||||
// int oldIndex = -1; // TODO: Compare with 1st to fix loops?
|
||||
// if (i >= framePaths.Length) // If this is the last frame, compare with 1st to avoid OutOfRange error
|
||||
// {
|
||||
@@ -144,12 +191,6 @@ namespace Flowframes.Magick
|
||||
// i = 0;
|
||||
// }
|
||||
|
||||
if (i % 3 == 0)
|
||||
await Task.Delay(1);
|
||||
|
||||
if (Interpolate.canceled) return;
|
||||
}
|
||||
|
||||
foreach (string frame in framesToDelete)
|
||||
IoUtils.TryDeleteIfExists(frame);
|
||||
|
||||
@@ -167,31 +208,16 @@ namespace Flowframes.Magick
|
||||
if (statsFramesKept <= 0)
|
||||
Interpolate.Cancel("No frames were left after de-duplication!\n\nTry decreasing the de-duplication threshold.");
|
||||
}
|
||||
|
||||
static float GetDifference (string img1Path, string img2Path)
|
||||
static float GetDifference(MagickImage img1, MagickImage img2)
|
||||
{
|
||||
MagickImage img2 = GetImage(img2Path);
|
||||
MagickImage img1 = GetImage(img1Path);
|
||||
|
||||
double err = img1.Compare(img2, ErrorMetric.Fuzz);
|
||||
float errPercent = (float)err * 100f;
|
||||
return errPercent;
|
||||
}
|
||||
|
||||
static async Task<int> GetBufferSize ()
|
||||
static float GetDifference(string img1Path, string img2Path)
|
||||
{
|
||||
Size res = Interpolate.currentSettings.ScaledResolution;
|
||||
long pixels = res.Width * res.Height; // 4K = 8294400, 1440p = 3686400, 1080p = 2073600, 720p = 921600, 540p = 518400, 360p = 230400
|
||||
int bufferSize = 100;
|
||||
if (pixels < 518400) bufferSize = 1800;
|
||||
if (pixels >= 518400) bufferSize = 1400;
|
||||
if (pixels >= 921600) bufferSize = 800;
|
||||
if (pixels >= 2073600) bufferSize = 400;
|
||||
if (pixels >= 3686400) bufferSize = 200;
|
||||
if (pixels >= 8294400) bufferSize = 100;
|
||||
if (pixels == 0) bufferSize = 100;
|
||||
Logger.Log($"Using magick dedupe buffer size {bufferSize} for frame resolution {res.Width}x{res.Height}", true);
|
||||
return bufferSize;
|
||||
return GetDifference(GetImage(img1Path), GetImage(img2Path));
|
||||
}
|
||||
|
||||
public static async Task CreateDupesFile(string framesPath, int lastFrameNum, string ext)
|
||||
@@ -205,30 +231,25 @@ namespace Flowframes.Magick
|
||||
|
||||
Dictionary<string, List<string>> frames = new Dictionary<string, List<string>>();
|
||||
|
||||
|
||||
for (int i = 0; i < frameFiles.Length; i++)
|
||||
{
|
||||
bool isLastItem = (i + 1) == frameFiles.Length;
|
||||
|
||||
int frameNum1 = frameFiles[i].Name.GetInt();
|
||||
int frameNum2 = isLastItem ? lastFrameNum : frameFiles[i+1].Name.GetInt();
|
||||
String fnameCur = Path.GetFileNameWithoutExtension(frameFiles[i].Name);
|
||||
int frameNumCur = fnameCur.GetInt();
|
||||
|
||||
int diff = frameNum2 - frameNum1;
|
||||
int dupes = (diff - 1).Clamp(0, int.MaxValue);
|
||||
frames[fnameCur] = new List<string>();
|
||||
|
||||
if(debug)
|
||||
Logger.Log($"{(isLastItem ? "[isLastItem] " : "")}frameNum1 (frameFiles[{i}]) = {frameNum1}, frameNum2 (frameFiles[{i+1}]) = {frameNum2} => dupes = {dupes}", true, false, "dupes");
|
||||
|
||||
try
|
||||
if(!isLastItem)
|
||||
{
|
||||
frames[Path.GetFileNameWithoutExtension(frameFiles[i].Name)] = new List<string>();
|
||||
String fnameNext = Path.GetFileNameWithoutExtension(frameFiles[i + 1].Name);
|
||||
int frameNumNext = fnameNext.GetInt();
|
||||
|
||||
for (int dupe = 1; dupe <= dupes; dupe++)
|
||||
frames[Path.GetFileNameWithoutExtension(frameFiles[i].Name)].Add(Path.GetFileNameWithoutExtension(frameFiles[i + dupe].Name));
|
||||
for(int j = frameNumCur + 1; j < frameNumNext; j++)
|
||||
{
|
||||
frames[fnameCur].Add(j.ToString().PadLeft(9,'0'));
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Logger.Log($"Deduplication error: {ex.Message}");
|
||||
Logger.Log($"Stack Trace:\n{ex.StackTrace}", true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -168,8 +168,6 @@ namespace Flowframes
|
||||
|
||||
if (Config.GetInt(Config.Key.dedupMode) == 1)
|
||||
await Dedupe.Run(currentSettings.framesFolder);
|
||||
else
|
||||
Dedupe.ClearCache();
|
||||
|
||||
if (!Config.GetBool(Config.Key.enableLoop))
|
||||
{
|
||||
|
||||
@@ -507,10 +507,9 @@ namespace Flowframes.Media
|
||||
foreach (string file in files.Where(x => validExtensions.Contains(Path.GetExtension(x).Replace(".", "").ToLowerInvariant())))
|
||||
{
|
||||
fileCount++;
|
||||
concatFileContent += $"file '{file.Replace(@"\", "/")}'\n";
|
||||
concatFile.WriteLine($"file '{file.Replace(@"\", "/")}'\n");
|
||||
}
|
||||
|
||||
File.WriteAllText(outputPath, concatFileContent);
|
||||
return fileCount;
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,6 @@ namespace Flowframes.Ui
|
||||
CheckExistingFolder(path, outputTbox.Text.Trim());
|
||||
await Task.Delay(10);
|
||||
await PrintResolution(path);
|
||||
Dedupe.ClearCache();
|
||||
await Task.Delay(10);
|
||||
InterpolationProgress.SetPreviewImg(await GetThumbnail(path));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user