【问题标题】:How to get a batch response from google vision text detection API?如何从谷歌视觉文本检测 API 获得批量响应?
【发布时间】:2019-08-16 08:13:46
【问题描述】:

我目前正在使用谷歌视觉的text_detection API 处理单个图像,但我想获得批量响应。我尝试使用BatchAnnotateImagesRequest,但我还没有让它工作。

我正在做什么以获取一张图片的响应。

client = vision.ImageAnnotatorClient() with io.open(path, 'rb') as image_file: content = image_file.read() image = vision.types.Image(content=content) response = client.document_text_detection(image=image) texts = response.text_annotations

【问题讨论】:

    标签: python google-cloud-platform google-vision


    【解决方案1】:

    public documentation 中有关于对 Google 文本检测 API 的批量请求的信息。

    在文档中,您可以找到一些用 python 编写的示例,您可以使用这些示例来执行批处理请求,每个批处理限制为 2000 个文件:

    from google.cloud import vision_v1
    from google.cloud.vision_v1 import enums
    import six
    
    def sample_async_batch_annotate_images(input_image_uri, output_uri):
      """Perform async batch image annotation"""
    
      client = vision_v1.ImageAnnotatorClient()
    
      # input_image_uri = 'gs://cloud-samples-data/vision/label/wakeupcat.jpg'
      # output_uri = 'gs://your-bucket/prefix/'
    
      if isinstance(input_image_uri, six.binary_type):
        input_image_uri = input_image_uri.decode('utf-8')
      if isinstance(output_uri, six.binary_type):
        output_uri = output_uri.decode('utf-8')
      source = {'image_uri': input_image_uri}
      image = {'source': source}
      type_ = enums.Feature.Type.LABEL_DETECTION
      features_element = {'type': type_}
      type_2 = enums.Feature.Type.IMAGE_PROPERTIES
      features_element_2 = {'type': type_2}
      features = [features_element, features_element_2]
      requests_element = {'image': image, 'features': features}
      requests = [requests_element]
      gcs_destination = {'uri': output_uri}
    
      # The max number of responses to output in each JSON file
      batch_size = 2
      output_config = {'gcs_destination': gcs_destination, 'batch_size': batch_size}
    
      operation = client.async_batch_annotate_images(requests, output_config)
    
      print('Waiting for operation to complete...')
      response = operation.result()
    
      # The output is written to GCS with the provided output_uri as prefix
      gcs_output_uri = response.output_config.gcs_destination.uri
      print('Output written to GCS with prefix: {}'.format(gcs_output_uri))
    

    除了示例代码,您还可以找到执行批处理请求时可以预期的输出示例。有关批处理请求的更多信息可以找到here。

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 2018-03-14
      • 2017-02-26
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 2018-07-14
      • 2017-09-22
      相关资源
      最近更新 更多