Skip to main content

Optical Character Recognition Sample Process (OCR Sample)

Optical Character Recognition Sample Process (OCR Sample)

Overview

The OCR Sample is designed to test and demonstrate a variety of document processing and image preprocessing techniques used to prepare documents for Optical Character Recognition

The sample allows to explore such techniques as image deskewing, rotation, resizing, perspective correction, noise reduction, contrast enhancement, and other ImageMagick-based preprocessing operations. These adjustments significantly improve OCR accuracy, ensuring that text within images or scanned documents is recognized as precisely as possible.

Beyond image-based preparation, the updated OCR Sample includes demo document sets that demonstrate multi-format document conversion, OCR pipeline automation, and advanced extraction capabilities:

  • LibreOffice to HTML: Demonstrates converting input documents of different formats (XLS, XLSX, ODS, DOC, DOCX, ODT, RTF) to HTML for further processing.
  • LibreOffice to PDF: Demonstrates converting input documents of different formats (XLS, XLSX, ODS, DOC, DOCX, ODT, RTF) to PDF for further processing.
  • Ghostscript + PaddleOCR: Demonstrates preprocessing documents with Ghostscript (DPI assignment and page splitting) and performing OCR with PaddleOCR.
  • PaddleOCR to Table: Demonstrates PaddleOCR’s ability to detect and reconstruct table structures, exporting results as HTML and/or Excel files per page.
  • PDFBox: Demonstrates direct text extraction from readable PDF documents without performing OCR when the text layer is available.
  • Auto OCR: Demonstrates a fully automated OCR service capable of analyzing incoming PDF files to determine their content type (text-based, image-based, or hybrid) and dynamically routing them through the most efficient OCR pipeline using PDFBox, ImageMagick, Ghostscript, PaddleOCR, or Tesseract.

All these capabilities allow users to explore and compare different OCR workflows, adjust script parameters, and observe the impact of preprocessing, format conversion, and automated routing on OCR quality and performance.

Prerequisites

In order to successfully set up and run OCR Sample:

  1. Ensure that you have a running node with the "AP_RUN" and "SELENIUM" capabilities.
  2. Upload the OCR Sample package to the Control Server. The package can be found in the following directory: http://<CS host>/nexus/repository/rpaplatform/eu/ibagroup/samples/ap/easy-rpa-ocr-ap/<EasyRPA version>/easy-rpa-ocr-ap-<EasyRA version>-bin.zip
    The source code can be found here: https://code.easyrpa.eu/easyrpa/easy-rpa-samples/-/tree/dev/easy-rpa-ml-aps/easy-rpa-ocr-ap
  3. To run the demo "Preprocess" option needs to be launched on a selected document set of the package.

OCR Sample Package Structure

FolderTypeDescription
Document ProcessorAutomation ProcessStandard document processing automation process
LIBREOFFICE_TO_HTML_SAMPLE_DOCSETDocument SetContains documents and document set settings to demonstrate converting input documents (XLS, XLSX, ODS, DOC, DOCX, ODT, RTF) to HTML using LibreOffice.
LIBREOFFICE_TO_PDF_SAMPLE_DOCSETDocument SetContains documents and document set settings to demonstrate converting input documents (XLS, XLSX, ODS, DOC, DOCX, ODT, RTF) to PDF using LibreOffice.
GS_PADDLEOCR_SAMPLE_DOCSETDocument SetDemonstrates processing documents (assign DPI and split to pages) with Ghostscript, then performing OCR with PaddleOCR.
PADDLEOCR_TO_TABLEDocument SetDemonstrates PaddleOCR’s ability to extract table structure layout and output HTML and/or Excel results per page.
PDFBOX_SAMPLE_DOCSETDocument SetDemonstrates processing readable documents directly via PDFBox for text extraction.
AUTO_OCRDocument SetDemonstrates the AUTO OCR service for automatically analyzing incoming PDFs and routing them through the optimal OCR pipeline.
OCR_SAMPLE_TEXTCLEANERDocument SetContains documents and document set settings to demonstrate textcleaner script execution results.
OCR_SAMPLE_UNPERSPECTIVEDocument SetContains documents and document set settings to demonstrate otsuthresh and unperspective scripts execution results.
MULTIFORMAT_HTMLDocument TypeInformation Extraction Document Type
MULTIFORMAT_PDFDocument TypeInformation Extraction Document Type
OCR_SAMPLEDocument TypeInformation Extraction Document Type
OCR_SAMPLE_TEXTCLEANERDocument TypeInformation Extraction Document Type
storage/dataStorageProvides image preprocesing scripts.

