Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 484f1ad

Browse filesBrowse files
authored
[DO NOT MERGE] Product search (GoogleCloudPlatform#1580)
Product search
1 parent a918a34 commit 484f1ad
Copy full SHA for 484f1ad
Expand file treeCollapse file tree

17 files changed

+1517
-0
lines changed
+95Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This application demonstrates how to perform import product sets operations
18+
on Product set in Cloud Vision Product Search.
19+
20+
For more information, see the tutorial page at
21+
https://cloud.google.com/vision/product-search/docs/
22+
"""
23+
24+
# [START product_search_import]
25+
import argparse
26+
27+
from google.cloud import vision_v1p3beta1 as vision
28+
# [END product_search_import]
29+
30+
31+
# [START product_search_import_product_sets]
32+
def import_product_sets(project_id, location, gcs_uri):
33+
"""Import images of different products in the product set.
34+
Args:
35+
project_id: Id of the project.
36+
location: A compute region name.
37+
gcs_uri: Google Cloud Storage URI.
38+
Target files must be in Product Search CSV format.
39+
"""
40+
client = vision.ProductSearchClient()
41+
42+
# A resource that represents Google Cloud Platform location.
43+
location_path = client.location_path(
44+
project=project_id, location=location)
45+
46+
# Set the input configuration along with Google Cloud Storage URI
47+
gcs_source = vision.types.ImportProductSetsGcsSource(
48+
csv_file_uri=gcs_uri)
49+
input_config = vision.types.ImportProductSetsInputConfig(
50+
gcs_source=gcs_source)
51+
52+
# Import the product sets from the input URI.
53+
response = client.import_product_sets(
54+
parent=location_path, input_config=input_config)
55+
56+
print('Processing operation name: {}'.format(response.operation.name))
57+
# synchronous check of operation status
58+
result = response.result()
59+
print('Processing done.')
60+
61+
for i, status in enumerate(result.statuses):
62+
print('Status of processing line {} of the csv: {}'.format(
63+
i, status))
64+
# Check the status of reference image
65+
# `0` is the code for OK in google.rpc.Code.
66+
if status.code == 0:
67+
reference_image = result.reference_images[i]
68+
print(reference_image)
69+
else:
70+
print('Status code not OK: {}'.format(status.message))
71+
# [END product_search_import_product_sets]
72+
73+
74+
if __name__ == '__main__':
75+
parser = argparse.ArgumentParser(
76+
description=__doc__,
77+
formatter_class=argparse.RawDescriptionHelpFormatter)
78+
subparsers = parser.add_subparsers(dest='command')
79+
parser.add_argument(
80+
'--project_id',
81+
help='Project id. Required',
82+
required=True)
83+
parser.add_argument(
84+
'--location',
85+
help='Compute region name',
86+
default='us-west1')
87+
88+
import_product_sets_parser = subparsers.add_parser(
89+
'import_product_sets', help=import_product_sets.__doc__)
90+
import_product_sets_parser.add_argument('gcs_uri')
91+
92+
args = parser.parse_args()
93+
94+
if args.command == 'import_product_sets':
95+
import_product_sets(args.project_id, args.location, args.gcs_uri)
+93Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import pytest
18+
19+
from import_product_sets import import_product_sets
20+
from product_in_product_set_management import list_products_in_product_set
21+
from product_management import delete_product, list_products
22+
from product_set_management import delete_product_set, list_product_sets
23+
from reference_image_management import list_reference_images
24+
25+
26+
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
27+
LOCATION = 'us-west1'
28+
29+
GCS_URI = 'gs://python-docs-samples-tests/product_search/product_sets.csv'
30+
PRODUCT_SET_DISPLAY_NAME = 'fake_product_set_display_name_for_testing'
31+
PRODUCT_SET_ID = 'fake_product_set_id_for_testing'
32+
PRODUCT_ID_1 = 'fake_product_id_for_testing_1'
33+
PRODUCT_ID_2 = 'fake_product_id_for_testing_2'
34+
IMAGE_URI_1 = 'shoes_1.jpg'
35+
IMAGE_URI_2 = 'shoes_2.jpg'
36+
37+
38+
@pytest.fixture
39+
def teardown():
40+
# no set up, tear down only
41+
yield None
42+
43+
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID_1)
44+
delete_product(PROJECT_ID, LOCATION, PRODUCT_ID_2)
45+
delete_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
46+
47+
48+
def test_import_product_sets(capsys, teardown):
49+
list_product_sets(PROJECT_ID, LOCATION)
50+
out, _ = capsys.readouterr()
51+
assert PRODUCT_SET_ID not in out
52+
53+
list_products(PROJECT_ID, LOCATION)
54+
out, _ = capsys.readouterr()
55+
assert PRODUCT_ID_1 not in out
56+
assert PRODUCT_ID_2 not in out
57+
58+
list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
59+
out, _ = capsys.readouterr()
60+
assert PRODUCT_ID_1 not in out
61+
assert PRODUCT_ID_2 not in out
62+
63+
list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_1)
64+
out, _ = capsys.readouterr()
65+
assert IMAGE_URI_1 not in out
66+
67+
list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_2)
68+
out, _ = capsys.readouterr()
69+
assert IMAGE_URI_2 not in out
70+
71+
import_product_sets(PROJECT_ID, LOCATION, GCS_URI)
72+
73+
list_product_sets(PROJECT_ID, LOCATION)
74+
out, _ = capsys.readouterr()
75+
assert PRODUCT_SET_ID in out
76+
77+
list_products(PROJECT_ID, LOCATION)
78+
out, _ = capsys.readouterr()
79+
assert PRODUCT_ID_1 in out
80+
assert PRODUCT_ID_2 in out
81+
82+
list_products_in_product_set(PROJECT_ID, LOCATION, PRODUCT_SET_ID)
83+
out, _ = capsys.readouterr()
84+
assert PRODUCT_ID_1 in out
85+
assert PRODUCT_ID_2 in out
86+
87+
list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_1)
88+
out, _ = capsys.readouterr()
89+
assert IMAGE_URI_1 in out
90+
91+
list_reference_images(PROJECT_ID, LOCATION, PRODUCT_ID_2)
92+
out, _ = capsys.readouterr()
93+
assert IMAGE_URI_2 in out
+159Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This application demonstrates how to perform create operations
18+
on Product set in Cloud Vision Product Search.
19+
20+
For more information, see the tutorial page at
21+
https://cloud.google.com/vision/product-search/docs/
22+
"""
23+
24+
# [START product_search_import]
25+
import argparse
26+
27+
from google.cloud import vision_v1p3beta1 as vision
28+
# [END product_search_import]
29+
30+
31+
# [START product_search_add_product_to_product_set]
32+
def add_product_to_product_set(
33+
project_id, location, product_id, product_set_id):
34+
"""Add a product to a product set.
35+
Args:
36+
project_id: Id of the project.
37+
location: A compute region name.
38+
product_id: Id of the product.
39+
product_set_id: Id of the product set.
40+
"""
41+
client = vision.ProductSearchClient()
42+
43+
# Get the full path of the product set.
44+
product_set_path = client.product_set_path(
45+
project=project_id, location=location,
46+
product_set=product_set_id)
47+
48+
# Get the full path of the product.
49+
product_path = client.product_path(
50+
project=project_id, location=location, product=product_id)
51+
52+
# Add the product to the product set.
53+
client.add_product_to_product_set(
54+
name=product_set_path, product=product_path)
55+
print('Product added to product set.')
56+
# [END product_search_add_product_to_product_set]
57+
58+
59+
# [START product_search_list_products_in_product_set]
60+
def list_products_in_product_set(
61+
project_id, location, product_set_id):
62+
"""List all products in a product set.
63+
Args:
64+
project_id: Id of the project.
65+
location: A compute region name.
66+
product_set_id: Id of the product set.
67+
"""
68+
client = vision.ProductSearchClient()
69+
70+
# Get the full path of the product set.
71+
product_set_path = client.product_set_path(
72+
project=project_id, location=location,
73+
product_set=product_set_id)
74+
75+
# List all the products available in the product set.
76+
products = client.list_products_in_product_set(name=product_set_path)
77+
78+
# Display the product information.
79+
for product in products:
80+
print('Product name: {}'.format(product.name))
81+
print('Product id: {}'.format(product.name.split('/')[-1]))
82+
print('Product display name: {}'.format(product.display_name))
83+
print('Product description: {}'.format(product.description))
84+
print('Product category: {}'.format(product.product_category))
85+
print('Product labels: {}'.format(product.product_labels))
86+
# [END product_search_list_products_in_product_set]
87+
88+
89+
# [START product_search_remove_product_from_product_set]
90+
def remove_product_from_product_set(
91+
project_id, location, product_id, product_set_id):
92+
"""Remove a product from a product set.
93+
Args:
94+
project_id: Id of the project.
95+
location: A compute region name.
96+
product_id: Id of the product.
97+
product_set_id: Id of the product set.
98+
"""
99+
client = vision.ProductSearchClient()
100+
101+
# Get the full path of the product set.
102+
product_set_path = client.product_set_path(
103+
project=project_id, location=location,
104+
product_set=product_set_id)
105+
106+
# Get the full path of the product.
107+
product_path = client.product_path(
108+
project=project_id, location=location, product=product_id)
109+
110+
# Remove the product from the product set.
111+
client.remove_product_from_product_set(
112+
name=product_set_path, product=product_path)
113+
print('Product removed from product set.')
114+
# [END product_search_remove_product_from_product_set]
115+
116+
117+
if __name__ == '__main__':
118+
parser = argparse.ArgumentParser(
119+
description=__doc__,
120+
formatter_class=argparse.RawDescriptionHelpFormatter)
121+
subparsers = parser.add_subparsers(dest='command')
122+
parser.add_argument(
123+
'--project_id',
124+
help='Project id. Required',
125+
required=True)
126+
parser.add_argument(
127+
'--location',
128+
help='Compute region name',
129+
default='us-west1')
130+
131+
add_product_to_product_set_parser = subparsers.add_parser(
132+
'add_product_to_product_set', help=add_product_to_product_set.__doc__)
133+
add_product_to_product_set_parser.add_argument('product_id')
134+
add_product_to_product_set_parser.add_argument('product_set_id')
135+
136+
list_products_in_product_set_parser = subparsers.add_parser(
137+
'list_products_in_product_set',
138+
help=list_products_in_product_set.__doc__)
139+
list_products_in_product_set_parser.add_argument('product_set_id')
140+
141+
remove_product_from_product_set_parser = subparsers.add_parser(
142+
'remove_product_from_product_set',
143+
help=remove_product_from_product_set.__doc__)
144+
remove_product_from_product_set_parser.add_argument('product_id')
145+
remove_product_from_product_set_parser.add_argument('product_set_id')
146+
147+
args = parser.parse_args()
148+
149+
if args.command == 'add_product_to_product_set':
150+
add_product_to_product_set(
151+
args.project_id, args.location, args.product_id,
152+
args.product_set_id)
153+
elif args.command == 'list_products_in_product_set':
154+
list_products_in_product_set(
155+
args.project_id, args.location, args.product_set_id)
156+
elif args.command == 'remove_product_from_product_set':
157+
remove_product_from_product_set(
158+
args.project_id, args.location, args.product_id,
159+
args.product_set_id)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.