RED-7669: optimize OCR-module performance

* moar sigsegv
This commit is contained in:
Kilian Schuettler 2023-11-15 17:17:21 +01:00
parent 2632d2023d
commit 19747cbca5
2 changed files with 35 additions and 11 deletions

View File

@ -1,5 +1,6 @@
package com.knecon.fforesight.service.ocr.processor.service.threads;
import static com.knecon.fforesight.service.ocr.processor.utils.NativeMemoryAllocationUtils.getDetectionScriptBuffers;
import static net.sourceforge.tess4j.ITessAPI.TRUE;
import java.io.File;
@ -15,6 +16,7 @@ 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;
@ -128,21 +130,15 @@ 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,
orientationDegreeResultBuffer,
orientationDegreeConfidenceBuffer,
scriptureNameBuffer,
scriptureConfidenceBuffer);
buffers.orient_degB(),
buffers.orient_confB(),
buffers.script_nameB(),
buffers.script_confB());
if (result == TRUE) {
orient_deg = orientationDegreeResultBuffer.get();
orient_deg = buffers.orient_degB().get();
}
synchronized (OCRThread.class) {

View File

@ -0,0 +1,28 @@
package com.knecon.fforesight.service.ocr.processor.utils;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import com.knecon.fforesight.service.ocr.processor.service.threads.OCRThread;
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);
DetectionScriptBuffers buffers = new DetectionScriptBuffers(orient_degB, orient_confB, script_nameB, script_confB);
return buffers;
}
public record DetectionScriptBuffers(IntBuffer orient_degB, FloatBuffer orient_confB, PointerByReference script_nameB, FloatBuffer script_confB) {
}
}