I'm implementing a web app the uses the Cloud Vision API to detect text on user-generated images.
I'm using React and Firebase cloud functions.
The flow is the following:
- User gets image from Gallery or Camera, crops it using
react-image-crop
package - This package uses a
<canvas>
element to generate the cropped images - I'm converting the image to
base64
usingcanvas.toDataURL("image/jpeg",1.0);
- I'm passing the
imageBase64
string to a cloud function - The cloud function needs to read the image and call the Cloud Vision API
CLIENT CODE
const canvasBase64 = canvas.toDataURL("image/jpeg", 1.0);
const result = await firebase.functions().httpsCallable("readTextFromImage")({
image: canvasBase64
});
setTextareaValue(result.data.text);
CLOUD FUNCTION CODE
const Vision = require('@google-cloud/vision');
const vision = new Vision.ImageAnnotatorClient();
async function readTextFromImage(data) {
const imgBase64 = data.image;
const [textDetections] = await vision.textDetection(imgBase64);
// OTHER THINGS I'VE TRIED HERE
const buffer = new Buffer(imgBase64, 'base64');
const arraybuffer = Uint8Array.from(buffer).buffer;
const [textDetections] = await vision.textDetection(buffer);
const [textDetections] = await vision.textDetection(arraybuffer);
}
NOTE:
The base64
image seems to be generated correctly.
From Google docs on the Node.js Vision API:
https://googleapis.dev/nodejs/vision/latest/v1.ImageAnnotatorClient.html#textDetection
We get that:
When trying with the buffers, I was getting no image present
and passing the base64
string I'm getting code: ENAMETOOLONG
QUESTION
How should I convert the base64
string into something the the Cloud Vision API will accept?