Pleas help me with creating a product set

27 views
Skip to first unread message

Iyrin lee

unread,
May 29, 2020, 8:12:59 AM5/29/20
to cloud-vision-discuss
Hi I'm trying to use Vision API product search, but I can't understand creating a product set.

I'm working in colab and I copied code from github.page that is linked with google official guide. 

And this is what I tried

#!/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
#
#
# 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 operations
on Product set in Cloud Vision Product Search.
For more information, see the tutorial page at
"""

import argparse

# [START vision_product_search_delete_product_set]
# [START vision_product_search_list_product_sets]
# [START vision_product_search_get_product_set]
# [START vision_product_search_create_product_set]
from google.cloud import vision

# [END vision_product_search_delete_product_set]
# [END vision_product_search_list_product_sets]
# [END vision_product_search_get_product_set]
# [END vision_product_search_create_product_set]


# [START vision_product_search_create_product_set]
def create_product_set(
        project_idlocationproduct_set_idproduct_set_display_name):

    project_id: "drugrec-278004"
    location: "asia-east1"
    product_set_id: "product_set_01"
    product_set_display_name: "id_dr"

    client = vision.ProductSearchClient()

    # A resource that represents Google Cloud Platform location.
    location_path = client.location_path(
        project=project_id, location=location)

    # Create a product set with the product set specification in the region.
    product_set = vision.types.ProductSet(
            display_name=product_set_display_name)

    # The response is the product set with `name` populated.
    response = client.create_product_set(
        parent=location_path,
        product_set=product_set,
        product_set_id=product_set_id)

    # Display the product set information.
    print('Product set name: {}'.format(response.name))
# [END vision_product_search_create_product_set]


# [START vision_product_search_list_product_sets]
def list_product_sets(project_idlocation):
    """List all product sets.
    Args:
        project_id: Id of the project.
        location: A compute region name.
    """
    project_id: "drugrec-278004"
    location: "asia-east1"

    client = vision.ProductSearchClient()

    # A resource that represents Google Cloud Platform location.
    location_path = client.location_path(
        project=project_id, location=location)

    # List all the product sets available in the region.
    product_sets = client.list_product_sets(parent=location_path)

    # Display the product set information.
    for product_set in product_sets:
        print('Product set name: {}'.format(product_set.name))
        print('Product set id: {}'.format(product_set.name.split('/')[-1]))
        print('Product set display name: {}'.format(product_set.display_name))
        print('Product set index time:')
        print('  seconds: {}'.format(product_set.index_time.seconds))
        print('  nanos: {}\n'.format(product_set.index_time.nanos))
# [END vision_product_search_list_product_sets]


# [START vision_product_search_get_product_set]
def get_product_set(project_idlocationproduct_set_id):
    """Get info about the product set.
    Args:
        project_id: Id of the project.
        location: A compute region name.
        product_set_id: Id of the product set.
    """

    project_id: "drugrec-278004"
    location: "asia-east1"
    product_set_id: "product_set_01"

    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 complete detail of the product set.
    product_set = client.get_product_set(name=product_set_path)

    # Display the product set information.
    print('Product set name: {}'.format(product_set.name))
    print('Product set id: {}'.format(product_set.name.split('/')[-1]))
    print('Product set display name: {}'.format(product_set.display_name))
    print('Product set index time:')
    print('  seconds: {}'.format(product_set.index_time.seconds))
    print('  nanos: {}'.format(product_set.index_time.nanos))
# [END vision_product_search_get_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')

    create_product_set_parser = subparsers.add_parser(
        'create_product_set', help=create_product_set.__doc__)
    create_product_set_parser.add_argument('product_set_id')
    create_product_set_parser.add_argument('product_set_display_name')

    list_product_sets_parser = subparsers.add_parser(
        'list_product_sets', help=list_product_sets.__doc__)

    get_product_set_parser = subparsers.add_parser(
        'get_product_set', help=get_product_set.__doc__)
    get_product_set_parser.add_argument('product_set_id')

    delete_product_set_parser = subparsers.add_parser(
        'delete_product_set', help=delete_product_set.__doc__)
    delete_product_set_parser.add_argument('product_set_id')

    args = parser.parse_args()

    if args.command == 'create_product_set':
        create_product_set(
            args.project_id, args.location, args.product_set_id,
            args.product_set_display_name)
    elif args.command == 'list_product_sets':
        list_product_sets(args.project_id, args.location)
    elif args.command == 'get_product_set':
        get_product_set(args.project_id, args.location, args.product_set_id)
    elif args.command == 'delete_product_set':
        delete_product_set(
            args.project_id, args.location, args.product_set_id)

and the result was 
usage: ipykernel_launcher.py [-h] --project_id PROJECT_ID
                             [--location LOCATION]
                             {create_product_set,list_product_sets,get_product_set,delete_product_set}
                             ...
ipykernel_launcher.py: error: argument command: invalid choice: '/root/.local/share/jupyter/runtime/kernel-cb44c4e9-a032-45bb-a6f0-a7b0a1694e76.json' (choose from 'create_product_set', 'list_product_sets', 'get_product_set', 'delete_product_set')
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2
/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py:2890: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)


I can't figure out what was wrong. Please help me. 
Thank you! 

Jun (Cloud Platform Support)

unread,
Jun 1, 2020, 10:06:08 AM6/1/20
to cloud-vision-discuss
Hi, 

Google Groups are reserved for general product discussion, StackOverflow for technical questions whereas Issue Tracker for product bugs (unexpected behaviors) and feature requests.

To get a better support you should post to the relevant forum, thus please read the Community Support article for better understanding.


Brendan Lundy

unread,
Jun 1, 2020, 12:09:32 PM6/1/20
to cloud-vision-discuss

Iyrin lee

unread,
Jun 2, 2020, 8:25:13 AM6/2/20
to cloud-vision-discuss
Thank you for the help!
The problem is solved!
Reply all
Reply to author
Forward
0 new messages