Skip to main content

Extract text from scanned images with Tesseract

Source on GitHub: the workflows/scans-to-text folder, with its README and its workflow file.

Reads the text of scanned pages with Tesseract OCR. Each scan is fetched and read in its own tasks, all at the same time: once as plain text, and once as a table of words with Tesseract's confidence in each. A last step gathers the text and each scan's mean word confidence into one Parquet file, so the scans Tesseract struggled with stand out.

StepTask definitionCompute
Make the output foldersswaleio/bashcpu-4
For each scan: fetch itswaleio/http-requestcpu-4
For each scan: read its text, and its words with confidences, in parallelswaleio/tesseractcpu-4 each
Gather text and confidences into scans.parquetswaleio/duckdbcpu-4
Upload to the Hugging Face Hub, when hub_repo is setswaleio/hf-uploadcpu-4

The Compute column gives each step's compute type, the compute its tasks are allocated.

Every step is its own task.

Inputs​

InputDefaultDescription
image_urlstwo sample scans from Tesseract's test dataThe scans, as a JSON array of URLs.
languageeng+deu+fraTesseract language codes, joined with +.
hub_repoemptyHugging Face dataset repo to upload the dataset to, as data/train.parquet.

The upload reads the secret hf_token.

Output​

scans.parquet on the workspace, with url, text and mean_word_confidence (0 to 100) for each scan, and Tesseract's .txt and .tsv files in text/.

Run it​

You need the CLI, logged in to your account, and a project to add the workflow to; Getting started sets them up.

  1. Save the file under The workflow file as workflow.yaml, and add it to a project:

    swale workflow create --project-name my-project --file workflow.yaml

    The command prints Workflow created: followed by the workflow's ID.

  2. The workflow reads the secret hf_token. Create it as a project secret, or as an account secret that allows the project, as Use secrets in workflows shows.

  3. Start a run with the workflow's ID:

    swale workflow run start --project-name my-project --workflow-id <workflow-id>

    Each input you leave out takes its default from Inputs, and --input <name>=<value> gives it another value.

Create and manage workflows covers the other ways to add a workflow, Start and manage workflow runs how to follow a run, and View and stream task logs how to read its tasks' logs.

The workflow file​

Workflow definition syntax describes each key in it.

workflow.yaml
name: Extract text from scanned images with Tesseract
description: Reads the text of a list of scanned page images with Tesseract, one task per image, and collects the text and word confidences into one Parquet dataset.
compute_type: cpu-4

inputs:
image_urls:
description: The scans to read, as a JSON array of URLs.
default: '["https://raw.githubusercontent.com/tesseract-ocr/test/232ff181c66516116ec0e84c4963f70de15050fd/testing/phototest.tif", "https://raw.githubusercontent.com/tesseract-ocr/test/232ff181c66516116ec0e84c4963f70de15050fd/testing/eurotext.tif"]'
language:
description: Tesseract language code, or several joined with +.
default: eng+deu+fra
hub_repo:
description: Hugging Face dataset repo to upload the dataset to, as namespace/name. Leave empty to keep it on the workspace only.
default: ""

entry_point: main

blocks:
main:
tasks:
make_dirs:
name: Make output folders
uses: swaleio/bash
args:
script: mkdir -p "$WORKFLOW_STORAGE/scans" "$WORKFLOW_STORAGE/text"

each_scan:
name: Read each scan
start_on:
- make_dirs
for_each:
items: ${{inputs.image_urls}}
block: read_one

collect:
name: Collect into a dataset
uses: swaleio/duckdb
start_on:
- each_scan
args:
sql: |
CREATE TABLE urls AS
SELECT unnest(from_json($$${{inputs.image_urls}}$$, '["VARCHAR"]')) AS url,
generate_subscripts(from_json($$${{inputs.image_urls}}$$, '["VARCHAR"]'), 1) - 1 AS item;
-- Tesseract's TSV has one row per word, with its confidence from 0 to 100.
CREATE TABLE words AS
SELECT regexp_extract(filename, '(\d+)\.tsv$', 1)::INT AS item, conf, text
FROM read_csv('${{env.WORKFLOW_STORAGE}}/text/*.tsv', delim = '\t', quote = '', header = true,
filename = true, ignore_errors = true)
WHERE conf >= 0;
CREATE TABLE scans AS
SELECT u.url, t.content AS text,
(SELECT round(avg(conf), 1) FROM words w WHERE w.item = u.item) AS mean_word_confidence
FROM urls u
JOIN read_text('${{env.WORKFLOW_STORAGE}}/text/*.txt') t
ON regexp_extract(t.filename, '(\d+)\.txt$', 1)::INT = u.item
ORDER BY u.item;
SELECT url, length(text) AS characters, mean_word_confidence FROM scans;
COPY scans TO '${{env.WORKFLOW_STORAGE}}/scans.parquet' (FORMAT parquet);

publish:
name: Upload when asked
start_on:
- collect
if:
condition: ${{inputs.hub_repo}}
true: upload

read_one:
tasks:
fetch:
name: Fetch scan
uses: swaleio/http-request
args:
url: ${{tasks.each_scan.for_each.item}}
expected_status: "200"
response_path: ${{env.WORKFLOW_STORAGE}}/scans/${{tasks.each_scan.for_each.index}}
read_text:
name: Read text
uses: swaleio/tesseract
start_on:
- fetch
args:
image: ${{env.WORKFLOW_STORAGE}}/scans/${{tasks.each_scan.for_each.index}}
output_base: ${{env.WORKFLOW_STORAGE}}/text/${{tasks.each_scan.for_each.index}}
language: ${{inputs.language}}
read_words:
name: Read words with confidences
uses: swaleio/tesseract
start_on:
- fetch
args:
image: ${{env.WORKFLOW_STORAGE}}/scans/${{tasks.each_scan.for_each.index}}
output_base: ${{env.WORKFLOW_STORAGE}}/text/${{tasks.each_scan.for_each.index}}
language: ${{inputs.language}}
format: tsv

upload:
tasks:
upload_dataset:
name: Upload to the Hugging Face Hub
uses: swaleio/hf-upload
args:
repo: ${{inputs.hub_repo}}
repo_type: dataset
source: ${{env.WORKFLOW_STORAGE}}/scans.parquet
path_in_repo: data/train.parquet
token: ${{secrets.hf_token}}

Licenses of the defaults​