public IEnumerator TestClassify()
        {
            Log.Debug("VisualRecognitionServiceV3IntegrationTests", "Attempting to Classify...");
            ClassifiedImages classifyResponse = null;

            using (FileStream fs = File.OpenRead(turtleImageFilepath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    service.Classify(
                        callback: (DetailedResponse <ClassifiedImages> response, IBMError error) =>
                    {
                        Log.Debug("VisualRecognitionServiceV3IntegrationTests", "Classify result: {0}", response.Response);
                        classifyResponse = response.Result;
                        Assert.IsNotNull(classifyResponse);
                        Assert.IsNotNull(classifyResponse.Images);
                        Assert.IsTrue(classifyResponse.Images.Count > 0);
                        Assert.IsNotNull(classifyResponse.Images[0].Classifiers);
                        Assert.IsTrue(classifyResponse.Images[0].Classifiers.Count > 0);
                        Assert.IsNull(error);
                    },
                        imagesFile: ms,
                        imagesFileContentType: turtleImageContentType,
                        imagesFilename: Path.GetFileName(turtleImageFilepath)
                        );

                    while (classifyResponse == null)
                    {
                        yield return(null);
                    }
                }
            }
        }
コード例 #2
0
    // CLASSIFY IMAGE
    //public void Classify()
    //{
    //    bool v = _visualRecognition.Classify(successCallback: OnClassify, failCallback: OnFail, imageData: imageData, imageMimeType: imageMimeType, classifierIDs: classifierIds.ToArray(), acceptLanguage: "en");
    //    Debug.Log(v);
    //    if (!v)
    //    {
    //        Debug.Log("Couldn't classify the image!");
    //        Log.Debug("ExampleVisualRecognition.Classify()", "Classifiy image failed!");
    //    }
    //}

    //Once the image is classified, output the results and determine who it is
    public void OnClassify(ClassifiedImages classify, Dictionary <string, object> customData)
    {
        Debug.Log("Classified the image!");
        Debug.Log("Here are your classification results: " + customData["json"].ToString());

        // If a value was not returned by the classifier (e.g. null) then it requests a new classifier and states that the image is not you
        if (classify.images[0].classifiers[0].classes.Length < 1)
        {
            Debug.Log("Index out of bounds, no classification available! " + classify.images[0].classifiers[0].ToString());
            dataOutput.text = "You're not Alberto! Please put Alberto on the screen!";
            //cd.CaptureImage();
        }
        // If a value is returned by the classifier it has correctly classified you but categorizes the score
        else
        {
            //If the classification score is above 50% it says hello to you and prints the score
            if (classify.images[0].classifiers[0].classes[0].score > 0.5f)
            {
                dataOutput.text = "Hello Alberto!\n" + classify.images[0].classifiers[0].classes[0].score;
                Log.Debug("ExampleVisualRecognition.OnClassify()", "Classify result: Score: {0}", classify.images[0].classifiers[0].classes[0].score);
            }

            //If the classification score is below 50% it lets you know it cannot recognize you properly and to take a better picture
            else
            {
                dataOutput.text = "Hard to tell who you are, please retake your photo!\n" + classify.images[0].classifiers[0].classes[0].score;
                Log.Debug("ExampleVisualRecognition.OnClassify()", "Classify result: Score: {0}", classify.images[0].classifiers[0].classes[0].score);
            }
        }
    }
コード例 #3
0
        public IEnumerator TestClassify()
        {
            //  Create credential and instantiate service
            Credentials credentials = null;

            //  Authenticate using iamApikey
            TokenOptions tokenOptions = new TokenOptions()
            {
                IamApiKey = _iamApikey
            };

            credentials = new Credentials(tokenOptions, _serviceUrl);

            //  Wait for tokendata
            while (!credentials.HasIamTokenData())
            {
                yield return(null);
            }

            if (string.IsNullOrEmpty(_iamApikey))
            {
                throw new IBMException("Plesae provide IAM ApiKey for the service.");
            }
            service = new VisualRecognitionService("2018-03-19", credentials);
            // service.StreamMultipart = true;

            Log.Debug("VisualRecognitionServiceV3IntegrationTests", "Attempting to Classify...");
            ClassifiedImages classifyResponse = null;

            using (FileStream fs = System.IO.File.OpenRead(turtleImageFilepath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    service.Classify(
                        callback: (DetailedResponse <ClassifiedImages> response, IBMError error) =>
                    {
                        Log.Debug("VisualRecognitionServiceV3IntegrationTests", "Classify result: {0}", response.Response);
                        classifyResponse = response.Result;
                    },
                        imagesFile: ms,
                        imagesFilename: System.IO.Path.GetFileName(turtleImageFilepath),
                        imagesFileContentType: turtleImageContentType,
                        url: picUrl,
                        classifierIds: myList
                        );

                    while (classifyResponse == null)
                    {
                        yield return(null);
                    }
                }
            }
        }
コード例 #4
0
        public void GuardarInteraccionConWatson(ClassifiedImages result, int idImagen)
        {
            ConsultasWatson consulta = new ConsultasWatson
            {
                Clase    = result.Images.FirstOrDefault()?.Classifiers.FirstOrDefault()?.Classes.FirstOrDefault()?.ClassName,
                Score    = result.Images.FirstOrDefault()?.Classifiers.FirstOrDefault()?.Classes.FirstOrDefault()?.Score.Value * 100,
                IdImagen = idImagen
            };

            this.context.ConsultasWatson.Add(consulta);

            this.context.SaveChanges();
        }
コード例 #5
0
 private void OnClassifyPost(ClassifiedImages classify, Dictionary <string, object> customData)
 {
     Log.Debug("TestVisualRecognition.OnClassifyPost()", "VisualRecognition - ClassifyPost Response: {0}", customData["json"].ToString());
     Test(classify != null);
     _classifyPostTested = true;
 }
