@@ -33,6 +33,7 @@ public class VisualRecognitionServiceExample
3333 private string _faceUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/220px-President_Barack_Obama.jpg" ;
3434 private string _localGiraffeFilePath = @"VisualRecognitionTestData\giraffe_to_classify.jpg" ;
3535 private string _localFaceFilePath = @"VisualRecognitionTestData\obama.jpg" ;
36+ private string _localTurtleFilePath = @"VisualRecognitionTestData\turtle_to_classify.jpg" ;
3637 private string _localGiraffePositiveExamplesFilePath = @"VisualRecognitionTestData\giraffe_positive_examples.zip" ;
3738 private string _giraffeClassname = "giraffe" ;
3839 private string _localTurtlePositiveExamplesFilePath = @"VisualRecognitionTestData\turtle_positive_examples.zip" ;
@@ -59,6 +60,10 @@ public VisualRecognitionServiceExample(string url, string apikey)
5960 IsClassifierReady ( _createdClassifierId ) ;
6061 autoEvent . WaitOne ( ) ;
6162 UpdateClassifier ( ) ;
63+ IsClassifierReady ( _createdClassifierId ) ;
64+ autoEvent . WaitOne ( ) ;
65+ ClassifyWithClassifier ( ) ;
66+ GetClassifiersVerbose ( ) ;
6267 DeleteClassifier ( ) ;
6368
6469 Console . WriteLine ( "\n \n Operation complete" ) ;
@@ -96,6 +101,18 @@ private void ClassifyPost()
96101 }
97102 }
98103
104+ private void ClassifyWithClassifier ( )
105+ {
106+ string [ ] classifierIDs = { _createdClassifierId } ;
107+ using ( FileStream fs = File . OpenRead ( _localTurtleFilePath ) )
108+ {
109+ Console . WriteLine ( string . Format ( "\n Calling Classify(\" {0}\" )..." , _localTurtleFilePath ) ) ;
110+ var result = _visualRecognition . Classify ( ( fs as Stream ) . ReadAllBytes ( ) , Path . GetFileName ( _localTurtleFilePath ) , "image/jpeg" , classifierIDs : classifierIDs ) ;
111+
112+ Console . WriteLine ( JsonConvert . SerializeObject ( result , Formatting . Indented ) ) ;
113+ }
114+ }
115+
99116 private void DetectFacesGet ( )
100117 {
101118 Console . WriteLine ( string . Format ( "\n Calling DetectFaces(\" {0}\" )..." , _faceUrl ) ) ;
@@ -244,6 +261,10 @@ private void DeleteClassifier()
244261 if ( string . IsNullOrEmpty ( _createdClassifierId ) )
245262 throw new ArgumentNullException ( nameof ( _createdClassifierId ) ) ;
246263
264+ #region Delay
265+ Delay ( _delayTime ) ;
266+ #endregion
267+
247268 Console . WriteLine ( string . Format ( "\n Calling DeleteClassifier(\" {0}\" )..." , _createdClassifierId ) ) ;
248269
249270 var result = _visualRecognition . DeleteClassifier ( _createdClassifierId ) ;
@@ -307,7 +328,29 @@ private void UpdateClassifier()
307328 }
308329 }
309330 }
310-
331+
332+ private void DeleteAllClassifiers ( )
333+ {
334+ Console . WriteLine ( "Getting classifiers" ) ;
335+ var classifiers = _visualRecognition . GetClassifiersBrief ( ) ;
336+
337+ List < string > classifierIds = new List < string > ( ) ;
338+
339+ foreach ( GetClassifiersPerClassifierBrief classifier in classifiers . Classifiers )
340+ classifierIds . Add ( classifier . ClassifierId ) ;
341+
342+ Console . WriteLine ( string . Format ( "Deleting {0} classifiers" , classifierIds . Count ) ) ;
343+
344+ foreach ( string classifierId in classifierIds )
345+ {
346+ Console . WriteLine ( string . Format ( "Deleting classifier {0}" , classifierId ) ) ;
347+ _visualRecognition . DeleteClassifier ( classifierId ) ;
348+ Console . WriteLine ( string . Format ( "\t Classifier {0} deleted" , classifierId ) ) ;
349+ }
350+
351+ Console . WriteLine ( "Operation complete" ) ;
352+ }
353+
311354 private bool IsClassifierReady ( string classifierId )
312355 {
313356 var result = _visualRecognition . GetClassifier ( classifierId ) ;
@@ -326,5 +369,16 @@ private bool IsClassifierReady(string classifierId)
326369
327370 return result . Status . ToLower ( ) == "ready" ;
328371 }
372+
373+ #region Delay
374+ // Introducing a delay because of a known issue with Visual Recognition where newly created classifiers
375+ // will disappear without being deleted if a delete is attempted less than ~10 seconds after creation.
376+ private int _delayTime = 15000 ;
377+ private void Delay ( int delayTime )
378+ {
379+ Console . WriteLine ( string . Format ( "Delaying for {0} ms" , delayTime ) ) ;
380+ Thread . Sleep ( delayTime ) ;
381+ }
382+ #endregion
329383 }
330384}
0 commit comments