如何使用NodeJ获得textDetection和LabelDetection?

时间:2018-07-24 18:47:39

标签: node.js firebase google-cloud-functions google-cloud-vision

const results = await visionClient.labelDetection(imageUri).safeSearchDetection(imageUri);

我正在尝试通过云视觉获得图像响应。

2 个答案:

答案 0 :(得分:1)

以下是HTTPS云功能的代码示例,该功能将对存储在Firebase Storage中的图像执行OCR(即文本检测)。例如,通过将图像名称传递到HTTP请求的正文中,将图像上传到Firebase Storage(在gs://myproject.com/imgtoocr/存储桶中)后,您可以在应用中调用它。

....
const vision = require('@google-cloud/vision');
const client = new vision.ImageAnnotatorClient();


exports.imageOCR = functions.https.onRequest((req, res) => {
  cors(req, res, () => {
    const imageFilename = req.body.imageFilename;

    let result;

    return client
      .documentTextDetection(
        'gs://myproject.com/imgtoocr/' + imageFilename
      )
      .then(results => {
        const blocks = results[0].fullTextAnnotation.pages[0].blocks;

        blocks.forEach(blockDetail => {
          blockDetail.paragraphs.forEach(paragraph => {
            //Here you build the result you want to send back
          });
        });

        return {
          result: result
        };
      })
      .then(ocrResult => {
        return res.status(200).json(ocrResult);
      })
      .catch(err => {
        console.error('ERROR:', err);
        res.status(400).send(err);
      });
  });
});

您将在以下有关node.js的文档中找到更多信息和示例(特别是对于标签检测):

https://cloud.google.com/vision/docs/ocr-tutorial

https://cloud.google.com/vision/docs/detecting-labels

https://cloud.google.com/nodejs/docs/reference/vision/0.19.x/

答案 1 :(得分:0)

以这种方式解决了0.21.0版

import * as vision from '@google-cloud/vision';
const visionClient = new vision.ImageAnnotatorClient();

const request = {
                "image": {
                    "source": {
                        "imageUri": imageUri
                    }
                },
                "features": [
                    {
                        "type": "FACE_DETECTION"
                    },
                    {
                        "type": "LABEL_DETECTION"
                    },
                    {
                        "type": "SAFE_SEARCH_DETECTION"
                    },
                    {
                        "type": "WEB_DETECTION"
                    },
                    {
                        "type": "CROP_HINTS"
                    },
                    {
                        "type": "IMAGE_PROPERTIES"
                    },
                    {
                        "type": "DOCUMENT_TEXT_DETECTION"
                    },
                    {
                        "type": "TEXT_DETECTION"
                    },
                    {
                        "type": "LOGO_DETECTION"
                    },
                    {
                        "type": "LANDMARK_DETECTION"
                    },
                    {
                        "type": "TYPE_UNSPECIFIED"
                    },
                    // Other detection types here...
                ]
        };

        return await visionClient.annotateImage(request).then((res) => {
            console.log(JSON.stringify(res));
        });