LIBREOFFICE_TO_HTML_SAMPLE_DOCSET 

Purpose: Convert input office documents (XLS, XLSX, ODS, DOC, DOCX, ODT, RTF) to HTML using LibreOffice. The process does not require specific document processing JSON settings.

LIBREOFFICE_TO_HTML results example

Input RTF Document Example:

Output HTML Example:

Output PDF Document Example:


LIBREOFFICE_TO_PDF_SAMPLE_DOCSET

Purpose: Convert input office documents (XLS, XLSX, ODS, DOC, DOCX, ODT, RTF) to PDF using LibreOffice. The process does not require specific document processing JSON settings.

Input DOC Document Example:

Output PDF Document Example:

GS_PADDLEOCR_SAMPLE_DOCSET

Purpose: Demonstrates document processing with Ghostscript and OCR with PaddleOCR.

GHOSTSCRIPT+ PADDLEOCR JSON Settings

Below you can find an example of JSON settings provided in the GS_PADDLEOCR_SAMPLE_DOCSET  Document Set Details:

GS PADDLEOCR JSON settings example
{
	"bucket": "data",
	"ocrType": "paddleocr",
	"gsPdfPageSplitter": true,
	"gsDpi": 270,
	"imageMagickOptions": [
		"-units",
		"PixelsPerInch",
		"-density",
		"180",
		"${source}",
		"-background",
		"white",
		"-alpha",
		"remove",
		"-quality",
		"100",
		"-strip",
		"-resample",
		"180"
	],
	"tesseractOptions": [
		"-l",
		"eng",
		"--psm",
		"12",
		"--oem",
		"3",
		"--dpi",
		"320"
	],
	"paddleOcrOptions": [
		"--lang",
		"en",
		"--text_det_unclip_ratio",
		"0.8",
		"--use_doc_unwarping",
		"false"
	],
	"debug": [
		"images",
		"keepFiles",
		"paddleocr"
	],
	"workspaceGroup": "Default"
}

 GHOSTSCRIPT+ PADDLEOCR JSON settings contain:

  • "ocrType": "paddleocr" - Specifies the OCR mode paddleocr"
  • "gsPdfPageSplitter": true - This flag switches document convertation into pages from ImageMagic to GhostScript. 
  •  "gsDpi": 270 - DPI for rendering images for GhostScript pdf split.
  • "paddleOcrOptions":  
    •  "--lang", "en" - Sets the language for OCR recognition to English    
    •  "--text_det_unclip_ratio", "0.8" - Sets how much to expand text detection boxes   
    • "--use_doc_unwarping", "false" - Disables document unwarping correction  

Input PDF Document:

Output GHOSTSCRIPT+ PADDLEOCR result:

PADDLEOCR_TO_TABLE

Purpose: Demonstrates the capability of PaddleOCR to detect and extract table structures from scanned or image-based documents, generating structured HTML and Excel output per page.

PADDLEOCR_TO_TABLE JSON Settings

Below you can find an example of JSON settings provided in the PADDLEOCR_TO_TABLE Document Set Details:

PADDLEOCR_TO_TABLE JSON settings example
{
	"bucket": "data",
	"ocrType": "paddleocrToTable",
	"paddleOcrSoftWordWrap": true,
	"gsPdfPageSplitter": true,
	"ocrImageType": "jpg",
	"paddleocrToTable": {
		"cellTolerance": 10,
		"wordTolerance": 10,
		"rowClusterTol": 10,
		"colClusterTol": 10,
		"coverTol": 5,
		"minCellSize": 30,
		"scaleFactorCol": 45000,
		"scaleFactorRow": 54,
		"cellMergeStrategy": "sentencesBased",
		"tableExporter": "SimpleHtmlExporter,SimpleExcelExporter"
	},
	"gsDpi": 200,
	"paddleOcrOptions": [
		"--lang",
		"en",
		"--text_det_unclip_ratio",
		"0.9",
		"--use_doc_unwarping",
		"false"
	],
	"imageMagickOptions": [
		"-units",
		"PixelsPerInch",
		"-density",
		"180",
		"${source}",
		"-background",
		"white",
		"-alpha",
		"remove",
		"-deskew",
		"40%",
		"-quality",
		"100",
		"-resample",
		"180"
	],
	"tesseractOptions": [
		"-l",
		"eng",
		"--psm",
		"3",
		"--oem",
		"3",
		"--dpi",
		"180"
	],
	"pdfBoxDpi": 180,
	"debug": [
		"keepFilesX",
		"paddleocr"
	],
	"workspaceGroup": "Default"
}


