Perform OCR using Google's Drive API v3
Tags: python, googledrive, ocr, tooling
Published: 2022-03-03 09:20:37

{% embed https://github.com/hrishikeshrt/google_drive_ocr %}

Google's Drive API can be used to perform OCR on images from any language. [`google-drive-ocr`](https://pypi.org/project/google-drive-ocr/) is a python package that allows users to do this with utmost ease, right from the terminal.

* Free software: GNU General Public License v3
* Documentation: https://google-drive-ocr.readthedocs.io.

## Features

* Perform OCR using Google's Drive API v3
* Class `GoogleOCRApplication()` for use in projects
* Highly configurable CLI
* Run OCR on a single image file
* Run OCR on multiple image files
* Run OCR on all images in directory
* Use multiple workers (`multiprocessing`)
* Work on a PDF document directly

## Install

To install Google OCR (Drive API v3), run this command in your terminal:

```console
pip install google-drive-ocr
```

**Note**: One must setup a Google application and download `client_secrets.json` file before using `google_drive_ocr`.

## Setup

* Create a project on Google Cloud Platform
* Wizard: https://console.developers.google.com/start/api?id=drive

### Instructions
* https://cloud.google.com/genomics/downloading-credentials-for-api-access
* Select application type as "Installed Application"
* Create credentials "OAuth consent screen" --> "OAuth client ID"
* Save `client_secret.json`

## Usage


### Using in a Project

Create a `GoogleOCRApplication` application instance:

```python
from google_drive_ocr import GoogleOCRApplication

app = GoogleOCRApplication('client_secret.json')
```

Perform OCR on a single image:

```python
app.perform_ocr('image.png')
```

Perform OCR on mupltiple images:

```python
app.perform_batch_ocr(['image_1.png', 'image_2.png', 'image_3.png'])
```

Perform OCR on multiple images using multiple workers (`multiprocessing`):

```python
app.perform_batch_ocr(['image_1.png', 'image_3.png', 'image_2.png'], workers=2)
```

### Using Command Line Interface

Typical usage with several options:

```console
google-ocr --client-secret client_secret.json \
--upload-folder-id <google-drive-folder-id>  \
--image-dir images/ --extension .jpg \
--workers 4 --no-keep
```

Show help message with the full set of options:

```console
google-ocr --help
```

#### Configuration

The default location for configuration is `~/.gdo.cfg`.
If configuration is written to this location with a set of options,
we don't have to specify those options again on the subsequent runs.

Save configuration and exit:

```console
google-ocr --client-secret client_secret.json --write-config ~/.gdo.cfg
```

Read configuration from a custom location (if it was written to a custom location):

```console
google-ocr --config ~/.my_config_file ..
```

#### Performing OCR

**Note**: It is assumed that the `client-secret` option is saved in configuration file.

Single image file:

```console
google-ocr -i image.png
```

Multiple image files:

```console
google-ocr -b image_1.png image_2.png image_3.png
```

All image files from a directory with a specific extension:

```console
google-ocr --image-dir images/ --extension .png
```

Multiple workers (`multiprocessing`):

```console
google-ocr -b image_1.png image_2.png image_3.png --workers 2
```

PDF files:

```console
google-ocr --pdf document.pdf --pages 1-3 5 7-10 13
```