Skip to content

Feature vr training mime type #161

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 8 commits into from
Oct 7, 2016
77 changes: 59 additions & 18 deletions Scripts/Services/VisualRecognition/VisualRecognition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ public class VisualRecognition : IWatsonService
private const float REQUEST_TIMEOUT = 10.0f * 60.0f;
#endregion

#region Public Functions
public static void ClearApiKey()
{
mp_ApiKey = default(string);
}
#endregion

#region Classify Image
/// <summary>
/// Classifies image specified by URL.
Expand Down Expand Up @@ -896,7 +903,8 @@ private void OnGetClassifierResp(RESTConnector.Request req, RESTConnector.Respon
/// <param name="classifierName">Classifier name.</param>
/// <param name="positiveExamples">Dictionary of class name and positive example paths.</param>
/// <param name="negativeExamplesPath">Negative example file path.</param>
public bool TrainClassifier(OnTrainClassifier callback, string classifierName, Dictionary<string, string> positiveExamples, string negativeExamplesPath = default(string), string customData = default(string))
/// <param name="mimeType">Mime type of the positive examples and negative examples data. Use GetMimeType to get Mimetype from filename.</param>
public bool TrainClassifier(OnTrainClassifier callback, string classifierName, Dictionary<string, string> positiveExamples, string negativeExamplesPath = default(string), string mimeType = "application/zip", string customData = default(string))
{
if (string.IsNullOrEmpty(mp_ApiKey))
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
Expand Down Expand Up @@ -932,18 +940,19 @@ private void OnGetClassifierResp(RESTConnector.Request req, RESTConnector.Respon
if (positiveExamplesData.Count == 0 || negativeExamplesData == null)
Log.Error("VisualRecognition", "Failed to upload positive or negative examples!");

return TrainClassifier(callback, classifierName, positiveExamplesData, negativeExamplesData, customData);
return TrainClassifier(callback, classifierName, positiveExamplesData, negativeExamplesData, mimeType, customData);
}

/// <summary>
/// Trains a classifier
/// </summary>
/// <param name="callback">Callback.</param>
/// <param name="classifierName">Classifier name.</param>
/// <param name="positiveExamplesData">Dictionary of class name and class training zip byte data.</param>
/// <param name="negativeExamplesData">Negative examples zip byte data.</param>
/// <param name="positiveExamplesData">Dictionary of class name and class training zip or image byte data.</param>
/// <param name="negativeExamplesData">Negative examples zip or image byte data.</param>
/// <param name="mimeType">Mime type of the positive examples and negative examples data.</param>
/// <returns></returns>
public bool TrainClassifier(OnTrainClassifier callback, string classifierName, Dictionary<string, byte[]> positiveExamplesData, byte[] negativeExamplesData = null, string customData = default(string))
public bool TrainClassifier(OnTrainClassifier callback, string classifierName, Dictionary<string, byte[]> positiveExamplesData, byte[] negativeExamplesData = null, string mimeType = "application/zip", string customData = default(string))
{
if (string.IsNullOrEmpty(mp_ApiKey))
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
Expand All @@ -968,10 +977,11 @@ private void OnGetClassifierResp(RESTConnector.Request req, RESTConnector.Respon
req.Parameters["version"] = VisualRecognitionVersion.Version;
req.Forms = new Dictionary<string, RESTConnector.Form>();
req.Forms["name"] = new RESTConnector.Form(classifierName);

foreach (KeyValuePair<string, byte[]> kv in positiveExamplesData)
req.Forms[kv.Key + "_positive_examples"] = new RESTConnector.Form(kv.Value, kv.Key + "_positive_examples.zip", "application/zip");
req.Forms[kv.Key + "_positive_examples"] = new RESTConnector.Form(kv.Value, kv.Key + "_positive_examples" + GetExtension(mimeType), mimeType);
if(negativeExamplesData != null)
req.Forms["negative_examples"] = new RESTConnector.Form(negativeExamplesData, "negative_examples.zip", "application/zip");
req.Forms["negative_examples"] = new RESTConnector.Form(negativeExamplesData, "negative_examples" + GetExtension(mimeType), mimeType);

return connector.Send(req);
}
Expand Down Expand Up @@ -1030,7 +1040,8 @@ private void OnTrainClassifierResp(RESTConnector.Request req, RESTConnector.Resp
/// <param name="classifierName">Classifier name.</param>
/// <param name="positiveExamples">Dictionary of class name and positive example paths.</param>
/// <param name="negativeExamplesPath">Negative example file path.</param>
public bool UpdateClassifier(OnTrainClassifier callback, string classifierID, string classifierName, Dictionary<string, string> positiveExamples, string negativeExamplesPath = default(string), string customData = default(string))
/// <param name="mimeType">Mimetype of the file. Use GetMimeType to get Mimetype from filename.</param>
public bool UpdateClassifier(OnTrainClassifier callback, string classifierID, string classifierName, Dictionary<string, string> positiveExamples, string negativeExamplesPath = default(string), string mimeType = "application/zip", string customData = default(string))
{
if (string.IsNullOrEmpty(mp_ApiKey))
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
Expand Down Expand Up @@ -1068,7 +1079,7 @@ private void OnTrainClassifierResp(RESTConnector.Request req, RESTConnector.Resp
if (positiveExamplesData.Count == 0 && negativeExamplesData == null)
Log.Error("VisualRecognition", "Failed to upload positive or negative examples!");

return UpdateClassifier(callback, classifierID, classifierName, positiveExamplesData, negativeExamplesData, customData);
return UpdateClassifier(callback, classifierID, classifierName, positiveExamplesData, negativeExamplesData, mimeType, customData);
}

/// <summary>
Expand All @@ -1077,10 +1088,11 @@ private void OnTrainClassifierResp(RESTConnector.Request req, RESTConnector.Resp
/// <param name="callback">Callback.</param>
/// <param name="classifierID">Classifier identifier.</param>
/// <param name="classifierName">Classifier name.</param>
/// <param name="positiveExamplesData">Dictionary of class name and class training zip byte data.</param>
/// <param name="negativeExamplesData">Negative examples zip byte data.</param>
/// <param name="positiveExamplesData">Dictionary of class name and class training zip or image byte data.</param>
/// <param name="negativeExamplesData">Negative examples zip or image byte data.</param>
/// <param name="mimeType">Mimetype of the file. Use GetMimeType to get Mimetype from filename.</param>
/// <returns></returns>
public bool UpdateClassifier(OnTrainClassifier callback, string classifierID, string classifierName, Dictionary<string, byte[]> positiveExamplesData, byte[] negativeExamplesData = null, string customData = default(string))
public bool UpdateClassifier(OnTrainClassifier callback, string classifierID, string classifierName, Dictionary<string, byte[]> positiveExamplesData, byte[] negativeExamplesData = null, string mimeType = "application/zip", string customData = default(string))
{
if (string.IsNullOrEmpty(mp_ApiKey))
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
Expand All @@ -1105,10 +1117,11 @@ private void OnTrainClassifierResp(RESTConnector.Request req, RESTConnector.Resp
req.Parameters["version"] = VisualRecognitionVersion.Version;
req.Forms = new Dictionary<string, RESTConnector.Form>();
req.Forms["name"] = new RESTConnector.Form(classifierName);

foreach (KeyValuePair<string, byte[]> kv in positiveExamplesData)
req.Forms[kv.Key + "_positive_examples"] = new RESTConnector.Form(kv.Value, kv.Key + "_positive_examples.zip", "application/zip");
req.Forms[kv.Key + "_positive_examples"] = new RESTConnector.Form(kv.Value, kv.Key + "_positive_examples" + GetExtension(mimeType), mimeType);
if (negativeExamplesData != null)
req.Forms["negative_examples"] = new RESTConnector.Form(negativeExamplesData, "negative_examples.zip", "application/zip");
req.Forms["negative_examples"] = new RESTConnector.Form(negativeExamplesData, "negative_examples" + GetExtension(mimeType), mimeType);

return connector.Send(req);
}
Expand Down Expand Up @@ -2230,11 +2243,35 @@ private string GetMetadataJson(Dictionary<string, string> metadata)

return json;
}
#endregion

#region IWatsonService implementation
/// <exclude />
public string GetServiceID()
private string GetExtension(string mimeType)
{
string extension = "";
switch (mimeType)
{
case "image/jpeg":
extension = ".jpg";
break;
case "image/png":
extension = ".png";
break;
case "image/gif":
extension = ".gif";
break;
case "application/zip":
extension = ".zip";
break;
default:
throw new WatsonException("Cannot classify unsupported mime type " + mimeType);
}

return extension;
}
#endregion

#region IWatsonService implementation
/// <exclude />
public string GetServiceID()
{
return SERVICE_ID;
}
Expand Down Expand Up @@ -2303,6 +2340,10 @@ private void OnCheckServices(GetClassifiersTopLevelBrief classifiers, string cus
else
{
Log.Debug("VisualRecognition", "Classifiers in null!");
if (m_Callback != null && m_Callback.Target != null)
{
m_Callback(SERVICE_ID, false);
}
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Utilities/Config.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public bool HasAPIKey()
/// Returns true if the configuration is loaded or not.
/// </summary>
[fsIgnore]
public bool ConfigLoaded { get; private set; }
public bool ConfigLoaded { get; set; }
/// <summary>
/// Returns the singleton instance.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions Scripts/Widgets/WebCamDisplayWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class WebCamDisplayWidget : Widget
public RawImage RawImage
{
get { return m_RawImage; }
set { m_RawImage = value; }
}
/// <summary>
/// The Material displaying the WebCam stream on Geometry.
Expand Down
3 changes: 3 additions & 0 deletions Scripts/Widgets/WebCamWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ public WebCamTexture WebCamTexture
public int RequestedWidth
{
get { return m_RequestedWidth; }
set { m_RequestedWidth = value; }
}
/// <summary>
/// The requested height of the WebCamTexture.
/// </summary>
public int RequestedHeight
{
get { return m_RequestedHeight; }
set { m_RequestedHeight = value; }
}

/// <summary>
Expand All @@ -154,6 +156,7 @@ public int RequestedHeight
public int RequestedFPS
{
get { return m_RequestedFPS; }
set { m_RequestedFPS = value; }
}
#endregion

Expand Down