Skip to content

Commit 959ca2c

Browse files
gguussdpebot
authored andcommitted
Crop hints tutorial (#861)
* Adds crop hints tutorial. * Uses aspect ratio so that we actually crop. * Addresses review feedback * nits * Restructures samples for CI
1 parent e6a5ff4 commit 959ca2c

23 files changed

+406
-16
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
output-crop.jpg
2+
output-hint.jpg
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Vision API Python Samples
4+
===============================================================================
5+
6+
This directory contains samples for Google Cloud Vision API. `Google Cloud Vision API`_ allows developers to easily integrate vision detection features within applications, including image labeling, face and landmark detection, optical character recognition (OCR), and tagging of explicit content
7+
8+
9+
10+
11+
.. _Google Cloud Vision API: https://cloud.google.com/vision/docs
12+
13+
Setup
14+
-------------------------------------------------------------------------------
15+
16+
17+
Authentication
18+
++++++++++++++
19+
20+
Authentication is typically done through `Application Default Credentials`_,
21+
which means you do not have to change the code to authenticate as long as
22+
your environment has credentials. You have a few options for setting up
23+
authentication:
24+
25+
#. When running locally, use the `Google Cloud SDK`_
26+
27+
.. code-block:: bash
28+
29+
gcloud beta auth application-default login
30+
31+
32+
#. When running on App Engine or Compute Engine, credentials are already
33+
set-up. However, you may need to configure your Compute Engine instance
34+
with `additional scopes`_.
35+
36+
#. You can create a `Service Account key file`_. This file can be used to
37+
authenticate to Google Cloud Platform services from any environment. To use
38+
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
39+
the path to the key file, for example:
40+
41+
.. code-block:: bash
42+
43+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
44+
45+
.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
46+
.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using
47+
.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
48+
49+
Install Dependencies
50+
++++++++++++++++++++
51+
52+
#. Install `pip`_ and `virtualenv`_ if you do not already have them.
53+
54+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
55+
56+
.. code-block:: bash
57+
58+
$ virtualenv env
59+
$ source env/bin/activate
60+
61+
#. Install the dependencies needed to run the samples.
62+
63+
.. code-block:: bash
64+
65+
$ pip install -r requirements.txt
66+
67+
.. _pip: https://pip.pypa.io/
68+
.. _virtualenv: https://virtualenv.pypa.io/
69+
70+
Samples
71+
-------------------------------------------------------------------------------
72+
73+
Crop Hints Tutorial
74+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
75+
76+
77+
78+
To run this sample:
79+
80+
.. code-block:: bash
81+
82+
$ python crop_hints.py
83+
84+
usage: crop_hints.py [-h] image_file mode
85+
86+
positional arguments:
87+
image_file The image you'd like to crop.
88+
mode Set to "crop" or "draw".
89+
90+
optional arguments:
91+
-h, --help show this help message and exit
92+
93+
94+
95+
96+
The client library
97+
-------------------------------------------------------------------------------
98+
99+
This sample uses the `Google Cloud Client Library for Python`_.
100+
You can read the documentation for more details on API usage and use GitHub
101+
to `browse the source`_ and `report issues`_.
102+
103+
.. Google Cloud Client Library for Python:
104+
https://googlecloudplatform.github.io/google-cloud-python/
105+
.. browse the source:
106+
https://github.com/GoogleCloudPlatform/google-cloud-python
107+
.. report issues:
108+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
109+
110+
111+
.. _Google Cloud SDK: https://cloud.google.com/sdk/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Vision API
5+
short_name: Cloud Vision API
6+
url: https://cloud.google.com/vision/docs
7+
description: >
8+
`Google Cloud Vision API`_ allows developers to easily integrate vision
9+
detection features within applications, including image labeling, face and
10+
landmark detection, optical character recognition (OCR), and tagging of
11+
explicit content
12+
13+
setup:
14+
- auth
15+
- install_deps
16+
17+
samples:
18+
- name: Crop Hints Tutorial
19+
file: crop_hints.py
20+
show_help: True
21+
22+
cloud_client_library: true
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Outputs a cropped image or an image highlighting crop regions on an image.
18+
19+
Examples:
20+
python crop_hints.py resources/cropme.jpg draw
21+
python crop_hints.py resources/cropme.jpg crop
22+
"""
23+
# [START full_tutorial]
24+
# [START imports]
25+
import argparse
26+
import io
27+
28+
from google.cloud import vision
29+
from PIL import Image, ImageDraw
30+
# [END imports]
31+
32+
33+
def get_crop_hint(path):
34+
# [START get_crop_hint]
35+
"""Detect crop hints on a single image and return the first result."""
36+
vision_client = vision.Client()
37+
38+
with io.open(path, 'rb') as image_file:
39+
content = image_file.read()
40+
41+
image = vision_client.image(content=content)
42+
43+
# Return bounds for the first crop hint using an aspect ratio of 1.77.
44+
return image.detect_crop_hints({1.77})[0].bounds.vertices
45+
# [END get_crop_hint]
46+
47+
48+
def draw_hint(image_file):
49+
"""Draw a border around the image using the hints in the vector list."""
50+
# [START draw_hint]
51+
vects = get_crop_hint(image_file)
52+
53+
im = Image.open(image_file)
54+
draw = ImageDraw.Draw(im)
55+
draw.line([vects[0].x_coordinate, vects[0].y_coordinate,
56+
vects[1].x_coordinate, vects[1].y_coordinate],
57+
fill='red', width=3)
58+
draw.line([vects[1].x_coordinate, vects[1].y_coordinate,
59+
vects[2].x_coordinate, vects[2].y_coordinate],
60+
fill='red', width=3)
61+
draw.line([vects[2].x_coordinate, vects[2].y_coordinate,
62+
vects[3].x_coordinate, vects[3].y_coordinate],
63+
fill='red', width=3)
64+
draw.line([vects[3].x_coordinate, vects[3].y_coordinate,
65+
vects[0].x_coordinate, vects[0].y_coordinate],
66+
fill='red', width=3)
67+
im.save('output-hint.jpg', 'JPEG')
68+
# [END draw_hint]
69+
70+
71+
def crop_to_hint(image_file):
72+
"""Crop the image using the hints in the vector list."""
73+
# [START crop_to_hint]
74+
vects = get_crop_hint(image_file)
75+
76+
im = Image.open(image_file)
77+
im2 = im.crop((vects[0].x_coordinate, vects[0].y_coordinate,
78+
vects[2].x_coordinate - 1, vects[2].y_coordinate - 1))
79+
im2.save('output-crop.jpg', 'JPEG')
80+
# [END crop_to_hint]
81+
82+
83+
if __name__ == '__main__':
84+
# [START run_crop]
85+
parser = argparse.ArgumentParser()
86+
parser.add_argument('image_file', help='The image you\'d like to crop.')
87+
parser.add_argument('mode', help='Set to "crop" or "draw".')
88+
args = parser.parse_args()
89+
90+
parser = argparse.ArgumentParser()
91+
92+
if args.mode == 'crop':
93+
crop_to_hint(args.image_file)
94+
elif args.mode == 'draw':
95+
draw_hint(args.image_file)
96+
# [END run_crop]
97+
# [END full_tutorial]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2017 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import crop_hints
18+
19+
20+
def test_crop(cloud_config, capsys):
21+
"""Checks the output image for cropping the image is created."""
22+
file_name = os.path.join(
23+
os.path.dirname(__file__),
24+
'resources/cropme.jpg')
25+
crop_hints.crop_to_hint(file_name)
26+
out, _ = capsys.readouterr()
27+
assert os.path.isfile('output-crop.jpg')
28+
29+
30+
def test_draw(cloud_config, capsys):
31+
"""Checks the output image for drawing the crop hint is created."""
32+
file_name = os.path.join(
33+
os.path.dirname(__file__),
34+
'resources/cropme.jpg')
35+
crop_hints.draw_hint(file_name)
36+
out, _ = capsys.readouterr()
37+
assert os.path.isfile('output-hint.jpg')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-cloud-vision==0.23.2
2+
pillow==4.0.0
Loading

vision/cloud-client/README.rst renamed to vision/cloud-client/detect/README.rst

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,6 @@ Install Dependencies
7070
Samples
7171
-------------------------------------------------------------------------------
7272

73-
Quickstart
74-
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
75-
76-
77-
78-
To run this sample:
79-
80-
.. code-block:: bash
81-
82-
$ python quickstart.py
83-
84-
8573
Detect
8674
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8775

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Vision API
5+
short_name: Cloud Vision API
6+
url: https://cloud.google.com/vision/docs
7+
description: >
8+
`Google Cloud Vision API`_ allows developers to easily integrate vision
9+
detection features within applications, including image labeling, face and
10+
landmark detection, optical character recognition (OCR), and tagging of
11+
explicit content
12+
13+
setup:
14+
- auth
15+
- install_deps
16+
17+
samples:
18+
- name: Detect
19+
file: detect.py
20+
show_help: True
21+
22+
cloud_client_library: true

vision/cloud-client/detect.py renamed to vision/cloud-client/detect/detect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def detect_crop_hints(path):
356356
content = image_file.read()
357357
image = vision_client.image(content=content)
358358

359-
hints = image.detect_crop_hints()
359+
hints = image.detect_crop_hints({1.77})
360360

361361
for n, hint in enumerate(hints):
362362
print('\nCrop Hint: {}'.format(n))
@@ -372,7 +372,7 @@ def detect_crop_hints_uri(uri):
372372
vision_client = vision.Client()
373373
image = vision_client.image(source_uri=uri)
374374

375-
hints = image.detect_crop_hints()
375+
hints = image.detect_crop_hints({1.77})
376376
for n, hint in enumerate(hints):
377377
print('\nCrop Hint: {}'.format(n))
378378

0 commit comments

Comments
 (0)