代码之家  ›  专栏  ›  技术社区  ›  Yushin

如何使用NodeJs获取文本检测和LabelDetection?

  •  0
  • Yushin  · 技术社区  · 6 年前
    const results = await visionClient.labelDetection(imageUri).safeSearchDetection(imageUri);
    

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

    2 回复  |  直到 6 年前
        1
  •  1
  •   Renaud Tarnec    6 年前

    下面是一个HTTPS云函数的代码示例,该函数将对存储在Firebase存储器中的图像执行OCR(即文本检测)例如,您可以在将图像上载到Firebase存储器(在 gs://myproject.com/imgtoocr/ bucket),通过在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/

        2
  •  0
  •   Yushin    6 年前

    用这种方法解决了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));
            });