|
| 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