Last active
October 29, 2018 16:14
-
-
Save mediumTaj/9135a27abe102a16c0bcaf3ee972e506 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using IBM.Watson.DeveloperCloud.Connection; | |
using IBM.Watson.DeveloperCloud.Logging; | |
using IBM.Watson.DeveloperCloud.Services.VisualRecognition.v3; | |
using IBM.Watson.DeveloperCloud.Utilities; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class ClassifyExample : MonoBehaviour | |
{ | |
private VisualRecognition visualRecognition; | |
private string visualRecognitionApiKey = ""; | |
private string visualRecognitionServiceUrl = ""; | |
private string imagePath; | |
private string versionDate = "2018-10-29"; | |
void Start() | |
{ | |
LogSystem.InstallDefaultReactors(); | |
// Set image path | |
imagePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/giraffe_to_classify.jpg"; | |
// Create service in coroutine | |
Runnable.Run(CreateService()); | |
} | |
private IEnumerator CreateService() | |
{ | |
// Create tokenOptions | |
TokenOptions visualRecognitionTokenOptions = new TokenOptions() | |
{ | |
IamApiKey = visualRecognitionApiKey | |
}; | |
// Create credentials | |
Credentials visualRecognitionCredentials = new Credentials(visualRecognitionTokenOptions, visualRecognitionServiceUrl); | |
// Wait for tokendata | |
while (!visualRecognitionCredentials.HasIamTokenData()) | |
yield return null; | |
// Instantiate service | |
visualRecognition = new VisualRecognition(visualRecognitionCredentials); | |
// Set version date | |
visualRecognition.VersionDate = versionDate; | |
// Classify | |
visualRecognition.Classify(OnClassify, OnFail, imagePath); | |
} | |
// Fail callback | |
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData) | |
{ | |
Log.Debug("ClassifyExample", "Failed to classify"); | |
} | |
// Success callback | |
private void OnClassify(ClassifiedImages response, Dictionary<string, object> customData) | |
{ | |
// Print response json to console | |
Log.Debug("ClassifyExample", "{0}", customData["json"].ToString()); | |
// Print class and confidence to console | |
Log.Debug("ClassifyExample", "class: {0}, score: {1}", response.images[0].classifiers[0].classes[0]._class, response.images[0].classifiers[0].classes[0].score); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment