Dense document text detection tutorial

Audience

The goal of this tutorial is to help you develop applications using Google Cloud Vision API Document Text Detection. It assumes you are familiar with basic programming constructs and techniques, but even if you are a beginning programmer, you should be able to follow along and run this tutorial without difficulty, then use the Cloud Vision API reference documentation to create basic applications.

Prerequisites

Annotating an image using Document Text OCR

This tutorial walks you through a basic Vision API application that makes a DOCUMENT_TEXT_DETECTION request, then processes the fullTextAnnotation response.

A fullTextAnnotation is a structured hierarchical response for the UTF-8 text extracted from the image, organized as Pages→Blocks→Paragraphs→Words→Symbols:

  • Page is a collection of blocks, plus meta-information about the page: sizes, resolutions (X resolution and Y resolution may differ).

  • Block represents one "logical" element of the page—for example, an area covered by text, or a picture or separator between columns. The text and table blocks contain the main information needed to extract the text.

  • Paragraph is a structural unit of text representing an ordered sequence of words. By default, words are considered to be separated by word breaks.

  • Word is the smallest unit of text. It is represented as an array of Symbols.

  • Symbol represents a character or a punctuation mark.

The fullTextAnnotation also can provide URLs to Web images that partially or fully match the image in the request.

Complete code listing

As you read the code, we recommend that you follow along by referring to the Cloud Vision API Python reference.

import argparse
from enum import Enum

from google.cloud import vision
from PIL import Image, ImageDraw



class FeatureType(Enum):
    PAGE = 1
    BLOCK = 2
    PARA = 3
    WORD = 4
    SYMBOL = 5


def draw_boxes(image, bounds, color):
    """Draws a border around the image using the hints in the vector list.

    Args:
        image: the input image object.
        bounds: list of coordinates for the boxes.
        color: the color of the box.

    Returns:
        An image with colored bounds added.
    """
    draw = ImageDraw.Draw(image)

    for bound in bounds:
        draw.polygon(
            [
                bound.vertices[0].x,
                bound.vertices[0].y,
                bound.vertices[1]