-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Product search #1580
Merged
dizcology
merged 16 commits into
GoogleCloudPlatform:master
from
dizcology:product-search
Jul 23, 2018
Merged
Product search #1580
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
66b2996
add product_management and tests
dizcology f348653
flake
dizcology 04f8c3a
add reference image management and tests
dizcology b239ba1
add product set management and tests
dizcology 3ecc747
add product_in_product_set management and tests
dizcology f15ac83
flake
dizcology 8e28757
use fixtures
dizcology e7fa0f7
flake
dizcology bdae459
import order
dizcology 7a94a88
add import product sets and tests
dizcology 4879178
add sample resources
dizcology c41ac2b
flake
dizcology b6531ab
add product search and tests
dizcology 4168901
flake
dizcology f40a0bf
flake
dizcology b561e29
remove print line
dizcology File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2018 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""This application demonstrates how to perform import product sets operations | ||
on Product set in Cloud Vision Product Search. | ||
|
||
For more information, see the tutorial page at | ||
https://cloud.google.com/vision/product-search/docs/ | ||
""" | ||
|
||
# [START product_search_import] | ||
import argparse | ||
|
||
from google.cloud import vision_v1p3beta1 as vision | ||
# [END product_search_import] | ||
|
||
|
||
# [START product_search_import_product_sets] | ||
def import_product_sets(project_id, location, gcs_uri): | ||
"""Import images of different products in the product set. | ||
Args: | ||
project_id: Id of the project. | ||
location: A compute region name. | ||
gcs_uri: Google Cloud Storage URI. | ||
Target files must be in Product Search CSV format. | ||
""" | ||
client = vision.ProductSearchClient() | ||
|
||
# A resource that represents Google Cloud Platform location. | ||
location_path = client.location_path( | ||
project=project_id, location=location) | ||
|
||
# Set the input configuration along with Google Cloud Storage URI | ||
gcs_source = vision.types.ImportProductSetsGcsSource( | ||
csv_file_uri=gcs_uri) | ||
input_config = vision.types.ImportProductSetsInputConfig( | ||
gcs_source=gcs_source) | ||
|
||
# Import the product sets from the input URI. | ||
response = client.import_product_sets( | ||
parent=location_path, input_config=input_config) | ||
|
||
print('Processing operation name: {}'.format(response.operation.name)) | ||
# synchronous check of operation status | ||
result = response.result() | ||
print('Processing done.') | ||
print('Results of the processing:') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think just 'Results:' might read better? |
||
|
||
for i, status in enumerate(result.statuses): | ||
print('Status of processing line {} of the csv: {}'.format( | ||
i, status)) | ||
# Check the status of reference image | ||
# `0` is the code for OK in google.rpc.Code. | ||
if status.code == 0: | ||
reference_image = result.reference_images[i] | ||
print(reference_image) | ||
else: | ||
print('Status code not OK: {}'.format(status.message)) | ||
# [END product_search_import_product_sets] | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter) | ||
subparsers = parser.add_subparsers(dest='command') | ||
parser.add_argument( | ||
'--project_id', | ||
help='Project id. Required', | ||
required=True) | ||
parser.add_argument( | ||
'--location', | ||
help='Compute region name', | ||
default='us-west1') | ||
|
||
import_product_sets_parser = subparsers.add_parser( | ||
'import_product_sets', help=import_product_sets.__doc__) | ||
import_product_sets_parser.add_argument('gcs_uri') | ||
|
||
args = parser.parse_args() | ||
|
||
if args.command == 'import_product_sets': | ||
import_product_sets(args.project_id, args.location, args.gcs_uri) |
93 changes: 93 additions & 0 deletions
93
vision/cloud-client/product_search/import_product_sets_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Copyright 2016 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
|
||
import pytest | ||
|
||
from import_product_sets import import_product_sets | ||
from product_in_product_set_management import list_products_in_product_set | ||
from product_management import delete_product, list_products | ||
from product_set_management import delete_product_set, list_product_sets | ||
from reference_image_management import list_reference_images | ||
|
||
|
||
PROJECT_ID = os.getenv('GCLOUD_PROJECT') | ||
LOCATION = 'us-west1' | ||
|
||
GCS_URI = 'gs://python-docs-samples-tests/product_search/product_sets.csv' | ||
PRODUCT_SET_DISPLAY_NAME = 'fake_product_set_display_name_for_testing' | ||
PRODUCT_SET_ID = 'fake_product_set_id_for_testing' | ||
PRODUCT_ID_1 = 'fake_product_id_for_testing_1' | ||
PRODUCT_ID_2 = 'fake_product_id_for_testing_2' | ||
IMAGE_URI_1 = 'shoes_1.jpg' | ||
IMAGE_URI_2 = 'shoes_2.jpg' | ||
|
||
|
||
@pytest.fixture | ||
def teardown(): | ||
# no set up, tear down only | ||
yield None | ||
|
||
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID_1) | ||
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID_2) | ||
delete_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID) | ||
|
||
|
||
def test_import_product_sets(capsys, teardown): | ||
list_product_sets(PROJECT_ID, LOCATION) | ||
out, _ = capsys.readouterr() | ||
assert PRODUCT_SET_ID not in out | ||
|
||
list_products(PROJECT_ID, LOCATION) | ||
out, _ = capsys.readouterr() | ||
assert PRODUCT_ID_1 not in out | ||
assert PRODUCT_ID_2 not in out | ||
|
||
list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID) | ||
out, _ = capsys.readouterr() | ||
assert PRODUCT_ID_1 not in out | ||
assert PRODUCT_ID_2 not in out | ||
|
||
list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_1) | ||
out, _ = capsys.readouterr() | ||
assert IMAGE_URI_1 not in out | ||
|
||
list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_2) | ||
out, _ = capsys.readouterr() | ||
assert IMAGE_URI_2 not in out | ||
|
||
import_product_sets(PROJECT_ID, LOCATION, GCS_URI) | ||
|
||
list_product_sets(PROJECT_ID, LOCATION) | ||
out, _ = capsys.readouterr() | ||
assert PRODUCT_SET_ID in out | ||
|
||
list_products(PROJECT_ID, LOCATION) | ||
out, _ = capsys.readouterr() | ||
assert PRODUCT_ID_1 in out | ||
assert PRODUCT_ID_2 in out | ||
|
||
list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID) | ||
out, _ = capsys.readouterr() | ||
assert PRODUCT_ID_1 in out | ||
assert PRODUCT_ID_2 in out | ||
|
||
list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_1) | ||
out, _ = capsys.readouterr() | ||
assert IMAGE_URI_1 in out | ||
|
||
list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_2) | ||
out, _ = capsys.readouterr() | ||
assert IMAGE_URI_2 in out |
159 changes: 159 additions & 0 deletions
159
vision/cloud-client/product_search/product_in_product_set_management.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2018 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""This application demonstrates how to perform create operations | ||
on Product set in Cloud Vision Product Search. | ||
|
||
For more information, see the tutorial page at | ||
https://cloud.google.com/vision/product-search/docs/ | ||
""" | ||
|
||
# [START product_search_import] | ||
import argparse | ||
|
||
from google.cloud import vision_v1p3beta1 as vision | ||
# [END product_search_import] | ||
|
||
|
||
# [START product_search_add_product_to_product_set] | ||
def add_product_to_product_set( | ||
project_id, location, product_id, product_set_id): | ||
"""Add a product to a product set. | ||
Args: | ||
project_id: Id of the project. | ||
location: A compute region name. | ||
product_id: Id of the product. | ||
product_set_id: Id of the product set. | ||
""" | ||
client = vision.ProductSearchClient() | ||
|
||
# Get the full path of the product set. | ||
product_set_path = client.product_set_path( | ||
project=project_id, location=location, | ||
product_set=product_set_id) | ||
|
||
# Get the full path of the product. | ||
product_path = client.product_path( | ||
project=project_id, location=location, product=product_id) | ||
|
||
# Add the product to the product set. | ||
client.add_product_to_product_set( | ||
name=product_set_path, product=product_path) | ||
print('Product added to product set.') | ||
# [END product_search_add_product_to_product_set] | ||
|
||
|
||
# [START product_search_list_products_in_product_set] | ||
def list_products_in_product_set( | ||
project_id, location, product_set_id): | ||
"""List all products in a product set. | ||
Args: | ||
project_id: Id of the project. | ||
location: A compute region name. | ||
product_set_id: Id of the product set. | ||
""" | ||
client = vision.ProductSearchClient() | ||
|
||
# Get the full path of the product set. | ||
product_set_path = client.product_set_path( | ||
project=project_id, location=location, | ||
product_set=product_set_id) | ||
|
||
# List all the products available in the product set. | ||
products = client.list_products_in_product_set(name=product_set_path) | ||
|
||
# Display the product information. | ||
for product in products: | ||
print('Product name: {}'.format(product.name)) | ||
print('Product id: {}'.format(product.name.split('/')[-1])) | ||
print('Product display name: {}'.format(product.display_name)) | ||
print('Product description: {}'.format(product.description)) | ||
print('Product category: {}'.format(product.product_category)) | ||
print('Product labels: {}'.format(product.product_labels)) | ||
# [END product_search_list_products_in_product_set] | ||
|
||
|
||
# [START product_search_remove_product_from_product_set] | ||
def remove_product_from_product_set( | ||
project_id, location, product_id, product_set_id): | ||
"""Remove a product from a product set. | ||
Args: | ||
project_id: Id of the project. | ||
location: A compute region name. | ||
product_id: Id of the product. | ||
product_set_id: Id of the product set. | ||
""" | ||
client = vision.ProductSearchClient() | ||
|
||
# Get the full path of the product set. | ||
product_set_path = client.product_set_path( | ||
project=project_id, location=location, | ||
product_set=product_set_id) | ||
|
||
# Get the full path of the product. | ||
product_path = client.product_path( | ||
project=project_id, location=location, product=product_id) | ||
|
||
# Remove the product from the product set. | ||
client.remove_product_from_product_set( | ||
name=product_set_path, product=product_path) | ||
print('Product removed from product set.') | ||
# [END product_search_remove_product_from_product_set] | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter) | ||
subparsers = parser.add_subparsers(dest='command') | ||
parser.add_argument( | ||
'--project_id', | ||
help='Project id. Required', | ||
required=True) | ||
parser.add_argument( | ||
'--location', | ||
help='Compute region name', | ||
default='us-west1') | ||
|
||
add_product_to_product_set_parser = subparsers.add_parser( | ||
'add_product_to_product_set', help=add_product_to_product_set.__doc__) | ||
add_product_to_product_set_parser.add_argument('product_id') | ||
add_product_to_product_set_parser.add_argument('product_set_id') | ||
|
||
list_products_in_product_set_parser = subparsers.add_parser( | ||
'list_products_in_product_set', | ||
help=list_products_in_product_set.__doc__) | ||
list_products_in_product_set_parser.add_argument('product_set_id') | ||
|
||
remove_product_from_product_set_parser = subparsers.add_parser( | ||
'remove_product_from_product_set', | ||
help=remove_product_from_product_set.__doc__) | ||
remove_product_from_product_set_parser.add_argument('product_id') | ||
remove_product_from_product_set_parser.add_argument('product_set_id') | ||
|
||
args = parser.parse_args() | ||
|
||
if args.command == 'add_product_to_product_set': | ||
add_product_to_product_set( | ||
args.project_id, args.location, args.product_id, | ||
args.product_set_id) | ||
elif args.command == 'list_products_in_product_set': | ||
list_products_in_product_set( | ||
args.project_id, args.location, args.product_set_id) | ||
elif args.command == 'remove_product_from_product_set': | ||
remove_product_from_product_set( | ||
args.project_id, args.location, args.product_id, | ||
args.product_set_id) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generated libraries use 'llc'
I have seen both but I think Google LLC is the more recent form?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some other python samples use Inc. Keeping these samples consistent for now.