Skip to content

Commit 01759a2

Browse files
authored
Merge pull request #357 from watson-developer-cloud/4182/visual-recognition-revisions
Support Core ML Models in Visual Recognition
2 parents 00bfdda + d1e1c31 commit 01759a2

File tree

5 files changed

+359
-667
lines changed

5 files changed

+359
-667
lines changed

Examples/ServiceExamples/Scripts/ExampleVisualRecognition.cs

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class ExampleVisualRecognition : MonoBehaviour
4242

4343
private string _classifierID = "";
4444
private string _imageURL = "https://upload.wikimedia.org/wikipedia/commons/e/e9/Official_portrait_of_Barack_Obama.jpg";
45-
//private string _imageTextURL = "http://i.stack.imgur.com/ZS6nH.png";
45+
private string _coreMLDownloadPath = "";
4646

4747
#if DELETE_TRAINED_CLASSIFIER
4848
private string _classifierToDelete;
@@ -60,6 +60,7 @@ public class ExampleVisualRecognition : MonoBehaviour
6060
private bool _classifyPostTested = false;
6161
private bool _detectFacesGetTested = false;
6262
private bool _detectFacesPostTested = false;
63+
private bool _getCoreMLModelTested = false;
6364

6465
void Start()
6566
{
@@ -78,32 +79,32 @@ private IEnumerator Examples()
7879
{
7980
// Get all classifiers
8081
Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to get all classifiers");
81-
if (!_visualRecognition.GetClassifiers(OnGetClassifiers, OnFail))
82+
if (!_visualRecognition.GetClassifiersBrief(OnGetClassifiers, OnFail))
8283
Log.Debug("ExampleVisualRecognition.GetClassifiers()", "Failed to get all classifiers!");
8384

8485
while (!_getClassifiersTested)
8586
yield return null;
8687

8788
#if TRAIN_CLASSIFIER
88-
// Train classifier
89-
Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to train classifier");
90-
string positiveExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/giraffe_positive_examples.zip";
91-
string negativeExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/negative_examples.zip";
92-
Dictionary<string, string> positiveExamples = new Dictionary<string, string>();
93-
positiveExamples.Add("giraffe", positiveExamplesPath);
94-
if (!_visualRecognition.TrainClassifier(OnTrainClassifier, OnFail, "unity-test-classifier-example", positiveExamples, negativeExamplesPath))
95-
Log.Debug("ExampleVisualRecognition.TrainClassifier()", "Failed to train classifier!");
96-
97-
while (!_trainClassifierTested)
98-
yield return null;
99-
100-
// Find classifier by ID
101-
Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to find classifier by ID");
102-
if (!_visualRecognition.GetClassifier(OnGetClassifier, OnFail, _classifierID))
103-
Log.Debug("ExampleVisualRecognition.GetClassifier()", "Failed to get classifier!");
104-
105-
while (!_getClassifierTested)
106-
yield return null;
89+
// Train classifier
90+
Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to train classifier");
91+
string positiveExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/giraffe_positive_examples.zip";
92+
string negativeExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/negative_examples.zip";
93+
Dictionary<string, string> positiveExamples = new Dictionary<string, string>();
94+
positiveExamples.Add("giraffe", positiveExamplesPath);
95+
if (!_visualRecognition.TrainClassifier(OnTrainClassifier, OnFail, "unity-test-classifier-example", positiveExamples, negativeExamplesPath))
96+
Log.Debug("ExampleVisualRecognition.TrainClassifier()", "Failed to train classifier!");
97+
98+
while (!_trainClassifierTested)
99+
yield return null;
100+
101+
// Find classifier by ID
102+
Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to find classifier by ID");
103+
if (!_visualRecognition.GetClassifier(OnGetClassifier, OnFail, _classifierID))
104+
Log.Debug("ExampleVisualRecognition.GetClassifier()", "Failed to get classifier!");
105+
106+
while (!_getClassifierTested)
107+
yield return null;
107108
#endif
108109

109110
// Classify get
@@ -144,29 +145,33 @@ private IEnumerator Examples()
144145

145146
#if DELETE_TRAINED_CLASSIFIER
146147
#region Delay
147-
Runnable.Run(Delay(_delayTime));
148-
while (_isWaitingForDelay)
149-
yield return null;
148+
Runnable.Run(Delay(_delayTime));
149+
while (_isWaitingForDelay)
150+
yield return null;
150151
#endregion
151152

152-
// Delete classifier by ID
153-
Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to delete classifier");
154-
if (!_visualRecognition.DeleteClassifier(OnDeleteClassifier, OnFail, _classifierToDelete))
155-
Log.Debug("ExampleVisualRecognition.DeleteClassifier()", "Failed to delete classifier!");
153+
// Delete classifier by ID
154+
Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to delete classifier");
155+
if (!_visualRecognition.DeleteClassifier(OnDeleteClassifier, OnFail, _classifierToDelete))
156+
Log.Debug("ExampleVisualRecognition.DeleteClassifier()", "Failed to delete classifier!");
156157

157-
while (!_deleteClassifierTested)
158-
yield return null;
158+
while (!_deleteClassifierTested)
159+
yield return null;
159160
#endif
160161

162+
_visualRecognition.DownloadCoreMLModel(_classifierID, _coreMLDownloadPath);
163+
while (!_getCoreMLModelTested)
164+
yield return null;
165+
161166
Log.Debug("ExampleVisualRecognition.Examples()", "Visual Recogition tests complete");
162167
}
163168

164-
private void OnGetClassifiers(GetClassifiersTopLevelBrief classifiers, Dictionary<string, object> customData)
165-
{
166-
Log.Debug("ExampleVisualRecognition.OnGetClassifiers()", "VisualRecognition - GetClassifiers Response: {0}", customData["json"].ToString());
169+
private void OnGetClassifiers(ClassifiersBrief classifiers, Dictionary<string, object> customData)
170+
{
171+
Log.Debug("ExampleVisualRecognition.OnGetClassifiers()", "VisualRecognition - GetClassifiers Response: {0}", customData["json"].ToString());
167172

168-
_getClassifiersTested = true;
169-
}
173+
_getClassifiersTested = true;
174+
}
170175

171176
#if DELETE_TRAINED_CLASSIFIER
172177
private void OnGetClassifier(GetClassifiersPerClassifierVerbose classifier, Dictionary<string, object> customData)
@@ -197,34 +202,34 @@ private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier, Di
197202
}
198203
#endif
199204

200-
private void OnClassifyGet(ClassifyTopLevelMultiple classify, Dictionary<string, object> customData)
201-
{
202-
Log.Debug("ExampleVisualRecognition.OnClassifyGet()", "{0}", customData["json"].ToString());
203-
_classifyGetTested = true;
205+
private void OnClassifyGet(ClassifiedImages classify, Dictionary<string, object> customData)
206+
{
207+
Log.Debug("ExampleVisualRecognition.OnClassifyGet()", "{0}", customData["json"].ToString());
208+
_classifyGetTested = true;
204209

205-
}
210+
}
206211

207-
private void OnClassifyPost(ClassifyTopLevelMultiple classify, Dictionary<string, object> customData)
208-
{
209-
Log.Debug("ExampleVisualRecognition.OnClassifyPost()", "{0}", customData["json"].ToString());
210-
_classifyPostTested = true;
211-
}
212+
private void OnClassifyPost(ClassifiedImages classify, Dictionary<string, object> customData)
213+
{
214+
Log.Debug("ExampleVisualRecognition.OnClassifyPost()", "{0}", customData["json"].ToString());
215+
_classifyPostTested = true;
216+
}
212217

213-
private void OnDetectFacesGet(FacesTopLevelMultiple multipleImages, Dictionary<string, object> customData)
214-
{
215-
Log.Debug("ExampleVisualRecognition.OnDetectFacesGet()", "{0}", customData["json"].ToString());
216-
_detectFacesGetTested = true;
217-
}
218+
private void OnDetectFacesGet(DetectedFaces multipleImages, Dictionary<string, object> customData)
219+
{
220+
Log.Debug("ExampleVisualRecognition.OnDetectFacesGet()", "{0}", customData["json"].ToString());
221+
_detectFacesGetTested = true;
222+
}
218223

219-
private void OnDetectFacesPost(FacesTopLevelMultiple multipleImages, Dictionary<string, object> customData)
220-
{
221-
Log.Debug("ExampleVisualRecognition.OnDetectFacesPost()", "{0}", customData["json"].ToString());
222-
_detectFacesPostTested = true;
223-
}
224+
private void OnDetectFacesPost(DetectedFaces multipleImages, Dictionary<string, object> customData)
225+
{
226+
Log.Debug("ExampleVisualRecognition.OnDetectFacesPost()", "{0}", customData["json"].ToString());
227+
_detectFacesPostTested = true;
228+
}
224229

225-
#region Delay
226-
// Introducing a delay because of a known issue with Visual Recognition where newly created classifiers
227-
// will disappear without being deleted if a delete is attempted less than ~10 seconds after creation.
230+
#region Delay
231+
// Introducing a delay because of a known issue with Visual Recognition where newly created classifiers
232+
// will disappear without being deleted if a delete is attempted less than ~10 seconds after creation.
228233
#if DELETE_TRAINED_CLASSIFIER
229234
private float _delayTime = 15f;
230235
private bool _isWaitingForDelay = false;
@@ -238,10 +243,10 @@ private IEnumerator Delay(float delayTime)
238243
_isWaitingForDelay = false;
239244
}
240245
#endif
241-
#endregion
246+
#endregion
242247

243-
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
244-
{
245-
Log.Error("ExampleRetrieveAndRank.OnFail()", "Error received: {0}", error.ToString());
246-
}
248+
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
249+
{
250+
Log.Error("ExampleRetrieveAndRank.OnFail()", "Error received: {0}", error.ToString());
251+
}
247252
}

0 commit comments

Comments
 (0)