Skip to content

Commit

Permalink
docs: Add example of how to use max_results (#277)
Browse files Browse the repository at this point in the history
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 🦕
  • Loading branch information
andrewferlitsch authored and dandhlee committed Nov 15, 2022
1 parent e71dc96 commit b18bea8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
29 changes: 20 additions & 9 deletions vision/snippets/product_search/product_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
Expand All @@ -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: ')
Expand Down Expand Up @@ -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__)
Expand All @@ -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)
5 changes: 3 additions & 2 deletions vision/snippets/product_search/product_search_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b18bea8

Please sign in to comment.