|
| 1 | +/** |
| 2 | +* Copyright 2018 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.WatsonDeveloperCloud.VisualRecognition.v3; |
| 19 | +using IBM.WatsonDeveloperCloud.VisualRecognition.v3.Model; |
| 20 | +using Newtonsoft.Json; |
| 21 | +using System; |
| 22 | +using System.Collections.Generic; |
| 23 | +using System.IO; |
| 24 | +using System.Text; |
| 25 | +using System.Threading.Tasks; |
| 26 | + |
| 27 | +namespace IBM.WatsonDeveloperCloud.VisRec.v3.Example |
| 28 | +{ |
| 29 | + class VisualRecognitionServiceExample |
| 30 | + { |
| 31 | + private string _apikey; |
| 32 | + private string _versionDate; |
| 33 | + private VisualRecognitionService _service; |
| 34 | + private string _localGiraffeFilePath = @"VisualRecognitionTestData/giraffe_to_classify.jpg"; |
| 35 | + private string _imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Kittyply_edit1.jpg/1200px-Kittyply_edit1.jpg"; |
| 36 | + |
| 37 | + #region Constructor |
| 38 | + public VisualRecognitionServiceExample(string apikey, string versionDate) |
| 39 | + { |
| 40 | + |
| 41 | + _service = new VisualRecognitionService(apikey, versionDate); |
| 42 | + _service.Client.BaseClient.Timeout = TimeSpan.FromMinutes(120); |
| 43 | + |
| 44 | + ClassifyImage(); |
| 45 | + ClassifyUrl(); |
| 46 | + //ListAllClassifiers(); |
| 47 | + |
| 48 | + Console.WriteLine("\n~ VisualRecognition examples complete."); |
| 49 | + } |
| 50 | + #endregion |
| 51 | + |
| 52 | + private void ClassifyImage() |
| 53 | + { |
| 54 | + using (FileStream fs = File.OpenRead(_localGiraffeFilePath)) |
| 55 | + { |
| 56 | + var result = Classify(fs, imagesFileContentType: "image/jpeg"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private void ClassifyUrl() |
| 61 | + { |
| 62 | + var result = Classify(url: _imageUrl); |
| 63 | + } |
| 64 | + |
| 65 | + private void ListAllClassifiers() |
| 66 | + { |
| 67 | + var listClassifiersResult = ListClassifiers(); |
| 68 | + } |
| 69 | + |
| 70 | + #region Generated |
| 71 | + #region Classify |
| 72 | + private ClassifiedImages Classify(System.IO.FileStream imagesFile = null, string acceptLanguage = null, string url = null, float? threshold = null, List<string> owners = null, List<string> classifierIds = null, string imagesFileContentType = null, Dictionary<string, object> customData = null) |
| 73 | + { |
| 74 | + Console.WriteLine("\nAttempting to Classify()"); |
| 75 | + var result = _service.Classify(imagesFile: imagesFile, acceptLanguage: acceptLanguage, url: url, threshold: threshold, owners: owners, classifierIds: classifierIds, imagesFileContentType: imagesFileContentType, customData: customData); |
| 76 | + |
| 77 | + if (result != null) |
| 78 | + { |
| 79 | + Console.WriteLine("Classify() succeeded:\n{0}", JsonConvert.SerializeObject(result, Formatting.Indented)); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + Console.WriteLine("Failed to Classify()"); |
| 84 | + } |
| 85 | + |
| 86 | + return result; |
| 87 | + } |
| 88 | + #endregion |
| 89 | + |
| 90 | + #region DetectFaces |
| 91 | + private DetectedFaces DetectFaces(System.IO.FileStream imagesFile = null, string url = null, string imagesFileContentType = null, Dictionary<string, object> customData = null) |
| 92 | + { |
| 93 | + Console.WriteLine("\nAttempting to DetectFaces()"); |
| 94 | + var result = _service.DetectFaces(imagesFile: imagesFile, url: url, imagesFileContentType: imagesFileContentType, customData: customData); |
| 95 | + |
| 96 | + if (result != null) |
| 97 | + { |
| 98 | + Console.WriteLine("DetectFaces() succeeded:\n{0}", JsonConvert.SerializeObject(result, Formatting.Indented)); |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + Console.WriteLine("Failed to DetectFaces()"); |
| 103 | + } |
| 104 | + |
| 105 | + return result; |
| 106 | + } |
| 107 | + #endregion |
| 108 | + |
| 109 | + #region CreateClassifier |
| 110 | + private Classifier CreateClassifier(CreateClassifier createClassifier, Dictionary<string, object> customData = null) |
| 111 | + { |
| 112 | + Console.WriteLine("\nAttempting to CreateClassifier()"); |
| 113 | + var result = _service.CreateClassifier(createClassifier: createClassifier, customData: customData); |
| 114 | + |
| 115 | + if (result != null) |
| 116 | + { |
| 117 | + Console.WriteLine("CreateClassifier() succeeded:\n{0}", JsonConvert.SerializeObject(result, Formatting.Indented)); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + Console.WriteLine("Failed to CreateClassifier()"); |
| 122 | + } |
| 123 | + |
| 124 | + return result; |
| 125 | + } |
| 126 | + #endregion |
| 127 | + |
| 128 | + #region DeleteClassifier |
| 129 | + private BaseModel DeleteClassifier(string classifierId, Dictionary<string, object> customData = null) |
| 130 | + { |
| 131 | + Console.WriteLine("\nAttempting to DeleteClassifier()"); |
| 132 | + var result = _service.DeleteClassifier(classifierId: classifierId, customData: customData); |
| 133 | + |
| 134 | + if (result != null) |
| 135 | + { |
| 136 | + Console.WriteLine("DeleteClassifier() succeeded:\n{0}", JsonConvert.SerializeObject(result, Formatting.Indented)); |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + Console.WriteLine("Failed to DeleteClassifier()"); |
| 141 | + } |
| 142 | + |
| 143 | + return result; |
| 144 | + } |
| 145 | + #endregion |
| 146 | + |
| 147 | + #region GetClassifier |
| 148 | + private Classifier GetClassifier(string classifierId, Dictionary<string, object> customData = null) |
| 149 | + { |
| 150 | + Console.WriteLine("\nAttempting to GetClassifier()"); |
| 151 | + var result = _service.GetClassifier(classifierId: classifierId, customData: customData); |
| 152 | + |
| 153 | + if (result != null) |
| 154 | + { |
| 155 | + Console.WriteLine("GetClassifier() succeeded:\n{0}", JsonConvert.SerializeObject(result, Formatting.Indented)); |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + Console.WriteLine("Failed to GetClassifier()"); |
| 160 | + } |
| 161 | + |
| 162 | + return result; |
| 163 | + } |
| 164 | + #endregion |
| 165 | + |
| 166 | + #region ListClassifiers |
| 167 | + private Classifiers ListClassifiers(bool? verbose = null, Dictionary<string, object> customData = null) |
| 168 | + { |
| 169 | + Console.WriteLine("\nAttempting to ListClassifiers()"); |
| 170 | + var result = _service.ListClassifiers(verbose: verbose, customData: customData); |
| 171 | + |
| 172 | + if (result != null) |
| 173 | + { |
| 174 | + Console.WriteLine("ListClassifiers() succeeded:\n{0}", JsonConvert.SerializeObject(result, Formatting.Indented)); |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + Console.WriteLine("Failed to ListClassifiers()"); |
| 179 | + } |
| 180 | + |
| 181 | + return result; |
| 182 | + } |
| 183 | + #endregion |
| 184 | + |
| 185 | + #region UpdateClassifier |
| 186 | + private Classifier UpdateClassifier(UpdateClassifier updateClassifier, Dictionary<string, object> customData = null) |
| 187 | + { |
| 188 | + Console.WriteLine("\nAttempting to UpdateClassifier()"); |
| 189 | + var result = _service.UpdateClassifier(updateClassifier: updateClassifier, customData: customData); |
| 190 | + |
| 191 | + if (result != null) |
| 192 | + { |
| 193 | + Console.WriteLine("UpdateClassifier() succeeded:\n{0}", JsonConvert.SerializeObject(result, Formatting.Indented)); |
| 194 | + } |
| 195 | + else |
| 196 | + { |
| 197 | + Console.WriteLine("Failed to UpdateClassifier()"); |
| 198 | + } |
| 199 | + |
| 200 | + return result; |
| 201 | + } |
| 202 | + #endregion |
| 203 | + |
| 204 | + #region GetCoreMlModel |
| 205 | + private Task<Stream> GetCoreMlModel(string classifierId, Dictionary<string, object> customData = null) |
| 206 | + { |
| 207 | + Console.WriteLine("\nAttempting to GetCoreMlModel()"); |
| 208 | + var result = _service.GetCoreMlModel(classifierId: classifierId, customData: customData); |
| 209 | + |
| 210 | + if (result != null) |
| 211 | + { |
| 212 | + Console.WriteLine("GetCoreMlModel() succeeded:\n{0}", JsonConvert.SerializeObject(result, Formatting.Indented)); |
| 213 | + } |
| 214 | + else |
| 215 | + { |
| 216 | + Console.WriteLine("Failed to GetCoreMlModel()"); |
| 217 | + } |
| 218 | + |
| 219 | + return result; |
| 220 | + } |
| 221 | + #endregion |
| 222 | + |
| 223 | + #region DeleteUserData |
| 224 | + private BaseModel DeleteUserData(string customerId, Dictionary<string, object> customData = null) |
| 225 | + { |
| 226 | + Console.WriteLine("\nAttempting to DeleteUserData()"); |
| 227 | + var result = _service.DeleteUserData(customerId: customerId, customData: customData); |
| 228 | + |
| 229 | + if (result != null) |
| 230 | + { |
| 231 | + Console.WriteLine("DeleteUserData() succeeded:\n{0}", JsonConvert.SerializeObject(result, Formatting.Indented)); |
| 232 | + } |
| 233 | + else |
| 234 | + { |
| 235 | + Console.WriteLine("Failed to DeleteUserData()"); |
| 236 | + } |
| 237 | + |
| 238 | + return result; |
| 239 | + } |
| 240 | + #endregion |
| 241 | + |
| 242 | + #endregion |
| 243 | + } |
| 244 | +} |
0 commit comments