Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Google cloud Vision API's on numpy matrices

I'm using the Google Text detection API for performing OCR on images.

I've found that my OCR results are much better when I perform some pre-processing on the images using opencv.

My question is - how can I call the Google cloud Vision API's on images I have in memory as Numpy arrays? The official Google docs only show the vision api accepting an image in disk as the input.

I want to avoid unnecessary disk writes.

like image 944
Thalish Sajeed Avatar asked May 18 '18 15:05

Thalish Sajeed


People also ask

Is Google Cloud Vision API free?

Pricing is tiered - the first 1000 units used each month are free, units 1001 to 5,000,000 are priced as marked, etc. If you pay in a currency other than USD, the prices listed in your currency on Cloud Platform SKUs apply.


1 Answers

One way of providing the image to Google Cloud Vison API is as a base64 encoded string. The quickstart example uses the Python client API, which accepts a stream of bytes for the image content.

Instead of writing to file you can encode your OpenCV image img to a byte string you can directly pass to Cloud Vision API:

image = types.Image(content=cv2.imencode('.jpg', img)[1].tostring())
like image 147
kynan Avatar answered Sep 30 '22 01:09

kynan