Skip to content

Commit e7da179

Browse files
authored
Merge pull request #377 from watson-developer-cloud/6678-visual-recognition-v3-examples
Add Visual Recognition V3 examples
2 parents ba3a77f + c22c299 commit e7da179

File tree

3 files changed

+297
-1
lines changed

3 files changed

+297
-1
lines changed

IBM.Watson.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IBM.Watson.SpeechToText.v1.
115115
EndProject
116116
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IBM.Watson.TextToSpeech.v1.Examples", "examples\IBM.Watson.TextToSpeech.v1.Examples\IBM.Watson.TextToSpeech.v1.Examples.csproj", "{D8E0E626-B930-42A9-88E7-13C05A08B868}"
117117
EndProject
118-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IBM.Watson.ToneAnalyzer.v3.Examples", "examples\IBM.Watson.ToneAnalyzer.v3.Examples\IBM.Watson.ToneAnalyzer.v3.Examples.csproj", "{523AE6E0-11CD-40CA-ABB1-8010E35C416D}"
118+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IBM.Watson.ToneAnalyzer.v3.Examples", "examples\IBM.Watson.ToneAnalyzer.v3.Examples\IBM.Watson.ToneAnalyzer.v3.Examples.csproj", "{523AE6E0-11CD-40CA-ABB1-8010E35C416D}"
119+
EndProject
120+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IBM.Watson.VisualRecognition.v3.Examples", "examples\IBM.Watson.VisualRecognition.v3.Examples\IBM.Watson.VisualRecognition.v3.Examples.csproj", "{EBED7B56-15D3-472E-A758-6452415AA941}"
119121
EndProject
120122
Global
121123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -299,6 +301,10 @@ Global
299301
{523AE6E0-11CD-40CA-ABB1-8010E35C416D}.Debug|Any CPU.Build.0 = Debug|Any CPU
300302
{523AE6E0-11CD-40CA-ABB1-8010E35C416D}.Release|Any CPU.ActiveCfg = Release|Any CPU
301303
{523AE6E0-11CD-40CA-ABB1-8010E35C416D}.Release|Any CPU.Build.0 = Release|Any CPU
304+
{EBED7B56-15D3-472E-A758-6452415AA941}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
305+
{EBED7B56-15D3-472E-A758-6452415AA941}.Debug|Any CPU.Build.0 = Debug|Any CPU
306+
{EBED7B56-15D3-472E-A758-6452415AA941}.Release|Any CPU.ActiveCfg = Release|Any CPU
307+
{EBED7B56-15D3-472E-A758-6452415AA941}.Release|Any CPU.Build.0 = Release|Any CPU
302308
EndGlobalSection
303309
GlobalSection(SolutionProperties) = preSolution
304310
HideSolutionNode = FALSE
@@ -347,6 +353,7 @@ Global
347353
{0F974417-CFC9-4B58-8D92-F4B5DB9005DA} = {254924C9-64CD-4902-97C4-5F9E48DCDF7F}
348354
{D8E0E626-B930-42A9-88E7-13C05A08B868} = {A86D4D5B-AECE-49F2-A2BC-F6897B3EB2E8}
349355
{523AE6E0-11CD-40CA-ABB1-8010E35C416D} = {C7873F44-7188-49BE-84C5-532BE4E07147}
356+
{EBED7B56-15D3-472E-A758-6452415AA941} = {A1BC3262-1837-40D9-A530-DCFE679927C2}
350357
EndGlobalSection
351358
GlobalSection(ExtensibilityGlobals) = postSolution
352359
SolutionGuid = {B9D9D17B-1C17-402F-B701-DC671528690A}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="IBM.Cloud.SDK.Core" Version="0.7.1" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\src\IBM.Watson.VisualRecognition.v3\IBM.Watson.VisualRecognition.v3.csproj" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
/**
2+
* Copyright 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using IBM.Cloud.SDK.Core.Http;
19+
using IBM.Cloud.SDK.Core.Util;
20+
using IBM.Watson.VisualRecognition.v3.Model;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.IO;
24+
25+
namespace IBM.Watson.VisualRecognition.v3.Examples
26+
{
27+
public class ServiceExample
28+
{
29+
string apikey = "{apikey}";
30+
string url = "{url}";
31+
private string versionDate = "{versionDate}";
32+
33+
private string localGiraffeFilePath = @"VisualRecognitionTestData/giraffe_to_classify.jpg";
34+
private string localFaceFilePath = @"VisualRecognitionTestData/obama.jpg";
35+
private string localGiraffePositiveExamplesFilePath = @"VisualRecognitionTestData/giraffe_positive_examples.zip";
36+
private string giraffeClassname = "giraffe";
37+
private string localTurtlePositiveExamplesFilePath = @"VisualRecognitionTestData/turtle_positive_examples.zip";
38+
private string turtleClassname = "turtle";
39+
private string localNegativeExamplesFilePath = @"VisualRecognitionTestData/negative_examples.zip";
40+
private string createdClassifierName = "dotnet-standard-test-integration-classifier";
41+
private string classifierId;
42+
43+
static void Main(string[] args)
44+
{
45+
ServiceExample example = new ServiceExample();
46+
47+
example.Classify();
48+
example.DetectFaces();
49+
50+
example.ListClassifiers();
51+
example.CreateClassifier();
52+
example.GetClassifier();
53+
//example.UpdateClassifier(); // Commented since we cannot update a classifier until it has been trainied.
54+
//example.DeleteClassifier(); // Commented since we cannot delte a classifier until the status is `ready` or `failed`
55+
56+
example.GetCoreMlModel();
57+
58+
example.DeleteUserData();
59+
60+
Console.WriteLine("Examples complete. Press any key to close the application.");
61+
Console.ReadKey();
62+
}
63+
64+
#region General
65+
public void Classify()
66+
{
67+
TokenOptions tokenOptions = new TokenOptions()
68+
{
69+
IamApiKey = apikey,
70+
ServiceUrl = url
71+
};
72+
73+
VisualRecognitionService service = new VisualRecognitionService(tokenOptions, versionDate);
74+
75+
DetailedResponse<ClassifiedImages> result;
76+
using (FileStream fs = File.OpenRead(localGiraffeFilePath))
77+
{
78+
using (MemoryStream ms = new MemoryStream())
79+
{
80+
fs.CopyTo(ms);
81+
result = service.Classify(
82+
imagesFile: ms,
83+
imagesFilename: Path.GetFileName(localGiraffeFilePath),
84+
imagesFileContentType: "image/jpeg",
85+
threshold: 0.5f,
86+
acceptLanguage: "en-US"
87+
);
88+
}
89+
}
90+
91+
Console.WriteLine(result.Response);
92+
}
93+
#endregion
94+
95+
#region Face
96+
public void DetectFaces()
97+
{
98+
TokenOptions tokenOptions = new TokenOptions()
99+
{
100+
IamApiKey = apikey,
101+
ServiceUrl = url
102+
};
103+
104+
VisualRecognitionService service = new VisualRecognitionService(tokenOptions, versionDate);
105+
106+
DetailedResponse<DetectedFaces> result;
107+
using (FileStream fs = File.OpenRead(localFaceFilePath))
108+
{
109+
using (MemoryStream ms = new MemoryStream())
110+
{
111+
fs.CopyTo(ms);
112+
result = service.DetectFaces(
113+
imagesFile: ms,
114+
imagesFilename: Path.GetFileName(localFaceFilePath),
115+
imagesFileContentType: "image/jpeg",
116+
acceptLanguage: "en"
117+
);
118+
}
119+
120+
}
121+
Console.WriteLine(result.Response);
122+
}
123+
#endregion
124+
125+
#region Custom
126+
public void ListClassifiers()
127+
{
128+
TokenOptions tokenOptions = new TokenOptions()
129+
{
130+
IamApiKey = apikey,
131+
ServiceUrl = url
132+
};
133+
134+
VisualRecognitionService service = new VisualRecognitionService(tokenOptions, versionDate);
135+
136+
var result = service.ListClassifiers();
137+
138+
Console.WriteLine(result.Response);
139+
}
140+
141+
public void CreateClassifier()
142+
{
143+
TokenOptions tokenOptions = new TokenOptions()
144+
{
145+
IamApiKey = apikey,
146+
ServiceUrl = url
147+
};
148+
149+
VisualRecognitionService service = new VisualRecognitionService(tokenOptions, versionDate);
150+
151+
DetailedResponse<Classifier> result = null;
152+
using (FileStream positiveExamplesFileStream = File.OpenRead(localGiraffePositiveExamplesFilePath), negativeExamplesFileStream = File.OpenRead(localNegativeExamplesFilePath))
153+
{
154+
using (MemoryStream positiveExamplesMemoryStream = new MemoryStream(), negativeExamplesMemoryStream = new MemoryStream())
155+
{
156+
positiveExamplesFileStream.CopyTo(positiveExamplesMemoryStream);
157+
negativeExamplesFileStream.CopyTo(negativeExamplesMemoryStream);
158+
Dictionary<string, MemoryStream> positiveExamples = new Dictionary<string, MemoryStream>();
159+
positiveExamples.Add(giraffeClassname, positiveExamplesMemoryStream);
160+
result = service.CreateClassifier(
161+
name: createdClassifierName,
162+
positiveExamples: positiveExamples,
163+
negativeExamples: negativeExamplesMemoryStream,
164+
negativeExamplesFilename: Path.GetFileName(localNegativeExamplesFilePath)
165+
);
166+
}
167+
}
168+
169+
Console.WriteLine(result.Response);
170+
classifierId = result.Result.ClassifierId;
171+
}
172+
173+
public void GetClassifier()
174+
{
175+
TokenOptions tokenOptions = new TokenOptions()
176+
{
177+
IamApiKey = apikey,
178+
ServiceUrl = url
179+
};
180+
181+
VisualRecognitionService service = new VisualRecognitionService(tokenOptions, versionDate);
182+
183+
var result = service.GetClassifier(
184+
classifierId: classifierId
185+
);
186+
187+
Console.WriteLine(result.Response);
188+
}
189+
190+
public void UpdateClassifier()
191+
{
192+
TokenOptions tokenOptions = new TokenOptions()
193+
{
194+
IamApiKey = apikey,
195+
ServiceUrl = url
196+
};
197+
198+
VisualRecognitionService service = new VisualRecognitionService(tokenOptions, versionDate);
199+
200+
DetailedResponse<Classifier> result = null;
201+
using (FileStream positiveExamplesStream = File.OpenRead(localTurtlePositiveExamplesFilePath))
202+
{
203+
using (MemoryStream positiveExamplesMemoryStream = new MemoryStream())
204+
{
205+
Dictionary<string, MemoryStream> positiveExamples = new Dictionary<string, MemoryStream>();
206+
positiveExamples.Add(turtleClassname, positiveExamplesMemoryStream);
207+
result = service.UpdateClassifier(
208+
classifierId: classifierId,
209+
positiveExamples: positiveExamples
210+
);
211+
}
212+
}
213+
214+
Console.WriteLine(result.Response);
215+
}
216+
217+
public void DeleteClassifier()
218+
{
219+
TokenOptions tokenOptions = new TokenOptions()
220+
{
221+
IamApiKey = apikey,
222+
ServiceUrl = url
223+
};
224+
225+
VisualRecognitionService service = new VisualRecognitionService(tokenOptions, versionDate);
226+
227+
var result = service.DeleteClassifier(
228+
classifierId: classifierId
229+
);
230+
231+
Console.WriteLine(result.StatusCode);
232+
}
233+
#endregion
234+
235+
#region Core ML
236+
public void GetCoreMlModel()
237+
{
238+
TokenOptions tokenOptions = new TokenOptions()
239+
{
240+
IamApiKey = apikey,
241+
ServiceUrl = url
242+
};
243+
244+
VisualRecognitionService service = new VisualRecognitionService(tokenOptions, versionDate);
245+
246+
var result = service.GetCoreMlModel(
247+
classifierId: classifierId
248+
);
249+
250+
Console.WriteLine(result.Response);
251+
}
252+
#endregion
253+
254+
#region User Data
255+
public void DeleteUserData()
256+
{
257+
TokenOptions tokenOptions = new TokenOptions()
258+
{
259+
IamApiKey = apikey,
260+
ServiceUrl = url
261+
};
262+
263+
VisualRecognitionService service = new VisualRecognitionService(tokenOptions, versionDate);
264+
265+
var result = service.DeleteUserData(
266+
customerId: "customerId"
267+
);
268+
269+
Console.WriteLine(result.Response);
270+
}
271+
#endregion
272+
}
273+
}

0 commit comments

Comments
 (0)