首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >云视觉api注释来自单个调用的多个映像。

云视觉api注释来自单个调用的多个映像。
EN

Stack Overflow用户
提问于 2021-12-28 07:56:57
回答 2查看 486关注 0票数 1

在我的图像处理项目中,我使用python、云视觉和云存储。在第一个链接中,google解释了单个图像处理,但我需要多个图像处理。我在云存储中有一个文件夹,我正在使用这些图像进行处理。

我尝试了第二和第三个环节,但它们是旧的答案,没有解决我的问题。我认为我应该为"input_image_uri“创建一个算法,比如将函数调用为for循环。我该怎么办,我不知道怎么走。

1- https://cloud.google.com/vision/docs/batch#vision_async_batch_annotate_images-python

2- How to annotate MULTIPLE images from a single call using Google's vision API? Python

3- https://www.py4u.net/discuss/247010

EN

回答 2

Stack Overflow用户

发布于 2021-12-28 09:03:16

这是我使用的一种方法,它可以很好地从图像中进行OCR。我们将文件上传到一个桶文件夹中,获取该文件夹中所有图像的URIS,准备一个批处理请求并触发它。下面是一个例子。

代码语言:javascript
复制
import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "../my-creds.json" # If you are using service account
from google.cloud import storage
from google.cloud import vision


def file_list(bucket_name, bucket_dir: str):
    # list all files in bucket
    client: storage.Client = storage.Client()
    bucket = client.get_bucket(bucket_name)
    blob_list = list(bucket.list_blobs(prefix=bucket_dir, max_results=200))

    return [f"gs://{bucket_name}/{blob.name}" for blob in blob_list]


def prepare_request(file_uris: list):
    # Create the batch request
    requests = []
    # Example feature for OCR
    features = [vision.types.Feature(type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)]

    for image in file_uris:
        source_image = vision.types.ImageSource(image_uri=image)
        request = vision.types.AnnotateImageRequest(image=vision.types.Image(source=source_image),
                                                    features=features)

        requests.append(request)
    return requests


def run_cloud_vision(gcv_credentials=None):
    client = vision.ImageAnnotatorClient(credentials=gcv_credentials)
    src_bucket = ""  # Your bucket name
    src_dir = "test-images/"  # relative path to your directory in bucket. Empty if no dir

    # get file URIs
    file_uris = file_list(src_bucket, src_dir)
    requests = prepare_request(file_uris)

    # trigger a batch annotation request
    response = client.batch_annotate_images(requests)

    # parse your response
    print("parsed response:", response)
    

if __name__ == '__main__':
    run_cloud_vision()

如果文件夹/桶中还有其他文件,这可能会列出您需要的其他文件。因此,您可能需要根据文件模式过滤URIS。

类似的方法也适用于其他视觉API。

票数 2
EN

Stack Overflow用户

发布于 2021-12-28 18:59:47

我用局部方式用for循环来解决问题。

代码语言:javascript
复制
import os
from google.cloud import vision
import pandas as pd
import io

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '.json key'

client = vision.ImageAnnotatorClient()
folder_path = 'main_folder_path'
df_main = pd.DataFrame(columns=['file_name', 'object', 'score'])

for data_file in sorted(os.listdir(folder_path)):
    path = folder_path + data_file
    # Image_source
    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = vision.Image(content=content)

    response = client.object_localization(image=image)
    localized_object_annotations = response.localized_object_annotations

    df = pd.DataFrame(columns=['file_name', 'object', 'score'])
    for obj in localized_object_annotations:
        df = df.append(
            dict(
                file_name = data_file,
                object = obj.name,
                score = obj.score
            ), ignore_index=True
        )
    df_main = df_main.append(df)
df_main = df_main.to_excel('excel file path')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70504205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档