2020-11-23 16:51:05 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2020-11-26 20:17:18 +01:00
|
|
|
|
using System.Diagnostics;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Flowframes.IO;
|
|
|
|
|
|
using Flowframes.Main;
|
2020-11-26 20:17:18 +01:00
|
|
|
|
using Flowframes.UI;
|
|
|
|
|
|
using Microsoft.VisualBasic.Devices;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
using ImageMagick;
|
2020-11-26 20:17:18 +01:00
|
|
|
|
using Flowframes.OS;
|
2020-12-02 17:23:24 +01:00
|
|
|
|
using Flowframes.Data;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Flowframes.Magick
|
|
|
|
|
|
{
|
2020-11-24 02:23:19 +01:00
|
|
|
|
class MagickDedupe
|
2020-11-23 16:51:05 +01:00
|
|
|
|
{
|
|
|
|
|
|
public enum Mode { None, Info, Enabled, Auto }
|
|
|
|
|
|
public static Mode currentMode;
|
|
|
|
|
|
public static float currentThreshold;
|
|
|
|
|
|
|
|
|
|
|
|
public static async Task Run(string path, bool testRun = false, bool setStatus = true)
|
|
|
|
|
|
{
|
2020-12-13 23:44:23 +01:00
|
|
|
|
if (path == null || !Directory.Exists(path) || Interpolate.canceled)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2020-12-07 12:34:12 +01:00
|
|
|
|
currentMode = Mode.Auto;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
2020-12-13 23:44:23 +01:00
|
|
|
|
if(setStatus)
|
|
|
|
|
|
Program.mainForm.SetStatus("Running frame de-duplication");
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
|
|
|
|
|
currentThreshold = Config.GetFloat("dedupThresh");
|
2020-12-07 12:34:12 +01:00
|
|
|
|
Logger.Log("Running accurate frame de-duplication...");
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
|
|
|
|
|
if (currentMode == Mode.Enabled || currentMode == Mode.Auto)
|
2020-12-22 19:52:37 +01:00
|
|
|
|
await RemoveDupeFrames(path, currentThreshold, "png", testRun, true, (currentMode == Mode.Auto));
|
2020-11-23 16:51:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-26 20:17:18 +01:00
|
|
|
|
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();
|
|
|
|
|
|
}
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
|
|
|
|
|
public static async Task RemoveDupeFrames(string path, float threshold, string ext, bool testRun = false, bool debugLog = false, bool skipIfNoDupes = false)
|
|
|
|
|
|
{
|
2020-11-26 20:17:18 +01:00
|
|
|
|
Stopwatch sw = new Stopwatch();
|
|
|
|
|
|
sw.Restart();
|
2020-11-23 16:51:05 +01:00
|
|
|
|
Logger.Log("Removing duplicate frames - Threshold: " + threshold.ToString("0.00"));
|
|
|
|
|
|
|
2020-12-22 19:52:37 +01:00
|
|
|
|
FileInfo[] framePaths = IOUtils.GetFileInfosSorted(path, false, "*." + ext);
|
|
|
|
|
|
List<string> framesToDelete = new List<string>();
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
|
|
|
|
|
int currentOutFrame = 1;
|
|
|
|
|
|
int currentDupeCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
int statsFramesKept = 0;
|
|
|
|
|
|
int statsFramesDeleted = 0;
|
|
|
|
|
|
|
|
|
|
|
|
int skipAfterNoDupesFrames = Config.GetInt("autoDedupFrames");
|
|
|
|
|
|
bool hasEncounteredAnyDupes = false;
|
|
|
|
|
|
bool skipped = false;
|
|
|
|
|
|
|
2020-12-22 19:52:37 +01:00
|
|
|
|
bool hasReachedEnd = false;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < framePaths.Length; i++) // Loop through frames
|
2020-11-23 16:51:05 +01:00
|
|
|
|
{
|
2020-12-22 19:52:37 +01:00
|
|
|
|
if (hasReachedEnd)
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
Logger.Log("Base Frame: #" + i);
|
|
|
|
|
|
//int thisFrameDupeCount = 0;
|
|
|
|
|
|
|
2020-11-23 16:51:05 +01:00
|
|
|
|
string frame1 = framePaths[i].FullName;
|
2020-12-22 19:52:37 +01:00
|
|
|
|
//if (!File.Exists(framePaths[i].FullName)) // Skip if file doesn't exist (already deleted / used to be a duped frame)
|
|
|
|
|
|
// continue;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
2020-12-22 19:52:37 +01:00
|
|
|
|
int compareWithIndex = i + 1;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
2020-12-22 19:52:37 +01:00
|
|
|
|
while (true) // Loop dupes
|
2020-11-23 16:51:05 +01:00
|
|
|
|
{
|
2020-12-22 19:52:37 +01:00
|
|
|
|
//compareWithIndex++;
|
|
|
|
|
|
if (compareWithIndex >= framePaths.Length)
|
2020-11-23 16:51:05 +01:00
|
|
|
|
{
|
2020-12-22 19:52:37 +01:00
|
|
|
|
hasReachedEnd = true;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-22 19:52:37 +01:00
|
|
|
|
if (framesToDelete.Contains(framePaths[compareWithIndex].FullName) || !File.Exists(framePaths[compareWithIndex].FullName))
|
2020-11-23 16:51:05 +01:00
|
|
|
|
{
|
2020-12-22 19:52:37 +01:00
|
|
|
|
Logger.Log($"Frame {compareWithIndex} was already deleted - skipping");
|
|
|
|
|
|
compareWithIndex++;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//if (compareWithIndex >= framePaths.Length)
|
|
|
|
|
|
// hasReachedEnd = true;
|
|
|
|
|
|
|
|
|
|
|
|
Logger.Log("Compare With: #" + compareWithIndex);
|
|
|
|
|
|
|
|
|
|
|
|
string frame2 = framePaths[compareWithIndex].FullName;
|
|
|
|
|
|
// if (oldIndex >= 0)
|
|
|
|
|
|
// i = oldIndex;
|
|
|
|
|
|
|
|
|
|
|
|
float diff = GetDifference(frame1, frame2);
|
|
|
|
|
|
Logger.Log("Diff: " + diff);
|
|
|
|
|
|
|
|
|
|
|
|
string delStr = "Keeping";
|
|
|
|
|
|
if (diff < threshold) // Is a duped frame.
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!testRun)
|
|
|
|
|
|
{
|
|
|
|
|
|
delStr = "Deleting";
|
|
|
|
|
|
//File.Delete(frame2);
|
|
|
|
|
|
framesToDelete.Add(frame2);
|
|
|
|
|
|
if (debugLog) Logger.Log("[FrameDedup] Deleted " + Path.GetFileName(frame2));
|
|
|
|
|
|
hasEncounteredAnyDupes = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
statsFramesDeleted++;
|
|
|
|
|
|
currentDupeCount++;
|
|
|
|
|
|
Logger.Log($"Frame {i} has {currentDupeCount} dupes");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
statsFramesKept++;
|
|
|
|
|
|
currentOutFrame++;
|
|
|
|
|
|
currentDupeCount = 0;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (i % 15 == 0 || true)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.Log($"[FrameDedup] Difference from {Path.GetFileName(frame1)} to {Path.GetFileName(frame2)}: {diff.ToString("0.00")}% - {delStr}. Total: {statsFramesKept} kept / {statsFramesDeleted} deleted.", false, true);
|
|
|
|
|
|
Program.mainForm.SetProgress((int)Math.Round(((float)i / framePaths.Length) * 100f));
|
|
|
|
|
|
if (imageCache.Count > 750 || (imageCache.Count > 50 && OSUtils.GetFreeRamMb() < 2500))
|
|
|
|
|
|
ClearCache();
|
|
|
|
|
|
}
|
2020-11-23 16:51:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-22 19:52:37 +01:00
|
|
|
|
// int oldIndex = -1;
|
|
|
|
|
|
// if (i >= framePaths.Length) // If this is the last frame, compare with 1st to avoid OutOfRange error
|
|
|
|
|
|
// {
|
|
|
|
|
|
// oldIndex = i;
|
|
|
|
|
|
// i = 0;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// while (!File.Exists(framePaths[i+1].FullName)) // If frame2 doesn't exist, keep stepping thru the array
|
|
|
|
|
|
// {
|
|
|
|
|
|
// if (i >= framePaths.Length)
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// i++;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
2020-11-26 20:17:18 +01:00
|
|
|
|
if(i % 5 == 0)
|
|
|
|
|
|
await Task.Delay(1);
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
2020-11-25 14:04:31 +01:00
|
|
|
|
if (Interpolate.canceled) return;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
2020-12-22 19:52:37 +01:00
|
|
|
|
foreach (string frame in framesToDelete)
|
|
|
|
|
|
IOUtils.TryDeleteIfExists(frame);
|
|
|
|
|
|
|
2020-12-07 12:34:12 +01:00
|
|
|
|
if (!testRun && skipIfNoDupes && !hasEncounteredAnyDupes && skipAfterNoDupesFrames > 0 && i >= skipAfterNoDupesFrames)
|
2020-11-23 16:51:05 +01:00
|
|
|
|
{
|
|
|
|
|
|
skipped = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-15 14:46:33 +01:00
|
|
|
|
string testStr = testRun ? " [TestRun]" : "";
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
2020-11-25 14:04:31 +01:00
|
|
|
|
if (Interpolate.canceled) return;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
if (skipped)
|
|
|
|
|
|
{
|
2020-12-07 12:34:12 +01:00
|
|
|
|
Logger.Log($"[FrameDedup] First {skipAfterNoDupesFrames} frames did not have any duplicates - Skipping the rest!", false, true);
|
2020-11-23 16:51:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.Log($"[FrameDedup]{testStr} Done. Kept {statsFramesKept} frames, deleted {statsFramesDeleted} frames.", false, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-25 12:40:17 +01:00
|
|
|
|
if (statsFramesKept <= 0)
|
|
|
|
|
|
Interpolate.Cancel("No frames were left after de-duplication.");
|
2020-11-23 16:51:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-22 19:52:37 +01:00
|
|
|
|
static float GetDifference (string img1Path, string img2Path)
|
2020-11-23 16:51:05 +01:00
|
|
|
|
{
|
2020-12-22 19:52:37 +01:00
|
|
|
|
MagickImage img2 = GetImage(img2Path);
|
|
|
|
|
|
MagickImage img1 = GetImage(img1Path);
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
2020-12-22 19:52:37 +01:00
|
|
|
|
double err = img1.Compare(img2, ErrorMetric.Fuzz);
|
|
|
|
|
|
float errPercent = (float)err * 100f;
|
|
|
|
|
|
return errPercent;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|