Package: pytesstrain
Python tools for Tesseract OCR trainingTraining tools forTesseract OCR.InstallationInstall using pip:pip install pytesstrainThis will also install Python packagespytesseract(used for running Tesseract)
andeditdistance(used for calculation of error rates).Getting startedThis package contains tools for specific problems:text2image is crashing (issue #1781 @ Tesseract OCR)The text2image tool crashes, if text lines are too long. As stated in the issue above,
rewrapping text lines to smaller length is the official workaround for this problem.
For example, to reduce line length to 35 characters at most, runrewrapcorpus.txtcorpus-35.txt35Creating dictionary data from corpus fileIn case you do not have a dictionary file for the training language, you might want
to create one from the corpus file. To create dictionary file for the languagelang, runcreate_dictdata-llang-icorpus.txt-d./langdata/langThis tool creates following files:lang.training_text (copy of the corpus file)lang.wordlist (dictionary)lang.word.bigrams (word bigrams)lang.training_text.bigram_freqs (character bigram frequencies)lang.training_text.unigram_freqs (character frequencies)The filelang.wordlist.freqis usually created by training tools, such astesstrain.shand the likewise,
so there is no need to create it withcreate_dictdata.Language metricsThe toollanguage_metricsruns Tesseract OCR over images of random word sequences, which are created
out of the supplied wordlist, and calculatesmedianmetrics (currently CER and WER) from the results.
It enables you to assess the quality of your.traineddatafile.To calculate metrics for the languagelangwith fontsArialandCourierusing wordlist filelang.wordlist,
runlanguage_metrics-llang-wlang.wordlist--fontsArial,CourierCreating unicharambigs fileThere are two tools in this package, which enable automatic creation of an unicharambigs file.The first tool,collect_ambiguities, compares the recognised text with the reference text and
extracts smallest possible differences as error and correction pairs, and stores them sorted by
frequency of occurrence in a JSON file. You may look at the ambiguities by yourself before
converting them tounicharambigsfile with the second tool.The second tool,json2unicharambigs, takes the intermediate JSON file and puts the ambiguities
into theunicharambigsfile. The resulting file hasv2format. You may limit the ambiguities,
which go into theunicharambigsfile, with additional command-line switches.To create the filelang.unicharambigsfor the languagelangusing wordlist filelang.wordlist,
runcollect_ambiguities-llang-wlang.wordlist--fontsArial,Courier-oambigs.json
json2unicharambigs--modesafe--mandatory_onlyambigs.jsonlang.unicharambigsCreating ground truth filesTo help with training of Tesseract>=4, the toolcreate_ground_truthcreates single-line ground truth files
either from an input file or from a directory with.txtfiles (the tool searches for the latter ones recursively).To create ground truth files from a directorycorporain the directoryground-truth, runcreate_ground_truth--fontsArial,Couriercorporaground-truthAPI ReferenceThe main workhorse is the functionpytesstrain.train.run_test. There is also a parallel version,pytesstrain.train.run_tests, which uses a pool of threads to run the former function on multiple processors
simultaneously (using threads instead of processes for parallelisation is possible, because therun_testfunction starts processes itself and is thus I/O-bound).The subpackagetesseractsimply imports the packagepytesseract. The subpackagetext2imageimitates
the former one, but for thetext2imagetool instead oftesseract.The subpackagemetricscontains implementation of evaluation metrics, such as CER and WER. The subpackageutilshas
often-used, miscellaneous functions and the subpackageambigscontains ambiguity processing functions.Finally, the subpackageclicontains the console scripts.LicensePytesstrain is released underApache License 2.0.