コード例 #6
0
        /// <summary>
        /// Classify images. Classify images with built-in or custom classifiers.
        /// </summary>
        /// <param name="imagesFile">An image file (.jpg, .png) or .zip file with images. Maximum image size is 10 MB. Include no more than 20 images and limit the .zip file to 100 MB. Encode the image and .zip file names in UTF-8 if they contain non-ASCII characters. The service assumes UTF-8 encoding if it encounters non-ASCII characters. You can also include images with the `url` property in the **parameters** object. (optional)</param>
        /// <param name="acceptLanguage">Specifies the language of the output class names.  Can be `en` (English), `ar` (Arabic), `de` (German), `es` (Spanish), `it` (Italian), `ja` (Japanese), or `ko` (Korean).  Classes for which no translation is available are omitted.  The response might not be in the specified language under these conditions: - English is returned when the requested language is not supported. - Classes are not returned when there is no translation for them. - Custom classifiers returned with this method return tags in the language of the custom classifier. (optional, default to en)</param>
        /// <param name="url">A string with the image URL to analyze. Must be in .jpg, or .png format. The minimum recommended pixel density is 32X32 pixels per inch, and the maximum image size is 10 MB. You can also include images in the **images_file** parameter. (optional)</param>
        /// <param name="threshold">A floating point value that specifies the minimum score a class must have to be displayed in the response. The default threshold for returning scores from a classifier is `0.5`. Set the threshold to `0.0` to ignore the classification score and return all values. (optional)</param>
        /// <param name="owners">An array of the categories of classifiers to apply. Use `IBM` to classify against the `default` general classifier, and use `me` to classify against your custom classifiers. To analyze the image against both classifier categories, set the value to both `IBM` and `me`.   The built-in `default` classifier is used if both **classifier_ids** and **owners** parameters are empty.  The **classifier_ids** parameter overrides **owners**, so make sure that **classifier_ids** is empty. (optional)</param>
        /// <param name="classifierIds">The **classifier_ids** parameter overrides **owners**, so make sure that **classifier_ids** is empty. - **classifier_ids**: Specifies which classifiers to apply and overrides the **owners** parameter. You can specify both custom and built-in classifiers. The built-in `default` classifier is used if both **classifier_ids** and **owners** parameters are empty.  The following built-in classifier IDs require no training: - `default`: Returns classes from thousands of general tags. - `food`: (Beta) Enhances specificity and accuracy for images of food items. - `explicit`: (Beta) Evaluates whether the image might be pornographic.  Example: `"classifier_ids="CarsvsTrucks_1479118188","explicit"`. (optional)</param>
        /// <param name="imagesFileContentType">The content type of imagesFile. (optional)</param>
        /// <returns><see cref="ClassifiedImages" />ClassifiedImages</returns>
        public ClassifiedImages Classify(System.IO.Stream imagesFile = null, string acceptLanguage = null, string url = null, float?threshold = null, List <string> owners = null, List <string> classifierIds = null, string imagesFileContentType = null)
        {
            if (string.IsNullOrEmpty(VersionDate))
            {
                throw new ArgumentNullException("versionDate cannot be null.");
            }

            ClassifiedImages result = null;

            try
            {
                var formData = new MultipartFormDataContent();

                if (imagesFile != null)
                {
                    var imagesFileContent = new ByteArrayContent((imagesFile as Stream).ReadAllBytes());
                    System.Net.Http.Headers.MediaTypeHeaderValue contentType;
                    System.Net.Http.Headers.MediaTypeHeaderValue.TryParse(imagesFileContentType, out contentType);
                    imagesFileContent.Headers.ContentType = contentType;
                    formData.Add(imagesFileContent, "images_file", "filename");
                }

                if (url != null)
                {
                    var urlContent = new StringContent(url, Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                    urlContent.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
                    formData.Add(urlContent, "url");
                }

                if (threshold != null)
                {
                    var thresholdContent = new StringContent(threshold.ToString(), Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                    formData.Add(thresholdContent, "threshold");
                }

                if (owners != null)
                {
                    var ownersContent = new StringContent(string.Join(", ", owners.ToArray()), Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                    formData.Add(ownersContent, "owners");
                }

                if (classifierIds != null)
                {
                    var classifierIdsContent = new StringContent(string.Join(", ", classifierIds.ToArray()), Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                    formData.Add(classifierIdsContent, "classifier_ids");
                }

                var request = this.Client.PostAsync($"{this.Endpoint}/v3/classify");
                request.WithArgument("api_key", ApiKey);
                request.WithArgument("version", VersionDate);
                request.WithHeader("Accept-Language", acceptLanguage);
                request.WithBodyContent(formData);
                result = request.As <ClassifiedImages>().Result;
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }

            return(result);
        }
コード例 #7
0
        /// <summary>
        /// Classify images.
        ///
        /// Classify images with built-in or custom classifiers.
        /// </summary>
        /// <param name="imagesFile">An image file (.jpg, .png) or .zip file with images. Maximum image size is 10 MB.
        /// Include no more than 20 images and limit the .zip file to 100 MB. Encode the image and .zip file names in
        /// UTF-8 if they contain non-ASCII characters. The service assumes UTF-8 encoding if it encounters non-ASCII
        /// characters.
        ///
        /// You can also include an image with the **url** parameter. (optional)</param>
        /// <param name="acceptLanguage">The language of the output class names. The full set of languages is supported
        /// for the built-in classifier IDs: `default`, `food`, and `explicit`. The class names of custom classifiers
        /// are not translated.
        ///
        /// The response might not be in the specified language when the requested language is not supported or when
        /// there is no translation for the class name. (optional, default to en)</param>
        /// <param name="url">The URL of an image to analyze. Must be in .jpg, or .png format. The minimum recommended
        /// pixel density is 32X32 pixels per inch, and the maximum image size is 10 MB.
        ///
        /// You can also include images with the **images_file** parameter. (optional)</param>
        /// <param name="threshold">The minimum score a class must have to be displayed in the response. Set the
        /// threshold to `0.0` to ignore the classification score and return all values. (optional, default to
        /// 0.5)</param>
        /// <param name="owners">The categories of classifiers to apply. Use `IBM` to classify against the `default`
        /// general classifier, and use `me` to classify against your custom classifiers. To analyze the image against
        /// both classifier categories, set the value to both `IBM` and `me`.
        ///
        /// The built-in `default` classifier is used if both **classifier_ids** and **owners** parameters are empty.
        ///
        /// The **classifier_ids** parameter overrides **owners**, so make sure that **classifier_ids** is empty.
        /// (optional)</param>
        /// <param name="classifierIds">Which classifiers to apply. Overrides the **owners** parameter. You can specify
        /// both custom and built-in classifier IDs. The built-in `default` classifier is used if both
        /// **classifier_ids** and **owners** parameters are empty.
        ///
        /// The following built-in classifier IDs require no training:
        /// - `default`: Returns classes from thousands of general tags.
        /// - `food`: Enhances specificity and accuracy for images of food items.
        /// - `explicit`: Evaluates whether the image might be pornographic. (optional)</param>
        /// <param name="imagesFileContentType">The content type of imagesFile. (optional)</param>
        /// <param name="customData">Custom data object to pass data including custom request headers.</param>
        /// <returns><see cref="ClassifiedImages" />ClassifiedImages</returns>
        public ClassifiedImages Classify(System.IO.FileStream imagesFile = null, string acceptLanguage = null, string url = null, float?threshold = null, List <string> owners = null, List <string> classifierIds = null, string imagesFileContentType = null, Dictionary <string, object> customData = null)
        {
            if (string.IsNullOrEmpty(VersionDate))
            {
                throw new ArgumentNullException("versionDate cannot be null.");
            }

            ClassifiedImages result = null;

            try
            {
                var formData = new MultipartFormDataContent();

                if (imagesFile != null)
                {
                    var imagesFileContent = new ByteArrayContent((imagesFile as Stream).ReadAllBytes());
                    System.Net.Http.Headers.MediaTypeHeaderValue contentType;
                    System.Net.Http.Headers.MediaTypeHeaderValue.TryParse(imagesFileContentType, out contentType);
                    imagesFileContent.Headers.ContentType = contentType;
                    formData.Add(imagesFileContent, "images_file", imagesFile.Name);
                }

                if (url != null)
                {
                    var urlContent = new StringContent(url, Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                    urlContent.Headers.ContentType = null;
                    formData.Add(urlContent, "url");
                }

                if (threshold != null)
                {
                    var thresholdContent = new StringContent(threshold.ToString(), Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                    formData.Add(thresholdContent, "threshold");
                }

                if (owners != null)
                {
                    var ownersContent = new StringContent(string.Join(", ", owners.ToArray()), Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                    formData.Add(ownersContent, "owners");
                }

                if (classifierIds != null)
                {
                    var classifierIdsContent = new StringContent(string.Join(", ", classifierIds.ToArray()), Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
                    formData.Add(classifierIdsContent, "classifier_ids");
                }

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

                restRequest.WithArgument("version", VersionDate);
                if (!string.IsNullOrEmpty(acceptLanguage))
                {
                    restRequest.WithHeader("Accept-Language", acceptLanguage);
                }
                restRequest.WithBodyContent(formData);
                if (customData != null)
                {
                    restRequest.WithCustomData(customData);
                }

                restRequest.WithHeader("X-IBMCloud-SDK-Analytics", "service_name=watson_vision_combined;service_version=v3;operation_id=Classify");
                result = restRequest.As <ClassifiedImages>().Result;
                if (result == null)
                {
                    result = new ClassifiedImages();
                }
                result.CustomData = restRequest.CustomData;
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }

            return(result);
        }
コード例 #8
0
 private void OnClassifyPost(ClassifiedImages classify, Dictionary <string, object> customData)
 {
     Log.Debug("ExampleVisualRecognition.OnClassifyPost()", "{0}", customData["json"].ToString());
     _classifyPostTested = true;
 }
コード例 #9
0
 public void GuardarInteraccionConWatson(ClassifiedImages result, int idImagen)
 {
     this.consultasWatsonRepository.GuardarInteraccionConWatson(result, idImagen);
 }
コード例 #10
0
 private void GuardarInteraccionConWatson(ClassifiedImages result, int idImagen)
 {
     this.consultasWatsonService.GuardarInteraccionConWatson(result, idImagen);
 }