python - 对从 Google Vision API 中提取为完整单词的文本进行分组

标签 python image-recognition google-vision

我正在尝试通过 Google Vision API 重现“文档文本检测”示例 UI uploader 的输出。但是,我从 sample code 得到的输出当我需要将单词组合在一起时,仅提供单个字符作为输出。

库中是否有允许按“单词”而不是 DOCUMENT_TEXT_DETECT 端点或 Python 中的 image.detect_full_text() 函数分组的功能?

我不是在寻找全文提取,因为我的 .jpg 文件的视觉结构不是 image.detect_text() 函数所满足的。

Google 的示例代码:

def detect_document(path):
    """Detects document features in an image."""
    vision_client = vision.Client()

    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = vision_client.image(content=content)

    document = image.detect_full_text()

    for page in document.pages:
        for block in page.blocks:
            block_words = []
            for paragraph in block.paragraphs:
                block_words.extend(paragraph.words)

            block_symbols = []
            for word in block_words:
                block_symbols.extend(word.symbols)

            block_text = ''
            for symbol in block_symbols:
                block_text = block_text + symbol.text

            print('Block Content: {}'.format(block_text))
            print('Block Bounds:\n {}'.format(block.bounding_box))

Google 提供的现成示例的示例输出:

property {
  detected_languages {
    language_code: "mt"
  }
}
bounding_box {
  vertices {
    x: 1193
    y: 1664
  }
  vertices {
    x: 1206
    y: 1664
  }
  vertices {
    x: 1206
    y: 1673
  }
  vertices {
    x: 1193
    y: 1673
  }
}
symbols {
  property {
    detected_languages {
      language_code: "en"
    }
  }
  bounding_box {
    vertices {
      x: 1193
      y: 1664
    }
    vertices {
      x: 1198
      y: 1664
    }
    vertices {
      x: 1198
      y: 1673
    }
    vertices {
      x: 1193
      y: 1673
    }
  }
  text: "P"
}
symbols {
  property {
    detected_languages {
      language_code: "en"
    }
    detected_break {
      type: LINE_BREAK
    }
  }
  bounding_box {
    vertices {
      x: 1200
      y: 1664
    }
    vertices {
      x: 1206
      y: 1664
    }
    vertices {
      x: 1206
      y: 1673
    }
    vertices {
      x: 1200
      y: 1673
    }
  }
  text: "M"
}


block_words
Out[47]: 
[property {
   detected_languages {
     language_code: "en"
   }
 }
 bounding_box {
   vertices {
     x: 1166
     y: 1664
   }
   vertices {
     x: 1168
     y: 1664
   }
   vertices {
     x: 1168
     y: 1673
   }
   vertices {
     x: 1166
     y: 1673
   }
 }
 symbols {
   property {
     detected_languages {
       language_code: "en"
     }
   }
   bounding_box {
     vertices {
       x: 1166
       y: 1664
     }
     vertices {
       x: 1168
       y: 1664
     }
     vertices {
       x: 1168
       y: 1673
     }
     vertices {
       x: 1166
       y: 1673
     }
   }
   text: "2"
 }

最佳答案

此回复来晚了。我猜您正在寻找类似下面的内容。

def parse_image(image_path=None):
    """
    Parse the image using Google Cloud Vision API, Detects "document" features in an image
    :param image_path: path of the image
    :return: text content
    :rtype: str
    """

    client = vision.ImageAnnotatorClient()
    response = client.text_detection(image=open(image_path, 'rb'))
    text = response.text_annotations
    del response

    return text[0].description

该函数返回图像中的完整文本。

关于python - 对从 Google Vision API 中提取为完整单词的文本进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44762298/

相关文章:

python - 如何在 Django 中序列化多个对象

python - TFIDFVectorizer Pipeline 上具有不同 ngram 范围的 Word 和 Char ngram

android - 谷歌视觉 OCR 阿拉伯语文本检测

python - 如何优雅地处理 SIGTERM 信号?

python - .css 不会影响我在 django 中的模板

image - 静态图像的 OpenCV haar 训练

python - 用于图像相似性的连体网络

JavaCV:如何获取描述符的二进制字符串

java.lang.NoClassDefFoundError Google Vision API android

Node.js - 使用 Google Cloud Vision API 从 PDF 文件中提取文本时出现问题