Skip to content

Commit a6f9d95

Browse files
authored
Merge pull request #323 from watson-developer-cloud/320-visual-recognition-classify-post
Revise Vis Rec Classify POST to send data in form rather than body
2 parents 9497b97 + cd3e7b8 commit a6f9d95

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

Examples/ServiceExamples/Scripts/ExampleVisualRecognition.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*
1616
*/
1717
// Uncomment to train a new classifier
18-
#define TRAIN_CLASSIFIER
18+
//#define TRAIN_CLASSIFIER
1919
// Uncommnent to delete the trained classifier
20-
#define DELETE_TRAINED_CLASSIFIER
20+
//#define DELETE_TRAINED_CLASSIFIER
2121

2222
using UnityEngine;
2323
using System.Collections;
@@ -163,11 +163,13 @@ private void OnGetClassifiers(GetClassifiersTopLevelBrief classifiers, Dictionar
163163
_getClassifiersTested = true;
164164
}
165165

166+
#if DELETE_TRAINED_CLASSIFIER
166167
private void OnGetClassifier(GetClassifiersPerClassifierVerbose classifier, Dictionary<string, object> customData)
167168
{
168169
Log.Debug("ExampleVisualRecognition.OnGetClassifier()", "VisualRecognition - GetClassifier Response: {0}", customData["json"].ToString());
169170
_getClassifierTested = true;
170171
}
172+
#endif
171173

172174
#if DELETE_TRAINED_CLASSIFIER
173175
private void OnDeleteClassifier(bool success, Dictionary<string, object> customData)
@@ -215,7 +217,7 @@ private void OnDetectFacesPost(FacesTopLevelMultiple multipleImages, Dictionary<
215217
_detectFacesPostTested = true;
216218
}
217219

218-
#region Delay
220+
#region Delay
219221
// Introducing a delay because of a known issue with Visual Recognition where newly created classifiers
220222
// will disappear without being deleted if a delete is attempted less than ~10 seconds after creation.
221223
private float _delayTime = 15f;
@@ -228,7 +230,7 @@ private IEnumerator Delay(float delayTime)
228230
yield return new WaitForSeconds(delayTime);
229231
_isWaitingForDelay = false;
230232
}
231-
#endregion
233+
#endregion
232234

233235
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
234236
{

Scripts/Services/VisualRecognition/v3/VisualRecognition.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public VisualRecognition(Credentials credentials)
226226
Log.Error("VisualRecognition.Classify()", "Failed to upload {0}!", imagePath);
227227
}
228228

229-
return Classify(successCallback, failCallback, imageData, owners, classifierIDs, threshold, acceptLanguage, customData);
229+
return Classify(successCallback, failCallback, imageData, GetMimeType(imagePath), owners, classifierIDs, threshold, acceptLanguage, customData);
230230
}
231231

232232
/// <summary>
@@ -235,13 +235,14 @@ public VisualRecognition(Credentials credentials)
235235
/// <param name="successCallback">The success callback.</param>
236236
/// <param name="failCallback">The fail callback.</param>
237237
/// <param name="imageData">Byte array of image data.</param>
238+
/// <param name="imageMimeType">The mimetype of the image data.</param>
238239
/// <param name="owners">Owners.</param>
239240
/// <param name="classifierIDs">An array of classifier identifiers.</param>
240241
/// <param name="threshold">Threshold.</param>
241242
/// <param name="acceptLanguage">Accepted language.</param>
242243
/// <param name="customData">Custom data.</param>
243244
/// <returns></returns>
244-
public bool Classify(SuccessCallback<ClassifyTopLevelMultiple> successCallback, FailCallback failCallback, byte[] imageData, string[] owners = default(string[]), string[] classifierIDs = default(string[]), float threshold = default(float), string acceptLanguage = "en", Dictionary<string, object> customData = null)
245+
public bool Classify(SuccessCallback<ClassifyTopLevelMultiple> successCallback, FailCallback failCallback, byte[] imageData, string imageMimeType, string[] owners = default(string[]), string[] classifierIDs = default(string[]), float threshold = default(float), string acceptLanguage = "en", Dictionary<string, object> customData = null)
245246
{
246247
if (successCallback == null)
247248
throw new ArgumentNullException("successCallback");
@@ -264,8 +265,8 @@ public VisualRecognition(Credentials credentials)
264265
req.OnResponse = OnClassifyResp;
265266
req.Parameters["api_key"] = _apikey;
266267
req.Parameters["version"] = VersionDate;
267-
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
268-
req.Headers["Accept-Language"] = acceptLanguage;
268+
req.Headers["Content-Type"] = "multipart/form-data";
269+
req.Headers["Accept"] = "application/json";
269270

270271
if (owners != default(string[]))
271272
req.Parameters["owners"] = string.Join(",", owners);
@@ -275,7 +276,10 @@ public VisualRecognition(Credentials credentials)
275276
req.Parameters["threshold"] = threshold;
276277

277278
if (imageData != null)
278-
req.Send = imageData;
279+
{
280+
req.Forms = new Dictionary<string, RESTConnector.Form>();
281+
req.Forms.Add("images_file", new RESTConnector.Form(imageData, imageMimeType));
282+
}
279283

280284
return connector.Send(req);
281285
}

Scripts/UnitTests/TestVisualRecognition.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*/
1717

1818
// Uncomment to train a new classifier
19-
#define TRAIN_CLASSIFIER
19+
//#define TRAIN_CLASSIFIER
2020
// Uncommnent to delete the trained classifier
21-
#define DELETE_TRAINED_CLASSIFIER
21+
//#define DELETE_TRAINED_CLASSIFIER
2222

2323
using UnityEngine;
2424
using System.Collections;
@@ -202,12 +202,14 @@ private void OnGetClassifiers(GetClassifiersTopLevelBrief classifiers, Dictionar
202202
_getClassifiersTested = true;
203203
}
204204

205+
#if DELETE_TRAINED_CLASSIFIER
205206
private void OnGetClassifier(GetClassifiersPerClassifierVerbose classifier, Dictionary<string, object> customData)
206207
{
207208
Log.Debug("TestVisualRecognition.OnGetClassifier()", "VisualRecognition - GetClassifier Response: {0}", customData["json"].ToString());
208209
Test(classifier != null);
209210
_getClassifierTested = true;
210211
}
212+
#endif
211213

212214
#if DELETE_TRAINED_CLASSIFIER
213215
private void OnDeleteClassifier(bool success, Dictionary<string, object> customData)
@@ -261,7 +263,7 @@ private void OnDetectFacesPost(FacesTopLevelMultiple multipleImages, Dictionary<
261263
_detectFacesPostTested = true;
262264
}
263265

264-
#region Delay
266+
#region Delay
265267
// Introducing a delay because of a known issue with Visual Recognition where newly created classifiers
266268
// will disappear without being deleted if a delete is attempted less than ~10 seconds after creation.
267269
private float _delayTime = 15f;
@@ -274,7 +276,7 @@ private IEnumerator Delay(float delayTime)
274276
yield return new WaitForSeconds(delayTime);
275277
_isWaitingForDelay = false;
276278
}
277-
#endregion
279+
#endregion
278280

279281
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
280282
{

0 commit comments

Comments
 (0)