|
| 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 | +"""Demonstrates web detection using the Google Cloud Vision API. |
| 18 | +
|
| 19 | +Example usage: |
| 20 | + python web_detect.py https://goo.gl/X4qcB6 |
| 21 | + python web_detect.py ../detect/resources/landmark.jpg |
| 22 | + python web_detect.py gs://your-bucket/image.png |
| 23 | +""" |
| 24 | +# [START full_tutorial] |
| 25 | +# [START imports] |
| 26 | +import argparse |
| 27 | +import io |
| 28 | + |
| 29 | +from google.cloud import vision |
| 30 | +# [END imports] |
| 31 | + |
| 32 | + |
| 33 | +def annotate(path): |
| 34 | + """Returns web annotations given the path to an image.""" |
| 35 | + # [START get_annotations] |
| 36 | + image = None |
| 37 | + vision_client = vision.Client() |
| 38 | + |
| 39 | + if path.startswith('http') or path.startswith('gs:'): |
| 40 | + image = vision_client.image(source_uri=path) |
| 41 | + |
| 42 | + else: |
| 43 | + with io.open(path, 'rb') as image_file: |
| 44 | + content = image_file.read() |
| 45 | + |
| 46 | + image = vision_client.image(content=content) |
| 47 | + |
| 48 | + return image.detect_web() |
| 49 | + # [END get_annotations] |
| 50 | + |
| 51 | + |
| 52 | +def report(annotations): |
| 53 | + """Prints detected features in the provided web annotations.""" |
| 54 | + # [START print_annotations] |
| 55 | + if annotations.pages_with_matching_images: |
| 56 | + print('\n{} Pages with matching images retrieved') |
| 57 | + |
| 58 | + for page in annotations.pages_with_matching_images: |
| 59 | + print('Score : {}'.format(page.score)) |
| 60 | + print('Url : {}'.format(page.url)) |
| 61 | + |
| 62 | + if annotations.full_matching_images: |
| 63 | + print ('\n{} Full Matches found: '.format( |
| 64 | + len(annotations.full_matching_images))) |
| 65 | + |
| 66 | + for image in annotations.full_matching_images: |
| 67 | + print('Score: {}'.format(image.score)) |
| 68 | + print('Url : {}'.format(image.url)) |
| 69 | + |
| 70 | + if annotations.partial_matching_images: |
| 71 | + print ('\n{} Partial Matches found: '.format( |
| 72 | + len(annotations.partial_matching_images))) |
| 73 | + |
| 74 | + for image in annotations.partial_matching_images: |
| 75 | + print('Score: {}'.format(image.score)) |
| 76 | + print('Url : {}'.format(image.url)) |
| 77 | + |
| 78 | + if annotations.web_entities: |
| 79 | + print ('\n{} Web entities found: '.format( |
| 80 | + len(annotations.web_entities))) |
| 81 | + |
| 82 | + for entity in annotations.web_entities: |
| 83 | + print('Score : {}'.format(entity.score)) |
| 84 | + print('Description: {}'.format(entity.description)) |
| 85 | + # [END print_annotations] |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == '__main__': |
| 89 | + # [START run_web] |
| 90 | + parser = argparse.ArgumentParser( |
| 91 | + description=__doc__, |
| 92 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 93 | + path_help = str('The image to detect, can be web URI, ' |
| 94 | + 'Google Cloud Storage, or path to local file.') |
| 95 | + parser.add_argument('image_url', help=path_help) |
| 96 | + args = parser.parse_args() |
| 97 | + |
| 98 | + report(annotate(args.image_url)) |
| 99 | + # [END run_web] |
| 100 | +# [END full_tutorial] |
0 commit comments