Refactor ProcessDirectoryAsync to ProcessFilesAsync for improved file handling and parallelism
This commit is contained in:
parent
8020fb6d25
commit
e34b71beda
1 changed files with 27 additions and 11 deletions
|
|
@ -533,17 +533,23 @@ namespace AIFotoONLUS.Core
|
||||||
return ProcessDirectoryAsync(directoryPath, skipTextNegative).GetAwaiter().GetResult();
|
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);
|
if (filePaths is null) throw new ArgumentNullException(nameof(filePaths));
|
||||||
var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
|
|
||||||
var files = Directory.EnumerateFiles(directoryPath, "*.*", searchOption)
|
var files = filePaths
|
||||||
.Where(f => f.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase))
|
.Where(f => !string.IsNullOrWhiteSpace(f))
|
||||||
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
var bag = new ConcurrentBag<ImageResult>();
|
var bag = new ConcurrentBag<ImageResult>();
|
||||||
|
|
||||||
var dop = Environment.ProcessorCount;
|
var dop = maxDegreeOfParallelism.GetValueOrDefault();
|
||||||
|
if (dop <= 0)
|
||||||
|
{
|
||||||
|
dop = Environment.ProcessorCount;
|
||||||
|
}
|
||||||
|
|
||||||
var total = files.Length;
|
var total = files.Length;
|
||||||
var processed = 0;
|
var processed = 0;
|
||||||
var sw = System.Diagnostics.Stopwatch.StartNew();
|
var sw = System.Diagnostics.Stopwatch.StartNew();
|
||||||
|
|
@ -578,14 +584,11 @@ namespace AIFotoONLUS.Core
|
||||||
|
|
||||||
var regions = DetectTextRegions(nets.detNet, image).ToArray();
|
var regions = DetectTextRegions(nets.detNet, image).ToArray();
|
||||||
var texts = new List<string>();
|
var texts = new List<string>();
|
||||||
// minimal logging for performance
|
|
||||||
foreach (var r in regions)
|
foreach (var r in regions)
|
||||||
{
|
{
|
||||||
using var crop = new Mat(image, r.BoundingBox);
|
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 ctx = $"{filename}_{r.BoundingBox.X}_{r.BoundingBox.Y}_{r.BoundingBox.Width}x{r.BoundingBox.Height}";
|
||||||
var txt = RecognizeDigits(crop, nets.recNet, ctx);
|
var txt = RecognizeDigits(crop, nets.recNet, ctx);
|
||||||
// minimal logging for performance
|
|
||||||
// Fallback: if empty, try a fresh net (diagnostic)
|
|
||||||
if (string.IsNullOrEmpty(txt))
|
if (string.IsNullOrEmpty(txt))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
@ -597,10 +600,10 @@ namespace AIFotoONLUS.Core
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(txt)) texts.Add(txt);
|
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)
|
if (texts.Count == 0)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
@ -610,6 +613,7 @@ namespace AIFotoONLUS.Core
|
||||||
{
|
{
|
||||||
sharedRegions = DetectTextRegions(image).ToArray();
|
sharedRegions = DetectTextRegions(image).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
var sharedTexts = new List<string>();
|
var sharedTexts = new List<string>();
|
||||||
foreach (var r2 in sharedRegions)
|
foreach (var r2 in sharedRegions)
|
||||||
{
|
{
|
||||||
|
|
@ -620,11 +624,13 @@ namespace AIFotoONLUS.Core
|
||||||
{
|
{
|
||||||
txt2 = RecognizeDigits(crop2, ctx2);
|
txt2 = RecognizeDigits(crop2, ctx2);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(txt2))
|
if (!string.IsNullOrEmpty(txt2))
|
||||||
{
|
{
|
||||||
sharedTexts.Add(txt2);
|
sharedTexts.Add(txt2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sharedTexts.Count > 0)
|
if (sharedTexts.Count > 0)
|
||||||
{
|
{
|
||||||
texts = sharedTexts;
|
texts = sharedTexts;
|
||||||
|
|
@ -665,7 +671,6 @@ namespace AIFotoONLUS.Core
|
||||||
}
|
}
|
||||||
}, cancellationToken).ConfigureAwait(false);
|
}, cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
// dispose created nets
|
|
||||||
while (netsBag.TryTake(out var pair))
|
while (netsBag.TryTake(out var pair))
|
||||||
{
|
{
|
||||||
try { pair.detNet.Dispose(); } catch { }
|
try { pair.detNet.Dispose(); } catch { }
|
||||||
|
|
@ -676,6 +681,17 @@ namespace AIFotoONLUS.Core
|
||||||
return bag.OrderBy(b => b.FileName).ToList();
|
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
|
// Overload RecognizeDigits that accepts a Net for worker threads
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Worker overload of <see cref="RecognizeDigits(Mat,string?)"/> that
|
/// Worker overload of <see cref="RecognizeDigits(Mat,string?)"/> that
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue