Skip to content

Commit 504ae46

Browse files
puneithJon Wayne Parrott
authored andcommitted
Vision a11 - adding crop hints and web annotation (GoogleCloudPlatform#671)
1 parent 1f9432d commit 504ae46

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

vision/api/label/snippets.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
import argparse
18+
import base64
19+
import json
20+
21+
from googleapiclient import discovery
22+
import httplib2
23+
from oauth2client.client import GoogleCredentials
24+
25+
DISCOVERY_URL = (
26+
'https://vision.googleapis.com/$discovery/rest?'
27+
'labels=TRUSTED_TESTER&version=v1'
28+
)
29+
30+
31+
def get_service():
32+
"""Get vision service using discovery."""
33+
credentials = GoogleCredentials.get_application_default()
34+
scoped_credentials = credentials.create_scoped(
35+
['https://www.googleapis.com/auth/cloud-platform'])
36+
http = httplib2.Http()
37+
scoped_credentials.authorize(http)
38+
return discovery.build(
39+
'vision', 'v1',
40+
http=http,
41+
discoveryServiceUrl=DISCOVERY_URL
42+
)
43+
44+
45+
def crop_hint(photo_file):
46+
"""Run a crop hint request on the image."""
47+
48+
service = get_service()
49+
50+
with open(photo_file, 'rb') as image:
51+
image_content = base64.b64encode(image.read())
52+
53+
service_request = service.images().annotate(body={
54+
'requests': [{
55+
'image': {
56+
'content': image_content.decode('UTF-8')
57+
},
58+
'features': [{
59+
'type': 'CROP_HINTS'
60+
}]
61+
}]
62+
})
63+
64+
response = service_request.execute()
65+
print(json.dumps(response, indent=2))
66+
67+
68+
def web_annotation(photo_file):
69+
"""Run a web annotation request on the image."""
70+
71+
service = get_service()
72+
73+
with open(photo_file, 'rb') as image:
74+
image_content = base64.b64encode(image.read())
75+
76+
service_request = service.images().annotate(body={
77+
'requests': [{
78+
'image': {
79+
'content': image_content.decode('UTF-8')
80+
},
81+
'features': [{
82+
'type': 'WEB_ANNOTATION',
83+
'maxResults': 10
84+
}]
85+
}]
86+
})
87+
88+
response = service_request.execute()
89+
print(json.dumps(response, indent=2))
90+
91+
92+
if __name__ == '__main__':
93+
parser = argparse.ArgumentParser()
94+
parser.add_argument('command', choices=['crop_hint', 'web_annotation'])
95+
parser.add_argument('image_file', help='The image you\'d like to process.')
96+
args = parser.parse_args()
97+
98+
if args.command == 'crop_hint':
99+
response = crop_hint(args.image_file)
100+
elif args.command == 'web_annotation':
101+
response = web_annotation(args.image_file)

vision/api/label/snippets_test.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google, Inc
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+
18+
import json
19+
20+
import snippets
21+
22+
23+
def test_crop_hint_response_count(capsys, resource):
24+
snippets.crop_hint(resource('cat.jpg'))
25+
stdout, _ = capsys.readouterr()
26+
result = json.loads(stdout)
27+
assert len(result['responses']) == 1
28+
29+
30+
def test_crop_hint_response_dim(capsys, resource):
31+
snippets.crop_hint(resource('cat.jpg'))
32+
stdout, _ = capsys.readouterr()
33+
result = json.loads(stdout)
34+
crop_hint = result['responses'][0]
35+
crop_hint_annotation = crop_hint['cropHintsAnnotation']['cropHints'][0]
36+
confidence = crop_hint_annotation['confidence']
37+
38+
assert 0.5 < confidence < 0.9
39+
40+
41+
def test_web_annotations(capsys, resource):
42+
snippets.web_annotation(resource('cat.jpg'))
43+
stdout, _ = capsys.readouterr()
44+
result = json.loads(stdout)
45+
web_annotation = result['responses'][0]['webAnnotation']
46+
web_entities = web_annotation['webEntities']
47+
48+
assert len(web_entities) == 10
49+
russian_blue = False
50+
51+
for entity in web_entities:
52+
entity_id = entity['entityId']
53+
desc = entity['description']
54+
55+
if entity_id == '/m/012cc2' and desc == 'Russian Blue':
56+
russian_blue = True
57+
58+
assert russian_blue is True

0 commit comments

Comments
 (0)