Tesseract
In this section:
Description
According to the page of Tesseract, Tesseract is an open-source optical character recognition (OCR) engine, available under the Apache 2.0 license.
Available Versions
tesseract/4.00.00alpha (default)
Loading the Module
# Load Tesseract
module load tesseract/4.00.00alpha
# Verify installation
tesseract --version
# Available variables
echo $TESSDATA # Directory with training data
echo $LEPTONICA # Path to Leptonica
Note
Tesseract on GridUnesp was installed with a dependency on Leptonica/1.75.3, which includes tools for image format conversion.
PDF to Image Conversion
Leptonica includes tools for converting PDF files to image formats processable by Tesseract.
#!/bin/bash
#SBATCH -J convert_pdf
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 02:00:00
#SBATCH --mem=4G
export INPUT="document.pdf"
export OUTPUT="pages/"
module load tesseract/4.00.00alpha
mkdir -p pages
# Convert PDF to image format (using Leptonica)
# The convertfilestopdf or convertformat commands can be used
$LEPTONICA/bin/convertformat document.pdf pages/page.png
Basic Job Submission
#!/bin/bash
#SBATCH -J tesseract
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 23:00:00
#SBATCH --mem=4G
export INPUT="$TESSDATA"
export OUTPUT="out.txt"
module load tesseract/4.00.00alpha
job-nanny tesseract --tessdata-dir $TESSDATA -l heb hebrew.png out
Batch Image Processing
#!/bin/bash
#SBATCH -J tesseract_batch
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 48:00:00
#SBATCH --mem=8G
export INPUT="images/"
export OUTPUT="text/"
module load tesseract/4.00.00alpha
mkdir -p text
# Process all images
for img in images/*.png images/*.jpg images/*.tiff; do
if [ -f "$img" ]; then
base=$(basename "$img" | sed 's/\.[^.]*$//')
echo "Processing $img ..."
job-nanny tesseract --tessdata-dir $TESSDATA -l por "$img" "text/${base}"
fi
done
Job Array for Multiple Images
#!/bin/bash
#SBATCH -J tesseract_array
#SBATCH --array=1-50
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 12:00:00
#SBATCH --mem=4G
IMAGES=($(ls images/*.png images/*.jpg 2>/dev/null))
IMAGE=${IMAGES[$SLURM_ARRAY_TASK_ID-1]}
BASE=$(basename "$IMAGE" | sed 's/\.[^.]*$//')
export INPUT="$IMAGE"
export OUTPUT="text/${BASE}.txt"
module load tesseract/4.00.00alpha
mkdir -p text
job-nanny tesseract --tessdata-dir $TESSDATA -l por "$IMAGE" "text/${BASE}"
Multiple Languages
#!/bin/bash
#SBATCH -J tesseract_langs
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 24:00:00
#SBATCH --mem=4G
export INPUT="document.tiff"
export OUTPUT="document_*.txt"
module load tesseract/4.00.00alpha
# Portuguese
job-nanny tesseract --tessdata-dir $TESSDATA -l por document.tiff document_por
# English
job-nanny tesseract --tessdata-dir $TESSDATA -l eng document.tiff document_eng
# Spanish
job-nanny tesseract --tessdata-dir $TESSDATA -l spa document.tiff document_spa
# Multiple languages
job-nanny tesseract --tessdata-dir $TESSDATA -l por+eng+spa document.tiff document_multi
Advanced Settings
#!/bin/bash
#SBATCH -J tesseract_advanced
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 24:00:00
#SBATCH --mem=8G
export INPUT="document.tiff"
export OUTPUT="document_advanced"
module load tesseract/4.00.00alpha
# Create configuration file
cat > config.txt << 'EOF'
tessedit_char_whitelist 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
tessedit_pageseg_mode 6
textord_heavy_nr 1
edges_max_children_per_outline 40
EOF
# Run with custom settings
job-nanny tesseract --tessdata-dir $TESSDATA -l por \
document.tiff document_advanced \
config.txt
# Page segmentation modes:
# 0 = Orientation and script detection
# 1 = Automatic segmentation with OSD
# 2 = Automatic segmentation without OSD
# 3 = Fully automatic (default)
# 4 = Single column of text of variable sizes
# 5 = Single uniform block of vertically aligned text
# 6 = Single uniform block of text (recommended for documents)
# 7 = Single text line
# 8 = Single word
# 9 = Single word in a circle
# 10 = Single character
PDF Processing with Leptonica
Leptonica, available as a Tesseract dependency, offers tools for PDF conversion.
#!/bin/bash
#SBATCH -J pdf_to_text
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 48:00:00
#SBATCH --mem=16G
export INPUT="document.pdf"
export OUTPUT="text/"
module load tesseract/4.00.00alpha
mkdir -p text pages
# Convert PDF to images using Leptonica
# The convertformat executable can convert between various formats
$LEPTONICA/bin/convertformat document.pdf pages/page.png
# OCR on each page
for page in pages/*.png; do
base=$(basename "$page" .png)
job-nanny tesseract --tessdata-dir $TESSDATA -l por \
"$page" "text/${base}"
done
# Combine texts
cat text/*.txt > document_complete.txt
echo "OCR completed. Result in document_complete.txt"
Available Leptonica Tools
Leptonica provides several command-line tools for image manipulation:
# List available tools
ls $LEPTONICA/bin/
# Main tools:
# convertformat - Converts between image formats (PNG, TIFF, JPEG, etc.)
# convertfilestopdf - Converts images to PDF
# convertfilestops - Converts images to PostScript
# converttopdf - Converts to PDF
# fileinfo - Information about image files
Example Conversion with Leptonica
#!/bin/bash
#SBATCH -J convert_images
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 02:00:00
#SBATCH --mem=4G
export INPUT="scan.tiff"
export OUTPUT="scan.png"
module load tesseract/4.00.00alpha
# Convert TIFF to PNG
$LEPTONICA/bin/convertformat scan.tiff scan.png
# Convert to PDF (multiple images)
$LEPTONICA/bin/convertfilestopdf image1.png image2.png document.pdf
References
Tesseract documentation: https://tesseract-ocr.github.io/tessdoc/
Training data: https://github.com/tesseract-ocr/tessdata
Leptonica: http://www.leptonica.org/
See also
Running Simulations - How to submit jobs