Skip to content

Commit 8d936fa

Browse files
authored
Merge pull request #119 from watson-developer-cloud/feature-106-visualRecognitionRetraining
Feature 106 visual recognition retraining
2 parents 14bd910 + 9c95c23 commit 8d936fa

File tree

14 files changed

+635
-290
lines changed

14 files changed

+635
-290
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
Change Log
22
==========
3+
## Version 0.7.0
4+
_2016-07-29_
5+
6+
* New: Visual Recognition: Added retraining functionality.
7+
* New: Visual Recognition: Use byteArray data to classify, detect faces and recognize text.
8+
* Fix: Updated integration testing.
9+
10+
311
## Version 0.6.1
412
_2016-07-17_
513

Config.json.enc

0 Bytes
Binary file not shown.

Examples/ServiceExamples/Scripts/ExampleVisualRecognition.cs

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
using UnityEngine;
1919
using System.Collections;
20+
using System.Collections.Generic;
2021
using IBM.Watson.DeveloperCloud.Services.VisualRecognition.v3;
2122
using IBM.Watson.DeveloperCloud.Logging;
2223
using IBM.Watson.DeveloperCloud.Utilities;
@@ -35,60 +36,70 @@ void Start()
3536
LogSystem.InstallDefaultReactors();
3637

3738
// Get all classifiers
39+
Log.Debug("ExampleVisualRecognition", "Attempting to get all classifiers");
3840
if (!m_VisualRecognition.GetClassifiers(OnGetClassifiers))
3941
Log.Debug("ExampleVisualRecognition", "Getting classifiers failed!");
40-
//
41-
// Find classifier by name
42-
m_VisualRecognition.FindClassifier(m_classifierName, OnFindClassifier);
43-
44-
// Find classifier by ID
45-
if (!m_VisualRecognition.GetClassifier(m_classifierID, OnGetClassifier))
46-
Log.Debug("ExampleVisualRecognition", "Getting classifier failed!");
47-
48-
// Delete classifier by ID
49-
if (!m_VisualRecognition.DeleteClassifier(m_classifierToDelete, OnDeleteClassifier))
50-
Log.Debug("ExampleVisualRecognition", "Deleting classifier failed!");
51-
52-
// Train classifier
53-
string m_positiveExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/giraffe_positive_examples.zip";
54-
string m_negativeExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/negative_examples.zip";
55-
if (!m_VisualRecognition.TrainClassifier("unity-test-classifier5", "giraffe", m_positiveExamplesPath, m_negativeExamplesPath, OnTrainClassifier))
56-
Log.Debug("ExampleVisualRecognition", "Train classifier failed!");
57-
58-
// Classify get
59-
if (!m_VisualRecognition.Classify(m_imageURL, OnClassify))
60-
Log.Debug("ExampleVisualRecognition", "Classify image failed!");
61-
62-
// Classify post image
63-
string m_imagesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/obama.jpg";
64-
string[] m_owners = { "IBM", "me" };
65-
string[] m_classifierIDs = { "default" };
66-
if (!m_VisualRecognition.Classify(OnClassify, m_imagesPath, m_owners, m_classifierIDs, 0.5f))
67-
Log.Debug("ExampleVisualRecognition", "Classify image failed!");
68-
69-
70-
// Detect faces get
71-
if (!m_VisualRecognition.DetectFaces(m_imageURL, OnDetectFaces))
72-
Log.Debug("ExampleVisualRecogntiion", "Detect faces failed!");
73-
74-
// Detect faces post image
75-
string m_faceExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/obama.jpg";
76-
if (!m_VisualRecognition.DetectFaces(OnDetectFaces, m_faceExamplePath))
77-
Log.Debug("ExampleVisualRecognition", "Detect faces failed!");
78-
79-
80-
81-
// Recognize text get
82-
if (!m_VisualRecognition.RecognizeText(m_imageTextURL, OnRecognizeText))
83-
Log.Debug("ExampleVisualRecognition", "Recognize text failed!");
8442

85-
// Recognize text post image
86-
string m_textExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/from_platos_apology.png";
87-
if (!m_VisualRecognition.RecognizeText(OnRecognizeText, m_textExamplePath))
88-
Log.Debug("ExampleVisualRecognition", "Recognize text failed!");
43+
//// Find classifier by name
44+
//Log.Debug("ExampleVisualRecognition", "Attempting to find classifier by name");
45+
//m_VisualRecognition.FindClassifier(OnFindClassifier, m_classifierName);
46+
47+
//// Find classifier by ID
48+
//Log.Debug("ExampleVisualRecognition", "Attempting to find classifier by ID");
49+
//if (!m_VisualRecognition.GetClassifier(OnGetClassifier, m_classifierID))
50+
// Log.Debug("ExampleVisualRecognition", "Getting classifier failed!");
51+
52+
//// Delete classifier by ID
53+
//Log.Debug("ExampleVisualRecognition", "Attempting to delete classifier");
54+
//if (!m_VisualRecognition.DeleteClassifier(OnDeleteClassifier, m_classifierToDelete))
55+
// Log.Debug("ExampleVisualRecognition", "Deleting classifier failed!");
56+
57+
//// Train classifier
58+
//Log.Debug("ExampleVisualRecognition", "Attempting to train classifier");
59+
//string m_positiveExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/giraffe_positive_examples.zip";
60+
//string m_negativeExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/negative_examples.zip";
61+
//Dictionary<string, string> positiveExamples = new Dictionary<string, string>();
62+
//positiveExamples.Add("giraffe", m_positiveExamplesPath);
63+
//if (!m_VisualRecognition.TrainClassifier(OnTrainClassifier, "unity-test-classifier-example", positiveExamples, m_negativeExamplesPath))
64+
// Log.Debug("ExampleVisualRecognition", "Train classifier failed!");
65+
66+
//// Classify get
67+
//Log.Debug("ExampleVisualRecognition", "Attempting to get classify via URL");
68+
//if (!m_VisualRecognition.Classify(m_imageURL, OnClassify))
69+
// Log.Debug("ExampleVisualRecognition", "Classify image failed!");
70+
71+
//// Classify post image
72+
//Log.Debug("ExampleVisualRecognition", "Attempting to classify via image on file system");
73+
//string m_imagesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/obama.jpg";
74+
//string[] m_owners = { "IBM", "me" };
75+
//string[] m_classifierIDs = { "default" };
76+
//if (!m_VisualRecognition.Classify(OnClassify, m_imagesPath, m_owners, m_classifierIDs, 0.5f))
77+
// Log.Debug("ExampleVisualRecognition", "Classify image failed!");
78+
79+
//// Detect faces get
80+
//Log.Debug("ExampleVisualRecognition", "Attempting to detect faces via URL");
81+
//if (!m_VisualRecognition.DetectFaces(m_imageURL, OnDetectFaces))
82+
// Log.Debug("ExampleVisualRecogntiion", "Detect faces failed!");
83+
84+
//// Detect faces post image
85+
//Log.Debug("ExampleVisualRecognition", "Attempting to detect faces via image");
86+
//string m_faceExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/obama.jpg";
87+
//if (!m_VisualRecognition.DetectFaces(OnDetectFaces, m_faceExamplePath))
88+
// Log.Debug("ExampleVisualRecognition", "Detect faces failed!");
89+
90+
//// Recognize text get
91+
//Log.Debug("ExampleVisualRecognition", "Attempting to recognizeText via URL");
92+
//if (!m_VisualRecognition.RecognizeText(m_imageTextURL, OnRecognizeText))
93+
// Log.Debug("ExampleVisualRecognition", "Recognize text failed!");
94+
95+
//// Recognize text post image
96+
//Log.Debug("ExampleVisualRecognition", "Attempting to recognizeText via image");
97+
//string m_textExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/from_platos_apology.png";
98+
//if (!m_VisualRecognition.RecognizeText(OnRecognizeText, m_textExamplePath))
99+
// Log.Debug("ExampleVisualRecognition", "Recognize text failed!");
89100
}
90101

91-
private void OnGetClassifiers(GetClassifiersTopLevelBrief classifiers)
102+
private void OnGetClassifiers(GetClassifiersTopLevelBrief classifiers, string data)
92103
{
93104
if (classifiers != null && classifiers.classifiers.Length > 0)
94105
{
@@ -103,7 +114,7 @@ private void OnGetClassifiers(GetClassifiersTopLevelBrief classifiers)
103114
}
104115
}
105116

106-
private void OnFindClassifier(GetClassifiersPerClassifierVerbose classifier)
117+
private void OnFindClassifier(GetClassifiersPerClassifierVerbose classifier, string data)
107118
{
108119
if (classifier != null)
109120
{
@@ -115,7 +126,7 @@ private void OnFindClassifier(GetClassifiersPerClassifierVerbose classifier)
115126
}
116127
}
117128

118-
private void OnGetClassifier(GetClassifiersPerClassifierVerbose classifier)
129+
private void OnGetClassifier(GetClassifiersPerClassifierVerbose classifier, string data)
119130
{
120131
if (classifier != null)
121132
{
@@ -127,7 +138,7 @@ private void OnGetClassifier(GetClassifiersPerClassifierVerbose classifier)
127138
}
128139
}
129140

130-
private void OnDeleteClassifier(bool success)
141+
private void OnDeleteClassifier(bool success, string data)
131142
{
132143
if (success)
133144
{
@@ -139,7 +150,7 @@ private void OnDeleteClassifier(bool success)
139150
}
140151
}
141152

142-
private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier)
153+
private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier, string data)
143154
{
144155
if (classifier != null)
145156
{
@@ -151,7 +162,7 @@ private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier)
151162
}
152163
}
153164

154-
private void OnClassify(ClassifyTopLevelMultiple classify)
165+
private void OnClassify(ClassifyTopLevelMultiple classify, string data)
155166
{
156167
if (classify != null)
157168
{
@@ -173,7 +184,7 @@ private void OnClassify(ClassifyTopLevelMultiple classify)
173184
}
174185
}
175186

176-
private void OnDetectFaces(FacesTopLevelMultiple multipleImages)
187+
private void OnDetectFaces(FacesTopLevelMultiple multipleImages, string data)
177188
{
178189
if (multipleImages != null)
179190
{
@@ -196,7 +207,7 @@ private void OnDetectFaces(FacesTopLevelMultiple multipleImages)
196207
}
197208
}
198209

199-
private void OnRecognizeText(TextRecogTopLevelMultiple multipleImages)
210+
private void OnRecognizeText(TextRecogTopLevelMultiple multipleImages, string data)
200211
{
201212
if (multipleImages != null)
202213
{
Binary file not shown.
Binary file not shown.

Examples/ServiceExamples/TestData/visual-recognition-classifiers/turtle_positive_examples.zip.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Loading

Examples/ServiceExamples/TestData/visual-recognition-classifiers/turtle_to_classify.jpg.meta

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ private void OnGetClassifier(GetClassifiersPerClassifierVerbose classifier)
440440
}
441441
}
442442
```
443+
443444
##### Training classifiers
444445
Train a new classifier by uploading image data. Two compressed zip files containing at least two positive example files or one positive and one negative example file. The prefix of the positive example file is used as the classname for the new classifier `<Class Name>_positive_examples`. Negative examples zip must be named `negative_examples`. After a successful call, training the classifier takes a few minutes.
445446

@@ -466,6 +467,36 @@ private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier)
466467
}
467468
}
468469
```
470+
471+
##### Updating a classifier
472+
Update an existing classifier by adding new classes, or by adding new images to existing classes. To update the existing classifier, use several compressed `.zip` files, including files containing positive or negative images `.jpg` or `.png`. You must supply at least one compressed file, with additional positive or negative examples.
473+
474+
Compressed files containing positive examples are used to add or create `classes` that define the updated classifier. The prefix that you specify for each positive example parameter is used as the class name within the classifier. The `_positive_examples` suffix is required. There is no limit on the number of positive example files you can upload in a single call.
475+
The compressed file containing negative examples is not used to create a class within the created classifier, but does define what the new classifier is not. Negative example files should contain images that do not depict the subject of any of the positive examples. You can only specify one negative example file in a single call.
476+
477+
```cs
478+
private VisualRecognition m_VisualRecognition = new VisualRecognition();
479+
480+
void Start()
481+
{
482+
string m_positiveExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/<Class Name>_positive_examples.zip";
483+
if(!m_VisualRecognition.UpdateClassifier(OnUpdateClassifier, "<ClassifierID>", "<Classifier Name>", "<Class Name>", m_positiveExamplesPath))
484+
Log.Debug("ExampleVisualRecognition", "Update classifier failed!");
485+
}
486+
487+
private void OnUpdateClassifier(GetClassifiersPerClassifierVerbose classifier)
488+
{
489+
if(classifier != null)
490+
{
491+
Log.Debug("ExampleVisualRecognition", "Classifier is retraining! " + classifier);
492+
}
493+
else
494+
{
495+
Log.Debug("ExampleVisualRecognition", "Failed to update classifier!");
496+
}
497+
}
498+
```
499+
469500
##### Deleting classifiers
470501
Delete a classifier by Classifier ID
471502

Scripts/Connection/RESTConnector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
// uncomment to enable debugging
19-
#define ENABLE_DEBUGGING
19+
//#define ENABLE_DEBUGGING
2020

2121
using IBM.Watson.DeveloperCloud.Utilities;
2222
using IBM.Watson.DeveloperCloud.Logging;

0 commit comments

Comments
 (0)