Skip to content

Commit 93a201d

Browse files
authored
Merge pull request #161 from watson-developer-cloud/feature-vrTrainingMimeType
visual recognition revisions
2 parents 20c54de + ff69ebf commit 93a201d

File tree

4 files changed

+64
-19
lines changed

4 files changed

+64
-19
lines changed

Scripts/Services/VisualRecognition/VisualRecognition.cs

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ public class VisualRecognition : IWatsonService
181181
private const float REQUEST_TIMEOUT = 10.0f * 60.0f;
182182
#endregion
183183

184+
#region Public Functions
185+
public static void ClearApiKey()
186+
{
187+
mp_ApiKey = default(string);
188+
}
189+
#endregion
190+
184191
#region Classify Image
185192
/// <summary>
186193
/// Classifies image specified by URL.
@@ -896,7 +903,8 @@ private void OnGetClassifierResp(RESTConnector.Request req, RESTConnector.Respon
896903
/// <param name="classifierName">Classifier name.</param>
897904
/// <param name="positiveExamples">Dictionary of class name and positive example paths.</param>
898905
/// <param name="negativeExamplesPath">Negative example file path.</param>
899-
public bool TrainClassifier(OnTrainClassifier callback, string classifierName, Dictionary<string, string> positiveExamples, string negativeExamplesPath = default(string), string customData = default(string))
906+
/// <param name="mimeType">Mime type of the positive examples and negative examples data. Use GetMimeType to get Mimetype from filename.</param>
907+
public bool TrainClassifier(OnTrainClassifier callback, string classifierName, Dictionary<string, string> positiveExamples, string negativeExamplesPath = default(string), string mimeType = "application/zip", string customData = default(string))
900908
{
901909
if (string.IsNullOrEmpty(mp_ApiKey))
902910
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
@@ -932,18 +940,19 @@ private void OnGetClassifierResp(RESTConnector.Request req, RESTConnector.Respon
932940
if (positiveExamplesData.Count == 0 || negativeExamplesData == null)
933941
Log.Error("VisualRecognition", "Failed to upload positive or negative examples!");
934942

935-
return TrainClassifier(callback, classifierName, positiveExamplesData, negativeExamplesData, customData);
943+
return TrainClassifier(callback, classifierName, positiveExamplesData, negativeExamplesData, mimeType, customData);
936944
}
937945

938946
/// <summary>
939947
/// Trains a classifier
940948
/// </summary>
941949
/// <param name="callback">Callback.</param>
942950
/// <param name="classifierName">Classifier name.</param>
943-
/// <param name="positiveExamplesData">Dictionary of class name and class training zip byte data.</param>
944-
/// <param name="negativeExamplesData">Negative examples zip byte data.</param>
951+
/// <param name="positiveExamplesData">Dictionary of class name and class training zip or image byte data.</param>
952+
/// <param name="negativeExamplesData">Negative examples zip or image byte data.</param>
953+
/// <param name="mimeType">Mime type of the positive examples and negative examples data.</param>
945954
/// <returns></returns>
946-
public bool TrainClassifier(OnTrainClassifier callback, string classifierName, Dictionary<string, byte[]> positiveExamplesData, byte[] negativeExamplesData = null, string customData = default(string))
955+
public bool TrainClassifier(OnTrainClassifier callback, string classifierName, Dictionary<string, byte[]> positiveExamplesData, byte[] negativeExamplesData = null, string mimeType = "application/zip", string customData = default(string))
947956
{
948957
if (string.IsNullOrEmpty(mp_ApiKey))
949958
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
@@ -968,10 +977,11 @@ private void OnGetClassifierResp(RESTConnector.Request req, RESTConnector.Respon
968977
req.Parameters["version"] = VisualRecognitionVersion.Version;
969978
req.Forms = new Dictionary<string, RESTConnector.Form>();
970979
req.Forms["name"] = new RESTConnector.Form(classifierName);
980+
971981
foreach (KeyValuePair<string, byte[]> kv in positiveExamplesData)
972-
req.Forms[kv.Key + "_positive_examples"] = new RESTConnector.Form(kv.Value, kv.Key + "_positive_examples.zip", "application/zip");
982+
req.Forms[kv.Key + "_positive_examples"] = new RESTConnector.Form(kv.Value, kv.Key + "_positive_examples" + GetExtension(mimeType), mimeType);
973983
if(negativeExamplesData != null)
974-
req.Forms["negative_examples"] = new RESTConnector.Form(negativeExamplesData, "negative_examples.zip", "application/zip");
984+
req.Forms["negative_examples"] = new RESTConnector.Form(negativeExamplesData, "negative_examples" + GetExtension(mimeType), mimeType);
975985

976986
return connector.Send(req);
977987
}
@@ -1030,7 +1040,8 @@ private void OnTrainClassifierResp(RESTConnector.Request req, RESTConnector.Resp
10301040
/// <param name="classifierName">Classifier name.</param>
10311041
/// <param name="positiveExamples">Dictionary of class name and positive example paths.</param>
10321042
/// <param name="negativeExamplesPath">Negative example file path.</param>
1033-
public bool UpdateClassifier(OnTrainClassifier callback, string classifierID, string classifierName, Dictionary<string, string> positiveExamples, string negativeExamplesPath = default(string), string customData = default(string))
1043+
/// <param name="mimeType">Mimetype of the file. Use GetMimeType to get Mimetype from filename.</param>
1044+
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))
10341045
{
10351046
if (string.IsNullOrEmpty(mp_ApiKey))
10361047
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
@@ -1068,7 +1079,7 @@ private void OnTrainClassifierResp(RESTConnector.Request req, RESTConnector.Resp
10681079
if (positiveExamplesData.Count == 0 && negativeExamplesData == null)
10691080
Log.Error("VisualRecognition", "Failed to upload positive or negative examples!");
10701081

1071-
return UpdateClassifier(callback, classifierID, classifierName, positiveExamplesData, negativeExamplesData, customData);
1082+
return UpdateClassifier(callback, classifierID, classifierName, positiveExamplesData, negativeExamplesData, mimeType, customData);
10721083
}
10731084

10741085
/// <summary>
@@ -1077,10 +1088,11 @@ private void OnTrainClassifierResp(RESTConnector.Request req, RESTConnector.Resp
10771088
/// <param name="callback">Callback.</param>
10781089
/// <param name="classifierID">Classifier identifier.</param>
10791090
/// <param name="classifierName">Classifier name.</param>
1080-
/// <param name="positiveExamplesData">Dictionary of class name and class training zip byte data.</param>
1081-
/// <param name="negativeExamplesData">Negative examples zip byte data.</param>
1091+
/// <param name="positiveExamplesData">Dictionary of class name and class training zip or image byte data.</param>
1092+
/// <param name="negativeExamplesData">Negative examples zip or image byte data.</param>
1093+
/// <param name="mimeType">Mimetype of the file. Use GetMimeType to get Mimetype from filename.</param>
10821094
/// <returns></returns>
1083-
public bool UpdateClassifier(OnTrainClassifier callback, string classifierID, string classifierName, Dictionary<string, byte[]> positiveExamplesData, byte[] negativeExamplesData = null, string customData = default(string))
1095+
public bool UpdateClassifier(OnTrainClassifier callback, string classifierID, string classifierName, Dictionary<string, byte[]> positiveExamplesData, byte[] negativeExamplesData = null, string mimeType = "application/zip", string customData = default(string))
10841096
{
10851097
if (string.IsNullOrEmpty(mp_ApiKey))
10861098
mp_ApiKey = Config.Instance.GetAPIKey(SERVICE_ID);
@@ -1105,10 +1117,11 @@ private void OnTrainClassifierResp(RESTConnector.Request req, RESTConnector.Resp
11051117
req.Parameters["version"] = VisualRecognitionVersion.Version;
11061118
req.Forms = new Dictionary<string, RESTConnector.Form>();
11071119
req.Forms["name"] = new RESTConnector.Form(classifierName);
1120+
11081121
foreach (KeyValuePair<string, byte[]> kv in positiveExamplesData)
1109-
req.Forms[kv.Key + "_positive_examples"] = new RESTConnector.Form(kv.Value, kv.Key + "_positive_examples.zip", "application/zip");
1122+
req.Forms[kv.Key + "_positive_examples"] = new RESTConnector.Form(kv.Value, kv.Key + "_positive_examples" + GetExtension(mimeType), mimeType);
11101123
if (negativeExamplesData != null)
1111-
req.Forms["negative_examples"] = new RESTConnector.Form(negativeExamplesData, "negative_examples.zip", "application/zip");
1124+
req.Forms["negative_examples"] = new RESTConnector.Form(negativeExamplesData, "negative_examples" + GetExtension(mimeType), mimeType);
11121125

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

22312244
return json;
22322245
}
2233-
#endregion
22342246

2235-
#region IWatsonService implementation
2236-
/// <exclude />
2237-
public string GetServiceID()
2247+
private string GetExtension(string mimeType)
2248+
{
2249+
string extension = "";
2250+
switch (mimeType)
2251+
{
2252+
case "image/jpeg":
2253+
extension = ".jpg";
2254+
break;
2255+
case "image/png":
2256+
extension = ".png";
2257+
break;
2258+
case "image/gif":
2259+
extension = ".gif";
2260+
break;
2261+
case "application/zip":
2262+
extension = ".zip";
2263+
break;
2264+
default:
2265+
throw new WatsonException("Cannot classify unsupported mime type " + mimeType);
2266+
}
2267+
2268+
return extension;
2269+
}
2270+
#endregion
2271+
2272+
#region IWatsonService implementation
2273+
/// <exclude />
2274+
public string GetServiceID()
22382275
{
22392276
return SERVICE_ID;
22402277
}
@@ -2303,6 +2340,10 @@ private void OnCheckServices(GetClassifiersTopLevelBrief classifiers, string cus
23032340
else
23042341
{
23052342
Log.Debug("VisualRecognition", "Classifiers in null!");
2343+
if (m_Callback != null && m_Callback.Target != null)
2344+
{
2345+
m_Callback(SERVICE_ID, false);
2346+
}
23062347
}
23072348
}
23082349
else

Scripts/Utilities/Config.cs

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public bool HasAPIKey()
174174
/// Returns true if the configuration is loaded or not.
175175
/// </summary>
176176
[fsIgnore]
177-
public bool ConfigLoaded { get; private set; }
177+
public bool ConfigLoaded { get; set; }
178178
/// <summary>
179179
/// Returns the singleton instance.
180180
/// </summary>

Scripts/Widgets/WebCamDisplayWidget.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class WebCamDisplayWidget : Widget
6060
public RawImage RawImage
6161
{
6262
get { return m_RawImage; }
63+
set { m_RawImage = value; }
6364
}
6465
/// <summary>
6566
/// The Material displaying the WebCam stream on Geometry.

Scripts/Widgets/WebCamWidget.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,15 @@ public WebCamTexture WebCamTexture
139139
public int RequestedWidth
140140
{
141141
get { return m_RequestedWidth; }
142+
set { m_RequestedWidth = value; }
142143
}
143144
/// <summary>
144145
/// The requested height of the WebCamTexture.
145146
/// </summary>
146147
public int RequestedHeight
147148
{
148149
get { return m_RequestedHeight; }
150+
set { m_RequestedHeight = value; }
149151
}
150152

151153
/// <summary>
@@ -154,6 +156,7 @@ public int RequestedHeight
154156
public int RequestedFPS
155157
{
156158
get { return m_RequestedFPS; }
159+
set { m_RequestedFPS = value; }
157160
}
158161
#endregion
159162

0 commit comments

Comments
 (0)