Пример #1
0
        public void VisualSearchWithUrl()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "VisualSearchWithUrl");

                IVisualSearchClient client = new VisualSearchClient(new ApiKeyServiceClientCredentials(SubscriptionKey), HttpMockServer.CreateInstance());

                ImageInfo           ImageInfo           = new ImageInfo(url: ImageUrl, cropArea: CropArea);
                VisualSearchRequest VisualSearchRequest = new VisualSearchRequest(imageInfo: ImageInfo);

                var resp = client.Images.VisualSearchMethodAsync(knowledgeRequest: JsonConvert.SerializeObject(VisualSearchRequest)).Result;

                Assert.NotNull(resp);
                Assert.NotNull(resp.Tags);
                Assert.True(resp.Tags.Count > 0);

                var tag = resp.Tags[0];
                Assert.NotNull(tag.Actions);
                Assert.True(tag.Actions.Count > 0);

                var action = tag.Actions[0];
                Assert.NotNull(action.ActionType);
            }
        }
Пример #2
0
        public void VisualSearchWithInsightsToken()
        {
            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                HttpMockServer.Initialize(this.GetType().FullName, "VisualSearchWithInsightsToken");

                IVisualSearchClient client = new VisualSearchClient(new ApiKeyServiceClientCredentials(SubscriptionKey), HttpMockServer.CreateInstance());

                ImageInfo           ImageInfo           = new ImageInfo(imageInsightsToken: ImageInsightsToken, cropArea: CropArea);
                Filters             Filters             = new Filters(site: "www.bing.com");
                KnowledgeRequest    KnowledgeRequest    = new KnowledgeRequest(filters: Filters);
                VisualSearchRequest VisualSearchRequest = new VisualSearchRequest(imageInfo: ImageInfo, knowledgeRequest: KnowledgeRequest);

                var resp = client.Images.VisualSearchMethodAsync(knowledgeRequest: JsonConvert.SerializeObject(VisualSearchRequest)).Result;

                Assert.NotNull(resp);
                Assert.NotNull(resp.Tags);
                Assert.True(resp.Tags.Count > 0);

                var tag = resp.Tags[0];
                Assert.NotNull(tag.Actions);
                Assert.True(tag.Actions.Count > 0);

                var action = tag.Actions[0];
                Assert.NotNull(action.ActionType);
            }
        }
Пример #3
0
        public void VisualSearchWithBinary()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "VisualSearchWithBinary");

                IVisualSearchClient client = new VisualSearchClient(new ApiKeyServiceClientCredentials(SubscriptionKey), HttpMockServer.CreateInstance());

                using (FileStream stream = new FileStream(Path.Combine("TestImages", "image.jpg"), FileMode.Open))
                {
                    VisualSearchRequest VisualSearchRequest = new VisualSearchRequest();

                    var resp = client.Images.VisualSearchMethodAsync(image: stream, knowledgeRequest: JsonConvert.SerializeObject(VisualSearchRequest)).Result;

                    Assert.NotNull(resp);
                    Assert.NotNull(resp.Tags);
                    Assert.True(resp.Tags.Count > 0);

                    var tag = resp.Tags[0];
                    Assert.NotNull(tag.Actions);
                    Assert.True(tag.Actions.Count > 0);

                    var action = tag.Actions[0];
                    Assert.NotNull(action.ActionType);
                }
            }
        }
        // This method will get Visual Search results from an imageInsightsToken obtained from the return value of the ImageResults method.
        // The method includes imageInsightsToken the in a knowledgeRequest parameter, along with a cropArea object.
        // It prints out the imageInsightsToken uploaded in the request.
        // Finally the example prints URLs of images found using the imageInsightsToken.

        public static void VisualSearchInsightsToken(string subscriptionKey, string insightsTok)
        {
            var client = new VisualSearchClient(new Microsoft.Azure.CognitiveServices.Search.VisualSearch.ApiKeyServiceClientCredentials(subscriptionKey));

            try
            {
                // The image can be specified via an insights token, in the ImageInfo object.
                ImageInfo ImageInfo = new ImageInfo(imageInsightsToken: insightsTok);

                // An image binary is not necessary here, as the image is specified by insights token.
                VisualSearchRequest VisualSearchRequest = new VisualSearchRequest(ImageInfo);

                var visualSearchResults = client.Images.VisualSearchMethodAsync(knowledgeRequest: VisualSearchRequest).Result;
                Console.WriteLine("\r\nVisual search request with imageInsightsToken and knowledgeRequest");

                if (visualSearchResults == null)
                {
                    Console.WriteLine("No visual search result data.");
                }
                else
                {
                    // Visual Search results
                    if (visualSearchResults.Image?.ImageInsightsToken != null)
                    {
                        Console.WriteLine($"Uploaded image insights token: {insightsTok}");
                    }
                    else
                    {
                        Console.WriteLine("Couldn't find image insights token!");
                    }

                    if (visualSearchResults.Tags.Count > 0)
                    {
                        // List of tags
                        foreach (ImageTag t in visualSearchResults.Tags)
                        {
                            foreach (ImageAction i in t.Actions)
                            {
                                Console.WriteLine("\r\n" + "ActionType: " + i.ActionType + " WebSearchURL: " + i.WebSearchUrl);

                                if (i.ActionType == "PagesIncluding")
                                {
                                    foreach (ImageObject o in (i as ImageModuleAction).Data.Value)
                                    {
                                        Console.WriteLine("ContentURL: " + o.ContentUrl);
                                    }
                                }
                            }
                        }
                    }

                    else
                    {
                        Console.WriteLine("Couldn't find image tags!");
                    }
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine("Encountered exception. " + ex.Message);
            }
        }
 public PhotoProcessingClient()
 {
     _client = new VisualSearchClient(new ApiKeyServiceClientCredentials("9c3526c0eb0f46ee9c8e304d9e576f55"));
 }