예제 #1
0
        /// <summary>
        /// Create a classifier. Train a new multi-faceted classifier on the uploaded image data. Create your custom classifier with positive or negative examples. Include at least two sets of examples, either two positive example files or one positive and one negative file. You can upload a maximum of 256 MB per call.  Encode all names in UTF-8 if they contain non-ASCII characters (.zip and image file names, and classifier and class names). The service assumes UTF-8 encoding if it encounters non-ASCII characters.
        /// </summary>
        /// <param name="createClassifier">Object used to define options for creating a classifier.</param>
        /// <returns><see cref="Classifier" />Classifier</returns>
        public Classifier CreateClassifier(CreateClassifier createClassifier, Dictionary <string, object> customData = null)
        {
            if (createClassifier == null)
            {
                throw new ArgumentNullException(nameof(createClassifier));
            }

            if (!createClassifier.IsValid())
            {
                throw new ArgumentException("At least one positive example and one negative example is required to train a classifier.");
            }

            if (string.IsNullOrEmpty(VersionDate))
            {
                throw new ArgumentNullException("versionDate cannot be null.");
            }

            Classifier result = null;

            try
            {
                var formData = new MultipartFormDataContent();

                if (createClassifier.Name != null)
                {
                    var nameContent = new StringContent(createClassifier.Name, Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                    nameContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                    formData.Add(nameContent, "name");
                }

                if (createClassifier.PositiveExamples != null && createClassifier.PositiveExamples.Count > 0)
                {
                    foreach (KeyValuePair <string, Stream> kvp in createClassifier.PositiveExamples)
                    {
                        var classnamePositiveExamplesContent = new ByteArrayContent((kvp.Value as Stream).ReadAllBytes());
                        MediaTypeHeaderValue contentType;
                        MediaTypeHeaderValue.TryParse("application/zip", out contentType);
                        classnamePositiveExamplesContent.Headers.ContentType = contentType;
                        formData.Add(classnamePositiveExamplesContent, string.Format("{0}_positive_examples", kvp.Key), string.Format("{0}_positive_examples.zip", kvp.Key));
                    }
                }

                if (createClassifier.NegativeExamples != null)
                {
                    var negativeExamplesContent = new ByteArrayContent((createClassifier.NegativeExamples as Stream).ReadAllBytes());
                    MediaTypeHeaderValue contentType;
                    MediaTypeHeaderValue.TryParse("application/zip", out contentType);
                    negativeExamplesContent.Headers.ContentType = contentType;
                    formData.Add(negativeExamplesContent, "negative_examples", "negative_examples.zip");
                }

                IClient client;
                if (_tokenManager == null)
                {
                    client = this.Client;
                }
                else
                {
                    client = this.Client.WithAuthentication(_tokenManager.GetToken());
                }
                var restRequest = client.PostAsync($"{this.Endpoint}/v3/classifiers");

                restRequest.WithArgument("version", VersionDate);
                if (!string.IsNullOrEmpty(ApiKey))
                {
                    restRequest.WithArgument("api_key", ApiKey);
                }
                restRequest.WithBodyContent(formData);
                if (customData != null)
                {
                    restRequest.WithCustomData(customData);
                }
                restRequest.WithFormatter(new MediaTypeHeaderValue("application/octet-stream"));
                restRequest.WithHeader("X-IBMCloud-SDK-Analytics", "service_name=watson_vision_combined;service_version=v3;operation_id=CreateClassifier");
                result = restRequest.As <Classifier>().Result;
                if (result == null)
                {
                    result = new Classifier();
                }
                result.CustomData = restRequest.CustomData;
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }

            return(result);
        }
예제 #2
0
        /// <summary>
        /// Create a classifier. Train a new multi-faceted classifier on the uploaded image data. Create your custom classifier with positive or negative examples. Include at least two sets of examples, either two positive example files or one positive and one negative file. You can upload a maximum of 256 MB per call.  Encode all names in UTF-8 if they contain non-ASCII characters (.zip and image file names, and classifier and class names). The service assumes UTF-8 encoding if it encounters non-ASCII characters.
        /// </summary>
        /// <param name="createClassifier">Object used to define options for creating a classifier.</param>
        /// <returns><see cref="Classifier" />Classifier</returns>
        public Classifier CreateClassifier(CreateClassifier createClassifier, Dictionary <string, object> customData = null)
        {
            if (createClassifier == null)
            {
                throw new ArgumentNullException(nameof(createClassifier));
            }

            if (!createClassifier.IsValid())
            {
                throw new ArgumentException("At least one positive example and one negative example is required to train a classifier.");
            }

            if (string.IsNullOrEmpty(VersionDate))
            {
                throw new ArgumentNullException("versionDate cannot be null.");
            }

            Classifier result = null;

            try
            {
                var formData = new MultipartFormDataContent();

                if (createClassifier.Name != null)
                {
                    var nameContent = new StringContent(createClassifier.Name, Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                    nameContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                    formData.Add(nameContent, "name");
                }

                if (createClassifier.PositiveExamples != null && createClassifier.PositiveExamples.Count > 0)
                {
                    foreach (KeyValuePair <string, Stream> kvp in createClassifier.PositiveExamples)
                    {
                        var classnamePositiveExamplesContent = new ByteArrayContent((kvp.Value as Stream).ReadAllBytes());
                        MediaTypeHeaderValue contentType;
                        MediaTypeHeaderValue.TryParse("application/zip", out contentType);
                        classnamePositiveExamplesContent.Headers.ContentType = contentType;
                        formData.Add(classnamePositiveExamplesContent, string.Format("{0}_positive_examples", kvp.Key), string.Format("{0}_positive_examples.zip", kvp.Key));
                    }
                }

                if (createClassifier.NegativeExamples != null)
                {
                    var negativeExamplesContent = new ByteArrayContent((createClassifier.NegativeExamples as Stream).ReadAllBytes());
                    MediaTypeHeaderValue contentType;
                    MediaTypeHeaderValue.TryParse("application/zip", out contentType);
                    negativeExamplesContent.Headers.ContentType = contentType;
                    formData.Add(negativeExamplesContent, "negative_examples", "negative_examples.zip");
                }

                result = this.Client.PostAsync($"{this.Endpoint}/v3/classifiers")
                         .WithArgument("api_key", ApiKey)
                         .WithArgument("version", VersionDate)
                         .WithBodyContent(formData)
                         .WithFormatter(new MediaTypeHeaderValue("application/octet-stream"))
                         .As <Classifier>()
                         .Result;
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }

            return(result);
        }