Skip to content

Commit 8ed683d

Browse files
committed
Merge pull request #365 from GoogleCloudPlatform/label
Move vision api labelling sample.
2 parents 3091c1b + 18fbd8f commit 8ed683d

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

vision/api/label/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Python label detection [Vision API](https://cloud.google.com/vision/) example.
2+
See the [tutorial](https://cloud.google.com/vision/docs/label-tutorial).
3+

vision/api/label/label.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python
2+
# Copyright 2015 Google Inc. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""
17+
This script uses the Vision API's label detection capabilities to find a label
18+
based on an image's content.
19+
20+
To run the example, install the necessary libraries by running:
21+
22+
pip install -r requirements.txt
23+
24+
Run the script on an image to get a label, E.g.:
25+
26+
./label.py <path-to-image>
27+
"""
28+
29+
# [START import_libraries]
30+
import argparse
31+
import base64
32+
33+
from googleapiclient import discovery
34+
from oauth2client.client import GoogleCredentials
35+
36+
# The url template to retrieve the discovery document for trusted testers.
37+
DISCOVERY_URL = 'https://{api}.googleapis.com/$discovery/rest?version={apiVersion}' # noqa
38+
# [END import_libraries]
39+
40+
41+
def main(photo_file):
42+
"""Run a label request on a single image"""
43+
44+
# [START authenticate]
45+
credentials = GoogleCredentials.get_application_default()
46+
service = discovery.build('vision', 'v1', credentials=credentials,
47+
discoveryServiceUrl=DISCOVERY_URL)
48+
# [END authenticate]
49+
50+
# [START construct_request]
51+
with open(photo_file, 'rb') as image:
52+
image_content = base64.b64encode(image.read())
53+
service_request = service.images().annotate(body={
54+
'requests': [{
55+
'image': {
56+
'content': image_content.decode('UTF-8')
57+
},
58+
'features': [{
59+
'type': 'LABEL_DETECTION',
60+
'maxResults': 1
61+
}]
62+
}]
63+
})
64+
# [END construct_request]
65+
# [START parse_response]
66+
response = service_request.execute()
67+
label = response['responses'][0]['labelAnnotations'][0]['description']
68+
print('Found label: %s for %s' % (label, photo_file))
69+
# [END parse_response]
70+
71+
# [START run_application]
72+
if __name__ == '__main__':
73+
parser = argparse.ArgumentParser()
74+
parser.add_argument('image_file', help='The image you\'d like to label.')
75+
args = parser.parse_args()
76+
main(args.image_file)
77+
# [END run_application]

vision/api/label/label_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import re
15+
16+
from label import main
17+
18+
19+
def test_main(resource, capsys):
20+
in_file = resource('cat.jpg')
21+
22+
main(in_file)
23+
24+
stdout, _ = capsys.readouterr()
25+
assert re.match(r'Found label:.*cat', stdout)

vision/api/label/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-api-python-client==1.5.1

vision/api/label/resources/cat.jpg

120 KB
Loading

0 commit comments

Comments
 (0)