Skip to content

Instantly share code, notes, and snippets.

@gguuss
Last active November 19, 2019 11:47
Show Gist options
  • Save gguuss/bbb73c685fb806eb30f143613be0b35a to your computer and use it in GitHub Desktop.
Save gguuss/bbb73c685fb806eb30f143613be0b35a to your computer and use it in GitHub Desktop.
Process images with vision API
def image_to_object_localization(
project_id, sub_name, duration, binary, cloud_region, registry_id,
device_id):
import base64
import binascii
import io
import time
from google.cloud import pubsub
from google.cloud import vision
from PIL import Image, ImageDraw, ImageFont
subscriber = pubsub.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, sub_name)
global count
count = 0
def callback(message):
global count
try:
count = count + 1
print('Received image {}:'.format(count))
image_data = message.data
if not binary:
image_data = base64.b64decode(image_data)
client = vision.ImageAnnotatorClient()
image = vision.types.Image(content=image_data)
objects = client.object_localization(
image=image).localized_object_annotations
filename = 'vision.jpg'
with io.open(filename, 'wb') as f:
f.write(image_data)
print('Number of objects found: {}'.format(len(objects)))
label_csv = ''
for object_ in objects:
print('\n{} (confidence: {})'.format(
object_.name, object_.score))
with Image.open(filename) as img:
img.show()
message.ack()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment