From f4b084b4d9f5b5929459f755ac2d471feaa8debf Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Fri, 10 Dec 2021 15:04:16 -0800 Subject: [PATCH] docs: Add example of how to use max_results (#277) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-vision/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes b/203493363 🦕 --- .../snippets/product_search/product_search.py | 29 +++++++++++++------ .../product_search/product_search_test.py | 5 ++-- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/packages/google-cloud-vision/samples/snippets/product_search/product_search.py b/packages/google-cloud-vision/samples/snippets/product_search/product_search.py index abfd53429fbb..095f28e1939b 100755 --- a/packages/google-cloud-vision/samples/snippets/product_search/product_search.py +++ b/packages/google-cloud-vision/samples/snippets/product_search/product_search.py @@ -34,8 +34,14 @@ # [START vision_product_search_get_similar_products] def get_similar_products_file( - project_id, location, product_set_id, product_category, - file_path, filter): + project_id, + location, + product_set_id, + product_category, + file_path, + filter, + max_results +): """Search similar products to image. Args: project_id: Id of the project. @@ -44,10 +50,11 @@ def get_similar_products_file( product_category: Category of the product. file_path: Local file path of the image to be searched. filter: Condition to be applied on the labels. - Example for filter: (color = red OR color = blue) AND style = kids - It will search on all products with the following labels: - color:red AND style:kids - color:blue AND style:kids + Example for filter: (color = red OR color = blue) AND style = kids + It will search on all products with the following labels: + color:red AND style:kids + color:blue AND style:kids + max_results: The maximum number of results (matches) to return. If omitted, all results are returned. """ # product_search_client is needed only for its helper methods. product_search_client = vision.ProductSearchClient() @@ -73,7 +80,10 @@ def get_similar_products_file( # Search products similar to the image. response = image_annotator_client.product_search( - image, image_context=image_context) + image, + image_context=image_context, + max_results=max_results + ) index_time = response.product_search_results.index_time print('Product set index time: ') @@ -173,6 +183,7 @@ def get_similar_products_uri( parser.add_argument('--product_set_id') parser.add_argument('--product_category') parser.add_argument('--filter', default='') + parser.add_argument('--max_results', default='') get_similar_products_file_parser = subparsers.add_parser( 'get_similar_products_file', help=get_similar_products_file.__doc__) @@ -187,8 +198,8 @@ def get_similar_products_uri( if args.command == 'get_similar_products_file': get_similar_products_file( args.project_id, args.location, args.product_set_id, - args.product_category, args.file_path, args.filter) + args.product_category, args.file_path, args.filter, args.max_results) elif args.command == 'get_similar_products_uri': get_similar_products_uri( args.project_id, args.location, args.product_set_id, - args.product_category, args.image_uri, args.filter) + args.product_category, args.image_uri, args.filter, args.max_results) diff --git a/packages/google-cloud-vision/samples/snippets/product_search/product_search_test.py b/packages/google-cloud-vision/samples/snippets/product_search/product_search_test.py index 8f862fc215a0..c7d00998d337 100644 --- a/packages/google-cloud-vision/samples/snippets/product_search/product_search_test.py +++ b/packages/google-cloud-vision/samples/snippets/product_search/product_search_test.py @@ -30,13 +30,14 @@ FILE_PATH_1 = 'resources/shoes_1.jpg' IMAGE_URI_1 = 'gs://cloud-samples-data/vision/product_search/shoes_1.jpg' FILTER = 'style=womens' +MAX_RESULTS = 6 @pytest.mark.flaky(max_runs=5, min_passes=1) def test_get_similar_products_file(capsys): get_similar_products_file( PROJECT_ID, LOCATION, PRODUCT_SET_ID, PRODUCT_CATEGORY, FILE_PATH_1, - '') + '', MAX_RESULTS) out, _ = capsys.readouterr() assert PRODUCT_ID_1 in out assert PRODUCT_ID_2 in out @@ -54,7 +55,7 @@ def test_get_similar_products_uri(capsys): def test_get_similar_products_file_with_filter(capsys): get_similar_products_file( PROJECT_ID, LOCATION, PRODUCT_SET_ID, PRODUCT_CATEGORY, FILE_PATH_1, - FILTER) + FILTER, MAX_RESULTS) out, _ = capsys.readouterr() assert PRODUCT_ID_1 in out assert PRODUCT_ID_2 not in out