Ejemplo n.º 1
0
        // Matt Visual Search Part 1
        public static void CallBingVisualSearchAPI()
        {
            try
            {
                // Get the file name
                Console.WriteLine("Please enter a file name:");
                var imagePath = Console.ReadLine();

                // Get the website filter
                Console.WriteLine("Please enter in a website to filter to:");
                var site = Console.ReadLine();

                if (File.Exists(imagePath))
                {
                    var fileName = new FileInfo(imagePath).Name;
                    Console.WriteLine("Getting image Insights for image: " + fileName);
                    var query  = new BingVisualSearchQuery(new FileStream(imagePath, FileMode.Open), Constants.BING_VISUAL_SEARCH_KEY, site, "en-US");
                    var result = BingVisualSearchService.CallVisualSearchAPI(query).Result;
                    ExportJSON(JsonConvert.SerializeObject(result));
                    Console.WriteLine("\nPress Enter to exit ");
                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("Invalid Image Path");
                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                Console.ReadLine();
                throw;
            }
        }
Ejemplo n.º 2
0
        public static async Task <BingVisualSearchResponse> CallVisualSearchAPI(BingVisualSearchQuery query)
        {
            var url = ApiEndpoints.VISUAL_SEARCH_URL +
                      "mkt=" + query.Mkt +
                      "&safeSearch=" + query.SafeSearch +
                      "&setLang=" + query.SetLang;

            byte[] imageBinary = null;
            using (MemoryStream ms = new MemoryStream())
            {
                query.FileStream.CopyTo(ms);
                imageBinary = ms.ToArray();
            }
            var boundary = string.Format("batch_{0}", Guid.NewGuid());
            var content  = new MultipartFormDataContent(boundary)
            {
                { new ByteArrayContent(imageBinary), "image", "myimage" }
            };
            var kb = JsonConvert.SerializeObject(BuildKnowledgeRequest(query.Site, null));

            content.Add(new StringContent(kb, Encoding.UTF8, "application/json"), "knowledgeRequest");
            using (var bClient = new BingHttpClient(query.SearchKey))
            {
                var httpResponseMessage = await bClient.PostAsync(url, content);

                if (httpResponseMessage.IsSuccessStatusCode)
                {
                    var responseContent = httpResponseMessage.Content.ReadAsStringAsync().Result;
                    return(JsonConvert.DeserializeObject <BingVisualSearchResponse>(responseContent));
                }
                else
                {
                    throw new InvalidOperationException("An error occurred fetching the results from the video service");
                }
            }
        }