public async Task <TargetSearchResults> QuerySimilarTargets(byte[] image) { GoogleCredential cred = this.CreateCredentials(); var channel = new Channel(ProductSearchClient.DefaultEndpoint.Host, ProductSearchClient.DefaultEndpoint.Port, cred.ToChannelCredentials()); try { var imageAnnotatorClient = ImageAnnotatorClient.Create(channel); var options = new GetSimilarProductsOptions { ProjectID = this.options.Value.ProjectId, ComputeRegion = this.options.Value.LocationId, ProductSetId = this.options.Value.ProductSetId, ProductCategory = "apparel", Filter = string.Empty, ImageBinaries = image, }; return(await this.GetSimilarProductsFile(imageAnnotatorClient, options)); } catch (AnnotateImageException e) { this.logger.LogError(e, "The google cloud image recognition service threw an error."); return(new TargetSearchResults { Results = new TargetSearchResultEntry[0], }); } finally { await channel.ShutdownAsync(); } }
private async Task <TargetSearchResults> GetSimilarProductsFile(ImageAnnotatorClient imageAnnotatorClient, GetSimilarProductsOptions opts) { // Create annotate image request along with product search feature. Image image = Image.FromBytes(opts.ImageBinaries); // Product Search specific parameters var productSearchParams = new ProductSearchParams { ProductSetAsProductSetName = new ProductSetName(opts.ProjectID, opts.ComputeRegion, opts.ProductSetId), ProductCategories = { opts.ProductCategory }, Filter = opts.Filter }; // Search products similar to the image. ProductSearchResults results = await imageAnnotatorClient.DetectSimilarProductsAsync(image, productSearchParams); return(this.mapper.Map <TargetSearchResults>(results)); }