首页 > 解决方案 > 如何使用 Python 的 Google Vision OCR API 获取字数?

问题描述

我想扫描一个image文档并绘制word来自图像的图形,我还想获得word count. 这可以通过使用Google Vision API吗?

我没有在他们的文档中看到任何关于字数的相关信息。如果有人以前做到这一点,请帮助我。

标签: pythongoogle-vision

解决方案


改编自codelabs中可用示例的示例:

from google.cloud import vision

def count_words(str):
    ls1 = str.split()
    return len(ls1)


image_uri = 'gs://cloud-vision-codelab/otter_crossing.jpg'

client = vision.ImageAnnotatorClient()
image = vision.types.Image()
image.source.image_uri = image_uri

response = client.text_detection(image=image)

for text in response.text_annotations:
    print(text.description)
    print(count_words(text.description))

我这边的公平建议:也试试其他 OCR 库。


推荐阅读