Skip to content

Commit

Permalink
Add PAGE XML renderer / export (#4214)
Browse files Browse the repository at this point in the history
Add PAGE XML export and documentation.
To generate PAGE XML output just add 'page' to the tesseract command.

The output is outputname + '.page.xml' to avoid conflicts with ALTO export.

The output can be customized with the flags:
tessedit_create_page_polygon and tessedit_create_page_wordlevel.

Co-authored-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
JKamlah and stweil committed Apr 19, 2024
1 parent bae520e commit 577e8a8
Show file tree
Hide file tree
Showing 13 changed files with 1,220 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ set(TESSERACT_SRC
src/api/capi.cpp
src/api/renderer.cpp
src/api/altorenderer.cpp
src/api/pagerenderer.cpp
src/api/hocrrenderer.cpp
src/api/lstmboxrenderer.cpp
src/api/pdfrenderer.cpp
Expand All @@ -764,6 +765,7 @@ set(TESSERACT_CONFIGS
tessdata/configs/lstmbox
tessdata/configs/lstmdebug
tessdata/configs/makebox
tessdata/configs/page
tessdata/configs/pdf
tessdata/configs/quiet
tessdata/configs/rebox
Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ libtesseract_la_LDFLAGS += -version-info $(GENERIC_LIBRARY_VERSION)

libtesseract_la_SOURCES = src/api/baseapi.cpp
libtesseract_la_SOURCES += src/api/altorenderer.cpp
libtesseract_la_SOURCES += src/api/pagerenderer.cpp
libtesseract_la_SOURCES += src/api/capi.cpp
libtesseract_la_SOURCES += src/api/hocrrenderer.cpp
libtesseract_la_SOURCES += src/api/lstmboxrenderer.cpp
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Tesseract has **unicode (UTF-8) support**, and can **recognize [more than 100 la

Tesseract supports **[various image formats](https://tesseract-ocr.github.io/tessdoc/InputFormats)** including PNG, JPEG and TIFF.

Tesseract supports **various output formats**: plain text, hOCR (HTML), PDF, invisible-text-only PDF, TSV and ALTO.
Tesseract supports **various output formats**: plain text, hOCR (HTML), PDF, invisible-text-only PDF, TSV, ALTO and PAGE.

You should note that in many cases, in order to get better OCR results, you'll need to **[improve the quality](https://tesseract-ocr.github.io/tessdoc/ImproveQuality.html) of the image** you are giving Tesseract.

Expand Down
4 changes: 4 additions & 0 deletions doc/tesseract.1.asc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ OPTIONS

* *alto* -- Output in ALTO format ('OUTPUTBASE'`.xml`).
* *hocr* -- Output in hOCR format ('OUTPUTBASE'`.hocr`).
* *page* -- Output in PAGE format ('OUTPUTBASE'`.page.xml`).
The output can be customized with the flags:
page_xml_polygon -- Create polygons instead of bounding boxes (default: true)
page_xml_level -- Create the PAGE file on 0=linelevel or 1=wordlevel (default: 0)
* *pdf* -- Output PDF ('OUTPUTBASE'`.pdf`).
* *tsv* -- Output TSV ('OUTPUTBASE'`.tsv`).
* *txt* -- Output plain text ('OUTPUTBASE'`.txt`).
Expand Down
12 changes: 12 additions & 0 deletions include/tesseract/baseapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,18 @@ class TESS_API TessBaseAPI {
*/
char *GetAltoText(int page_number);

/**
* Make an XML-formatted string with PAGE markup from the internal
* data structures.
*/
char *GetPAGEText(ETEXT_DESC *monitor, int page_number);

/**
* Make an XML-formatted string with PAGE markup from the internal
* data structures.
*/
char *GetPAGEText(int page_number);

/**
* Make a TSV-formatted string from the internal data structures.
* page_number is 0-based but will appear in the output as 1-based.
Expand Down
17 changes: 17 additions & 0 deletions include/tesseract/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,23 @@ class TESS_API TessAltoRenderer : public TessResultRenderer {
bool begin_document;
};

/**
* Renders Tesseract output into a PAGE XML text string
*/
class TESS_API TessPAGERenderer : public TessResultRenderer {
public:
explicit TessPAGERenderer(const char *outputbase);

protected:
bool BeginDocumentHandler() override;
bool AddImageHandler(TessBaseAPI *api) override;
bool EndDocumentHandler() override;

private:
bool begin_document;
};


/**
* Renders Tesseract output into a TSV string
*/
Expand Down
8 changes: 8 additions & 0 deletions src/api/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ TessResultRenderer *TessAltoRendererCreate(const char *outputbase) {
return new tesseract::TessAltoRenderer(outputbase);
}

TessResultRenderer *TessPAGERendererCreate(const char *outputbase) {
return new tesseract::TessPAGERenderer(outputbase);
}

TessResultRenderer *TessTsvRendererCreate(const char *outputbase) {
return new tesseract::TessTsvRenderer(outputbase);
}
Expand Down Expand Up @@ -420,6 +424,10 @@ char *TessBaseAPIGetAltoText(TessBaseAPI *handle, int page_number) {
return handle->GetAltoText(page_number);
}

char *TessBaseAPIGetPAGEText(TessBaseAPI *handle, int page_number) {
return handle->GetPAGEText(page_number);
}

char *TessBaseAPIGetTsvText(TessBaseAPI *handle, int page_number) {
return handle->GetTSVText(page_number);
}
Expand Down
Loading

2 comments on commit 577e8a8

@nguyenq
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does capi.h need update as well?

@stweil
Copy link
Contributor

@stweil stweil commented on 577e8a8 Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's still missing. Thank you for your hint.

Please sign in to comment.