Skip to content

Commit a14c009

Browse files
docs: Add example of how to use max_results (#277)
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 🦕
1 parent dcc35c6 commit a14c009

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

vision/snippets/product_search/product_search.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@
3434

3535
# [START vision_product_search_get_similar_products]
3636
def get_similar_products_file(
37-
project_id, location, product_set_id, product_category,
38-
file_path, filter):
37+
project_id,
38+
location,
39+
product_set_id,
40+
product_category,
41+
file_path,
42+
filter,
43+
max_results
44+
):
3945
"""Search similar products to image.
4046
Args:
4147
project_id: Id of the project.
@@ -44,10 +50,11 @@ def get_similar_products_file(
4450
product_category: Category of the product.
4551
file_path: Local file path of the image to be searched.
4652
filter: Condition to be applied on the labels.
47-
Example for filter: (color = red OR color = blue) AND style = kids
48-
It will search on all products with the following labels:
49-
color:red AND style:kids
50-
color:blue AND style:kids
53+
Example for filter: (color = red OR color = blue) AND style = kids
54+
It will search on all products with the following labels:
55+
color:red AND style:kids
56+
color:blue AND style:kids
57+
max_results: The maximum number of results (matches) to return. If omitted, all results are returned.
5158
"""
5259
# product_search_client is needed only for its helper methods.
5360
product_search_client = vision.ProductSearchClient()
@@ -73,7 +80,10 @@ def get_similar_products_file(
7380

7481
# Search products similar to the image.
7582
response = image_annotator_client.product_search(
76-
image, image_context=image_context)
83+
image,
84+
image_context=image_context,
85+
max_results=max_results
86+
)
7787

7888
index_time = response.product_search_results.index_time
7989
print('Product set index time: ')
@@ -173,6 +183,7 @@ def get_similar_products_uri(
173183
parser.add_argument('--product_set_id')
174184
parser.add_argument('--product_category')
175185
parser.add_argument('--filter', default='')
186+
parser.add_argument('--max_results', default='')
176187

177188
get_similar_products_file_parser = subparsers.add_parser(
178189
'get_similar_products_file', help=get_similar_products_file.__doc__)
@@ -187,8 +198,8 @@ def get_similar_products_uri(
187198
if args.command == 'get_similar_products_file':
188199
get_similar_products_file(
189200
args.project_id, args.location, args.product_set_id,
190-
args.product_category, args.file_path, args.filter)
201+
args.product_category, args.file_path, args.filter, args.max_results)
191202
elif args.command == 'get_similar_products_uri':
192203
get_similar_products_uri(
193204
args.project_id, args.location, args.product_set_id,
194-
args.product_category, args.image_uri, args.filter)
205+
args.product_category, args.image_uri, args.filter, args.max_results)

vision/snippets/product_search/product_search_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@
3030
FILE_PATH_1 = 'resources/shoes_1.jpg'
3131
IMAGE_URI_1 = 'gs://cloud-samples-data/vision/product_search/shoes_1.jpg'
3232
FILTER = 'style=womens'
33+
MAX_RESULTS = 6
3334

3435

3536
@pytest.mark.flaky(max_runs=5, min_passes=1)
3637
def test_get_similar_products_file(capsys):
3738
get_similar_products_file(
3839
PROJECT_ID, LOCATION, PRODUCT_SET_ID, PRODUCT_CATEGORY, FILE_PATH_1,
39-
'')
40+
'', MAX_RESULTS)
4041
out, _ = capsys.readouterr()
4142
assert PRODUCT_ID_1 in out
4243
assert PRODUCT_ID_2 in out
@@ -54,7 +55,7 @@ def test_get_similar_products_uri(capsys):
5455
def test_get_similar_products_file_with_filter(capsys):
5556
get_similar_products_file(
5657
PROJECT_ID, LOCATION, PRODUCT_SET_ID, PRODUCT_CATEGORY, FILE_PATH_1,
57-
FILTER)
58+
FILTER, MAX_RESULTS)
5859
out, _ = capsys.readouterr()
5960
assert PRODUCT_ID_1 in out
6061
assert PRODUCT_ID_2 not in out

0 commit comments

Comments
 (0)