GHOSTSCRIPT+ PADDLEOCR JSON settings contain:

  • "ocrType": "paddleocrToTable" - Specifies the OCR mode paddleocr"
  • "paddleOcrSoftWordWrap": true - Switches off paddleocr word recognition from word wrap mode (the --return_word_box flag) into software mode. For some languages (models) the --return_word_box doesn't work, in this case the software bbox calculation will helps workers for tagging. 
  • "gsPdfPageSplitter": true - This flag switches document convertation into pages from ImageMagic to GhostScript. 
  •  "gsDpi": 200 - DPI for rendering images for GhostScript pdf split.
  • "ocrImageType": "jpg" - Converts documents to JPG format for processing.
  • "paddleocrToTable":
    • "cellTolerance": 10 - Pixel tolerance for cell boundaries.
    • "wordTolerance": 10 - Pixel tolerance for word grouping.
    • "rowClusterTol": 10 - Groups text into rows within 10px vertical distance.
    • "colClusterTol": 10 - Groups text into columns within 10px horizontal distance.
    • "coverTol": 5 - Tolerance for detecting cell coverage/merging.
    • "minCellSize": 30 - Sets the minimum cell size.
    • "scaleFactorCol": 45000 - Column width calculation factor
    • "scaleFactorRow": 54 - Row height calculation factor
    • "cellMergeStrategy": "sentencesBased" - Merges cells based on semantic content rather than just position.
    • "tableExporter": "SimpleHtmlExporter,SimpleExcelExporter"
  • "paddleOcrOptions":  
    •  "--lang", "en" - Sets the language for OCR recognition to English    
    •  "--text_det_unclip_ratio", "0.9" - Sets how much to expand text detection boxes   
    • "--use_doc_unwarping", "false" - Disables document unwarping correction                 

Input PDF Document:

Output HTML table rendering result (part):

PDFBOX_SAMPLE_DOCSET

Purpose: Demonstrates processing of readable or text-based PDF documents using PDFBox for direct text extraction, bypassing image-based OCR when possible.

PDFBOX JSON Settings

Below you can find an example of JSON settings provided in the PDFBOX_SAMPLE_DOCSET Document Set Details:

PDFBOX_SAMPLE_DOCSET JSON settings example
{
	"bucket": "data",
	"ocrType": "pdfbox",
	"pdfBoxDpi": 250,
	"debug": [
		"images"
	],
	"workspaceGroup": "Default"
}

Input readable PDF Document:

Output PDFBox result:

AUTO_OCR

Purpose:
The AUTO OCR document set demonstrates an intelligent, rules-based OCR workflow that automatically analyzes incoming files to determine their content type and selects the most appropriate processing engine. The system evaluates file characteristics such as text density, image coverage, number of images, and pages analyzed, as well as the file extension, to determine whether a document is text-based, image-based, hybrid, or belongs to a specific format category (JPG, TXT, XLSX, DOCX).

The AUTO OCR configuration uses a set of detection rules followed by engine-specific configurations to ensure that each document type is routed through the most efficient OCR pipeline.

Detection & Routing Logic Based on Rules

AUTO OCR analyzes the specified number of pages of an incoming document and applies the rules in the order listed. Once a rule matches, the corresponding configuration defines how the document is processed.

Here is an example of AUTO OCR JSON settings:

AUTO OCR JSON settings example
{
	"bucket": "data",
	"ocrType": "auto_ocr",
	"autoOcr": {
		"max_pages_to_analyze": 10,
		"rules": [
			{
				"jpg_file": "/.*\.(jpg|jpeg)$/.test(fileName)"
			},
			{
				"txt_file": "/.*\.txt$/.test(fileName)"
			},
			{
				"xlsx_file": "/.*\.(xlsx|xls)$/.test(fileName)"
			},
			{
				"docx_file": "/.*\.(docx|doc)$/.test(fileName)"
			},
			{
				"image_based": "avgTextDensity < 0.1 && totalImages > 0"
			},
			{
				"image_based": "avgImageCoverage > 0.7 && avgTextPerPage < 100"
			},
			{
				"hybrid": "avgTextDensity >= 0.3 && avgImageCoverage > 0.3 && totalImages > 0"
			},
			{
				"text_based": "avgTextDensity >= 0.5 && avgTextPerPage >= 100"
			},
			{
				"default": "true"
			}
		],
		"configs": {
			"txt_file": {
				"ocrType": "pdfbox"
			},
			"xlsx_file": {
				"ocrType": "pdfbox"
			},
			"docx_file": {
				"ocrType": "pdfbox"
			},
			"default": {
				"ocrType": "paddleocr",
				"gsPdfPageSplitter": true,
				"gsDpi": 180
			},
			"image_based": {
				"ocrType": "tesseract"
			},
			"hybrid": {
				"ocrType": "paddleocr",
				"gsPdfPageSplitter": true,
				"gsDpi": 180
			},
			"text_based": {
				"ocrType": "pdfbox"
			},
			"jpg_file": {
				"ocrType": "tesseract"
			}
		}
	},
	"tesseractOptions": [
		"-l",
		"eng",
		"--psm",
		"12",
		"--oem",
		"3",
		"--dpi",
		"180"
	],
	"paddleOcrOptions": [
		"--lang",
		"ru",
		"--text_det_thresh",
		"0.4",
		"--text_det_unclip_ratio",
		"0.8",
		"--use_doc_unwarping",
		"false",
		"--use_doc_orientation_classify",
		"false"
	],
	"imageMagickOptions": [
		"-units",
		"PixelsPerInch",
		"-density",
		"180",
		"${source}",
		"-background",
		"white",
		"-alpha",
		"remove",
		"-deskew",
		"40%",
		"-quality",
		"100",
		"-resample",
		"180"
	],
	"debug": [
		"keepFiles",
		"tesseract",
		"paddleocr",
		"pdfbox",
		"im"
	],
	"mlConfiguration": {},
	"workspaceGroup": "Default"
}

File-Type-Based Routing

These rules are specified before content analysis rules and force a specific engine:

  • JPG images → processed using Tesseract

  • TXT files → Converted to PDF with LibreOffice and processed directly with PDFBox (no OCR needed)

  • XLS/XLSX files → Converted to PDF with LibreOffice and processed with PDFBox

  • DOC/DOCX files → Converted to PDF with LibreOffice and processed with PDFBox

Content-Based Routing

If the file type does not match the extension rules, AUTO OCR evaluates document content characteristics:

Path 1 - Text-Based Documents

Condition:  avgTextDensity ≥ 0.5 and avgTextPerPage ≥ 100

Action: → Routed to PDFBox (direct text extraction, no OCR)

This path handles readable PDF documents.

Path 2 - Image-Based Documents

Conditions (either one qualifies): avgTextDensity < 0.1 and totalImages > 0, or avgImageCoverage > 0.7 and avgTextPerPage < 100

Action: → Routed to Tesseract OCR (with ImageMagick preprocessing)

This path is optimized for scanned or photo-like documents where OCR is required as well as quality improvement preprocessing.

Path 3 - Hybrid Documents

Condition:

  • avgTextDensity ≥ 0.3
  • avgImageCoverage > 0.3
  • totalImages > 0

Action: → Routed to PaddleOCR with:

  • Ghostscript PDF page splitting (gsPdfPageSplitter: true)
  • DPI assignment (gsDpi: 180)

This path handles documents containing a mix of readable text, embedded images, charts, or inconsistent page structure.

Default Path

If no rule matches: Routed to PaddleOCR (with Ghostscript preprocessing)

This ensures that any unclassified or unusual document still receives high-quality OCR processing.

Summary of Processing Engines Used

