@@ -181,6 +181,13 @@ public class VisualRecognition : IWatsonService
181
181
private const float REQUEST_TIMEOUT = 10.0f * 60.0f ;
182
182
#endregion
183
183
184
+ #region Public Functions
185
+ public static void ClearApiKey ( )
186
+ {
187
+ mp_ApiKey = default ( string ) ;
188
+ }
189
+ #endregion
190
+
184
191
#region Classify Image
185
192
/// <summary>
186
193
/// Classifies image specified by URL.
@@ -896,7 +903,8 @@ private void OnGetClassifierResp(RESTConnector.Request req, RESTConnector.Respon
896
903
/// <param name="classifierName">Classifier name.</param>
897
904
/// <param name="positiveExamples">Dictionary of class name and positive example paths.</param>
898
905
/// <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 ) )
900
908
{
901
909
if ( string . IsNullOrEmpty ( mp_ApiKey ) )
902
910
mp_ApiKey = Config . Instance . GetAPIKey ( SERVICE_ID ) ;
@@ -932,18 +940,19 @@ private void OnGetClassifierResp(RESTConnector.Request req, RESTConnector.Respon
932
940
if ( positiveExamplesData . Count == 0 || negativeExamplesData == null )
933
941
Log . Error ( "VisualRecognition" , "Failed to upload positive or negative examples!" ) ;
934
942
935
- return TrainClassifier ( callback , classifierName , positiveExamplesData , negativeExamplesData , customData ) ;
943
+ return TrainClassifier ( callback , classifierName , positiveExamplesData , negativeExamplesData , mimeType , customData ) ;
936
944
}
937
945
938
946
/// <summary>
939
947
/// Trains a classifier
940
948
/// </summary>
941
949
/// <param name="callback">Callback.</param>
942
950
/// <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>
945
954
/// <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 ) )
947
956
{
948
957
if ( string . IsNullOrEmpty ( mp_ApiKey ) )
949
958
mp_ApiKey = Config . Instance . GetAPIKey ( SERVICE_ID ) ;
@@ -968,10 +977,11 @@ private void OnGetClassifierResp(RESTConnector.Request req, RESTConnector.Respon
968
977
req . Parameters [ "version" ] = VisualRecognitionVersion . Version ;
969
978
req . Forms = new Dictionary < string , RESTConnector . Form > ( ) ;
970
979
req . Forms [ "name" ] = new RESTConnector . Form ( classifierName ) ;
980
+
971
981
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 ) ;
973
983
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 ) ;
975
985
976
986
return connector . Send ( req ) ;
977
987
}
@@ -1030,7 +1040,8 @@ private void OnTrainClassifierResp(RESTConnector.Request req, RESTConnector.Resp
1030
1040
/// <param name="classifierName">Classifier name.</param>
1031
1041
/// <param name="positiveExamples">Dictionary of class name and positive example paths.</param>
1032
1042
/// <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 ) )
1034
1045
{
1035
1046
if ( string . IsNullOrEmpty ( mp_ApiKey ) )
1036
1047
mp_ApiKey = Config . Instance . GetAPIKey ( SERVICE_ID ) ;
@@ -1068,7 +1079,7 @@ private void OnTrainClassifierResp(RESTConnector.Request req, RESTConnector.Resp
1068
1079
if ( positiveExamplesData . Count == 0 && negativeExamplesData == null )
1069
1080
Log . Error ( "VisualRecognition" , "Failed to upload positive or negative examples!" ) ;
1070
1081
1071
- return UpdateClassifier ( callback , classifierID , classifierName , positiveExamplesData , negativeExamplesData , customData ) ;
1082
+ return UpdateClassifier ( callback , classifierID , classifierName , positiveExamplesData , negativeExamplesData , mimeType , customData ) ;
1072
1083
}
1073
1084
1074
1085
/// <summary>
@@ -1077,10 +1088,11 @@ private void OnTrainClassifierResp(RESTConnector.Request req, RESTConnector.Resp
1077
1088
/// <param name="callback">Callback.</param>
1078
1089
/// <param name="classifierID">Classifier identifier.</param>
1079
1090
/// <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>
1082
1094
/// <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 ) )
1084
1096
{
1085
1097
if ( string . IsNullOrEmpty ( mp_ApiKey ) )
1086
1098
mp_ApiKey = Config . Instance . GetAPIKey ( SERVICE_ID ) ;
@@ -1105,10 +1117,11 @@ private void OnTrainClassifierResp(RESTConnector.Request req, RESTConnector.Resp
1105
1117
req . Parameters [ "version" ] = VisualRecognitionVersion . Version ;
1106
1118
req . Forms = new Dictionary < string , RESTConnector . Form > ( ) ;
1107
1119
req . Forms [ "name" ] = new RESTConnector . Form ( classifierName ) ;
1120
+
1108
1121
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 ) ;
1110
1123
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 ) ;
1112
1125
1113
1126
return connector . Send ( req ) ;
1114
1127
}
@@ -2230,11 +2243,35 @@ private string GetMetadataJson(Dictionary<string, string> metadata)
2230
2243
2231
2244
return json ;
2232
2245
}
2233
- #endregion
2234
2246
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 ( )
2238
2275
{
2239
2276
return SERVICE_ID ;
2240
2277
}
@@ -2303,6 +2340,10 @@ private void OnCheckServices(GetClassifiersTopLevelBrief classifiers, string cus
2303
2340
else
2304
2341
{
2305
2342
Log . Debug ( "VisualRecognition" , "Classifiers in null!" ) ;
2343
+ if ( m_Callback != null && m_Callback . Target != null )
2344
+ {
2345
+ m_Callback ( SERVICE_ID , false ) ;
2346
+ }
2306
2347
}
2307
2348
}
2308
2349
else
0 commit comments