Skip to content

Commit

Permalink
[DO NOT MERGE] Product search (#1580)
Browse files Browse the repository at this point in the history
Product search
  • Loading branch information
dizcology authored Jul 23, 2018
1 parent a918a34 commit 484f1ad
Show file tree
Hide file tree
Showing 17 changed files with 1,517 additions and 0 deletions.
95 changes: 95 additions & 0 deletions vision/cloud-client/product_search/import_product_sets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/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.')

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 vision/cloud-client/product_search/import_product_sets_test.py
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
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)
Loading

0 comments on commit 484f1ad

Please sign in to comment.