ScenarioEngine
Text-based documentsPDFBox
Image-based documentsTesseract
Hybrid documentsPaddleOCR + Ghostscript
JPG filesTesseract
TXT / XLS / XLSX / DOC / DOCXPDFBox
DefaultPaddleOCR + Ghostscript

OCR_SAMPLE_TEXTCLEANER Document Set 

In this example textcleaner script is run on a set of text documents images to remove noise and clean the text background.

JSON Settings for TEXTCLEANER script

Below you can find an example of JSON settings for TEXTCLEANER script provided in the Document Set Details. 

TEXTCLEANER script JSON settings example
"imagePostprocessScriptsBucket": "data/ocr_sample/scripts",
	"imagePostprocessScripts": {
	"textcleaner": [
		"-g",
		"-e",
		"stretch",
		"-f",
		"25",
		"-o",
		"15",
		"-t",
		"30",
		"-u",
		"-s",
		"1",
		"-T",
		"-p",
		"20"
	]
	}

TEXTCLEANER script JSON settings contain:

  • "imagePostprocessScriptsBucket": "data/ocr_sample/scripts"

    • This specifies the directory where the image preprocessing scripts are stored. 
  • "imagePostprocessScripts":

    • This section defines the specific image preprocessing scripts and their parameters.
  • "textcleaner":

    • The name of the preprocessing script being utilized. 
  • Parameters for "textcleaner" script:

    • "-g": converts the image to grayscale.
    • "-e stretch": enables automatic image brightness enhancement.
    • "-f 25": sets the filter radius to 25 pixels.
    • "-o 15": sets the offset value to 15.
    • "-t 30": sets the smoothing threshold value to 30.
    • "-u": applies deskew to the image.
    • "-s 1": sets the sharpness amount to 1.
    • "-T": enables image trimming.
    • "-p 20": sets the border pad amount to 20.

TEXTCLEANER script results example

Example of a document image before TEXTCLEANER script execution:

Example of a document image after TEXTCLEANER script execution:

OCR_SAMPLE_UNPERSPECTIVE Document Set 

In this example a sequence of otsuthresh and unperspective scripts is run on a set of document images to remove pespective distortions.

JSON Settings for OTSUTHRESH and UNPERSPECTIVE scripts

Below you can find an example of JSON settings for OTSUTHRESH and UNPERSPECTIVE scripts provided in the Document Set Details. 

OTSUTHRESH and UNPERSPECTIVE scripts JSON settings example
"imagePostprocessScriptsBucket": "data/ocr_sample/scripts",
	"imagePostprocessScripts": {
	"otsuthresh": [],
	"unperspective": [
		"-C",
		"black",
		"-i",
		"save",
		"-A",
		"12",
		"-s",
		"5",
		"-t",
		"10",
		"-B",
		"1",
		"-d",
		"h",
		"-a",
		"0.79"
	]
	}
  • "imagePostprocessScriptsBucket": "data/ocr_sample/scripts"

    • This specifies the directory where the image preprocessing scripts are stored. 
  • "imagePostprocessScripts":

    • This section defines the specific image preprocessing scripts and their parameters.
  • "otsuthresh":

    • This is the name of the first preprocessing script, which is "otsuthresh."
  • Parameters for "otsuthresh" script:

    • This script is invoked without any additional parameters.
  • "unperspective":

    • This is the name of the second post-processing script, which is "unperspective."
  • Parameters for "unperspective" script:

    • "-C black": sets the background color to black.
    • "-i save": saves intermediate images during processing.
    • "-A 12": sets the area threshold for connected components filtering to 12 degrees.
    • "-s 5": sets the smoothing amount to remove false peaks to 5.
    • "-t 10": sets the threshold value for removing false peaks to 10.
    • "-B 1": sets the blurring amount to 1.
    • "-d h": sets the output dimensions to input image height.
    • "-a 0.79": sets the desired width/height aspect ratio to 0.79.

OTSUTHRESH and UNPERSPECTIVE scripts results example

Example of a document image before a sequence of OTSUTHRESH and UNPERSPECTIVE scripts execution:

Example of a document image after a sequence of OTSUTHRESH and UNPERSPECTIVE scripts execution:


To read more about OCR image processing pipeline, see Digitizing Documents (OCR).

To explore the techniques of OCR quality analysis and improvement, see OCR Tuning Guide