pdfOCR module - onnxTR
With the release of iText Suite 9.0.3 (pdfOCR 4.1.0), we have also released a new pdfOCR module, called pdfocr-onnxtr
, which enables us to use Open Neural Network Exchange (ONNX) compatible models with iText.
It is really super easy, especially if you are already familiar with the pdfOCR API (Java/.NET).
Java
IDetectionPredictor detectionPredictor = OnnxDetectionPredictor.fast(FAST);
IRecognitionPredictor recognitionPredictor = OnnxRecognitionPredictor.crnnVgg16(CRNNVGG16);
OnnxTrOcrEngine ocrEngine = new OnnxTrOcrEngine(detectionPredictor, recognitionPredictor);
OcrPdfCreator ocrPdfCreator = new OcrPdfCreator(ocrEngine);
try (PdfWriter writer = new PdfWriter(PATH_TO_OUTPUT_PDF)) {
String imagePath = "src/images/rotatedBy90Degrees.png";
PdfDocument pdf = ocrPdfCreator.createPdf(Collections.singletonList(new File(imagePath)), writer);
}
You will notice, though, that with the OnnxTrOcrEngine constructor, there are two arguments that go into it.
Detection - this is the predictor that identifies where there is text present in the document.
Recognition - this is the predictor that identified which is present where the detection predictor said it would.
Even though by supporting ONNX we theoretically support multiple engines, the current ones we recommend are the following:
Felix92/onnxtr-fast-tiny for detection
Felix92/doctr-dummy-torch-crnn-vgg16-bn for recognition
You will have to download the model .onnx files and use them for OnnxDetectionPredictor.fast() and OnnxRecognitionPredictor.crnnVgg16() respectively