mirror of
https://github.com/cyanfish/naps2.git
synced 2024-10-27 01:22:49 +03:00
Move pre-processing inside TesseractOcrEngine
This commit is contained in:
parent
b12d9b53c2
commit
91717d1750
@ -70,6 +70,10 @@ public class TesseractOcrEngine : IOcrEngine
|
||||
string tempHocrFilePathWithExt = tempHocrFilePath + ".hocr";
|
||||
try
|
||||
{
|
||||
if (ocrParams.Mode is OcrMode.FastWithPreProcess or OcrMode.BestWithPreProcess)
|
||||
{
|
||||
PreProcessImage(scanningContext, imagePath);
|
||||
}
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = _tesseractPath,
|
||||
@ -192,6 +196,21 @@ public class TesseractOcrEngine : IOcrEngine
|
||||
|
||||
public event EventHandler? OcrTimeout;
|
||||
|
||||
private static void PreProcessImage(ScanningContext scanningContext, string imagePath)
|
||||
{
|
||||
IMemoryImage? image = null;
|
||||
try
|
||||
{
|
||||
image = scanningContext.ImageContext.Load(imagePath);
|
||||
image = image.PerformTransform(new CorrectionTransform(CorrectionMode.Document));
|
||||
image.Save(imagePath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
image?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private static string? GetNearestAncestorAttribute(XElement x, string attributeName)
|
||||
{
|
||||
var ancestor = x.AncestorsAndSelf().FirstOrDefault(x => x.Attribute(attributeName) != null);
|
||||
|
@ -344,16 +344,9 @@ public class PdfExporter
|
||||
// Save the image to a file for use in OCR.
|
||||
// We don't need to delete this file as long as we pass it to OcrRequestQueue.Enqueue, which takes
|
||||
// ownership and guarantees its eventual deletion.
|
||||
if (state.OcrParams!.Mode is OcrMode.FastWithPreProcess or OcrMode.BestWithPreProcess)
|
||||
{
|
||||
using var preProcessed = state.Image.WithTransform(new CorrectionTransform(CorrectionMode.Document));
|
||||
preProcessed.Save(ocrTempFilePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
using var fileStream = new FileStream(ocrTempFilePath, FileMode.Create, FileAccess.Write);
|
||||
state.Embedder.CopyToStream(fileStream);
|
||||
}
|
||||
// TODO: If the image has transforms and we're saving as JPEG, we should cache to avoid double-rendering
|
||||
using var fileStream = new FileStream(ocrTempFilePath, FileMode.Create, FileAccess.Write);
|
||||
state.Embedder.CopyToStream(fileStream);
|
||||
}
|
||||
|
||||
// Start OCR
|
||||
|
@ -1,6 +1,5 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NAPS2.Images.Bitwise;
|
||||
using NAPS2.Ocr;
|
||||
|
||||
namespace NAPS2.Scan.Internal;
|
||||
|
||||
@ -191,11 +190,6 @@ internal class RemotePostProcessor : IRemotePostProcessor
|
||||
{
|
||||
// TODO: If we use tesseract as a library, this is something that that could potentially improve (i.e. not having to save to disk)
|
||||
// But then again, that doesn't make as much sense on systems (i.e. linux) where tesseract would be provided as an external package
|
||||
if (options.OcrParams.Mode is OcrMode.FastWithPreProcess or OcrMode.BestWithPreProcess)
|
||||
{
|
||||
bitmap = _scanningContext.ImageContext.PerformTransform(bitmap,
|
||||
new CorrectionTransform(CorrectionMode.Document));
|
||||
}
|
||||
return _scanningContext.SaveToTempFile(bitmap);
|
||||
}
|
||||
return null;
|
||||
|
Loading…
Reference in New Issue
Block a user