Package: pero-ocr
pero-ocrThe package provides a full OCR pipeline including text paragraph detection,  text line detection, text transcription, and text refinement using a language model.
The package can be used as a command line application or as a python package which provides a document processing class and a class which represents document page content.Please citeIf you use pero-ocr, please cite:O Kodym, M Hradiš: Page Layout Analysis System for Unconstrained Historic Documents. ICDAR, 2021.M Kišš, K Beneš, M Hradiš: AT-ST: Self-Training Adaptation Strategy for OCR in Domains with Limited Transcriptions. ICDAR, 2021.J Kohút, M Hradiš: TS-Net: OCR Trained to Switch Between Text Transcription Styles. ICDAR, 2021.Running stuffScripts (as well as tests) assume that it is possible to importpero_ocrand its components.For the current shell session, this can be achieved by settingPYTHONPATHup:export PYTHONPATH=/path/to/the/repo:$PYTHONPATHAs a more permanent solution, a very simplisticsetup.pyis prepared:python setup.py developBeware that thesetup.pydoes not promise to bring all the required stuff, e.g. setting CUDA up is up to you.Pero can be later removed from your Python distribution by running:python setup.py develop --uninstallAvailable modelsGeneral layout analysis (printed and handwritten) with european printed OCR specialized to czech newspapers can bedownloaded here. The OCR engine is suitable for most european printed documents. It is specialized for low-quality czech newspapers digitized from microfilms, but it provides very good results for almast all types of printed documents in most languages. If you are interested in processing printed fraktur fonts, handwritten documents or medieval manuscripts, feel free to contact the authors. The newest OCR engines are available atpero-ocr.fit.vutbr.cz. OCR engines are available also through API runing atpero-ocr.fit.vutbr.cz/api,github repository.Command line applicationA command line application is ./user_scripts/parse_folder.py. It is able to process images in a directory using an OCR engine. It can render detected lines in an image and provide document content in Page XML and ALTO XML formats. Additionally, it is able to crop all text lines as rectangular regions of normalized size and save them into separate image files.Running command line application in containerA docker container can be built from the sourcecode to run scripts and programs based on the pero-ocr. Example of running theparse_folder.pyscript to generate page-xml files for images in input directory:dockerrun--rm--tty--interactive\--volumepath/to/input/dir:/input\--volumepath/to/output/dir:/output\--volumepath/to/ocr/engine:/engine\--gpusall\pero-ocr/usr/bin/python3user_scripts/parse_folder.py\--config/engine/config.ini\--input-image-path/input\--output-xml-path/outputBe sure to use container internal paths for passed in data in the command. All input and output data locations have to be passed to container via--volumeargument due to container isolation. Seedocker run command referencefor more information.Container can be built like this:dockerbuild-fDockerfile-tpero-ocr.Integration of the pero-ocr python moduleThis example shows how to directly use the OCR pipeline provided by pero-ocr package. This shows how to integrate pero-ocr into other applications. Class PageLayout represents content of a single document page and can be loaded from Page XMl and exported to Page XML and ALTO XML formats. The OCR pipeline is represented by the PageParser class.importosimportconfigparserimportcv2importnumpyasnpfrompero_ocr.document_ocr.layoutimportPageLayoutfrompero_ocr.document_ocr.page_parserimportPageParser# Read config file.config_path="./config_file.ini"config=configparser.ConfigParser()config.read(config_path)# Init the OCR pipeline.# You have to specify config_path to be able to use relative paths# inside the config file.page_parser=PageParser(config,config_path=os.path.dirname(config_path))# Read the document page image.input_image_path="page_image.jpg"image=cv2.imread(input_image_path,1)# Init empty page content.# This object will be updated by the ocr pipeline. id can be any string and it is used to identify the page.page_layout=PageLayout(id=input_image_path,page_size=(image.shape[0],image.shape[1]))# Process the image by the OCR pipelinepage_layout=page_parser.process_page(image,page_layout)page_layout.to_pagexml('output_page.xml')# Save results as Page XML.page_layout.to_altoxml('output_ALTO.xml')# Save results as ALTO XML.# Render detected text regions and text lines into the image and# save it into a file.rendered_image=page_layout.render_to_image(image)cv2.imwrite('page_image_render.jpg',rendered_image)# Save each cropped text line in a separate .jpg file.forregioninpage_layout.regions:forlineinregion.lines:cv2.imwrite(f'file_id-{line.id}.jpg',line.crop.astype(np.uint8))ContributingWorking changes are expected to happen ondevelopbranch, so if you plan to contribute, you better check it out right during cloning:git clone -b develop git@github.com:DCGM/pero-ocr.git pero-ocrTestingCurrently, only unittests are provided with the code. Some of the code. So simply run your preferred test runner, e.g.:~/pero-ocr $ greenSimple regression testingRegression testing can be done bytest/processing_test.sh. Script calls containerizedparser_folder.pyto process input images and page-xml files and calls user suplied comparison script to compare outputs to example outputs suplied by user.PERO-OCRcontainer have to be built in advance to run the test, see 'Running command line application in container' chapter. Script can be called like this:shtest/processing_test.sh\--input-imagespath/to/input/image/directory\--input-xmlspath/to/input/page-xml/directory\--output-dirpath/to/output/dir\--configurationpath/to/ocr/engine/config.ini\--examplepath/to/example/output/data\--test-utilitypath/to/test/script\--test-outputpath/to/testscript/output/dir\--gpu-idsgpuidsfordockercontainerFirst 4 arguments are manadatory,--gpu-idsis preset by value 'all' which passes all gpus to the container. Test utility, example outputs and test output folder have to be set only if comparison of results should be performed. Test utility is expected to be path toeval_ocr_pipeline_xml.pyscript fromperorepository. Be sure to correctly set PYTHONPATH and install dependencies forperorepository for the utility to work. Other script can be used if takes the same arguments. In other cases output data can be of course compared manually after processing.