private IEnumerator Examples()
    {
        //          Get all classifiers
        Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to get all classifiers");
        if (!_visualRecognition.GetClassifiersBrief(OnGetClassifiers, OnFail))
        {
            Log.Debug("ExampleVisualRecognition.GetClassifiers()", "Failed to get all classifiers!");
        }

        while (!_getClassifiersTested)
        {
            yield return(null);
        }

#if TRAIN_CLASSIFIER
        //          Train classifier
        Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to train classifier");
        string positiveExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/giraffe_positive_examples.zip";
        string negativeExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/negative_examples.zip";
        Dictionary <string, string> positiveExamples = new Dictionary <string, string>();
        positiveExamples.Add("giraffe", positiveExamplesPath);
        if (!_visualRecognition.TrainClassifier(OnTrainClassifier, OnFail, "unity-test-classifier-example", positiveExamples, negativeExamplesPath))
        {
            Log.Debug("ExampleVisualRecognition.TrainClassifier()", "Failed to train classifier!");
        }

        while (!_trainClassifierTested)
        {
            yield return(null);
        }

        //          Find classifier by ID
        Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to find classifier by ID");
        if (!_visualRecognition.GetClassifier(OnGetClassifier, OnFail, _classifierID))
        {
            Log.Debug("ExampleVisualRecognition.GetClassifier()", "Failed to get classifier!");
        }

        while (!_getClassifierTested)
        {
            yield return(null);
        }
#endif

        //          Classify get
        Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to get classify via URL");
        if (!_visualRecognition.Classify(_imageURL, OnClassifyGet, OnFail))
        {
            Log.Debug("ExampleVisualRecognition.Classify()", "Classify image failed!");
        }

        while (!_classifyGetTested)
        {
            yield return(null);
        }

        //          Classify post image
        Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to classify via image on file system");
        string   imagesPath    = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/giraffe_to_classify.jpg";
        string[] owners        = { "IBM", "me" };
        string[] classifierIDs = { "default", _classifierID };
        if (!_visualRecognition.Classify(OnClassifyPost, OnFail, imagesPath, owners, classifierIDs, 0.5f))
        {
            Log.Debug("ExampleVisualRecognition.Classify()", "Classify image failed!");
        }

        while (!_classifyPostTested)
        {
            yield return(null);
        }

        //          Detect faces get
        Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to detect faces via URL");
        if (!_visualRecognition.DetectFaces(_imageURL, OnDetectFacesGet, OnFail))
        {
            Log.Debug("ExampleVisualRecognition.DetectFaces()", "Detect faces failed!");
        }

        while (!_detectFacesGetTested)
        {
            yield return(null);
        }

        //          Detect faces post image
        Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to detect faces via image");
        string faceExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/obama.jpg";
        if (!_visualRecognition.DetectFaces(OnDetectFacesPost, OnFail, faceExamplePath))
        {
            Log.Debug("ExampleVisualRecognition.DetectFaces()", "Detect faces failed!");
        }

        while (!_detectFacesPostTested)
        {
            yield return(null);
        }

#if DELETE_TRAINED_CLASSIFIER
        Runnable.Run(IsClassifierReady(_classifierToDelete));
        while (!_isClassifierReady)
        {
            yield return(null);
        }

        //  Download Core ML Model
        Log.Debug("ExampleVisualRecognition.RunTest()", "Attempting to get Core ML Model");
        if (!_visualRecognition.GetCoreMLModel(OnGetCoreMLModel, OnFail, _classifierID))
        {
            Log.Debug("TestVisualRecognition.GetCoreMLModel()", "Failed to get core ml model!");
        }
        while (!_getCoreMLModelTested)
        {
            yield return(null);
        }

        //          Delete classifier by ID
        Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to delete classifier");
        if (!_visualRecognition.DeleteClassifier(OnDeleteClassifier, OnFail, _classifierToDelete))
        {
            Log.Debug("ExampleVisualRecognition.DeleteClassifier()", "Failed to delete classifier!");
        }

        while (!_deleteClassifierTested)
        {
            yield return(null);
        }
#endif

        Log.Debug("ExampleVisualRecognition.Examples()", "Visual Recogition tests complete");
    }
