private async Task <ProductSet> CreateProductSet(ProductSearchClient client, CreateProductSetsOptions opts)
        {
            // Create a product set with the product set specification in the region.
            var request = new CreateProductSetRequest
            {
                // A resource that represents Google Cloud Platform location
                ParentAsLocationName = new LocationName(opts.ProjectID, opts.ComputeRegion),
                ProductSetId         = opts.ProductSetId,
                ProductSet           = new ProductSet
                {
                    DisplayName = opts.ProductSetDisplayName
                }
            };

            // The response is the product set with the `name` populated
            var response = await client.CreateProductSetAsync(request);

            return(response);
        }
        // [START vision_product_search_create_product_set]
        private static object CreateProductSet(CreateProductSetsOptions opts)
        {
            var client = ProductSearchClient.Create();

            // Create a product set with the product set specification in the region.
            var request = new CreateProductSetRequest
            {
                // A resource that represents Google Cloud Platform location
                ParentAsLocationName = new LocationName(opts.ProjectID,
                                                        opts.ComputeRegion),
                ProductSetId = opts.ProductSetId,
                ProductSet = new ProductSet
                {
                    DisplayName = opts.ProductSetDisplayName
                }
            };

            // The response is the product set with the `name` populated
            var response = client.CreateProductSet(request);

            Console.WriteLine($"Product set name: {response.DisplayName}");

            return 0;
        }
예제 #3
0
        static int Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Arguments: <directory containing product.json> <project ID>");
                return(1);
            }
            string directory = args[0];
            var    json      = File.ReadAllText(Path.Combine(directory, "products.json"));
            var    products  = JsonConvert.DeserializeObject <List <JsonProduct> >(json);

            // Default the location and bucket so it's easily predictable in tests.
            string projectId    = args[1];
            string locationId   = "us-west1";
            string bucketName   = $"{projectId}_product_search_test";
            string bucketUri    = $"gs://{bucketName}";
            string productSetId = $"{projectId}_product_search_test";
            var    location     = LocationName.FromProjectLocation(projectId, locationId);

            var storageClient       = StorageClient.Create();
            var productSearchClient = ProductSearchClient.Create();

            // Create the GCS bucket and upload the images
            storageClient.CreateBucket(projectId, bucketName);
            foreach (var product in products)
            {
                using (var stream = File.OpenRead(Path.Combine(directory, product.Image)))
                {
                    // TODO: Autodetect the mime type if we ever have non-JPEG images
                    storageClient.UploadObject(bucketName, product.Image, "image/jpeg", stream);
                }
            }

            // Create the product set
            var createProductSetRequest = new CreateProductSetRequest
            {
                ParentAsLocationName = location,
                ProductSetId         = productSetId,
                ProductSet           = new ProductSet
                {
                    DisplayName = "Product set for testing"
                }
            };

            // Add the products. Each product needs to be:
            // - Created
            // - Added to the product set
            // - Associated with a reference image
            var productSetResource = productSearchClient.CreateProductSet(createProductSetRequest);

            foreach (var product in products)
            {
                var createProductRequest = new CreateProductRequest
                {
                    ParentAsLocationName = location,
                    ProductId            = product.Id,
                    Product = new Product
                    {
                        Description     = product.Description,
                        DisplayName     = product.DisplayName,
                        ProductCategory = product.Category,
                        ProductLabels   = { product.Labels.Select(kvp => new Product.Types.KeyValue {
                                Key = kvp.Key, Value = kvp.Value
                            }) }
                    }
                };
                var productResource = productSearchClient.CreateProduct(createProductRequest);
                productSearchClient.AddProductToProductSet(productSetResource.ProductSetName, productResource.ProductName);

                productSearchClient.CreateReferenceImage(productResource.ProductName, new ReferenceImage {
                    Uri = $"{bucketUri}/{product.Image}"
                }, product.Image);
            }
            return(0);
        }