Exemple #1
0
        private static HttpRequestMessage CreateImageRequest(ContentModeratorImageQuery query, string url)
        {
            var _httpRequest = new HttpRequestMessage();

            _httpRequest.Method     = new HttpMethod("POST");
            _httpRequest.RequestUri = new System.Uri(url);
            _httpRequest.Headers.TryAddWithoutValidation("Ocp-Apim-Subscription-Key", query.SubscriptionKey);
            _httpRequest.Content = new StreamContent(File.OpenRead(query.ImagePath));
            _httpRequest.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(query.ContentType);
            return(_httpRequest);
        }
Exemple #2
0
        // Matt Content Moderator Part 3
        public static void CallContentModeratorAPIImage()
        {
            try
            {
                // Get the file name
                Console.WriteLine("Please enter a file name:");
                var imagePath = Console.ReadLine();

                Console.WriteLine("Please enter a content type: (image/jpeg) is default");
                var contentType = "image/jpeg";
                var line        = Console.ReadLine();
                if (!String.IsNullOrEmpty(line))
                {
                    contentType = line;
                }

                if (File.Exists(imagePath))
                {
                    var fileName = new FileInfo(imagePath).Name;
                    Console.WriteLine("Getting image Insights for image: " + fileName);
                    Console.WriteLine();

                    var query = new ContentModeratorImageQuery();
                    query.ImagePath       = imagePath;
                    query.ContentType     = contentType;
                    query.Endpoint        = Constants.CONTENT_MODERATOR_IMAGE_URL;
                    query.SubscriptionKey = Constants.CONTENT_MODERATOR_KEY;

                    var result = ContentModeratorService.CallContentModeratorImageAPI(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;
            }
        }
Exemple #3
0
        public static async Task <FoundFaces> CallContentModeratorFacesAPI(ContentModeratorImageQuery query)
        {
            var url = $"{query.Endpoint + "/FindFaces"}{query.ToQueryString()}";

            using (var bingClient = new BingHttpClient(query.SubscriptionKey))
            {
                var cancellationToken           = default(CancellationToken);
                HttpRequestMessage _httpRequest = CreateImageRequest(query, url);
                var httpResponseMessage         = await bingClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);

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