Package: ocrd-tesserocr
ocrd_tesserocrCrop, deskew, segment into regions / tables / lines / words, or recognize with tesserocrIntroductionThis package offersOCR-Dcompliantworkspace processorsfor (much of) the functionality ofTesseractvia its Python API wrappertesserocr. (Each processor is a parameterizable step in a configurableworkflowof theOCR-D functional model. There are usually various alternative processor implementations for each step. Data is represented withMETSandPAGE.)It includes image preprocessing (cropping, binarization, deskewing), layout analysis (region, table, line, word segmentation), script identification, font style recognition and text recognition.Most processors can operate on different levels of the PAGE hierarchy, depending on the workflow configuration. In PAGE, image results are referenced (read and written) viaAlternativeImage, text results viaTextEquiv, font attributes viaTextStyle, script via@primaryScript, deskewing via@orientation, cropping viaBorderand segmentation viaRegion/TextLine/Wordelements withCoords/@points.InstallationWith dockerThis is the best option if you want to run the software in a container.You need to haveDockerdocker pull ocrd/tesserocrTo run with docker:docker run -v path/to/workspaces:/data ocrd/tesserocr ocrd-tesserocrd-crop ...From PyPI and PPAThis is the best option if you want to use the stable, released version.NOTEocrd_tesserocr requiresTesseract >= 4.1.0. The Tesseract packages
bundled withUbuntu < 19.10are too old. If you are on Ubuntu 18.04 LTS,
please useAlexander Pozdnyakov's PPArepository,
which has up-to-date builds of Tesseract and its dependencies:sudoadd-apt-repositoryppa:alex-p/tesseract-ocr
sudoapt-getupdatesudoapt-getinstallpython3python3-piplibtesseract-devlibleptonica-devtesseract-ocrwget
pipinstallocrd_tesserocrFrom gitUse this option if you want to change the source code or install the latest, unpublished changes.We strongly recommend to usevenv.gitclonehttps://github.com/OCR-D/ocrd_tesserocrcdocrd_tesserocr# install Tesseract:sudomakedeps-ubuntu# or manually from git or via ocrd_all# install tesserocr and ocrd_tesserocr:makedeps# or pip install -r requirementsmakeinstall# or pip install .ModelsTesseract comes with synthetically trained models for languages (tesseract-ocr-{eng,deu,frk,...}or scripts (tesseract-ocr-script-{latn,frak,...}). In addition, various modelstrainedon scan data are available from the community.Since all OCR-D processors must resolve file/data resources
in astandardized way,
and we want to stay interoperable with standalone Tesseract
(which uses a single compile-timetessdatadirectory),ocrd-tesserocr-recognizeexpects the recognition models to be installed
in itsmoduleresource locationonly.
Themodulelocation is determined by the underlying Tesseract installation
(compile-timetessdatadirectory, or run-time$TESSDATA_PREFIXenvironment variable).
Other resource locations (data/system/cwd) will be ignored, and should not be used
when installing models with theResource Manager(ocrd resmgr download).For a full description of available commands for resource management, see:ocrd resmgr --help
ocrd resmgr list-available --help
ocrd resmgr download --help
ocrd resmgr list-installed --help(In previous versions, the resource locations of standalone Tesseract and the OCR-D wrapper were different.
If you already have models under$XDG_DATA_HOME/ocrd-resources/ocrd-tesserocr-recognize,
usually~/.local/share/ocrd-resources/ocrd-tesserocr-recognize, then consider moving them
to the new default underocrd-tesserocr-recognize -D,
usually/usr/share/tesseract-ocr/4.00/tessdata,oralternatively overriding the module directory
by settingTESSDATA_PREFIX=$XDG_DATA_HOME/ocrd-resources/ocrd-tesserocr-recognizein the environment.)Cf.OCR-D model guide.Models always use the filename suffix.traineddata, but are just loaded by their basename.
You will needat leastengandosdinstalled (even for segmentation and deskewing),
probably alsoLatinandFrakturetc.As of v0.13.1, you can configureocrd-tesserocr-recognizeto select modelsdynamicallysegment by segment,
either via custom conditions on the PAGE-XML annotation (presented as XPath rules),
or by automatically choosing the model with highest confidence.UsageFor details, see docstrings in the individual processors
andocrd-tool.jsondescriptions,
or simply--help.AvailableOCR-D processorsare:ocrd-tesserocr-crop(simplistic)setsBorderof pages and addsAlternativeImagefiles to the output fileGrpocrd-tesserocr-deskew(for skew and orientation; mindoperation_level)sets@orientationof regions or pages and addsAlternativeImagefiles to the output fileGrpocrd-tesserocr-binarize(Otsu – not recommended, unless already binarized and usingtiseg)addsAlternativeImagefiles to the output fileGrpocrd-tesserocr-recognize(optionally including segmentation; mindsegmentation_levelandtextequiv_level)addsTextRegions,TableRegions,ImageRegions,MathsRegions,SeparatorRegions,NoiseRegions,ReadingOrderandAlternativeImagetoPageand sets their@orientation(optionally)addsTextRegions toTableRegions and sets their@orientation(optionally)addsTextLines toTextRegions (optionally)addsWords toTextLines (optionally)addsGlyphs toWords (optionally)addsTextEquivocrd-tesserocr-segment(all-in-one segmentation – recommended; delegates torecognize)addsTextRegions,TableRegions,ImageRegions,MathsRegions,SeparatorRegions,NoiseRegions,ReadingOrderandAlternativeImagetoPageand sets their@orientationaddsTextRegions toTableRegions and sets their@orientationaddsTextLines toTextRegionsaddsWords toTextLinesaddsGlyphs toWordsocrd-tesserocr-segment-region(only regions – with overlapping bboxes; delegates torecognize)addsTextRegions,TableRegions,ImageRegions,MathsRegions,SeparatorRegions,NoiseRegions andReadingOrdertoPageand sets their@orientationocrd-tesserocr-segment-table(only table cells; delegates torecognize)addsTextRegions toTableRegionsocrd-tesserocr-segment-line(only lines – from overlapping regions; delegates torecognize)addsTextLines toTextRegionsocrd-tesserocr-segment-word(only words; delegates torecognize)addsWords toTextLinesocrd-tesserocr-fontshape(only text style – via Tesseract 3 models)addsTextStyletoWordsThe text region@types detected are (from Tesseract'sPolyBlockType):paragraph: normal block (aligned with others in the column)floating: unaligned block (is in a cross-column pull-out region)heading: block thatspans more than one columncaption: block fortext that belongs to an imageIf you are unhappy with these choices, then consider post-processing
with a dedicated custom processor in Python, or by modifying the PAGE files directly
(e.g.xmlstarlet ed --inplace -u '//pc:TextRegion/@type[.="floating"]' -v paragraph filegrp/*.xml).All segmentation is currently done asbounding boxesonly by default,
i.e. without precise polygonal outlines. For dense page layouts this means
that neighbouring regions and neighbouring text lines may overlap a lot.
If this is a problem for your workflow, try post-processing like so:after line segmentation: useocrd-cis-ocropy-resegmentfor polygonalization,
orocrd-cis-ocropy-clipon the line levelafter region segmentation: useocrd-segment-repairwithplausibilize(andsanitizeafter line segmentation)It also means that Tesseract should be allowed to segment across multiple hierarchy levels
at once, to avoid introducing inconsistent/duplicate text line assignments in text regions,
or word assignments in text lines. Hence,preferocrd-tesserocr-recognizewithsegmentation_level=regionoverocrd-tesserocr-segmentfollowed byocrd-tesserocr-recognize,if you want to do all in one with Tesseract,preferocrd-tesserocr-recognizewithsegmentation_level=lineoverocrd-tesserocr-segment-linefollowed byocrd-tesserocr-recognize,if you want to do everything but region segmentation with Tesseract,preferocrd-tesserocr-segmentoverocrd-tesserocr-segment-regionfollowed by (ocrd-tesserocr-segment-tableand)ocrd-tesserocr-segment-line,if you want to do everything but recognition with Tesseract.However, you can also runocrd-tesserocr-segment*andocrd-tesserocr-recognizewithshrink_polygons=Trueto getpolygonsby post-processing each segment,
shrinking to the convex hull of all its symbol outlines.TestingmaketestThis downloads some test data fromhttps://github.com/OCR-D/assetsunderrepo/assets,
and runs some basic test of the Python API as well as the CLIs.SetPYTEST_ARGS="-s --verbose"to see log output (-s) and individual test results (--verbose).