{ "cells": [ { "cell_type": "markdown", "id": "0cb1a167", "metadata": {}, "source": [ "# Using Textract OCR\n", "\n", "If you only want to use the Amazon Textract OCR engine, you have to choose between the synchronous `DetectDocumentText` API and the asynchronous `StartDocumentTextDetection` API. The former will block until the OCR inference completes, while the latter will return a `job_id` that you can use to get the results later. \n", "\n", "## Installation\n", "\n", "To begin, install the `amazon-textract-textractor` package using pip.\n", "\n", "`pip install amazon-textract-textractor`\n", "\n", "There are various sets of dependencies available to tailor your installation to your use case. The base package will have sensible default, but you may want to install the PDF extra dependencies if your workflow uses PDFs with `pip install amazon-textract-textractor[pdfium]`. You can read more on extra dependencies [in the documentation](https://aws-samples.github.io/amazon-textract-textractor/installation.html)" ] }, { "cell_type": "markdown", "id": "1de98887", "metadata": {}, "source": [ "## Synchronous example\n", "\n", "This example assumes that you have set up your AWS credentials and have a default profile. If that is not the case see [this page](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html) to get started." ] }, { "cell_type": "code", "execution_count": 1, "id": "8df9f7a6", "metadata": {}, "outputs": [], "source": [ "from textractor import Textractor\n", "\n", "extractor = Textractor(profile_name=\"default\")\n", "# This path assumes that you are running the notebook from docs/source/notebooks\n", "document = extractor.detect_document_text(\"../../../tests/fixtures/form.png\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "d0d8379e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "This document holds the following data:\n", "Pages - 1\n", "Words - 259\n", "Lines - 74\n", "Key-values - 0\n", "Checkboxes - 0\n", "Tables - 0\n", "Identity Documents - 0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "document" ] }, { "cell_type": "markdown", "id": "c938812b", "metadata": {}, "source": [ "The next step will be dependent on your goal, if it's simply to get one long string of extracted OCR, you can use `document.text`." ] }, { "cell_type": "code", "execution_count": 4, "id": "c6d6ceac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\"Service You\\'ll Be\\nBragging\\n&\\nRoyal\\nAbout GUARANTEED!\"\\nYou have a full week to inspect your carpet.\\nIf a spot returns or if there is a concern we\\'ll return.\\nOnline@royalcarpetlincoln.com\\nCarpet & Upholstery Cleaning\\n5401 S. 20 St. Circle Lincoln NE 68512\\nAlways Free Estimates 402-423-7200\\nYour privacy is important to us! Your e-mail address will not be shared or sold.\\nName ELIZABETH CAMPBELL\\nE-Mail camp@gmail.com\\nAddress 90 OLD HICKORY BLVD\\nCity NEW YORK\\nPhone 615-373-6883 Work 726-448-6720\\nCellular\\n683-426-2200\\nDate 04/23/2020\\nDay Wednesday\\nTime 12.30 P.M.\\nCondition of Carpet or Furniture:\\nSpecial Instructions or Directions:\\nPet Odors\\nAllergy Concerns\\nExcessive Wear\\nSoiled Furniture\\nPermanent Wear\\nLoose Seams\\nPermanent Shading\\n-Laminate Floor Concerns\\nCarpet Cleaning\\n250\\nPet Odors\\n50\\nFood Stains\\n45\\nSteam cleaning\\n100\\nwater Damge Repair\\n400\\nloose seams repair\\n200\\nTile cleaning\\n200\\nDelivery cost\\n200\\nSales Tax\\nDue to Insurance regulations; Items such as: breakables Ns. computers\\n1445.00 00\\nglassware, grandfather clocks. bookshelves, or pianos can not be moved\\nPayment is due upon receipt.\\nAccounts over 30 days will\\nLate Fee\\nRoyal is not responsible for color transfer. change. bleeding, shrinking,\\n5.00\\n00\\nbe assessed a $5.00 late fee\\nseems in carpet or furniture Some stains. spots or shading may\\n& a 5% finance charge\\nor loose permanent all claims.\\nbe due to their nature. Royal Cleaning Service reserves the\\nTOTAL\\n1450\\n00\\nright to Replace, Repair or Refund the cost of cleaning on\\nCustomer ELIZABETH CAMPBELL\\nCleaning Tech JOHN LEWIS\\nWhen can we contact you about your next service?\\n6 Mo\\n12 Mo\\nOther\\nPhone 626-200-4890'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "document.text" ] }, { "cell_type": "markdown", "id": "8b89593a", "metadata": {}, "source": [ "## Asynchronous example\n", "\n", "If you have a lot of data or multipage PDFs, it quickly becomes unwieldy to use the synchronous API as it is rate-limited. A solution to this problem is to use the asynchronous API, which creates jobs and results that you can fetch later. The input file needs to be inside an S3 bucket. If you only have it locally you can provide an `s3_upload_path` and Textractor will take care of upload the file to that directory before calling Textract." ] }, { "cell_type": "code", "execution_count": 3, "id": "cbfe9870", "metadata": {}, "outputs": [], "source": [ "from textractor import Textractor\n", "\n", "extractor = Textractor(profile_name=\"default\")\n", "# This path assumes that you are running the notebook from docs/source/notebooks\n", "document = extractor.start_document_text_detection(\n", " \"../../../tests/fixtures/form.png\",\n", " s3_upload_path=\"s3://textract-ocr/temp/\",\n", ")" ] }, { "cell_type": "markdown", "id": "65817f60", "metadata": {}, "source": [ "Instead of a Document object, an asynchronous function returns a LazyDocument object which is functionally identical to Document but will not actually load the Textract response until you use it. This allows you to make as many requests as you want without ever blocking." ] }, { "cell_type": "code", "execution_count": 4, "id": "0f98c763", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "textractor.entities.lazy_document.LazyDocument" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(document)" ] }, { "cell_type": "markdown", "id": "e86a5f4e", "metadata": {}, "source": [ "If you use the document's property, the object will issue a Textract call to fetch the results." ] }, { "cell_type": "code", "execution_count": 6, "id": "6f4b95d4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\"Service You\\'ll Be\\nBragging\\n&\\nRoyal\\nAbout GUARANTEED!\"\\nYou have a full week to inspect your carpet.\\nIf a spot returns or if there is a concern we\\'ll return.\\nOnline@royalcarpetlincoln.com\\nCarpet & Upholstery Cleaning\\n5401 S. 20 St. Circle Lincoln NE 68512\\nAlways Free Estimates 402-423-7200\\nYour privacy is important to us! Your e-mail address will not be shared or sold.\\nName ELIZABETH CAMPBELL\\nE-Mail camp@gmail.com\\nAddress 90 OLD HICKORY BLVD\\nCity NEW YORK\\nPhone 615-373-6883 Work 726-448-6720\\nCellular\\n683-426-2200\\nDate 04/23/2020\\nDay Wednesday\\nTime 12.30 P.M.\\nCondition of Carpet or Furniture:\\nSpecial Instructions or Directions:\\nPet Odors\\nAllergy Concerns\\nExcessive Wear\\nSoiled Furniture\\nPermanent Wear\\nLoose Seams\\nPermanent Shading\\n-Laminate Floor Concerns\\nCarpet Cleaning\\n250\\nPet Odors\\n50\\nFood Stains\\n45\\nSteam cleaning\\n100\\nwater Damge Repair\\n400\\nloose seams repair\\n200\\nTile cleaning\\n200\\nDelivery cost\\n200\\nSales Tax\\nDue to Insurance regulations; Items such as: breakables Ns. computers\\n1445.00 00\\nglassware, grandfather clocks. bookshelves, or pianos can not be moved\\nPayment is due upon receipt.\\nAccounts over 30 days will\\nLate Fee\\nRoyal is not responsible for color transfer. change. bleeding, shrinking,\\n5.00\\n00\\nbe assessed a $5.00 late fee\\nseems in carpet or furniture Some stains. spots or shading may\\n& a 5% finance charge\\nor loose permanent all claims.\\nbe due to their nature. Royal Cleaning Service reserves the\\nTOTAL\\n1450\\n00\\nright to Replace, Repair or Refund the cost of cleaning on\\nCustomer ELIZABETH CAMPBELL\\nCleaning Tech JOHN LEWIS\\nWhen can we contact you about your next service?\\n6 Mo\\n12 Mo\\nOther\\nPhone 626-200-4890'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "document.text" ] }, { "cell_type": "markdown", "id": "600a903c", "metadata": {}, "source": [ "## Conclusion\n", "\n", "There are many more supported APIs and use cases in Textractor, if this did not address your use case, we encourage you to look at [the other examples](https://aws-samples.github.io/amazon-textract-textractor/examples.html)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.10" } }, "nbformat": 4, "nbformat_minor": 5 }