Refactor ProcessDirectoryAsync to ProcessFilesAsync for improved file handling and parallelism

This commit is contained in:
MaddoScientisto 2026-05-09 18:54:25 +02:00
commit e34b71beda

View file

@ -533,17 +533,23 @@ namespace AIFotoONLUS.Core
return ProcessDirectoryAsync(directoryPath, skipTextNegative).GetAwaiter().GetResult();
}
public async Task<IEnumerable<ImageResult>> ProcessDirectoryAsync(string directoryPath, bool skipTextNegative = false, bool recursive = false, IProgress<ProcessingStats>? progress = null, IProgress<ImageResult>? resultProgress = null, CancellationToken cancellationToken = default)
public async Task<IEnumerable<ImageResult>> ProcessFilesAsync(IEnumerable<string> filePaths, bool skipTextNegative = false, int? maxDegreeOfParallelism = null, IProgress<ProcessingStats>? progress = null, IProgress<ImageResult>? resultProgress = null, CancellationToken cancellationToken = default)
{
if (!Directory.Exists(directoryPath)) throw new DirectoryNotFoundException(directoryPath);
var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
var files = Directory.EnumerateFiles(directoryPath, "*.*", searchOption)
.Where(f => f.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase))
if (filePaths is null) throw new ArgumentNullException(nameof(filePaths));
var files = filePaths
.Where(f => !string.IsNullOrWhiteSpace(f))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToArray();
var bag = new ConcurrentBag<ImageResult>();
var dop = Environment.ProcessorCount;
var dop = maxDegreeOfParallelism.GetValueOrDefault();
if (dop <= 0)
{
dop = Environment.ProcessorCount;
}
var total = files.Length;
var processed = 0;
var sw = System.Diagnostics.Stopwatch.StartNew();
@ -578,14 +584,11 @@ namespace AIFotoONLUS.Core
var regions = DetectTextRegions(nets.detNet, image).ToArray();
var texts = new List<string>();
// minimal logging for performance
foreach (var r in regions)
{
using var crop = new Mat(image, r.BoundingBox);
var ctx = $"{filename}_{r.BoundingBox.X}_{r.BoundingBox.Y}_{r.BoundingBox.Width}x{r.BoundingBox.Height}";
var txt = RecognizeDigits(crop, nets.recNet, ctx);
// minimal logging for performance
// Fallback: if empty, try a fresh net (diagnostic)
if (string.IsNullOrEmpty(txt))
{
try
@ -597,10 +600,10 @@ namespace AIFotoONLUS.Core
}
catch { }
}
if (!string.IsNullOrEmpty(txt)) texts.Add(txt);
}
// If no text was recognized with per-thread nets, try one more time using the shared nets under a lock
if (texts.Count == 0)
{
try
@ -610,6 +613,7 @@ namespace AIFotoONLUS.Core
{
sharedRegions = DetectTextRegions(image).ToArray();
}
var sharedTexts = new List<string>();
foreach (var r2 in sharedRegions)
{
@ -620,11 +624,13 @@ namespace AIFotoONLUS.Core
{
txt2 = RecognizeDigits(crop2, ctx2);
}
if (!string.IsNullOrEmpty(txt2))
{
sharedTexts.Add(txt2);
}
}
if (sharedTexts.Count > 0)
{
texts = sharedTexts;
@ -665,7 +671,6 @@ namespace AIFotoONLUS.Core
}
}, cancellationToken).ConfigureAwait(false);
// dispose created nets
while (netsBag.TryTake(out var pair))
{
try { pair.detNet.Dispose(); } catch { }
@ -676,6 +681,17 @@ namespace AIFotoONLUS.Core
return bag.OrderBy(b => b.FileName).ToList();
}
public async Task<IEnumerable<ImageResult>> ProcessDirectoryAsync(string directoryPath, bool skipTextNegative = false, bool recursive = false, IProgress<ProcessingStats>? progress = null, IProgress<ImageResult>? resultProgress = null, CancellationToken cancellationToken = default)
{
if (!Directory.Exists(directoryPath)) throw new DirectoryNotFoundException(directoryPath);
var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
var files = Directory.EnumerateFiles(directoryPath, "*.*", searchOption)
.Where(f => f.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase))
.ToArray();
return await ProcessFilesAsync(files, skipTextNegative, maxDegreeOfParallelism: null, progress, resultProgress, cancellationToken).ConfigureAwait(false);
}
// Overload RecognizeDigits that accepts a Net for worker threads
/// <summary>
/// Worker overload of <see cref="RecognizeDigits(Mat,string?)"/> that