Home > Mobile >  How can I use base64 encoded image to scan for explicit content in Cloud Function?
How can I use base64 encoded image to scan for explicit content in Cloud Function?

Time:06-07

According to their documentation, safeSearchDetection accepts base64 encoded images but when I do that it throws an error:

No document found with that ID: Error: ENAMETOOLONG: name too long

 const visionClient = new vision.ImageAnnotatorClient();
 const data = await visionClient.safeSearchDetection(encoded_img);
 const safeSearchResult = data[0].safeSearchAnnotation;

 if (
    safeSearchResult.adult !== 'VERY_UNLIKELY' ||
    safeSearchResult.spoof !== 'VERY_UNLIKELY' ||
    safeSearchResult.medical !== 'VERY_UNLIKELY' ||
    safeSearchResult.violence !== 'VERY_UNLIKELY' ||
    safeSearchResult.racy !== 'VERY_UNLIKELY'
 ) {
    functions.logger.log('Offensive image found.');
 } 

What am I doing wrong here that I cant see?!

CodePudding user response:

From the documentation,

If the key (first param in safeSearchDetection) is content (base64 string in this case), the value should be a Buffer.

const imgBuffer = Buffer.from(encoded_img, "base64");

const data = await visionClient.safeSearchDetection(imgBuffer);
  • Related