UNPKG

ice.fo.utils

Version:

46 lines (38 loc) 1.24 kB
const fs = require('fs') const vision = require('@google-cloud/vision') module.exports = function ({ $config }) { const client = new vision.ImageAnnotatorClient({ keyFilename: $config.google_api?.api_key_file, }) return { async getImageMetaInfo ({ imagePath, imageBase64 }) { const features = [ { type: 'LABEL_DETECTION' }, { type: 'SAFE_SEARCH_DETECTION' }, ] const options = { image: { content: imageBase64 || Buffer.from(await fs.promises.readFile(imagePath)).toString('base64'), }, features, } const [result] = await client.annotateImage(options) if (result.error) { throw new Error(result.error.message) } const labels = result.labelAnnotations const safeSearch = result.safeSearchAnnotation return labels.map(i => ({ name: i.description, score: i.score, })).concat( Object.entries(safeSearch || {}) .filter(([key, value]) => typeof value == 'string' && value != 'UNKNOWN' && safeSearch[key + 'Confidence'] != 0) .map(([key, value]) => ({ name: key, score: safeSearch[key + 'Confidence'], })), ) }, } }