Esempio n. 1
0
        /// <summary>
        /// Update a classifier. Update a custom classifier by adding new positive or negative classes (examples) or by adding new images to existing classes. You must supply at least one set of positive or negative examples. For details, see [Updating custom classifiers](https://console.bluemix.net/docs/services/visual-recognition/customizing.html#updating-custom-classifiers).  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.  **Important:** You can't update a custom classifier with an API key for a Lite plan. To update a custom classifer on a Lite plan, create another service instance on a Standard plan and re-create your custom classifier.  **Tip:** Don't make retraining calls on a classifier until the status is ready. When you submit retraining requests in parallel, the last request overwrites the previous requests. The retrained property shows the last time the classifier retraining finished.
        /// </summary>
        /// <param name="updateClassifier">Object used to define options for updating a classifier.</param>
        /// <returns><see cref="Classifier" />Classifier</returns>
        public Classifier UpdateClassifier(UpdateClassifier updateClassifier, Dictionary <string, object> customData = null)
        {
            if (updateClassifier == null)
            {
                throw new ArgumentNullException(nameof(updateClassifier));
            }

            if (string.IsNullOrEmpty(updateClassifier.ClassifierId))
            {
                throw new ArgumentNullException(nameof(updateClassifier.ClassifierId));
            }

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

            Classifier result = null;

            try
            {
                var formData = new MultipartFormDataContent();

                if (updateClassifier.PositiveExamples != null && updateClassifier.PositiveExamples.Count > 0)
                {
                    foreach (KeyValuePair <string, Stream> kvp in updateClassifier.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 (updateClassifier.NegativeExamples != null)
                {
                    var negativeExamplesContent = new ByteArrayContent((updateClassifier.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/{updateClassifier.ClassifierId}")
                         .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);
        }
Esempio n. 2
0
        /// <summary>
        /// Update a classifier. Update a custom classifier by adding new positive or negative classes (examples) or by adding new images to existing classes. You must supply at least one set of positive or negative examples. For details, see [Updating custom classifiers](https://console.bluemix.net/docs/services/visual-recognition/customizing.html#updating-custom-classifiers).  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.  **Important:** You can't update a custom classifier with an API key for a Lite plan. To update a custom classifer on a Lite plan, create another service instance on a Standard plan and re-create your custom classifier.  **Tip:** Don't make retraining calls on a classifier until the status is ready. When you submit retraining requests in parallel, the last request overwrites the previous requests. The retrained property shows the last time the classifier retraining finished.
        /// </summary>
        /// <param name="updateClassifier">Object used to define options for updating a classifier.</param>
        /// <returns><see cref="Classifier" />Classifier</returns>
        public Classifier UpdateClassifier(UpdateClassifier updateClassifier, Dictionary <string, object> customData = null)
        {
            if (updateClassifier == null)
            {
                throw new ArgumentNullException(nameof(updateClassifier));
            }

            if (string.IsNullOrEmpty(updateClassifier.ClassifierId))
            {
                throw new ArgumentNullException(nameof(updateClassifier.ClassifierId));
            }

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

            Classifier result = null;

            try
            {
                var formData = new MultipartFormDataContent();

                if (updateClassifier.PositiveExamples != null && updateClassifier.PositiveExamples.Count > 0)
                {
                    foreach (KeyValuePair <string, Stream> kvp in updateClassifier.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 (updateClassifier.NegativeExamples != null)
                {
                    var negativeExamplesContent = new ByteArrayContent((updateClassifier.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/{updateClassifier.ClassifierId}");

                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=UpdateClassifier");
                result = restRequest.As <Classifier>().Result;
                if (result == null)
                {
                    result = new Classifier();
                }
                result.CustomData = restRequest.CustomData;
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }

            return(result);
        }