def detect_crop_hints_uri(uri): """Detects crop hints in the file located in Google Cloud Storage.""" from google.cloud import vision client = vision.ImageAnnotatorClient() image = vision.Image() image.source.image_uri = uri crop_hints_params = vision.CropHintsParams(aspect_ratios=[1.77]) image_context = vision.ImageContext(crop_hints_params=crop_hints_params) response = client.crop_hints(image=image, image_context=image_context) hints = response.crop_hints_annotation.crop_hints for n, hint in enumerate(hints): print('\nCrop Hint: {}'.format(n)) vertices = ([ '({},{})'.format(vertex.x, vertex.y) for vertex in hint.bounding_poly.vertices ]) print('bounds: {}'.format(','.join(vertices))) if response.error.message: raise Exception('{}\nFor more info on error messages, check: ' 'https://cloud.google.com/apis/design/errors'.format( response.error.message))
def detect_crop_hints(path): """Detects crop hints in an image.""" from google.cloud import vision import io client = vision.ImageAnnotatorClient() # [START vision_python_migration_crop_hints] with io.open(path, 'rb') as image_file: content = image_file.read() image = vision.Image(content=content) crop_hints_params = vision.CropHintsParams(aspect_ratios=[1.77]) image_context = vision.ImageContext(crop_hints_params=crop_hints_params) response = client.crop_hints(image=image, image_context=image_context) hints = response.crop_hints_annotation.crop_hints for n, hint in enumerate(hints): print('\nCrop Hint: {}'.format(n)) vertices = ([ '({},{})'.format(vertex.x, vertex.y) for vertex in hint.bounding_poly.vertices ]) print('bounds: {}'.format(','.join(vertices))) if response.error.message: raise Exception('{}\nFor more info on error messages, check: ' 'https://cloud.google.com/apis/design/errors'.format( response.error.message))
def get_crop_hint(path): # [START vision_crop_hints_tutorial_get_crop_hints] """Detect crop hints on a single image and return the first result.""" client = vision.ImageAnnotatorClient() with io.open(path, 'rb') as image_file: content = image_file.read() image = vision.Image(content=content) crop_hints_params = vision.CropHintsParams(aspect_ratios=[1.77]) image_context = vision.ImageContext(crop_hints_params=crop_hints_params) response = client.crop_hints(image=image, image_context=image_context) hints = response.crop_hints_annotation.crop_hints # Get bounds for the first crop hint using an aspect ratio of 1.77. vertices = hints[0].bounding_poly.vertices # [END vision_crop_hints_tutorial_get_crop_hints] return vertices