簡體   English   中英

如何使用NodeJ獲得textDetection和LabelDetection?

[英]How do i get textDetection and LabelDetection with NodeJs?

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

我正在嘗試通過雲視覺獲得圖像響應。

以下是HTTPS雲功能的代碼示例,該功能將對Firebase存儲中存儲的圖像執行OCR(即文本檢測)。 例如,在將圖像上傳到Firebase Storage(在gs://myproject.com/imgtoocr/存儲桶中)后,可以通過在HTTP請求的正文中傳遞圖像名稱,從應用程序中調用它。

....
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/

用這種方式解決了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));
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM