Tools,

Can Tesseract detect the language? Tested on 162 scans

85%

languages right: the most confident of 14 models

Get the code

No. It names the script, not the language

Tesseract has no language check. Its orientation and script check, --psm 0, prints a Script line such as Arabic, Han or Latin. One script holds many languages.

Proofread Wikisource scans where it named the right script:

Scans Pages Right
Arabic 44 44
Chinese, simplified, in lines 36 35
Chinese, traditional, in columns 36 20
German in Fraktur type 46 46

On the German pages, Fraktur and Latin both count: each leads to a Latin alphabet model.

What it said when it was wrong
  • Chinese columns: Latin on 10 pages, Cyrillic on 5, Fraktur on 1.
  • One simplified Chinese page: Japanese.
  • Script confidence gave no warning. Every right Han answer scored 1.43 or less. Wrong ones went up to 7.08.
  • It also asked to turn 4 of these upright pages. Turns are on the orientation page.

Twice the size fixed 7 pages

145 right at the scan's own size

152 right at 2 times the size

Small pages in columns failed most: 15 of 31 under 1,000 px wide were right.

At 2 times the size, the columns went to 26 of 36.

The top answer finds German only

The top answer on the most viewed of 3 Stack Overflow questions I checked reads the page with eng, then runs langdetect on the text.

46 of 46 German pages right

0 of 116 Arabic and Chinese pages right

eng writes Arabic and Chinese as Latin letters, and langdetect called 34 of the 44 Arabic pages English.

langdetect on the proofread text

Even on the proofread text itself, it called 18 of 36 traditional Chinese pages Korean.

Let the most confident model win

Read the page with each model and keep the one with the highest mean word confidence. Languages right, of 162:

Way Right
eng, then langdetect 46
--psm 0, then the most confident model of that script 128
The most confident of all 14 models 137

A wrong script sends the second way to the wrong models. All 14 models took 7.0 to 24.6 s a page (median per set), so test one page, then read the file with the winner.

Where the most confident model was wrong
  • Arabic: fas (Persian) on 2 pages.
  • Simplified Chinese: chi_tra on 5.
  • Chinese columns: ara on 9, chi_sim_vert on 2, hin and fas on 1 each.
  • German in Fraktur: eng on 5.

The code, run on all 162 scans

One line prints the script:

tesseract scan.png - --psm 0

In Python, list only the models your pages might need. Each one is a full read.

import statistics

import pytesseract

LANGS = ["eng", "spa", "fra", "deu", "por", "rus", "ara", "hin",
         "chi_sim", "jpn", "chi_tra", "chi_tra_vert",
         "chi_sim_vert", "fas"]


def mean_conf(image, lang):
    d = pytesseract.image_to_data(
        image, lang=lang, output_type=pytesseract.Output.DICT
    )
    c = [float(x) for x, t in zip(d["conf"], d["text"])
         if float(x) >= 0 and t.strip()]
    return statistics.fmean(c) if c else 0.0


def detect_language(image):
    return max(LANGS, key=lambda lang: mean_conf(image, lang))


print(detect_language("scan.png"))

On the 162 scans, this code got the same 137 right. tesseract --list-langs shows your models.

How I tested it
  • Scans: proofread pages from my Arabic, Chinese and Fraktur tests, 500 to 2,444 px wide. Each page is upright and in one language.
  • Models: tessdata_fast 4.1.0. The 10 languages our Actor ships, plus chi_tra, both vertical Chinese files and fas.
  • One run per scan and model, default settings, Tesseract 5.5.4. The mean counts words with a confidence of 0 or more.
  • langdetect 1.0.9, with its seed set to 0.
  • Times: 12 runs at once, one CPU thread each.
  • pytesseract rounds each confidence down to a whole number. So on one scan the code chose eng over ara, a near tie between two wrong models.
What I did not test
  • English, Hindi, Russian, Japanese or Korean pages.
  • Pages with two languages on them.
  • Whole PDFs. Each scan was one page.
  • tessdata_best, and other detectors such as fastText.
  • Windows and Linux. One Mac.
Sources

Our Actor needs the language first

Our Apify Actor reads 10 languages, and you choose one per run. It does not detect the language. To pick one, run the code above on a page first, with only the Actor's 10 languages in LANGS.

Two ways to skip the work

Run it yourself. Our Apify Actor turns scanned PDFs into Markdown, tables included.

Try the Actor

Or send us the scan. Tack, our AI, does the job for $29, up to 50 pages. First page free.

Get one page free

Questions people ask

Can Tesseract detect the language automatically?

No. --psm 0 names the script only. To get a language, read with each model and keep the most confident one: 137 of 162 scans right.

What does Script confidence mean in Tesseract OSD?

How sure the check is of the script. It gave no warning here: every right Han answer scored 1.43 or less, and wrong ones went up to 7.08.

Does langdetect work on Tesseract output?

Only when the model fits the script. After eng, it named all 46 German pages and called 34 of 44 Arabic pages English.