Пример #1
0
        public async Task <List <ProductSearchResult> > GetSimilarAsync(byte[] base64Image)
        {
            var client        = _clientFactory.CreateImageAnnotatorClient();
            var productClient = _clientFactory.CreateProductSearchClient();

            Image image = Image.FromBytes(base64Image);

            var productSearchParams = new ProductSearchParams
            {
                ProductSetAsProductSetName = new ProductSetName(_projectInfo.ProjectID,
                                                                _projectInfo.ComputeRegion,
                                                                "1"),
                ProductCategories = { "packagedgoods-v1" },
                Filter            = ""
            };

            // Search products similar to the image.
            var detectSimilar = await client.DetectSimilarProductsAsync(image, productSearchParams);

            var mapped = detectSimilar.Results.Select(async x =>
            {
                var item = new ProductSearchResult()
                {
                    Score       = x.Score,
                    ProductName = x.Product.Name.Split("/").Last(),
                };

                if (x.Score > 0.6)
                {
                    item.ReferenceImages = (await ListReferenceImagesOfProduct(productClient, item.ProductName)).ToList();
                }
                return(item);
            }).ToList();

            var results = await Task.WhenAll(mapped);

            return(results.ToList());
        }