RED-7669: optimize OCR-module performance

* try and synchronize all malloc calls
This commit is contained in:
Kilian Schuettler 2023-11-17 14:04:43 +01:00
parent 574f7ac25e
commit 6f99664906
3 changed files with 26 additions and 36 deletions

View File

@ -2,10 +2,15 @@ package com.knecon.fforesight.service.ocr.processor.model;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.IntBuffer;
import java.util.concurrent.Semaphore;
import org.apache.pdfbox.util.Matrix;
import com.knecon.fforesight.service.ocr.processor.service.threads.OCRThread;
import com.knecon.fforesight.service.ocr.processor.utils.ImageProcessingUtils;
import com.pdftron.sdf.Obj;
import lombok.AccessLevel;
import lombok.Getter;
@ -52,12 +57,15 @@ public class ExtractedOcrImage implements OcrImage {
@SneakyThrows
synchronized private Pix binarize(BufferedImage image, float imageDpi, int targetDpi) {
private Pix binarize(BufferedImage image, float imageDpi, int targetDpi) {
ImageProcessingUtils.setAlphaChannelToWhite(image);
Pix grayScale = ImageProcessingUtils.convertToGrayScale(image);
Pix scaledUp = ImageProcessingUtils.scaleToTargetDpi(imageDpi, targetDpi, grayScale);
return ImageProcessingUtils.despecklePix(scaledUp);
synchronized (OCRThread.class) { // must synchronize the mallocs here with the mallocs tesseract detection script.
Pix grayScale = ImageProcessingUtils.convertToGrayScale(image);
Pix scaledUp = ImageProcessingUtils.scaleToTargetDpi(imageDpi, targetDpi, grayScale);
return ImageProcessingUtils.despecklePix(scaledUp);
}
}

View File

@ -3,6 +3,8 @@ package com.knecon.fforesight.service.ocr.processor.service.threads;
import static net.sourceforge.tess4j.ITessAPI.TRUE;
import java.io.File;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.file.Path;
import java.util.List;
import java.util.NoSuchElementException;
@ -13,8 +15,8 @@ import com.knecon.fforesight.service.ocr.processor.model.OcrImage;
import com.knecon.fforesight.service.ocr.processor.model.OcrResult;
import com.knecon.fforesight.service.ocr.processor.service.OcrProgressLogger;
import com.knecon.fforesight.service.ocr.processor.service.Statistics;
import com.knecon.fforesight.service.ocr.processor.utils.NativeMemoryAllocationUtils;
import com.knecon.fforesight.service.ocr.processor.utils.Tesseract2;
import com.sun.jna.ptr.PointerByReference;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
@ -126,15 +128,21 @@ public class OCRThread extends Thread {
TessAPI1.TessBaseAPISetImage2(detectionScriptHandle, image.getPix());
TessAPI1.TessBaseAPISetSourceResolution(detectionScriptHandle, image.getDpi());
synchronized (OCRThread.class) { // must synchronize the mallocs here with the mallocs in leptonica binarization.
orientationDegreeResultBuffer = IntBuffer.allocate(1);
orientationDegreeConfidenceBuffer = FloatBuffer.allocate(1);
scriptureNameBuffer = new PointerByReference();
scriptureConfidenceBuffer = FloatBuffer.allocate(1);
}
int orient_deg = 0;
int result = TessAPI1.TessBaseAPIDetectOrientationScript(detectionScriptHandle,
buffers.orientationDegreeResultBuffer(),
buffers.orientationDegreeConfidenceBuffer(),
buffers.scriptureNameBuffer(),
buffers.scriptureConfidenceBuffer());
orientationDegreeResultBuffer,
orientationDegreeConfidenceBuffer,
scriptureNameBuffer,
scriptureConfidenceBuffer);
if (result == TRUE) {
orient_deg = buffers.orientationDegreeResultBuffer().get();
orient_deg = orientationDegreeResultBuffer.get();
}
synchronized (OCRThread.class) {

View File

@ -1,26 +0,0 @@
package com.knecon.fforesight.service.ocr.processor.utils;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import com.sun.jna.ptr.PointerByReference;
import lombok.experimental.UtilityClass;
@UtilityClass
public class NativeMemoryAllocationUtils {
synchronized public static DetectionScriptBuffers getDetectionScriptBuffers() {
IntBuffer orient_degB = IntBuffer.allocate(1);
FloatBuffer orient_confB = FloatBuffer.allocate(1);
PointerByReference script_nameB = new PointerByReference();
FloatBuffer script_confB = FloatBuffer.allocate(1);
return new DetectionScriptBuffers(orient_degB, orient_confB, script_nameB, script_confB);
}
public record DetectionScriptBuffers(IntBuffer orientationDegreeResultBuffer, FloatBuffer orientationDegreeConfidenceBuffer, PointerByReference scriptureNameBuffer, FloatBuffer scriptureConfidenceBuffer) {
}
}