Detect text on image using Google Cloud Vision API (python)

Requirements

  • You first have to enable Cloud Vision API in the Google Cloud Platform (create a new project if needed).
  • Then, follow steps from this link to create a service account to use Vision API and download the private key file. If you face trouble doing that, follow the instruction from there.
  • Once you are set, you then need to install Vision package for python :
pip install google-cloud-vision

Code

Import packages

Model definition

I use the following image to detect IMEIs :

Liftoff!

We now simply have to provide an image in parameters.

Get response

The response contains an array of dictionnaries :

text_annotations {
description: "PHILIPS"
bounding_poly {
vertices {
x: 224
y: 284
}
vertices {
x: 619
y: 286
}
vertices {
x: 619
y: 365
}
vertices {
x: 224
y: 363
}
}
}
PHILIPS GS$M
TCD128/34
996111004986
MADE IN France
PN: VY379949
CE168X
CN: VY609950K80780
IMEI: 448674528976410
KI
['448674528976410']

That’s all

All you need is a service account key and this code :

100% accurate doesn’t exist

Google Cloud Vision ain’t perfect. From all my tests, 10% were giving wrong results. With the current example, I also got [‘448674528976416’] (the algorithm identified a 6 instead of a 0 on the last digit).

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Beranger Natanelic

Beranger Natanelic

Future Unicorn Founder — Using tech for good