OCR (Optical Character Recognition) with Google's AI technology (Cloud Vision API).
The Vision API can detect and extract text from images.
-
Download Python 3.6+ or follow this guide from Google Cloud.
-
Clone or download this repository.
-
Install the required dependencies by running the following command in your terminal:
!pip install google-cloud-vision google-auth.
usage: OCR [-h] [--url] [--document] [--languages LANGUAGES] [--full] [--confidence CONFIDENCE] [--key KEY] path
positional arguments:
path path to image
optional arguments:
-h, --help show this help message and exit
--url specify the path for an external image located on the Web (http:// or https://) or in Google Cloud Storage (gs://)
--document optimized for dense images
--languages LANGUAGES, --language LANGUAGES
specify language hints from https://cloud.google.com/vision/docs/languages (comma separated)
--full, --verbose show full description (paragraphs, per-word confidence, boundaries...)
--confidence CONFIDENCE
display possible mistakes for symbols with low confidence. Default: 0.6
--key KEY explicitly define the path to your service account JSON credentials
Follow these instructions to set up a project with the Cloud Vision API enabled:
-
Select or create a Google Cloud Platform project. Project name suggestion: OCR
-
Create a service account and get your JSON credentials. Service account name suggestion: OCR
-
Make sure that billing is enabled for your project.
Pricing is based on Google Cloud Vision API quota: 1,000 requests/month free
To authenticate your project you need to reference the service account JSON credentials you just downloaded. You have different options to do it, choose what you prefer:
The JSON you downloaded in step 3 place it inside this repository folder.
Another option is to explicitly specify the --key
parameter on every script execution:
OCR image.jpg --key "/path/to/service_account.json"
You can also set the GOOGLE_APPLICATION_CREDENTIALS environmental variable:
bash
Add to your .bash_profile
file:
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service_account.json"
fish
Add to your config.fish
file:
set -gx GOOGLE_APPLICATION_CREDENTIALS "/path/to/service_account.json"
from File: TAJ.jpg
Result: output.txt