Example #1
0
        public virtual ClassifyPost Classify(byte[] imageData = null, string imageDataName = null, string imageDataMimeType = null, string[] urls = null, string[] classifierIDs = null, string[] owners = null, float threshold = 0, string acceptLanguage = "en")
        {
            if (imageData == null && (urls == null || urls.Length < 1))
            {
                throw new ArgumentNullException($"{nameof(imageData)} or 'urls' are required for 'Classify()'");
            }

            if (imageData != null && (string.IsNullOrEmpty(imageDataName) || string.IsNullOrEmpty(imageDataMimeType)))
            {
                throw new ArgumentException($"{nameof(imageDataName)} and {nameof(imageDataMimeType)} are required for 'Classify()'");
            }

            if (owners != null && owners.Select(a => a.ToLower()).Any(b => !b.Equals("ibm") && !b.Equals("me")))
            {
                throw new ArgumentOutOfRangeException("Owners can only be a combination of IBM and me ('IBM', 'me', 'IBM,me').");
            }

            ClassifyParameters parametersObject = new ClassifyParameters
            {
                classifier_ids = classifierIDs ?? (new string[] { "default" }),
                urls           = urls ?? new string[0],
                owners         = owners ?? new string[0],
                threshold      = threshold
            };

            string parameters = JsonConvert.SerializeObject(parametersObject);

            var formData = new MultipartFormDataContent();

            if (imageData != null)
            {
                var imageContent = new ByteArrayContent(imageData);
                imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse(imageDataMimeType);
                formData.Add(imageContent, "images_file", imageDataName);
            }

            if (!string.IsNullOrEmpty(parameters))
            {
                var parametersContent = new StringContent(parameters, Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                parametersContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                formData.Add(parametersContent, "parameters");
            }

            try
            {
                var result = RepositoryClient.PostAsync($"{ApiKeys.VisualRecognitionEndpoint}{classifyUrl}")
                             .WithHeader("Accept-Language", "en")
                             .WithArgument("version", versionDate)
                             .WithArgument("api_key", ApiKeys.VisualRecognition)
                             .WithBodyContent(formData)
                             .As <ClassifyPost>()
                             .Result;

                return(result);
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }
        }
Example #2
0
        public ClassifyPost Classify(byte[] imageData = null, string imageDataName = null, string imageDataMimeType = null, string[] urls = null, string[] classifierIDs = null, string[] owners = null, float threshold = 0, string acceptLanguage = "en")
        {
            ClassifyPost result = null;

            if (imageData == null && (urls == null || urls.Length < 1))
            {
                throw new ArgumentNullException(string.Format("{0} or {1} are required for 'Classify()'", nameof(imageData), "'urls'"));
            }

            if (imageData != null)
            {
                if (string.IsNullOrEmpty(imageDataName) || string.IsNullOrEmpty(imageDataMimeType))
                {
                    throw new ArgumentException(string.Format("{0} and {1} are required for 'Classify()'", nameof(imageDataName), nameof(imageDataMimeType)));
                }
            }

            if (owners != null)
            {
                foreach (string owner in owners)
                {
                    if (owner.ToLower() != "ibm" && owner.ToLower() != "me")
                    {
                        throw new ArgumentOutOfRangeException("Owners can only be a combination of IBM and me ('IBM', 'me', 'IBM,me').");
                    }
                }
            }

            try
            {
                ClassifyParameters parametersObject = new ClassifyParameters();
                parametersObject.ClassifierIds = classifierIDs != null ? classifierIDs : new string[] { "default" };
                parametersObject.URLs          = urls != null ? urls : new string[0];
                parametersObject.Owners        = owners != null ? owners : new string[0];
                parametersObject.Threshold     = threshold;

                string parameters = JsonConvert.SerializeObject(parametersObject);

                var formData = new MultipartFormDataContent();

                if (imageData != null)
                {
                    var imageContent = new ByteArrayContent(imageData);
                    imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse(imageDataMimeType);
                    formData.Add(imageContent, "images_file", imageDataName);
                }

                if (!string.IsNullOrEmpty(parameters))
                {
                    var parametersContent = new StringContent(parameters, Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                    parametersContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                    formData.Add(parametersContent, "parameters");
                }

                result = this.Client.PostAsync($"{ this.Endpoint}{PATH_CLASSIFY}")
                         .WithHeader("Accept-Language", "en")
                         .WithArgument("version", VERSION_DATE_2016_05_20)
                         .WithArgument("api_key", ApiKey)
                         .WithBodyContent(formData)
                         .As <ClassifyPost>()
                         .Result;
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }

            return(result);
        }