logo
Published on

You Do Not always need OCR

artificial-intelligence
Authors

‘’You Do not Always Need OCR - Try Text Extraction Whenever possible’’

Did you know that before AI, businesses spent countless hours manually typing information from printed documents into their digital systems? This is likely still happening today in some parts of the world!

Thanks to AI and Computer Vision technologies like Optical Character Recognition (OCR), manual capturing of information is now a thing of the past. In layman's terms, OCR works by treating your document like a giant picture puzzle. First, it separates the words and letters from the background. Then, it compares each piece to a library of characters it already knows, like the letters of the alphabet and numbers. By finding the best match, it can decipher the text and turn it into something you can edit and search on your computer, just like a regular typed document.

In contrast, Text Extraction works by assuming you already have the text in a digital format, like a PDF(not scanned) or a Word document. It bypasses the character recognition step of OCR and focuses on reading the text content directly from the document's internal structure. Since we do not need any Machine Learning models for Text Extraction, this is typically a much faster and cheaper method than OCR.

OCR vs Text Extraction: A Comparison

FeatureOCRText Extraction (Structure-Based)
DescriptionOCR technology converts images of text (scanned documents, PDFs, or photos) into machine-encoded text, digitizing written content for editing, storage, searching, and electronic display.Text extraction refers to the process of extracting plain text from digital document formats such as PDFs, Word documents, or HTML files, without requiring machine learning or natural language processing techniques.
Input FormatImages (scanned documents, photos), PDFsDigital text formats (PDF, Word documents, emails)
TechnologyOptical Character RecognitionDocument Structure Analysis
OutputMachine-encoded textExtracted text content
FocusConverting documents to images then images to textRetrieving text from document structure
StrengthsDigitizing historical documentsEfficiently extracts text from well-formatted documents
WeaknessesAccuracy limitations with complex layouts or unknown writing symbolsRequires machine-readable format
Common UsesDigitizing historical records, receiptsExtracting specific data from written reports, invoices, processing forms

When to Use OCR vs. Text Extraction

  • OCR is indispensable when you need to convert printed documents into a searchable and editable digital format. It's the go-to solution for digitizing historical records, receipts, legal contracts, and more.

Hint: OCR specializes in interpreting visual representations of text.

  • Use Text Extraction you already have the document in a digital format like a PDF, Word document, or email. Text extraction assumes the text is already machine-readable.

Examples Cases:

  1. Digitising legal documents
    • Imagine a law firm that needs to digitize decades-old case files. By utilizing OCR, they can create a searchable database of these documents, saving countless hours of manual data entry and enabling quick access to crucial information during legal proceedings.
  2. Processing industry regulations
    • Consider an insurance company that needs to process thousands of digital regulations from various regulators. By leveraging Text Extraction, they can automatically pull key information like penalties, reporting requirements etc.

Popular tools

  • Numerous options are available for performing both OCR and Text Extraction, including both open-source and proprietary tools. Here are some of the most common ones:

OCR Tools

  1. Tesseract (Open Source) Tesseract is a widely used open-source OCR engine maintained by Google. You can integrate Tesseract into your Python projects using the pytesseract library. Here's an example of how to extract text from a PDF using Tesseract:

    import pytesseract
    from pdf2image import convert_from_path
    
    # Convert PDF to a list of images
    images = convert_from_path("sample.pdf")
    
    # Perform OCR on each image
    for image in images:
        text = pytesseract.image_to_string(image)
        print(text)
    
    
    • In this example, we first use the pdf2image library to convert the PDF file "sample.pdf" into a list of images. Then, we iterate through each image and perform OCR using pytesseract's image_to_string() function to extract the text.

Text Extraction Tools

  1. PyMuPDF (Open Source) PyMuPDF, also known as fitz, is a powerful library for extracting text from PDF files without using OCR. Here's an example of a Python script to read text from a digitised PDF file:

    import fitz
    
    # Open the PDF file
    with fitz.open("sample.pdf") as doc:
        # Iterate through each page
        for page in doc:
            # Extract the text from the page
            text = page.get_text()
            print(text)
    
    
    • In this example, we use fitz to open the PDF file "sample.pdf". We then iterate through each page of the document and extract the text using the get_text() method.

Cloud OCR/Text Extraction services

  • If you prefer to use managed solutions, you can leverage REST APIs provided by various cloud platforms.
  • These services handle the infrastructure and scaling, allowing you to focus on integrating OCR and text extraction capabilities into your applications. Some popular cloud OCR/text extraction services include:
    1. Google Cloud Vision API Google Cloud Vision API offers OCR and text extraction capabilities as part of its feature set. It can process images and PDFs and supports multiple languages. If you want something more advanced, you can also check out Document AI.
    2. Amazon Textract API Amazon Textract is a fully managed service that extracts text and data from documents, including scanned PDFs, images, and tables. It provides APIs for synchronous and asynchronous text extraction.
    3. Azure Document Intelligence Azure Document Intelligence is a cloud-based service that offers a combination of OCR and intelligent document processing. It can extract text, handwriting, key-value pairs, and tables from various document formats.
  • In addition, to these offerings by the top 3 public cloud providers, you can find similar services from most providers exposed via easy-to-use REST APIs.

Conclusion

Understanding when to use OCR or text extraction is crucial for optimizing your document processing workflows. OCR excels at digitizing printed text, while Text Extraction reads the text directly from the internal document structure without using AI or ML models.

Remember that Text Extraction will only work for machine-readable digital documents(e.g. A PDF that has been converted directly from a Word document), it won't be able to process scanned documents, images or handwritten documents.

As you embark on your next text-processing project, keep these points in mind and choose the tool that best fits your needs to reach a good balance between speed, cost and accuracy