是否可以使用 google vision api 一次扫描 10 张图像 ocr?到目前为止只做了1

Posted

技术标签:

【中文标题】是否可以使用 google vision api 一次扫描 10 张图像 ocr?到目前为止只做了1【英文标题】:is it possible to scan 10 images at once ocr using google vision api? so far manage to do only 1 【发布时间】:2020-12-01 21:18:07 【问题描述】:

我们目前正在使用谷歌视觉 API 做一个 ocr 项目,其中图像返回一个文本值......但到目前为止我们只制作了 1 张图像,是否可以制作 10 张图像?我使用python,这段代码只运行一张图片..谢谢

import os, io
from google.cloud import vision
from google.cloud.vision import types
import pandas as pd

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r'anjir.json'

client = vision.ImageAnnotatorClient()

FILE_NAME = 'receipttest2.jpg'
FOLDER_PATH = r'C:\Users\Fadhlan\Desktop\Python venv\image\text'

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

image = vision.types.Image(content=content)
response = client.text_detection(image=image)
texts = response.text_annotations

df = pd.DataFrame(columns=['locale', 'description'])
for text in texts:
    df = df.append(
        dict(
            locale=text.locale,
            description=text.description
        ),
        ignore_index=True

    )
print(df['description'][0])

【问题讨论】:

【参考方案1】:

可以使用batch image annotation offline,因为异步模式支持"TEXT_DETECTION" feature。您可以找到 Python in here 的示例代码,如您所见,需要为每个图像创建一个请求元素并将其添加到请求数组中:

client = vision_v1.ImageAnnotatorClient()

//image one
source1 = "image_uri": image_uri_1
image1 = "source": source1
features1 = [
    "type": enums.Feature.Type.LABEL_DETECTION,
    "type": enums.Feature.Type.IMAGE_PROPERTIES
]

//image two
source2 = "image_uri": image_uri_2
image2 = "source": source2
features2 = [
    "type": enums.Feature.Type.LABEL_DETECTION
]

# Each requests element corresponds to a single image
requests = ["image": image1, "features": features1, "image": image2, "features": features2]
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)

【讨论】:

非常感谢您的帮助...但是您能否举一个小例子说明如何为每个图像创建请求元素并将其添加到请求数组中 我已经编辑了我的答案,显示了两个图像的示例。 好的,谢谢先生......但是我能知道什么是 image_uri、gcs_destination 和 batch_size 吗?

以上是关于是否可以使用 google vision api 一次扫描 10 张图像 ocr?到目前为止只做了1的主要内容,如果未能解决你的问题,请参考以下文章

是否需要图像预处理(Google Mobile Vision Text Recognition API)?

Google Vision API 中的 Euler X 支持

如何使用 Google Cloud Vision API 确认图像(包含手写和打印文本)是不是包含手写文本?

Google Vision API:图片上包含英文和阿拉伯文

Google Vision 隐私:图片删除

如何使用 Google Vision Api 检测块内的所有文本