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 = f"projects/{project_id}/locations/{location}" # Set the input configuration along with Google Cloud Storage URI gcs_source = vision.ImportProductSetsGcsSource(csv_file_uri=gcs_uri) input_config = vision.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 purge_products_in_product_set(project_id, location, product_set_id, force): """Delete 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. force: Perform the purge only when force is set to True. """ client = vision.ProductSearchClient() parent = client.location_path( project=project_id, location=location) product_set_purge_config = vision.types.ProductSetPurgeConfig( product_set_id=product_set_id) # The purge operation is async. operation = client.purge_products( parent=parent, product_set_purge_config=product_set_purge_config, # The operation is irreversible and removes multiple products. # The user is required to pass in force=True to actually perform the # purge. # If force is not set to True, the service raises an exception. force=force) operation.result(timeout=120) print('Deleted products in product set.')
def create_product(project_id, location, product_id, product_display_name, product_category): """Create one product. Args: project_id: Id of the project. location: A compute region name. product_id: Id of the product. product_display_name: Display name of the product. product_category: Category of the product. """ client = vision.ProductSearchClient() # A resource that represents Google Cloud Platform location. location_path = client.location_path(project=project_id, location=location) # Create a product with the product specification in the region. # Set product display name and product category. product = vision.types.Product(display_name=product_display_name, product_category=product_category) # The response is the product with the `name` field populated. response = client.create_product(parent=location_path, product=product, product_id=product_id) # Display the product information. print('Product name: {}'.format(response.name))
def update_product_labels(project_id, location, product_id, key, value): """Update the product labels. Args: project_id: Id of the project. location: A compute region name. product_id: Id of the product. key: The key of the label. value: The value of the label. """ client = vision.ProductSearchClient() # Get the name of the product. product_path = client.product_path(project=project_id, location=location, product=product_id) # Set product name, product label and product display name. # Multiple labels are also supported. key_value = vision.types.Product.KeyValue(key=key, value=value) product = vision.types.Product(name=product_path, product_labels=[key_value]) # Updating only the product_labels field here. update_mask = vision.types.FieldMask(paths=['product_labels']) # This overwrites the product_labels. updated_product = client.update_product(product=product, update_mask=update_mask) # Display the updated product information. print('Product name: {}'.format(updated_product.name)) print('Updated product labels: {}'.format(product.product_labels))
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 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.')
def purge_orphan_products(project_id, location, force): """Delete all products not in any product sets. Args: project_id: Id of the project. location: A compute region name. """ client = vision.ProductSearchClient() parent = f"projects/{project_id}/locations/{location}" # The purge operation is async. operation = client.purge_products( request={ "parent": parent, "delete_orphan_products": True, # The operation is irreversible and removes multiple products. # The user is required to pass in force=True to actually perform the # purge. # If force is not set to True, the service raises an exception. "force": force }) operation.result(timeout=500) print('Orphan products deleted.')
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.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.ProductSearchParams( product_set=product_set_path, product_categories=[product_category], filter=filter) image_context = vision.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(index_time) 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))
def create_product_set(project_id, location, product_set_id1, product_set_display_name): """Create a product set. Args: project_id: Id of the project. location: A compute region name. product_set_id: Id of the product set. product_set_display_"name": Display name of the product set. """ 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_id1) prodSet_to_prods[product_set_id1] = [] # Display the product set information. print('Product set "name": {}'.format(response.name))
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)) return image
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))
def import_product_sets(project_id, location, gcs_uri): 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(project_id=project_id, location=location), input_config=input_config) print('Procseeing 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 create_reference_image(project_id, location, product_id, reference_image_id, gcs_uri): """Create 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. gcs_"uri": Google Cloud Storage path of the input image. """ client = vision.ProductSearchClient() # Get the full path of the product. product_path = client.product_path( project=project_id, location=location, product=product_id) # Create a reference image. reference_image = vision.types.ReferenceImage(uri=gcs_uri) # The response is the reference image with `name` populated. image = client.create_reference_image( parent=product_path, reference_image=reference_image, reference_image_id=reference_image_id) # Display the reference image information. print('Reference image "name": {}'.format(image.name)) print('Reference image "uri": {}'.format(image.uri))
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 get_similar_products_file(base64IMG): project_id='smart-tracer-250813' location='us-east1' product_set_id='hack6' product_category='toys' # 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. content = base64IMG # 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]) 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) print(response)
def __init__(self, project_id, location): self.project_id = project_id self.location = location self.client = vision.ProductSearchClient() self.location_path = self.client.location_path(project=self.project_id, location=self.location)
def get_similar_products_uri(project_id, location, product_set_id, product_category, image_uri, 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. image_uri: Cloud Storage location of 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() # Create annotate image request along with product search feature. image_source = vision.ImageSource(image_uri=image_uri) image = vision.Image(source=image_source) # 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.ProductSearchParams( product_set=product_set_path, product_categories=[product_category], filter=filter) image_context = vision.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(index_time) results = response.product_search_results.results print('Search results:') output = [] for result in results: product = result.product print('Score(Confidence): {}'.format(result.score)) product_id = product.name.split('/')[-1] image_uri = get_reference_image_uri(PROJECT_ID, LOCATION_ID, product_id) blob_name = image_uri.split('/')[-1] meta = get_image_metadata(IMAGE_BUCKET, blob_name) print("Product Info: ", meta) output.append(meta) return output
def cleanProductIds(project_id, location, product_id): client = vision.ProductSearchClient() # Get the full path of the product. product_path = client.product_path( project=project_id, location=location, product=product_id) # Delete a product. client.delete_product(name=product_path) print('Product deleted.')
def setUpModule(): VisionSystemTestBase.client = vision.ImageAnnotatorClient() VisionSystemTestBase.ps_client = vision.ProductSearchClient() storage_client = storage.Client() bucket_name = "new" + unique_resource_id() VisionSystemTestBase.test_bucket = storage_client.bucket(bucket_name) # 429 Too Many Requests in case API requests rate-limited. retry_429 = RetryErrors(exceptions.TooManyRequests) retry_429(VisionSystemTestBase.test_bucket.create)()
def imageURLToFoodID(url): project_id = 'allergy-compass' location = 'us-east1' product_set_id = 'product_set0' product_category = 'packagedgoods-v1' filter = 'style=nothing' """Search similar products to image. Args: url """ import requests f = open('temp.jpg', 'wb') f.write(requests.get(url).content) f.close() # product_search_client is needed only for its helper methods. product_search_client = vision.ProductSearchClient() image_annotator_client = vision.ImageAnnotatorClient() with open('temp.jpg', '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) print(response) result = response.product_search_results.results[0] display_name = re.sub(r"(\w)([A-Z])", r"\1 \2", result.product.display_name) list = result.product.name.split("/") barcode = list[len(list) - 1] print("Barcode found: " + barcode) print(display_name) return display_name + "~" + getAllergyInfo(barcode)
def list_product_sets(project_id, location): 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(project_id=project_id, location=location)) # 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))
def delete_product(project_id, location, product_id): """Delete the product and all its reference images. 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) # Delete a product. client.delete_product(name=product_path) print('Product deleted.')
def get_similar_products_uri(project_id, location, product_set_id, product_category, image_uri, 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() # Create annotate image request along with product search feature. image_source = vision.types.ImageSource(image_uri=image_uri) image = vision.types.Image(source=image_source) # 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) print(response) results = response.product_search_results.results return results[0].product.name
def get_reference_image_uri(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: return image.uri
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 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 = f"projects/{project_id}/locations/{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(product_set.index_time)
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 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 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 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(product_set.index_time)