def delete_reference_image( project_id, location, product_id, reference_image_id): """Delete a reference image. Args: project_id: Id of the project. location: A compute region name. product_id: Id of the product. reference_image_id: Id of the reference image. """ client = vision.ProductSearchClient() # Get the full path of the reference image. reference_image_path = client.reference_image_path( project=project_id, location=location, product=product_id, reference_image=reference_image_id) # Delete the reference image. client.delete_reference_image(name=reference_image_path) print('Reference image deleted from product.')
def test_remove_product_from_product_set(self): channel = ChannelStub() patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = vision_v1p3beta1.ProductSearchClient() # Setup Request name = client.product_set_path("[PROJECT]", "[LOCATION]", "[PRODUCT_SET]") product = "product-309474065" client.remove_product_from_product_set(name, product) assert len(channel.requests) == 1 expected_request = product_search_service_pb2.RemoveProductFromProductSetRequest( name=name, product=product ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_add_product_to_product_set(self): channel = ChannelStub() patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = vision_v1p3beta1.ProductSearchClient() # Setup Request name = client.product_set_path('[PROJECT]', '[LOCATION]', '[PRODUCT_SET]') product = 'product-309474065' client.add_product_to_product_set(name, product) assert len(channel.requests) == 1 expected_request = product_search_service_pb2.AddProductToProductSetRequest( name=name, product=product) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_delete_reference_image(self): channel = ChannelStub() patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = vision_v1p3beta1.ProductSearchClient() # Setup Request name = client.reference_image_path( "[PROJECT]", "[LOCATION]", "[PRODUCT]", "[REFERENCE_IMAGE]" ) client.delete_reference_image(name) assert len(channel.requests) == 1 expected_request = product_search_service_pb2.DeleteReferenceImageRequest( name=name ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_import_product_sets_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_import_product_sets_exception", done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = vision_v1p3beta1.ProductSearchClient() # Setup Request parent = client.location_path("[PROJECT]", "[LOCATION]") input_config = {} response = client.import_product_sets(parent, input_config) exception = response.exception() assert exception.errors[0] == error
def list_products(project_id, location): """List all products. Args: project_id: Id of the project. location: A compute region name. """ client = vision.ProductSearchClient() # A resource that represents Google Cloud Platform location. location_path = client.location_path(project=project_id, location=location) # List all the products available in the region. products = client.list_products(parent=location_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: {}\n'.format(product.product_labels))
def list_product_sets(project_id, location): """List all product sets. Args: project_id: Id of the project. location: A compute region name. """ 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))
def get_product(project_id, location, product_id): """Get information about a product. Args: project_id: Id of the project. location: A compute region name. product_id: Id of the product. """ client = vision.ProductSearchClient() # Get the full path of the product. product_path = client.product_path( project=project_id, location=location, product=product_id) # Get complete detail of the product. product = client.get_product(name=product_path) # Display the product information. 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))
def test_list_reference_images(self): # Setup Expected Response page_size = 883849137 next_page_token = "" reference_images_element = {} reference_images = [reference_images_element] expected_response = { "page_size": page_size, "next_page_token": next_page_token, "reference_images": reference_images, } expected_response = product_search_service_pb2.ListReferenceImagesResponse( **expected_response ) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = vision_v1p3beta1.ProductSearchClient() # Setup Request parent = client.product_path("[PROJECT]", "[LOCATION]", "[PRODUCT]") paged_list_response = client.list_reference_images(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.reference_images[0] == resources[0] assert len(channel.requests) == 1 expected_request = product_search_service_pb2.ListReferenceImagesRequest( parent=parent ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def get_product_set(project_id, location, product_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. """ 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))
def test_get_product_set(self): # Setup Expected Response name_2 = 'name2-1052831874' display_name = 'displayName1615086568' expected_response = {'name': name_2, 'display_name': display_name} expected_response = product_search_service_pb2.ProductSet( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = vision_v1p3beta1.ProductSearchClient(channel=channel) # Setup Request name = client.product_set_path('[PROJECT]', '[LOCATION]', '[PRODUCT_SET]') response = client.get_product_set(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = product_search_service_pb2.GetProductSetRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
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.')
def test_get_product_set(self): # Setup Expected Response name_2 = "name2-1052831874" display_name = "displayName1615086568" expected_response = {"name": name_2, "display_name": display_name} expected_response = product_search_service_pb2.ProductSet(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = vision_v1p3beta1.ProductSearchClient() # Setup Request name = client.product_set_path("[PROJECT]", "[LOCATION]", "[PRODUCT_SET]") response = client.get_product_set(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = product_search_service_pb2.GetProductSetRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def get_reference_image( project_id, location, product_id, reference_image_id): """Get info about a reference image. Args: project_id: Id of the project. location: A compute region name. product_id: Id of the product. reference_image_id: Id of the reference image. """ client = vision.ProductSearchClient() # Get the full path of the reference image. reference_image_path = client.reference_image_path( project=project_id, location=location, product=product_id, reference_image=reference_image_id) # Get complete detail of the reference image. image = client.get_reference_image(name=reference_image_path) # Display the reference image information. print('Reference image name: {}'.format(image.name)) print('Reference image id: {}'.format(image.name.split('/')[-1])) print('Reference image uri: {}'.format(image.uri)) print('Reference image bounding polygons: {}'.format(image.bounding_polys))
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.')
def list_reference_images(project_id, location, product_id): """List all images in a product. Args: project_id: Id of the project. location: A compute region name. product_id: Id of the product. """ client = vision.ProductSearchClient() # Get the full path of the product. product_path = client.product_path(project=project_id, location=location, product=product_id) # List all the reference images available in the product. reference_images = client.list_reference_images(parent=product_path) # Display the reference image information. for image in reference_images: print('Reference image name: {}'.format(image.name)) print('Reference image id: {}'.format(image.name.split('/')[-1])) print('Reference image uri: {}'.format(image.uri)) print('Reference image bounding polygons: {}'.format( image.bounding_polys))
def test_get_reference_image(self): # Setup Expected Response name_2 = 'name2-1052831874' uri = 'uri116076' expected_response = {'name': name_2, 'uri': uri} expected_response = product_search_service_pb2.ReferenceImage( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = vision_v1p3beta1.ProductSearchClient(channel=channel) # Setup Request name = client.reference_image_path('[PROJECT]', '[LOCATION]', '[PRODUCT]', '[REFERENCE_IMAGE]') response = client.get_reference_image(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = product_search_service_pb2.GetReferenceImageRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_product_set(self): # Setup Expected Response name = 'name3373707' display_name = 'displayName1615086568' expected_response = {'name': name, 'display_name': display_name} expected_response = product_search_service_pb2.ProductSet( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = vision_v1p3beta1.ProductSearchClient(channel=channel) # Setup Request product_set = {} update_mask = {} response = client.update_product_set(product_set, update_mask) assert expected_response == response assert len(channel.requests) == 1 expected_request = product_search_service_pb2.UpdateProductSetRequest( product_set=product_set, update_mask=update_mask) actual_request = channel.requests[0][1] assert expected_request == actual_request
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))
def get_similar_products_file( project_id, location, product_set_id, product_category, file_path, filter): """Search similar products to image. Args: project_id: Id of the project. location: A compute region name. product_set_id: Id of the product set. 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 """ # product_search_client is needed only for its helper methods. product_search_client = vision.ProductSearchClient() image_annotator_client = vision.ImageAnnotatorClient() # Read the image as a stream of bytes. with open(file_path, 'rb') as image_file: content = image_file.read() # Create annotate image request along with product search feature. image = vision.types.Image(content=content) # product search specific parameters product_set_path = product_search_client.product_set_path( project=project_id, location=location, product_set=product_set_id) product_search_params = vision.types.ProductSearchParams( product_set=product_set_path, product_categories=[product_category], filter=filter) image_context = vision.types.ImageContext( product_search_params=product_search_params) # Search products similar to the image. response = image_annotator_client.product_search( image, image_context=image_context) index_time = response.product_search_results.index_time print('Product set index time:') print(' seconds: {}'.format(index_time.seconds)) print(' nanos: {}\n'.format(index_time.nanos)) results = response.product_search_results.results print('Search results:') for result in results: product = result.product print('Score(Confidence): {}'.format(result.score)) print('Image name: {}'.format(result.image)) print('Product name: {}'.format(product.name)) print('Product display name: {}'.format( product.display_name)) print('Product description: {}\n'.format(product.description)) print('Product labels: {}\n'.format(product.product_labels))