diff --git a/vision/cloud-client/README.rst b/vision/cloud-client/README.rst index 7dad184afe5..f85158fbb5d 100644 --- a/vision/cloud-client/README.rst +++ b/vision/cloud-client/README.rst @@ -8,7 +8,7 @@ This directory contains samples for Google Cloud Vision API. `Google Cloud Visio -.. _Google Cloud Vision API: https://cloud.google.com/vision/docs +.. _Google Cloud Vision API: https://cloud.google.com/vision/docs Setup ------------------------------------------------------------------------------- @@ -94,24 +94,24 @@ To run this sample: $ python detect.py usage: detect.py [-h] - {faces,faces-uri,labels,labels-uri,landmarks,landmarks-uri,text,text-uri,logos,logos-uri,safe-search,safe-search-uri,properties,properties-uri,web,web-uri,crophints,crophints-uri,fulltext,fulltext-uri} + {faces,faces-uri,labels,labels-uri,landmarks,landmarks-uri,text,text-uri,logos,logos-uri,safe-search,safe-search-uri,properties,properties-uri,web,web-uri,crophints,crophints-uri,document,document-uri} ... - + This application demonstrates how to perform basic operations with the Google Cloud Vision API. - + Example Usage: python detect.py text ./resources/wakeupcat.jpg python detect.py labels ./resources/landmark.jpg python detect.py web ./resources/landmark.jpg python detect.py web-uri http://wheresgus.com/dog.JPG python detect.py faces-uri gs://your-bucket/file.jpg - + For more information, the documentation at https://cloud.google.com/vision/docs. - + positional arguments: - {faces,faces-uri,labels,labels-uri,landmarks,landmarks-uri,text,text-uri,logos,logos-uri,safe-search,safe-search-uri,properties,properties-uri,web,web-uri,crophints,crophints-uri,fulltext,fulltext-uri} + {faces,faces-uri,labels,labels-uri,landmarks,landmarks-uri,text,text-uri,logos,logos-uri,safe-search,safe-search-uri,properties,properties-uri,web,web-uri,crophints,crophints-uri,document,document-uri} faces Detects faces in an image. faces-uri Detects faces in the file located in Google Cloud Storage or the web. @@ -133,16 +133,16 @@ To run this sample: properties Detects image properties in the file. properties-uri Detects image properties in the file located in Google Cloud Storage or on the Web. - web detects web annotations given an image. - web-uri detects web annotations in the file located in google - cloud storage. - crophints detects crop hints in an image. - crophints-uri detects crop hints in the file located in google cloud - storage. - fulltext extracts full text from an image. - fulltext-uri extracts full text in the file located in google cloud - storage. - + web Detects web annotations given an image. + web-uri Detects web annotations in the file located in Google + Cloud Storage. + crophints Detects crop hints in an image. + crophints-uri Detects crop hints in the file located in Google Cloud + Storage. + document Detects document features in an image. + document-uri Detects document features in the file located in + Google Cloud Storage. + optional arguments: -h, --help show this help message and exit @@ -164,4 +164,4 @@ to `browse the source`_ and `report issues`_. https://github.com/GoogleCloudPlatform/google-cloud-python/issues -.. _Google Cloud SDK: https://cloud.google.com/sdk/ +.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/vision/cloud-client/detect.py b/vision/cloud-client/detect.py index e4db634a4e8..13be99fb16d 100644 --- a/vision/cloud-client/detect.py +++ b/vision/cloud-client/detect.py @@ -270,7 +270,7 @@ def detect_properties_uri(uri): def detect_web(path): - """detects web annotations given an image.""" + """Detects web annotations given an image.""" vision_client = vision.Client() with io.open(path, 'rb') as image_file: @@ -312,7 +312,7 @@ def detect_web(path): def detect_web_uri(uri): - """detects web annotations in the file located in google cloud storage.""" + """Detects web annotations in the file located in Google Cloud Storage.""" vision_client = vision.Client() image = vision_client.image(source_uri=uri) @@ -350,7 +350,7 @@ def detect_web_uri(uri): def detect_crop_hints(path): - """detects crop hints in an image.""" + """Detects crop hints in an image.""" vision_client = vision.Client() with io.open(path, 'rb') as image_file: content = image_file.read() @@ -368,7 +368,7 @@ def detect_crop_hints(path): def detect_crop_hints_uri(uri): - """detects crop hints in the file located in google cloud storage.""" + """Detects crop hints in the file located in Google Cloud Storage.""" vision_client = vision.Client() image = vision_client.image(source_uri=uri) @@ -382,8 +382,8 @@ def detect_crop_hints_uri(uri): print('bounds: {}'.format(','.join(vertices))) -def detect_fulltext(path): - """extracts full text from an image.""" +def detect_document(path): + """Detects document features in an image.""" vision_client = vision.Client() with io.open(path, 'rb') as image_file: @@ -391,45 +391,68 @@ def detect_fulltext(path): image = vision_client.image(content=content) - fulltext = image.detect_full_text() + document = image.detect_full_text() + + for b, page in enumerate(document.pages): + page_text = '' - for b, page in enumerate(fulltext.pages): - print(page.width) for bb, block in enumerate(page.blocks): - print('Block: {}'.format(block.bounding_box)) - print('Type: {}'.format(dir(block))) - print('Type: {}'.format(block.block_type)) + block_text = '' + for p, paragraph in enumerate(block.paragraphs): - print('\tParagraph: ({})'.format(paragraph.bounding_box)) - print('\twords: ({})'.format((paragraph.words))) + para_text = '' + for w, word in enumerate(paragraph.words): + word_text = '' + for s, symbol in enumerate(word.symbols): - print('\t\t\t$:{}'.format(symbol.text)) + word_text = word_text + symbol.text + + para_text = para_text + word_text + + block_text = block_text + para_text + print('\n--\nContent Block: {}'.format(block_text)) + print('Block Bounding Box:\n{}'.format(block.bounding_box)) + + page_text = page_text + block_text - print(fulltext.text) + print('Page Content:\n{}'.format(page_text)) + print('Page Dimensions: w: {} h: {}'.format(page.width, page.height)) -def detect_fulltext_uri(uri): - """extracts full text in the file located in google cloud storage.""" +def detect_document_uri(uri): + """Detects document features in the file located in Google Cloud + Storage.""" vision_client = vision.Client() image = vision_client.image(source_uri=uri) - fulltext = image.detect_full_text() + document = image.detect_full_text() + + for b, page in enumerate(document.pages): + page_text = '' - for b, page in enumerate(fulltext.pages): - print(page.width) for bb, block in enumerate(page.blocks): - print('Block: {}'.format(block.bounding_box)) - print('Type: {}'.format(dir(block))) - print('Type: {}'.format(block.block_type)) + block_text = '' + for p, paragraph in enumerate(block.paragraphs): - print('\tParagraph: ({})'.format(paragraph.bounding_box)) - print('\twords: ({})'.format((paragraph.words))) + para_text = '' + for w, word in enumerate(paragraph.words): + word_text = '' + for s, symbol in enumerate(word.symbols): - print('\t\t\t$:{}'.format(symbol.text)) + word_text = word_text + symbol.text + + para_text = para_text + word_text + + block_text = block_text + para_text + print('\n--\nContent Block: {}'.format(block_text)) + print('Block Bounding Box:\n{}'.format(block.bounding_box)) + + page_text = page_text + block_text - print(fulltext.text) + print('Page Content:\n{}'.format(page_text)) + print('Page Dimensions: w: {} h: {}'.format(page.width, page.height)) def run_local(args): @@ -451,8 +474,8 @@ def run_local(args): detect_web(args.path) elif args.command == 'crophints': detect_crop_hints(args.path) - elif args.command == 'fulltext': - detect_fulltext(args.path) + elif args.command == 'document': + detect_document(args.path) def run_uri(args): @@ -474,8 +497,8 @@ def run_uri(args): detect_web_uri(args.uri) elif args.command == 'crophints-uri': detect_crop_hints_uri(args.uri) - elif args.command == 'fulltext-uri': - detect_fulltext_uri(args.uri) + elif args.command == 'document-uri': + detect_document_uri(args.uri) if __name__ == '__main__': @@ -560,13 +583,13 @@ def run_uri(args): 'crophints-uri', help=detect_crop_hints_uri.__doc__) crop_hints_uri_parser.add_argument('uri') - fulltext_parser = subparsers.add_parser( - 'fulltext', help=detect_fulltext.__doc__) - fulltext_parser.add_argument('path') + document_parser = subparsers.add_parser( + 'document', help=detect_document.__doc__) + document_parser.add_argument('path') - fulltext_uri_parser = subparsers.add_parser( - 'fulltext-uri', help=detect_fulltext_uri.__doc__) - fulltext_uri_parser.add_argument('uri') + document_uri_parser = subparsers.add_parser( + 'document-uri', help=detect_document_uri.__doc__) + document_uri_parser.add_argument('uri') args = parser.parse_args() diff --git a/vision/cloud-client/detect_test.py b/vision/cloud-client/detect_test.py index e0bf5b6a56a..5e7d2520a2f 100644 --- a/vision/cloud-client/detect_test.py +++ b/vision/cloud-client/detect_test.py @@ -17,7 +17,7 @@ import detect -def test_labels(capsys): +def test_labels(cloud_config, capsys): file_name = os.path.join( os.path.dirname(__file__), 'resources/wakeupcat.jpg') @@ -26,14 +26,15 @@ def test_labels(capsys): assert 'Labels' in out -def test_labels_uri(capsys): - file_name = 'gs://python-docs-samples-tests/vision/wakeupcat.jpg' +def test_labels_uri(cloud_config, capsys): + file_name = ('gs://{}/vision/wakeupcat.jpg'.format( + cloud_config.storage_bucket)) detect.detect_labels_uri(file_name) out, _ = capsys.readouterr() assert 'Labels' in out -def test_landmarks(capsys): +def test_landmarks(cloud_config, capsys): file_name = os.path.join( os.path.dirname(__file__), 'resources/landmark.jpg') @@ -42,14 +43,15 @@ def test_landmarks(capsys): assert 'Palace' in out -def test_landmarks_uri(capsys): - file_name = 'gs://python-docs-samples-tests/vision/landmark.jpg' +def test_landmarks_uri(cloud_config, capsys): + file_name = ('gs://{}/vision/landmark.jpg'.format( + cloud_config.storage_bucket)) detect.detect_landmarks_uri(file_name) out, _ = capsys.readouterr() assert 'Palace' in out -def test_faces(capsys): +def test_faces(cloud_config, capsys): file_name = os.path.join( os.path.dirname(__file__), 'resources/face_no_surprise.jpg') @@ -58,14 +60,15 @@ def test_faces(capsys): assert 'Likelihood.POSSIBLE' in out -def test_faces_uri(capsys): - file_name = 'gs://python-docs-samples-tests/vision/face_no_surprise.jpg' +def test_faces_uri(cloud_config, capsys): + file_name = ('gs://{}/vision/face_no_surprise.jpg'.format( + cloud_config.storage_bucket)) detect.detect_faces_uri(file_name) out, _ = capsys.readouterr() assert 'Likelihood.POSSIBLE' in out -def test_logos(capsys): +def test_logos(cloud_config, capsys): file_name = os.path.join( os.path.dirname(__file__), 'resources/logos.png') @@ -74,14 +77,15 @@ def test_logos(capsys): assert 'Google' in out -def test_logos_uri(capsys): - file_name = 'gs://python-docs-samples-tests/vision/logos.png' +def test_logos_uri(cloud_config, capsys): + file_name = ('gs://{}/vision/logos.png'.format( + cloud_config.storage_bucket)) detect.detect_logos_uri(file_name) out, _ = capsys.readouterr() assert 'Google' in out -def test_safe_search(capsys): +def test_safe_search(cloud_config, capsys): file_name = os.path.join( os.path.dirname(__file__), 'resources/wakeupcat.jpg') @@ -90,14 +94,15 @@ def test_safe_search(capsys): assert 'Likelihood.VERY_LIKELY' in out -def test_safe_search_uri(capsys): - file_name = 'gs://python-docs-samples-tests/vision/wakeupcat.jpg' +def test_safe_search_uri(cloud_config, capsys): + file_name = ('gs://{}/vision/wakeupcat.jpg'.format( + cloud_config.storage_bucket)) detect.detect_safe_search_uri(file_name) out, _ = capsys.readouterr() assert 'Likelihood.VERY_LIKELY' in out -def test_detect_text(capsys): +def test_detect_text(cloud_config, capsys): file_name = os.path.join( os.path.dirname(__file__), 'resources/text.jpg') @@ -106,14 +111,15 @@ def test_detect_text(capsys): assert '37%' in out -def test_detect_text_uri(capsys): - file_name = 'gs://python-docs-samples-tests/vision/text.jpg' +def test_detect_text_uri(cloud_config, capsys): + file_name = ('gs://{}/vision/text.jpg'.format( + cloud_config.storage_bucket)) detect.detect_text_uri(file_name) out, _ = capsys.readouterr() assert '37%' in out -def test_detect_properties(capsys): +def test_detect_properties(cloud_config, capsys): file_name = os.path.join( os.path.dirname(__file__), 'resources/landmark.jpg') @@ -122,15 +128,16 @@ def test_detect_properties(capsys): assert 'frac' in out -def test_detect_properties_uri(capsys): - file_name = 'gs://python-docs-samples-tests/vision/landmark.jpg' +def test_detect_properties_uri(cloud_config, capsys): + file_name = ('gs://{}/vision/landmark.jpg'.format( + cloud_config.storage_bucket)) detect.detect_properties_uri(file_name) out, _ = capsys.readouterr() assert 'frac' in out # Vision 1.1 tests -def test_detect_web(capsys): +def test_detect_web(cloud_config, capsys): file_name = os.path.join( os.path.dirname(__file__), 'resources/landmark.jpg') @@ -139,30 +146,32 @@ def test_detect_web(capsys): assert 'Description: Palace of Fine Arts Theatre' in out -def test_detect_web_uri(capsys): - file_name = 'gs://python-docs-samples-tests/vision/landmark.jpg' +def test_detect_web_uri(cloud_config, capsys): + file_name = ('gs://{}/vision/landmark.jpg'.format( + cloud_config.storage_bucket)) detect.detect_web_uri(file_name) out, _ = capsys.readouterr() assert 'Description: Palace of Fine Arts Theatre' in out -def test_detect_fulltext(capsys): +def test_detect_document(cloud_config, capsys): file_name = os.path.join( os.path.dirname(__file__), 'resources/text.jpg') - detect.detect_fulltext(file_name) + detect.detect_document(file_name) out, _ = capsys.readouterr() assert '37%' in out -def test_detect_fulltext_uri(capsys): - file_name = 'gs://python-docs-samples-tests/vision/text.jpg' - detect.detect_fulltext_uri(file_name) +def test_detect_document_uri(cloud_config, capsys): + file_name = ('gs://{}/vision/text.jpg'.format( + cloud_config.storage_bucket)) + detect.detect_document_uri(file_name) out, _ = capsys.readouterr() assert '37%' in out -def test_detect_crop_hints(capsys): +def test_detect_crop_hints(cloud_config, capsys): file_name = os.path.join( os.path.dirname(__file__), 'resources/wakeupcat.jpg') @@ -171,8 +180,9 @@ def test_detect_crop_hints(capsys): assert 'bounds: (0,0)' in out -def test_detect_crop_hints_uri(capsys): - file_name = 'gs://python-docs-samples-tests/vision/wakeupcat.jpg' +def test_detect_crop_hints_uri(cloud_config, capsys): + file_name = ('gs://{}/vision/wakeupcat.jpg'.format( + cloud_config.storage_bucket)) detect.detect_crop_hints_uri(file_name) out, _ = capsys.readouterr() assert 'bounds: (0,0)' in out