added test cases

This commit is contained in:
Boki 2026-02-12 13:08:19 -05:00
parent c1892230b7
commit 93e2234c4e
9 changed files with 320 additions and 141 deletions

View file

@ -108,6 +108,8 @@ static class Daemon
var engine = request.Engine ?? "tesseract";
var preprocess = request.Preprocess ?? "none";
var kernelSize = request.Params?.KernelSize ?? 41;
// No preprocess + tesseract = original fast path
if (engine == "tesseract" && preprocess == "none")
return ocrHandler.HandleOcr(request);
@ -119,7 +121,7 @@ static class Daemon
Bitmap processed;
if (preprocess == "tophat")
{
processed = ImagePreprocessor.PreprocessForOcr(bitmap);
processed = ImagePreprocessor.PreprocessForOcr(bitmap, kernelSize: kernelSize);
}
else if (preprocess == "bgsub")
{
@ -152,16 +154,24 @@ static class Daemon
private static object HandleDiffOcrPipeline(OcrHandler ocrHandler, PythonOcrBridge pythonBridge, Request request)
{
var engine = request.Engine ?? "tesseract";
var preprocess = request.Preprocess ?? "bgsub";
var isPythonEngine = engine is "easyocr" or "paddleocr";
var p = request.Params?.Clone() ?? new DiffOcrParams();
if (request.Threshold > 0) p.DiffThresh = request.Threshold;
// No engine override + no preprocess override = original Tesseract path (supports test/tune params)
if (engine == "tesseract" && request.Preprocess == null)
// Determine preprocess mode: explicit request.Preprocess > params.UseBackgroundSub > default "bgsub"
string preprocess;
if (request.Preprocess != null)
preprocess = request.Preprocess;
else if (request.Params != null)
preprocess = p.UseBackgroundSub ? "bgsub" : "tophat";
else
preprocess = "bgsub";
// No engine override + no preprocess override + no params = original Tesseract path
if (engine == "tesseract" && request.Preprocess == null && request.Params == null)
return ocrHandler.HandleDiffOcr(request);
var sw = System.Diagnostics.Stopwatch.StartNew();
var p = new DiffOcrParams();
if (request.Threshold > 0) p.DiffThresh = request.Threshold;
var cropResult = ocrHandler.DiffCrop(request, p);
if (cropResult == null)
@ -174,13 +184,14 @@ static class Daemon
Bitmap processed;
if (preprocess == "bgsub")
{
int upscale = isPythonEngine ? 1 : 2;
int upscale = isPythonEngine ? 1 : p.Upscale;
processed = ImagePreprocessor.PreprocessWithBackgroundSub(
cropped, refCropped, dimPercentile: 40, textThresh: 60, upscale: upscale, softThreshold: false);
cropped, refCropped, dimPercentile: p.DimPercentile, textThresh: p.TextThresh,
upscale: upscale, softThreshold: p.SoftThreshold);
}
else if (preprocess == "tophat")
{
processed = ImagePreprocessor.PreprocessForOcr(cropped);
processed = ImagePreprocessor.PreprocessForOcr(cropped, kernelSize: p.KernelSize);
}
else // "none"
{