Skip to content

Commit e8eda93

Browse files
committed
add helper method to download actual mlmodel, add integration tests
1 parent 32d37ca commit e8eda93

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

IBM.WatsonDeveloperCloud.sln

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IBM.WatsonDeveloperCloud.As
105105
EndProject
106106
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IBM.WatsonDeveloperCloud.Assistant.v1.IntegrationTests", "test\IBM.WatsonDeveloperCloud.Assistant.v1.IntegrationTests\IBM.WatsonDeveloperCloud.Assistant.v1.IntegrationTests.csproj", "{8616701B-F226-4462-9949-DE1F2C88B194}"
107107
EndProject
108-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IBM.WatsonDeveloperCloud.VisualRecogntion.v3.Example", "GetCoreMLModelExample\IBM.WatsonDeveloperCloud.VisualRecogntion.v3.Example.csproj", "{C57D4E65-87CF-4D93-BB3E-7473056C4D49}"
109-
EndProject
110108
Global
111109
GlobalSection(SolutionConfigurationPlatforms) = preSolution
112110
Debug|Any CPU = Debug|Any CPU
@@ -257,10 +255,6 @@ Global
257255
{8616701B-F226-4462-9949-DE1F2C88B194}.Debug|Any CPU.Build.0 = Debug|Any CPU
258256
{8616701B-F226-4462-9949-DE1F2C88B194}.Release|Any CPU.ActiveCfg = Release|Any CPU
259257
{8616701B-F226-4462-9949-DE1F2C88B194}.Release|Any CPU.Build.0 = Release|Any CPU
260-
{C57D4E65-87CF-4D93-BB3E-7473056C4D49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
261-
{C57D4E65-87CF-4D93-BB3E-7473056C4D49}.Debug|Any CPU.Build.0 = Debug|Any CPU
262-
{C57D4E65-87CF-4D93-BB3E-7473056C4D49}.Release|Any CPU.ActiveCfg = Release|Any CPU
263-
{C57D4E65-87CF-4D93-BB3E-7473056C4D49}.Release|Any CPU.Build.0 = Release|Any CPU
264258
EndGlobalSection
265259
GlobalSection(SolutionProperties) = preSolution
266260
HideSolutionNode = FALSE
@@ -315,7 +309,6 @@ Global
315309
{E3D238D7-7A09-4447-B836-8D6ABF4BFF80} = {28E61676-E9FF-4DA9-99CC-5E0D16F02380}
316310
{60793E3F-EF8D-4FF8-9C69-33D1ADBC3C39} = {E3D238D7-7A09-4447-B836-8D6ABF4BFF80}
317311
{8616701B-F226-4462-9949-DE1F2C88B194} = {E3D238D7-7A09-4447-B836-8D6ABF4BFF80}
318-
{C57D4E65-87CF-4D93-BB3E-7473056C4D49} = {A1BC3262-1837-40D9-A530-DCFE679927C2}
319312
EndGlobalSection
320313
GlobalSection(ExtensibilityGlobals) = postSolution
321314
SolutionGuid = {B9D9D17B-1C17-402F-B701-DC671528690A}

src/IBM.WatsonDeveloperCloud.VisualRecognition.v3/VisualRecognitionServiceExtension.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
using IBM.WatsonDeveloperCloud.Http;
1919
using IBM.WatsonDeveloperCloud.Http.Extensions;
2020
using IBM.WatsonDeveloperCloud.Service;
21+
using IBM.WatsonDeveloperCloud.Util;
2122
using IBM.WatsonDeveloperCloud.VisualRecognition.v3.Model;
2223
using System;
2324
using System.Collections.Generic;
2425
using System.IO;
2526
using System.Net.Http;
2627
using System.Net.Http.Headers;
2728
using System.Text;
29+
using System.Threading.Tasks;
2830

2931
namespace IBM.WatsonDeveloperCloud.VisualRecognition.v3
3032
{
@@ -155,7 +157,12 @@ public Classifier UpdateClassifier(UpdateClassifier updateClassifier)
155157
return result;
156158
}
157159

158-
public System.Threading.Tasks.Task<Stream> GetCoreMlModel(string classifierId)
160+
/// <summary>
161+
/// Gets the stream of a Core ML model file (.mlmodel) of a custom classifier that returns "core_ml_enabled": true in the classifier details.
162+
/// </summary>
163+
/// <param name="classifierId"></param>
164+
/// <returns>The Core ML model of the requested classifier.</returns>
165+
public Task<Stream> GetCoreMlModel(string classifierId)
159166
{
160167
if (string.IsNullOrEmpty(classifierId))
161168
throw new ArgumentNullException(nameof(classifierId));
@@ -178,6 +185,22 @@ public System.Threading.Tasks.Task<Stream> GetCoreMlModel(string classifierId)
178185

179186
return result;
180187
}
188+
189+
/// <summary>
190+
/// Downloads a Core ML model file (.mlmodel) of a custom classifier that returns "core_ml_enabled": true in the classifier details.
191+
/// The name of the retreived Core ML model will be [classifierId].mlmodel.
192+
/// </summary>
193+
/// <param name="classifierId">The requested Core ML model's classifier ID</param>
194+
/// <param name="filePath">The file path (without the filename) to download the Core ML model.</param>
195+
public void DownloadCoreMlModel(string classifierId, string filePath)
196+
{
197+
var data = GetCoreMlModel(classifierId);
198+
199+
using (Stream file = File.Create(string.Format("{0}{1}.mlmodel", filePath, classifierId)))
200+
{
201+
Utility.CopyStream(data.Result, file);
202+
}
203+
}
181204
}
182205

