0
votes

Just started exploring Google Cloud Vision APIs. From their guide:

const client = new vision.ImageAnnotatorClient();
const fileName = 'Local image file, e.g. /path/to/image.png';
const [result] = await client.textDetection(fileName);

However, I wanna use base64 representation of binary image data, since they claim that it's possible to use.

I found this reference on SO: Google Vision API Text Detection with Node.js set Language hint

Instead of imageUri I used "content": stringas mentioned here. But the SO sample uses const [result] = await client.batchAnnotateImages(request);method. I tried using same technique on const [result] = await client.textDetection( method and it gave me an error.

So my question is: Is it possible to use base64 encoded string to represent image in order to perform TEXT_DETECTION ? And if so, how?

Any kind of help is highly appreciated.

2

2 Answers

1
votes

content field need to be Buffer.

You use the nodejs client library. The library use the grpc API internally, and grpc API expect bytes type at content field.

However, JSON API expect base64 string.

References

https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#image https://googleapis.dev/nodejs/vision/latest/v1.ImageAnnotatorClient.html#textDetection

1
votes

You can use the quickstart guide and from there edit the lines after the creation of the client for the following:

// Value of the image in base64
const img_base64 = '/9j/...';

const request = {
  image: {
    content: Buffer.from(img_base64, 'base64')
  }
};

const [result] = await client.textDetection(request);
console.log(result.textAnnotations);
console.log(result.fullTextAnnotation);

You can take a look at the function here, read the description of the request parameter, in particular the following part:

A dictionary-like object representing the image. This should have a single key (source, content).

If the key is content, the value should be a Buffer.

Which leads to the structure used in the sample code from before. Opposed to when using imageUri or filename, which have to be inside of another object which key is source, as shown in the sample.