diff --git a/src/AIFotoONLUS.Core/NumberRecognitionEngine.cs b/src/AIFotoONLUS.Core/NumberRecognitionEngine.cs index e729141..0c1ac43 100644 --- a/src/AIFotoONLUS.Core/NumberRecognitionEngine.cs +++ b/src/AIFotoONLUS.Core/NumberRecognitionEngine.cs @@ -533,17 +533,23 @@ namespace AIFotoONLUS.Core return ProcessDirectoryAsync(directoryPath, skipTextNegative).GetAwaiter().GetResult(); } - public async Task> ProcessDirectoryAsync(string directoryPath, bool skipTextNegative = false, bool recursive = false, IProgress? progress = null, IProgress? resultProgress = null, CancellationToken cancellationToken = default) + public async Task> ProcessFilesAsync(IEnumerable filePaths, bool skipTextNegative = false, int? maxDegreeOfParallelism = null, IProgress? progress = null, IProgress? 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(); - 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(); - // 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(); 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> ProcessDirectoryAsync(string directoryPath, bool skipTextNegative = false, bool recursive = false, IProgress? progress = null, IProgress? 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 /// /// Worker overload of that