Amazon Textract Textractor

repository·master·Indexed 19 days ago

https://github.com/aws-samples/amazon-textract-textractor

A Python package and set of tools designed to simplify working with Amazon Textract. It includes amazon-textract-caller for high-level abstractions of text recognition, table extraction, and form processing; amazon-textract-helper for a CLI tool to interact with Textract APIs and visualize results; and amazon-textract-overlayer for retrieving and drawing bounding boxes on images and PDF documents.

Tokens
20.6K
Snippets
77
Records
93
Agent score
67%

What's inside amazon-textract-textractor

  1. Overview of Textractor capabilities

    master

    Textractor is a Python package designed to simplify working with four key Amazon Textract APIs: DocumentTextDetection, StartDocumentTextDetection, AnalyzeDocument, and StartDocumentAnalysis.

    It provides utilities to:

    • Call Textract services with reduced parameter overhead.
    • Convert JSON responses from Textract APIs into programmable objects.
    • Visualize entities directly on document images.
    • Export document data into compatible formats like Excel, CSV, and TXT.

    Key advanced features include:

    • Semantic Document Search.
    • Key-value querying using specific keys.
    • Table access using numpy indexing.
    • Detection of duplicated document entities.
    • Functionality available at both the Document and Page levels.
  2. Use the amazon-textract CLI

    master

    The amazon-textract CLI tool allows you to interact with Amazon Textract APIs (DetectDocumentText and AnalyzeDocument) directly from the terminal. You can provide input via an example document, a local file, an S3 URI, or STDIN. You can also request specific features like FORMS and TABLES, and apply overlays to visualize bounding boxes on images.

    usage: amazon-textract [-h] (--input-document INPUT_DOCUMENT | --example | --stdin) [--features {FORMS,TABLES} [{FORMS,TABLES} ...]]
                           [--pretty-print {WORDS,LINES,FORMS,TABLES} [{WORDS,LINES,FORMS,TABLES} ...]]
                           [--pretty-print-table-format {csv,plain,simple,github,grid,fancy_grid,pipe,orgtbl,jira,presto,pretty,psql,rst,mediawiki,moinmoin,youtrack,html,unsafehtml,latex,latex_raw,latex_booktabs,latex_longtable,textile,tsv}]
                           [--overlay {WORD,LINE,FORM,KEY,VALUE,TABLE,CELL} [{WORD,LINE,FORM,KEY,VALUE,TABLE,CELL} ...]]
                           [--pop-up-overlay-output] [--overlay-output-folder OVERLAY_OUTPUT_FOLDER] [--version] [--no-stdout] [-v | -vv]
  3. Navigate document hierarchy with Page, Line, and Word entities

    master

    Textractor models the document as a hierarchy of entities. You can traverse from the top-level Document down to individual characters or words:

    • Page: Represents a single page within a multi-page document.
    • Line: Represents a line of text.
    • Word: Represents an individual word.

    All these entities inherit from or are related to DocumentEntity and typically include spatial information via BoundingBox.

  4. Understand Document Entities in Textractor

    master

    Textractor provides programmatic classes to access information produced by Amazon Textract analysis APIs. The package maps Textract's recognized document entities into structured Python objects.

    Textract recognizes 6 primary document entities which are represented in Textractor as:

    • WORD
    • LINE
    • KEY_VALUE_SET
    • SELECTION_ELEMENT
    • TABLE
    • CELL

    These entities are contained within Document objects, allowing you to navigate the document structure (e.g., accessing pages, lines, or tables) using specialized entity classes.

  5. Use specialized entities for Expense and Identity documents

    master

    Textractor includes high-level entities for specific document types processed by Amazon Textract:

    Expense Documents:

    • ExpenseDocument: The top-level container for expense analysis.
    • Expense: Represents individual expense line items or fields.

    Identity Documents:

    • IdentityDocument: The top-level container for identity document analysis.
    • IdentityField: Represents specific fields within an identity document (e.g., Name, ID Number).
  6. How geometric area selection works

    master

    Instead of using hardcoded x,y coordinates, amazon-textract-geofinder uses the concept of an area.

    An area is defined by a bounding box (x_min, y_min, x_max, y_max). You can dynamically create these areas by finding specific words or phrases in the document. This allows you to associate key/value pairs with specific sections (e.g., associating all fields found between the 'Patient Information' header and the 'Emergency Contact' header as 'Patient' fields).

  7. Pretty print Textract output

    master

    Use the --pretty-print flag to format the JSON output into human-readable text for WORDS, LINES, FORMS, or TABLES. For tables, you can specify a format using --pretty-print-table-format (e.g., csv, grid, html).

    # Pretty print tables
    amazon-textract --example --features TABLES --pretty-print TABLES
    
    # Pretty print both forms and tables
    amazon-textract --example --features FORMS TABLES --pretty-print FORMS TABLES