Skip to content

Google - Cloud Vision

Use the OCR features offered by Google easily through the BotCity plugin for Google Cloud Vision.

Installation

pip install botcity-cloudvision-plugin

Importing the Plugin

After you installed this package, the next step is to import the package into your code and start using the functions.

from botcity.plugins.cloudvision import BotCloudVisionPlugin

As a demonstration of the library, let's build a simple example together that will parse the text from the following image:

Click here to Download

Instantiating the Plugin

To make the example we will instantiate the plugin and setup our Google Cloud Vision service account key. If you don't have one yet, access this section to create one and remember to activate billing as shown in this step.

# Instantiate the plugin
cloudvision = BotCloudVisionPlugin()
# Setup the path to the service account key credentials JSON file
cloudvision.credentials("<path_to_my>/credentials.json")

Reading text from the image

Now let's read the text from the image.

# Read the text from the image
cloudvision.read("otter_crossing.jpg")

# Print the text from the image
print(cloudvision.full_text())

The output should look like this:

CAUTION
Otters crossing
for next 6 miles

Complete code

Let's take a look into the complete code:

# Instantiate the plugin
cloudvision = BotCloudVisionPlugin()
# Setup the path to the service account key credentials JSON file
cloudvision.credentials("<path_to_my>/credentials.json")
# Read the text from the image
cloudvision.read("otter_crossing.jpg")

# Print the text from the image
print(cloudvision.full_text())

Tip

This plugin allow you to use method chaining so the code above could be written as:

text = BotCloudVisionPlugin() \
    .credentials("<path_to_my>/credentials.json") \
    .read("otter_crossing.jpg") \
    .full_text()
# Print the text from the image
print(text)