Skip to content

Commit

Permalink
Vision region tag update (#1635)
Browse files Browse the repository at this point in the history
  • Loading branch information
alixhami authored Aug 16, 2018
1 parent 69543d4 commit 902ecf4
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 176 deletions.
24 changes: 12 additions & 12 deletions vision/cloud-client/crop_hints/crop_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
python crop_hints.py resources/cropme.jpg draw
python crop_hints.py resources/cropme.jpg crop
"""
# [START full_tutorial]
# [START imports]
# [START vision_crop_hints_tutorial]
# [START vision_crop_hints_tutorial_imports]
import argparse
import io

from google.cloud import vision
from google.cloud.vision import types
from PIL import Image, ImageDraw
# [END imports]
# [END vision_crop_hints_tutorial_imports]


def get_crop_hint(path):
# [START get_crop_hint]
# [START vision_crop_hints_tutorial_get_crop_hints]
"""Detect crop hints on a single image and return the first result."""
client = vision.ImageAnnotatorClient()

Expand All @@ -49,14 +49,14 @@ def get_crop_hint(path):

# Get bounds for the first crop hint using an aspect ratio of 1.77.
vertices = hints[0].bounding_poly.vertices
# [END get_crop_hint]
# [END vision_crop_hints_tutorial_get_crop_hints]

return vertices


def draw_hint(image_file):
"""Draw a border around the image using the hints in the vector list."""
# [START draw_hint]
# [START vision_crop_hints_tutorial_draw_crop_hints]
vects = get_crop_hint(image_file)

im = Image.open(image_file)
Expand All @@ -67,23 +67,23 @@ def draw_hint(image_file):
vects[2].x, vects[2].y,
vects[3].x, vects[3].y], None, 'red')
im.save('output-hint.jpg', 'JPEG')
# [END draw_hint]
# [END vision_crop_hints_tutorial_draw_crop_hints]


def crop_to_hint(image_file):
"""Crop the image using the hints in the vector list."""
# [START crop_to_hint]
# [START vision_crop_hints_tutorial_crop_to_hints]
vects = get_crop_hint(image_file)

im = Image.open(image_file)
im2 = im.crop([vects[0].x, vects[0].y,
vects[2].x - 1, vects[2].y - 1])
im2.save('output-crop.jpg', 'JPEG')
# [END crop_to_hint]
# [END vision_crop_hints_tutorial_crop_to_hints]


if __name__ == '__main__':
# [START run_crop]
# [START vision_crop_hints_tutorial_run_application]
parser = argparse.ArgumentParser()
parser.add_argument('image_file', help='The image you\'d like to crop.')
parser.add_argument('mode', help='Set to "crop" or "draw".')
Expand All @@ -95,5 +95,5 @@ def crop_to_hint(image_file):
crop_to_hint(args.image_file)
elif args.mode == 'draw':
draw_hint(args.image_file)
# [END run_crop]
# [END full_tutorial]
# [END vision_crop_hints_tutorial_run_application]
# [END vision_crop_hints_tutorial]
8 changes: 4 additions & 4 deletions vision/cloud-client/detect/beta_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def localize_objects(path):
# [END vision_localize_objects]


# [START vision_localize_objects_uri]
# [START vision_localize_objects_gcs]
def localize_objects_uri(uri):
"""Localize objects in the image on Google Cloud Storage
Expand All @@ -81,7 +81,7 @@ def localize_objects_uri(uri):
print('Normalized bounding polygon vertices: ')
for vertex in object_.bounding_poly.normalized_vertices:
print(' - ({}, {})'.format(vertex.x, vertex.y))
# [END vision_localize_objects_uri]
# [END vision_localize_objects_gcs]


# [START vision_handwritten_ocr]
Expand Down Expand Up @@ -130,7 +130,7 @@ def detect_handwritten_ocr(path):
# [END vision_handwritten_ocr]


# [START vision_handwritten_ocr_uri]
# [START vision_handwritten_ocr_gcs]
def detect_handwritten_ocr_uri(uri):
"""Detects handwritten characters in the file located in Google Cloud
Storage.
Expand Down Expand Up @@ -171,7 +171,7 @@ def detect_handwritten_ocr_uri(uri):
for symbol in word.symbols:
print('\tSymbol: {} (confidence: {})'.format(
symbol.text, symbol.confidence))
# [END vision_handwritten_ocr_uri]
# [END vision_handwritten_ocr_gcs]


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit 902ecf4

Please sign in to comment.