Google Cloud Vision API批处理图像Anotater
我有一个目录,其中有近200张图像,我想使用image属性
使用google> google-cloud-vision
api。
我对这些图像的批处理请求遇到问题。 发布的代码是官方网站的教程。但它使用gs://
作为图像的来源。
我想使用我的本地目录,但无法弄清楚该怎么做。 第二是我写的。我试图用ImagelOdeLoader
加载图像,但失败了。
请帮忙!
from google.cloud import vision_v1
def sample_async_batch_annotate_images(
input_image_uri="gs://cloud-samples-data/vision/label/wakeupcat.jpg",
output_uri="gs://your-bucket/prefix/",
):
"""Perform async batch image annotation."""
client = vision_v1.ImageAnnotatorClient()
source = {"image_uri": input_image_uri}
image = {"source": source}
features = [
{"type_": vision_v1.Feature.Type.LABEL_DETECTION},
{"type_": vision_v1.Feature.Type.IMAGE_PROPERTIES},
]
# Each requests element corresponds to a single image. To annotate more
# images, create a request element for each image and add it to
# the array of requests
requests = [{"image": image, "features": features}]
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=requests, output_config=output_config)
print("Waiting for operation to complete...")
response = operation.result(90)
# 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))
我试图简单地将通往本地目录的路径更改,但是出现了一个错误,即它必须是gs://
,然后我开始使用imgs
将图像加载a imageloader
,但然后得到此错误typeError:无法设置Google.cloud.vision.vis.v1.imagesource.image_uri to [< pil; pil;
import io
import os
from google.cloud import vision_v1
from os import listdir
from PIL import Image as PImage
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = r"/Users/example/Documents/Project/ServiceAccountTolken.json"
def loadImages(path):
# return array of images
imagesList = listdir(path)
loadedImages = []
for image in imagesList:
img = PImage.open(path + image)
loadedImages.append(img)
return loadedImages
path = "/Users/Documents/Project/images/"
# your images in an array
imgs = loadImages(path)
# its a bit messy here since I tried some options
def sample_async_batch_annotate_images(
input_image_uri = imgs, #os.path.abspath("/Users/Documents/Project/images"),
output_uri = os.path.abspath("/Users/Documents/Project/images/output"),
):
"""Perform async batch image annotation."""
client = vision_v1.ImageAnnotatorClient()
source = {"image_uri": input_image_uri}
image = {"source": source}
features = [
{"type_": vision_v1.Feature.Type.IMAGE_PROPERTIES},
]
# Each requests element corresponds to a single image. To annotate more
# images, create a request element for each image and add it to
# the array of requests
requests = [{"image": image, "features": features}]
gcs_destination = {"uri": output_uri}
# The max number of responses to output in each JSON file
batch_size = 6
output_config = {"gcs_destination": gcs_destination,
"batch_size": batch_size}
operation = client.async_batch_annotate_images(requests=requests, output_config=output_config)
print("Waiting for operation to complete...")
response = operation.result(90)
# 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))
I have a directory with almost 200 images from which I want to get the image properties
using the google-cloud-vision
API.
I have a problem making a batch request for those images.
The code posted is a tutorial from the official site. But it's using gs://
as source for it's images.
I want to use my local directory, but can't figure how to do it.
The second is what I have wrote. I have tried to load the images with an imageLoader
but failed.
Please help!
from google.cloud import vision_v1
def sample_async_batch_annotate_images(
input_image_uri="gs://cloud-samples-data/vision/label/wakeupcat.jpg",
output_uri="gs://your-bucket/prefix/",
):
"""Perform async batch image annotation."""
client = vision_v1.ImageAnnotatorClient()
source = {"image_uri": input_image_uri}
image = {"source": source}
features = [
{"type_": vision_v1.Feature.Type.LABEL_DETECTION},
{"type_": vision_v1.Feature.Type.IMAGE_PROPERTIES},
]
# Each requests element corresponds to a single image. To annotate more
# images, create a request element for each image and add it to
# the array of requests
requests = [{"image": image, "features": features}]
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=requests, output_config=output_config)
print("Waiting for operation to complete...")
response = operation.result(90)
# 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))
I have tried to simply changing the path to a local directory, but got an error that it's must be a gs://
then I startet to load the images in imgs
with a imageLoader
but then got this error TypeError: Cannot set google.cloud.vision.v1.ImageSource.image_uri to [<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=960x817 at 0x1237E0CD0
import io
import os
from google.cloud import vision_v1
from os import listdir
from PIL import Image as PImage
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = r"/Users/example/Documents/Project/ServiceAccountTolken.json"
def loadImages(path):
# return array of images
imagesList = listdir(path)
loadedImages = []
for image in imagesList:
img = PImage.open(path + image)
loadedImages.append(img)
return loadedImages
path = "/Users/Documents/Project/images/"
# your images in an array
imgs = loadImages(path)
# its a bit messy here since I tried some options
def sample_async_batch_annotate_images(
input_image_uri = imgs, #os.path.abspath("/Users/Documents/Project/images"),
output_uri = os.path.abspath("/Users/Documents/Project/images/output"),
):
"""Perform async batch image annotation."""
client = vision_v1.ImageAnnotatorClient()
source = {"image_uri": input_image_uri}
image = {"source": source}
features = [
{"type_": vision_v1.Feature.Type.IMAGE_PROPERTIES},
]
# Each requests element corresponds to a single image. To annotate more
# images, create a request element for each image and add it to
# the array of requests
requests = [{"image": image, "features": features}]
gcs_destination = {"uri": output_uri}
# The max number of responses to output in each JSON file
batch_size = 6
output_config = {"gcs_destination": gcs_destination,
"batch_size": batch_size}
operation = client.async_batch_annotate_images(requests=requests, output_config=output_config)
print("Waiting for operation to complete...")
response = operation.result(90)
# 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))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
async_batch_annotate_images()
不支持读取本地文件。您需要使用batch_annotate_images()
。要读取本地图像,您需要将图像内容读取为字节,然后将其传递给请求。下面的代码包括将Response.json
保存到GCS存储桶中。但是,如果您不需要将其保存到GCS中,请在本地保存该文件的部分。请参阅下面的代码:
请参见GCS中的保存结果:
response.json
的示例摘要:async_batch_annotate_images()
does not support reading local files. You need to usebatch_annotate_images()
instead. To read local images, you need to read image content as bytes and pass it to the request. Code below includes saving theresponse.json
to a GCS bucket. But if you won't be needing to save it to GCS, uncomment the part where it saves the file locally.See code below:
See saved result in GCS:
Sample snippet of the
response.json
: