Skip to content

Revise Vis Rec Classify POST to send data in form rather than body #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Examples/ServiceExamples/Scripts/ExampleVisualRecognition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*
*/
// Uncomment to train a new classifier
#define TRAIN_CLASSIFIER
//#define TRAIN_CLASSIFIER
// Uncommnent to delete the trained classifier
#define DELETE_TRAINED_CLASSIFIER
//#define DELETE_TRAINED_CLASSIFIER

using UnityEngine;
using System.Collections;
Expand Down Expand Up @@ -163,11 +163,13 @@ private void OnGetClassifiers(GetClassifiersTopLevelBrief classifiers, Dictionar
_getClassifiersTested = true;
}

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

#if DELETE_TRAINED_CLASSIFIER
private void OnDeleteClassifier(bool success, Dictionary<string, object> customData)
Expand Down Expand Up @@ -215,7 +217,7 @@ private void OnDetectFacesPost(FacesTopLevelMultiple multipleImages, Dictionary<
_detectFacesPostTested = true;
}

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

private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
{
Expand Down
14 changes: 9 additions & 5 deletions Scripts/Services/VisualRecognition/v3/VisualRecognition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public VisualRecognition(Credentials credentials)
Log.Error("VisualRecognition.Classify()", "Failed to upload {0}!", imagePath);
}

return Classify(successCallback, failCallback, imageData, owners, classifierIDs, threshold, acceptLanguage, customData);
return Classify(successCallback, failCallback, imageData, GetMimeType(imagePath), owners, classifierIDs, threshold, acceptLanguage, customData);
}

/// <summary>
Expand All @@ -235,13 +235,14 @@ public VisualRecognition(Credentials credentials)
/// <param name="successCallback">The success callback.</param>
/// <param name="failCallback">The fail callback.</param>
/// <param name="imageData">Byte array of image data.</param>
/// <param name="imageMimeType">The mimetype of the image data.</param>
/// <param name="owners">Owners.</param>
/// <param name="classifierIDs">An array of classifier identifiers.</param>
/// <param name="threshold">Threshold.</param>
/// <param name="acceptLanguage">Accepted language.</param>
/// <param name="customData">Custom data.</param>
/// <returns></returns>
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)
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)
{
if (successCallback == null)
throw new ArgumentNullException("successCallback");
Expand All @@ -264,8 +265,8 @@ public VisualRecognition(Credentials credentials)
req.OnResponse = OnClassifyResp;
req.Parameters["api_key"] = _apikey;
req.Parameters["version"] = VersionDate;
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
req.Headers["Accept-Language"] = acceptLanguage;
req.Headers["Content-Type"] = "multipart/form-data";
req.Headers["Accept"] = "application/json";

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

if (imageData != null)
req.Send = imageData;
{
req.Forms = new Dictionary<string, RESTConnector.Form>();
req.Forms.Add("images_file", new RESTConnector.Form(imageData, imageMimeType));
}

return connector.Send(req);
}
Expand Down
10 changes: 6 additions & 4 deletions Scripts/UnitTests/TestVisualRecognition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/

// Uncomment to train a new classifier
#define TRAIN_CLASSIFIER
//#define TRAIN_CLASSIFIER
// Uncommnent to delete the trained classifier
#define DELETE_TRAINED_CLASSIFIER
//#define DELETE_TRAINED_CLASSIFIER

using UnityEngine;
using System.Collections;
Expand Down Expand Up @@ -202,12 +202,14 @@ private void OnGetClassifiers(GetClassifiersTopLevelBrief classifiers, Dictionar
_getClassifiersTested = true;
}

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

#if DELETE_TRAINED_CLASSIFIER
private void OnDeleteClassifier(bool success, Dictionary<string, object> customData)
Expand Down Expand Up @@ -261,7 +263,7 @@ private void OnDetectFacesPost(FacesTopLevelMultiple multipleImages, Dictionary<
_detectFacesPostTested = true;
}

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

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