Search

Write

Sign in

Extracting text from scanned files with Google Cloud Vision API

Shouvojit Sarker

Shouvojit Sarker

Follow

3 min read

·

Jul 7, 2021

A quick and dirty way to retrieve text from scanned images and pdfs.

Press enter or click to view image in full size

Mahrous Houses on Unsplash

Itcan be a bit annoying coming across scanned documents where you cannot search and find text, or copy something specific. In most cases, it is just an inconvenience to shrug off, but a lot of important documents, particularly those bigger than a page or two, can really benefit from having the text extracted from them. In fact, I was surprised when I tried to buy my first home, the sales contract was a scanned image where I could not search for specific clauses by text. This particular pitfall of scanning also affects a lot of public documents too, including documents released under the freedom of information act, scans of old books/files, anything people signed and took a picture of.

Solution?

There are text attraction tools that can help with this. All three major cloud providers, AWS, Google Cloud, and Azure have some form of computer vision service that can gather insight from images. They come with a variety of features and can even identify objects from images and label them, but for today we just want our computer to read texts from a picture!

Why Google Cloud?

I picked Google Cloud mainly because of its rich language support. I have a few old Bengali (my native language) books that I want to scan and turn into searchable text, so becoming familiarised with Google Cloud Vision API was a bit more beneficial to me. In contrast to AWS that only supports five languages, this is quite impressive.

Price comparison

Price-wise, there is not much of a difference between AWS and Google, the first 1000 pages/images are free, and then you get charged $1.50 for every thousand units. At AWS, after the first million units, the price becomes $0.60 per thousand units, wherewith Google Cloud, the price becomes $0.60 after the first five million units. Azure doesn’t have any freebies, but the overall price is a bit cheaper at $1 per thousand units for the first one million units and $0.65 per thousand units after that.

Let’s extract some texts!

First, create a project, set up authentication and enable the Google Cloud API from your Google Cloud Account (sign up if you don’t have one already). You will generate a service account with access to the Cloud API as a part of the process.

Get Shouvojit Sarker’s stories in your inbox

Join Medium for free to get updates from this writer.

Don’t forget to set the service key json for the generated service account as an environment variable on the command line.

export GOOGLE_APPLICATION_CREDENTIALS=”KEY_PATH”

Clone the scan2text repository.

git clone https://github.com/shouvSarker/scan2text.git

Load node version and install packages.

nvm use

npm install

To extract text from an image, run the following command. It will create a text file in your local directory with line breaks as seen in the image.

node scan2textImage.js /path/to/image path/to/output/text

To extract text from a pdf, run the following command. It will save the extracted text in a json file (with additional styling and positioning information as seen in the pdf, alongside perceived accuracy) in a folder in your Google Cloud storage bucket.

node scan2textPdf.js bucketname filename outputfolder

TEXT_DETECTION vs DOCUMENT_TEXT_DETECTION

Document extraction (available for application/pdf and image/tiff) only works for a file located in google cloud storage and is much more precise than text extraction. However, interpreting the output json file requires a bit of heavy lifting and thus not suitable for seamless text extraction. Text extraction from an image (TEXT_EXTRACTION) is quite functional, even for documents scanned and converted to image the results are fairly accurate and gives chunks of text as output. For general purpose use, Image text extraction may be more suitable for most use cases.

Wrapping up!

There you have it, burying people into uninterpretable paperwork just became a lot harder! This works for handwritten notes too, but depending on the quality of the handwriting, may not be as accurate.

Text Extraction

Nodejs

Machine Learning

Computer Vision

Google Cloud Platform

Shouvojit Sarker

Written by Shouvojit Sarker

146 followers

·107 following

A regular techie currently too involved with spatial data.

Follow

No responses yet

Write a response

What are your thoughts?

More from Shouvojit Sarker

How to run Cypress on WSL2

Shouvojit Sarker

Shouvojit Sarker

How to run Cypress on WSL2

Running a GUI based tool like Cypress from WSL2 could be a pain, so let’s solve it!

Jun 23, 2021

189

12

Cannot Upload Large Files to a Service Deployed in Cloud Run? Migrate to http2!

Better Programming

In

Better Programming

by

Shouvojit Sarker

Cannot Upload Large Files to a Service Deployed in Cloud Run? Migrate to http2!

Google Cloud Run limits file upload/download, but the size is unlimited for http2

Oct 20, 2022

242

3

Car with plug resembling plugins

agiledigital

In

agiledigital

by

Shouvojit Sarker

Creating an Atlassian Server Plugin

Thinking about extending Atlassian Jira or Confluence to do more for your team? This is the guide for you!

Jan 25, 2021

107

Uninstalling MongoDB on Ubuntu: Going in circles? Use dpkg!

Dev Genius

In

Dev Genius

by

Shouvojit Sarker

Uninstalling MongoDB on Ubuntu: Going in circles? Use dpkg!

Tldr; use `dpkg -l | grep mongo` to list all packages then `sudo dpkg -r packageName` to remove them one by one.

Sep 19, 2022

14

See all from Shouvojit Sarker

Recommended from Medium

The Python Tool I Built in a Weekend That Now Pays My Rent

Python in Plain English

In

Python in Plain English

by

Suleman Safdar

The Python Tool I Built in a Weekend That Now Pays My Rent

How I turned a tiny automation script into a paid product using libraries, clean OOP, and a little C++ speed where it mattered

Aug 12

1.2K

19

Automate Document Processing in Node.js Using AI OCR & NLP

GoPenAI

In

GoPenAI

by

Somendradev

Automate Document Processing in Node.js Using AI OCR & NLP

Imagine this: you run a business that receives hundreds — sometimes thousands — of documents every day. They come in all forms: invoices…

Aug 15

12

Google’s New LLM Runs on Just 0.5 GB RAM — Here’s How to Fine-Tune It Locally”

Coding Nexus

In

Coding Nexus

by

Civil Learning

Google’s New LLM Runs on Just 0.5 GB RAM — Here’s How to Fine-Tune It Locally”

A few days ago, Google quietly released a little AI model called Gemma 3 270M.

Aug 16

666

9

Building Real-Time Dashboards with FastAPI, HTMX & Plotly Python: The Pure Python Charts Edition

Daniel Boadzie

Daniel Boadzie

Building Real-Time Dashboards with FastAPI, HTMX & Plotly Python: The Pure Python Charts Edition

Welcome Back, Dashboard Builders!

6d ago

205

How to choose the best image embeddings for your e-commerce business ?

leboncoin tech

In

leboncoin tech

by

leboncoin tech

How to choose the best image embeddings for your e-commerce business ?

By Urszula CZERWINSKA, Data Scientist at Adevinta/leboncoin

Apr 30

13

DINOv3 Just Changed Computer Vision Forever

Towards Deep Learning

In

Towards Deep Learning

by

Sumit Pandey

DINOv3 Just Changed Computer Vision Forever

I’ve been waiting for this one. Meta’s DINOv3 feels like the moment self-supervised vision finally crosses from great features to a single…

Aug 14

44

2

See more recommendations

Help

Status

About

Careers

Press

Blog

Privacy

Rules

Terms

Text to speech