Beispiel #2
0
        public override IEnumerator RunTest()
        {
            LogSystem.InstallDefaultReactors();

            VcapCredentials vcapCredentials = new VcapCredentials();
            fsData          data            = null;

            string result = null;
            string credentialsFilepath = "../sdk-credentials/credentials.json";

            //  Load credentials file if it exists. If it doesn't exist, don't run the tests.
            if (File.Exists(credentialsFilepath))
            {
                result = File.ReadAllText(credentialsFilepath);
            }
            else
            {
                yield break;
            }

            //  Add in a parent object because Unity does not like to deserialize root level collection types.
            result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES");

            //  Convert json to fsResult
            fsResult r = fsJsonParser.Parse(result, out data);

            if (!r.Succeeded)
            {
                throw new WatsonException(r.FormattedMessages);
            }

            //  Convert fsResult to VcapCredentials
            object obj = vcapCredentials;

            r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
            if (!r.Succeeded)
            {
                throw new WatsonException(r.FormattedMessages);
            }

            //  Set credentials from imported credntials
            Credential credential = vcapCredentials.GetCredentialByname("visual-recognition-iam-sdk")[0].Credentials;

            //  Create credential and instantiate service
            TokenOptions tokenOptions = new TokenOptions()
            {
                IamApiKey = credential.IamApikey,
                IamUrl    = credential.IamUrl
            };

            Credentials credentials = new Credentials(tokenOptions, credential.Url);

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

            _visualRecognition             = new VisualRecognition(credentials);
            _visualRecognition.VersionDate = _visualRecognitionVersionDate;

            //          Get all classifiers
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to get all classifiers");
            if (!_visualRecognition.GetClassifiersBrief(OnGetClassifiers, OnFail))
            {
                Log.Debug("TestVisualRecognition.GetClassifiers()", "Failed to get all classifiers!");
            }

            while (!_getClassifiersTested)
            {
                yield return(null);
            }

#if TRAIN_CLASSIFIER
            _isClassifierReady = false;
            //          Train classifier
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to train classifier");
            string positiveExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/giraffe_positive_examples.zip";
            string negativeExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/negative_examples.zip";
            Dictionary <string, string> positiveExamples = new Dictionary <string, string>();
            positiveExamples.Add("giraffe", positiveExamplesPath);
            if (!_visualRecognition.TrainClassifier(OnTrainClassifier, OnFail, "unity-test-classifier-ok-to-delete", positiveExamples, negativeExamplesPath))
            {
                Log.Debug("TestVisualRecognition.TrainClassifier()", "Failed to train classifier!");
            }

            while (!_trainClassifierTested)
            {
                yield return(null);
            }

            //          Find classifier by ID
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to find classifier by ID");
            if (!_visualRecognition.GetClassifier(OnGetClassifier, OnFail, _classifierID))
            {
                Log.Debug("TestVisualRecognition.GetClassifier()", "Failed to get classifier!");
            }

            while (!_getClassifierTested)
            {
                yield return(null);
            }
#endif

            //  Classify get
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to get classify via URL");
            if (!_visualRecognition.Classify(_imageURL, OnClassifyGet, OnFail))
            {
                Log.Debug("TestVisualRecognition.Classify()", "Classify image failed!");
            }

            while (!_classifyGetTested)
            {
                yield return(null);
            }

            //  Classify post image
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to classify via image on file system");
            string   imagesPath    = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/giraffe_to_classify.jpg";
            string[] owners        = { "IBM", "me" };
            string[] classifierIDs = { "default", _classifierID };
            if (!_visualRecognition.Classify(OnClassifyPost, OnFail, imagesPath, owners, classifierIDs, 0.5f))
            {
                Log.Debug("TestVisualRecognition.Classify()", "Classify image failed!");
            }

            while (!_classifyPostTested)
            {
                yield return(null);
            }

            //  Detect faces get
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to detect faces via URL");
            if (!_visualRecognition.DetectFaces(_imageURL, OnDetectFacesGet, OnFail))
            {
                Log.Debug("TestVisualRecognition.DetectFaces()", "Detect faces failed!");
            }

            while (!_detectFacesGetTested)
            {
                yield return(null);
            }

            //  Detect faces post image
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to detect faces via image");
            string faceExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/obama.jpg";
            if (!_visualRecognition.DetectFaces(OnDetectFacesPost, OnFail, faceExamplePath))
            {
                Log.Debug("TestVisualRecognition.DetectFaces()", "Detect faces failed!");
            }

            while (!_detectFacesPostTested)
            {
                yield return(null);
            }

#if DELETE_TRAINED_CLASSIFIER
            Runnable.Run(IsClassifierReady(_classifierToDelete));
            while (!_isClassifierReady)
            {
                yield return(null);
            }

            //  Download Core ML Model
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to get Core ML Model");
            if (!_visualRecognition.GetCoreMLModel(OnGetCoreMLModel, OnFail, _classifierID))
            {
                Log.Debug("TestVisualRecognition.GetCoreMLModel()", "Failed to get core ml model!");
            }
            while (!_getCoreMLModelTested)
            {
                yield return(null);
            }

            //  Delete classifier by ID
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to delete classifier");
            if (!_visualRecognition.DeleteClassifier(OnDeleteClassifier, OnFail, _classifierToDelete))
            {
                Log.Debug("TestVisualRecognition.DeleteClassifier()", "Failed to delete classifier!");
            }

            while (!_deleteClassifierTested)
            {
                yield return(null);
            }
#endif

            Log.Debug("TestVisualRecognition.RunTest()", "Visual Recogition tests complete");
            yield break;
        }
        public override IEnumerator RunTest()
        {
            LogSystem.InstallDefaultReactors();

            VcapCredentials vcapCredentials = new VcapCredentials();
            fsData          data            = null;

            string result = null;

            var vcapUrl      = Environment.GetEnvironmentVariable("VCAP_URL");
            var vcapUsername = Environment.GetEnvironmentVariable("VCAP_USERNAME");
            var vcapPassword = Environment.GetEnvironmentVariable("VCAP_PASSWORD");

            using (SimpleGet simpleGet = new SimpleGet(vcapUrl, vcapUsername, vcapPassword))
            {
                while (!simpleGet.IsComplete)
                {
                    yield return(null);
                }

                result = simpleGet.Result;
            }

            //  Add in a parent object because Unity does not like to deserialize root level collection types.
            result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES");

            //  Convert json to fsResult
            fsResult r = fsJsonParser.Parse(result, out data);

            if (!r.Succeeded)
            {
                throw new WatsonException(r.FormattedMessages);
            }

            //  Convert fsResult to VcapCredentials
            object obj = vcapCredentials;

            r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
            if (!r.Succeeded)
            {
                throw new WatsonException(r.FormattedMessages);
            }

            //  Set credentials from imported credntials
            Credential credential = vcapCredentials.VCAP_SERVICES["visual_recognition"];

            _apikey = credential.Apikey.ToString();
            _url    = credential.Url.ToString();

            //  Create credential and instantiate service
            Credentials credentials = new Credentials(_apikey, _url);

            _visualRecognition             = new VisualRecognition(credentials);
            _visualRecognition.VersionDate = _visualRecognitionVersionDate;

            //          Get all classifiers
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to get all classifiers");
            if (!_visualRecognition.GetClassifiersBrief(OnGetClassifiers, OnFail))
            {
                Log.Debug("TestVisualRecognition.GetClassifiers()", "Failed to get all classifiers!");
            }

            while (!_getClassifiersTested)
            {
                yield return(null);
            }

#if TRAIN_CLASSIFIER
            //          Train classifier
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to train classifier");
            string positiveExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/giraffe_positive_examples.zip";
            string negativeExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/negative_examples.zip";
            Dictionary <string, string> positiveExamples = new Dictionary <string, string>();
            positiveExamples.Add("giraffe", positiveExamplesPath);
            if (!_visualRecognition.TrainClassifier(OnTrainClassifier, OnFail, "unity-test-classifier-example", positiveExamples, negativeExamplesPath))
            {
                Log.Debug("TestVisualRecognition.TrainClassifier()", "Failed to train classifier!");
            }

            while (!_trainClassifierTested)
            {
                yield return(null);
            }

            //          Find classifier by ID
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to find classifier by ID");
            if (!_visualRecognition.GetClassifier(OnGetClassifier, OnFail, _classifierID))
            {
                Log.Debug("TestVisualRecognition.GetClassifier()", "Failed to get classifier!");
            }

            while (!_getClassifierTested)
            {
                yield return(null);
            }

            //  Download Core ML Model
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to get Core ML Model");
            if (!_visualRecognition.GetCoreMLModel(OnGetCoreMLModel, OnFail, _classifierID))
            {
                Log.Debug("TestVisualRecognition.GetCoreMLModel()", "Failed to get core ml model!");
            }
            while (!_getCoreMLModelTested)
            {
                yield return(null);
            }
#endif

            //          Classify get
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to get classify via URL");
            if (!_visualRecognition.Classify(_imageURL, OnClassifyGet, OnFail))
            {
                Log.Debug("TestVisualRecognition.Classify()", "Classify image failed!");
            }

            while (!_classifyGetTested)
            {
                yield return(null);
            }

            //          Classify post image
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to classify via image on file system");
            string   imagesPath    = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/giraffe_to_classify.jpg";
            string[] owners        = { "IBM", "me" };
            string[] classifierIDs = { "default", _classifierID };
            if (!_visualRecognition.Classify(OnClassifyPost, OnFail, imagesPath, owners, classifierIDs, 0.5f))
            {
                Log.Debug("TestVisualRecognition.Classify()", "Classify image failed!");
            }

            while (!_classifyPostTested)
            {
                yield return(null);
            }

            //          Detect faces get
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to detect faces via URL");
            if (!_visualRecognition.DetectFaces(_imageURL, OnDetectFacesGet, OnFail))
            {
                Log.Debug("TestVisualRecognition.DetectFaces()", "Detect faces failed!");
            }

            while (!_detectFacesGetTested)
            {
                yield return(null);
            }

            //          Detect faces post image
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to detect faces via image");
            string faceExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/obama.jpg";
            if (!_visualRecognition.DetectFaces(OnDetectFacesPost, OnFail, faceExamplePath))
            {
                Log.Debug("TestVisualRecognition.DetectFaces()", "Detect faces failed!");
            }

            while (!_detectFacesPostTested)
            {
                yield return(null);
            }


#if DELETE_TRAINED_CLASSIFIER
            #region Delay
            Runnable.Run(Delay(_delayTime));
            while (_isWaitingForDelay)
            {
                yield return(null);
            }
            #endregion

            //          Delete classifier by ID
            Log.Debug("TestVisualRecognition.RunTest()", "Attempting to delete classifier");
            if (!_visualRecognition.DeleteClassifier(OnDeleteClassifier, OnFail, _classifierToDelete))
            {
                Log.Debug("TestVisualRecognition.DeleteClassifier()", "Failed to delete classifier!");
            }

            while (!_deleteClassifierTested)
            {
                yield return(null);
            }
#endif

            Log.Debug("TestVisualRecognition.RunTest()", "Visual Recogition tests complete");
            yield break;
        }