183206
/// <summary>

src/IBM.WatsonDeveloperCloud/Util/Utility.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net;
1+
using System.IO;
2+
using System.Net;
23
using System.Net.Http;
34
using System.Threading.Tasks;
45
/**
@@ -50,5 +51,20 @@ public static async Task<string> SimpleGet(string url, string username = null, s
5051

5152
return msg;
5253
}
54+
55+
/// <summary>
56+
/// Copies an input stream to an output stream.
57+
/// </summary>
58+
/// <param name="input">The input stream to copy.</param>
59+
/// <param name="output">The output stream to copy to.</param>
60+
public static void CopyStream(Stream input, Stream output)
61+
{
62+
byte[] buffer = new byte[8 * 1024];
63+
int len;
64+
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
65+
{
66+
output.Write(buffer, 0, len);
67+
}
68+
}
5369
}
5470
}

test/IBM.WatsonDeveloperCloud.VisualRecognition.v3.IntegrationTests/VisualRecognitionServiceIntegrationTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void DetectFacesURL_Success()
137137
#endregion
138138

139139
[TestMethod]
140-
public void ListClassifiersSuccess()
140+
public void ListClassifiers_Success()
141141
{
142142
Classifiers listClassifiersResult = null;
143143

@@ -201,7 +201,7 @@ public void TestClassifiers_Success()
201201
}
202202
autoEvent.WaitOne();
203203

204-
Stream getCoreMlModelResult = null;
204+
Task<Stream> getCoreMlModelResult = null;
205205
try
206206
{
207207
getCoreMlModelResult = GetCoreMlModel(createdClassifierId);
@@ -321,9 +321,9 @@ private Classifiers ListClassifiers(bool? verbose = null)
321321
#endregion
322322

323323
#region Get Core ML Model
324-
private Stream GetCoreMlModel(string createdClassifierId)
324+
private Task<Stream> GetCoreMlModel(string createdClassifierId)
325325
{
326-
Stream getCoreMlModelResult = null;
326+
Task<Stream> getCoreMlModelResult = null;
327327

328328
try
329329
{

0 commit comments

Comments
 (0)