Skip to content

Commit ee49cde

Browse files
authored
Merge pull request #157 from watson-developer-cloud/gh-156-vrDelayInTestsAndExamples
Delay before deleting classifier
2 parents d54c081 + 9a4844a commit ee49cde

File tree

2 files changed

+27
-320
lines changed

2 files changed

+27
-320
lines changed

examples/IBM.WatsonDeveloperCloud.VisualRecognition.v3.Example/VisualRecognitionServiceExample.cs

Lines changed: 12 additions & 320 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,6 @@ public VisualRecognitionServiceExample(string apikey)
6262
IsClassifierReady(_createdClassifierId);
6363
autoEvent.WaitOne();
6464
UpdateClassifier();
65-
GetCollections();
66-
67-
DeleteDotnetCollections();
68-
69-
CreateCollection();
70-
GetCollection();
71-
GetCollectionImages();
72-
AddCollectionImages();
73-
GetCollectionImage();
74-
GetCollectionImageMetadata();
75-
AddCollectionImageMetadata();
76-
DeleteCollectionImageMetadata();
77-
FindSimilar();
78-
DeleteCollectionImage();
79-
DeleteCollection();
8065
DeleteClassifier();
8166

8267
Console.WriteLine("\n\nOperation complete");
@@ -262,6 +247,10 @@ private void DeleteClassifier()
262247
if (string.IsNullOrEmpty(_createdClassifierId))
263248
throw new ArgumentNullException(nameof(_createdClassifierId));
264249

250+
#region Delay
251+
Delay(_delayTime);
252+
#endregion
253+
265254
Console.WriteLine(string.Format("\nCalling DeleteClassifier(\"{0}\")...", _createdClassifierId));
266255

267256
var result = _visualRecognition.DeleteClassifier(_createdClassifierId);
@@ -326,282 +315,6 @@ private void UpdateClassifier()
326315
}
327316
}
328317

329-
private void GetCollections()
330-
{
331-
Console.WriteLine("\nCalling GetCollections()...");
332-
333-
var result = _visualRecognition.GetCollections();
334-
335-
if (result != null)
336-
{
337-
if (result.Collections != null && result.Collections.Count > 0)
338-
{
339-
foreach (CreateCollection collection in result.Collections)
340-
Console.WriteLine(string.Format("name: {0} | collection id: {1} | status: {2}", collection.Name, collection.CollectionId, collection.Status));
341-
}
342-
else
343-
{
344-
Console.WriteLine("There are no collections.");
345-
}
346-
}
347-
else
348-
{
349-
Console.WriteLine("Result is null.");
350-
}
351-
}
352-
353-
private void CreateCollection()
354-
{
355-
Console.WriteLine(string.Format("\nCalling CreateCollection(\"{0}\")...", _collectionNameToCreate));
356-
357-
var result = _visualRecognition.CreateCollection(_collectionNameToCreate);
358-
359-
if (result != null)
360-
{
361-
Console.WriteLine(string.Format("name: {0} | collection id: {1} | status: {2}", result.Name, result.CollectionId, result.Status));
362-
363-
_createdCollectionId = result.CollectionId;
364-
}
365-
else
366-
{
367-
Console.WriteLine("Result is null.");
368-
}
369-
}
370-
371-
private void DeleteCollection()
372-
{
373-
if (string.IsNullOrEmpty(_createdCollectionId))
374-
throw new ArgumentNullException(nameof(_createdCollectionId));
375-
376-
Console.WriteLine(string.Format("\nCalling DeleteCollection(\"{0}\")...", _createdCollectionId));
377-
378-
var result = _visualRecognition.DeleteCollection(_createdCollectionId);
379-
380-
if (result != null)
381-
Console.WriteLine(string.Format("Collection {0} deleted.", _createdCollectionId));
382-
}
383-
384-
private void GetCollection()
385-
{
386-
if (string.IsNullOrEmpty(_createdCollectionId))
387-
throw new ArgumentNullException(nameof(_createdCollectionId));
388-
389-
Console.WriteLine(string.Format("\nCalling GetCollection(\"{0}\")...", _createdCollectionId));
390-
391-
var result = _visualRecognition.GetCollection(_createdCollectionId);
392-
393-
if (result != null)
394-
{
395-
Console.WriteLine(string.Format("name: {0} | collection id: {1} | status: {2}", result.Name, result.CollectionId, result.Status));
396-
}
397-
else
398-
{
399-
Console.WriteLine("Result is null.");
400-
}
401-
}
402-
403-
private void GetCollectionImages()
404-
{
405-
if (string.IsNullOrEmpty(_createdCollectionId))
406-
throw new ArgumentNullException(nameof(_createdCollectionId));
407-
408-
Console.WriteLine(string.Format("\nCalling GetCollectionImages(\"{0}\")...", _createdCollectionId));
409-
410-
var result = _visualRecognition.GetCollectionImages(_createdCollectionId);
411-
412-
if (result != null)
413-
{
414-
if (result.Images != null && result.Images.Count > 0)
415-
{
416-
foreach (GetCollectionsBrief image in result.Images)
417-
Console.WriteLine(string.Format("imageId: {0} | imageFile: {1} | created: {2} | metadata{3}", image.ImageId, image.ImageFile, image.Created, image.Metadata));
418-
}
419-
else
420-
{
421-
Console.WriteLine("There are no images.");
422-
}
423-
}
424-
else
425-
{
426-
Console.WriteLine("Result is null.");
427-
}
428-
}
429-
430-
private void AddCollectionImages()
431-
{
432-
if (string.IsNullOrEmpty(_createdCollectionId))
433-
throw new ArgumentNullException(nameof(_createdCollectionId));
434-
435-
using (FileStream imageStream = File.OpenRead(_localGiraffeFilePath), metadataStream = File.OpenRead(_localImageMetadataPath))
436-
{
437-
Console.WriteLine(string.Format("\nCalling AddImage(\"{0}\", \"{1}\")...", _createdCollectionId, _localGiraffeFilePath));
438-
439-
var result = _visualRecognition.AddImage(_createdCollectionId, imageStream.ReadAllBytes(), Path.GetFileName(_localGiraffeFilePath), metadataStream.ReadAllBytes());
440-
441-
if (result != null)
442-
{
443-
Console.WriteLine("Number of images processed: {0}", result.ImagesProcessed);
444-
foreach (CollectionImagesConfig image in result.Images)
445-
{
446-
Console.WriteLine("file: {0} | id: {1}", image.ImageFile, image.ImageId);
447-
448-
if (image.Metadata != null && image.Metadata.Count > 0)
449-
{
450-
foreach (KeyValuePair<string, string> kvp in image.Metadata)
451-
Console.WriteLine("\t{0} : {1}", kvp.Key, kvp.Value);
452-
}
453-
else
454-
{
455-
Console.WriteLine("There is no metadata for this image.");
456-
}
457-
458-
_addedImageId = image.ImageId;
459-
}
460-
}
461-
else
462-
{
463-
Console.WriteLine("Result is null.");
464-
}
465-
}
466-
}
467-
468-
private void DeleteCollectionImage()
469-
{
470-
if (string.IsNullOrEmpty(_createdCollectionId))
471-
throw new ArgumentNullException(nameof(_createdCollectionId));
472-
473-
if (string.IsNullOrEmpty(_addedImageId))
474-
throw new ArgumentNullException(nameof(_addedImageId));
475-
476-
Console.WriteLine(string.Format("\nCalling DeleteImage(\"{0}\", \"{1}\")...", _createdCollectionId, _addedImageId));
477-
478-
var result = _visualRecognition.DeleteImage(_createdCollectionId, _addedImageId);
479-
480-
if (result != null)
481-
Console.WriteLine(string.Format("Image {0} deleted from collection {1}.", _addedImageId, _createdCollectionId));
482-
}
483-
484-
private void GetCollectionImage()
485-
{
486-
if (string.IsNullOrEmpty(_createdCollectionId))
487-
throw new ArgumentNullException(nameof(_createdCollectionId));
488-
489-
if (string.IsNullOrEmpty(_addedImageId))
490-
throw new ArgumentNullException(nameof(_addedImageId));
491-
492-
Console.WriteLine(string.Format("\nCalling GetCollectionImages(\"{0}\", \"{1}\")...", _createdCollectionId, _addedImageId));
493-
494-
var result = _visualRecognition.GetImage(_createdCollectionId, _addedImageId);
495-
496-
if (result != null)
497-
{
498-
Console.WriteLine(string.Format("imageId: {0} | imageFile: {1} | created: {2} | metadata{3}", result.ImageId, result.ImageFile, result.Created, result.Metadata));
499-
}
500-
else
501-
{
502-
Console.WriteLine("Result is null.");
503-
}
504-
}
505-
506-
private void DeleteCollectionImageMetadata()
507-
{
508-
if (string.IsNullOrEmpty(_createdCollectionId))
509-
throw new ArgumentNullException(nameof(_createdCollectionId));
510-
511-
if (string.IsNullOrEmpty(_addedImageId))
512-
throw new ArgumentNullException(nameof(_addedImageId));
513-
514-
Console.WriteLine(string.Format("\nCalling DeleteImageMetadata(\"{0}\", \"{1}\")...", _createdCollectionId, _addedImageId));
515-
516-
var result = _visualRecognition.DeleteImageMetadata(_createdCollectionId, _addedImageId);
517-
}
518-
519-
private void GetCollectionImageMetadata()
520-
{
521-
if (string.IsNullOrEmpty(_createdCollectionId))
522-
throw new ArgumentNullException(nameof(_createdCollectionId));
523-
524-
if (string.IsNullOrEmpty(_addedImageId))
525-
throw new ArgumentNullException(nameof(_addedImageId));
526-
527-
Console.WriteLine(string.Format("\nCalling GetMetadata(\"{0}\", \"{1}\")...", _createdCollectionId, _addedImageId));
528-
529-
var result = _visualRecognition.GetMetadata(_createdCollectionId, _addedImageId);
530-
531-
if (result != null)
532-
{
533-
foreach (KeyValuePair<string, string> item in result)
534-
Console.WriteLine(string.Format("\tMetadata: {0}, {1}", item.Key, item.Value));
535-
}
536-
else
537-
{
538-
Console.WriteLine("Result is null.");
539-
}
540-
}
541-
542-
private void AddCollectionImageMetadata()
543-
{
544-
if (string.IsNullOrEmpty(_createdCollectionId))
545-
throw new ArgumentNullException(nameof(_createdCollectionId));
546-
547-
if (string.IsNullOrEmpty(_addedImageId))
548-
throw new ArgumentNullException(nameof(_addedImageId));
549-
550-
using (FileStream metadataStream = File.OpenRead(_localImageMetadataPath))
551-
{
552-
Console.WriteLine(string.Format("\nCalling AddMetadata(\"{0}\", \"{1}\")...", _createdCollectionId, _addedImageId));
553-
554-
var result = _visualRecognition.AddImageMetadata(_createdCollectionId, _addedImageId, metadataStream.ReadAllBytes());
555-
556-
if (result != null && result.Count > 0)
557-
{
558-
foreach (KeyValuePair<string, string> kvp in result)
559-
Console.WriteLine("\t{0} : {1}", kvp.Key, kvp.Value);
560-
}
561-
else
562-
{
563-
Console.WriteLine("Result is null.");
564-
}
565-
}
566-
567-
}
568-
569-
private void FindSimilar()
570-
{
571-
if (string.IsNullOrEmpty(_createdCollectionId))
572-
throw new ArgumentNullException(nameof(_createdCollectionId));
573-
574-
using (FileStream imageStream = File.OpenRead(_localTurtleFilePath))
575-
{
576-
Console.WriteLine(string.Format("\nCalling FindSimilar(\"{0}\", \"{1}\")...", _createdCollectionId, _localTurtleFilePath));
577-
578-
var result = _visualRecognition.FindSimilar(_createdCollectionId, imageStream.ReadAllBytes(), Path.GetFileName(_localGiraffeFilePath));
579-
580-
if (result != null)
581-
{
582-
Console.WriteLine("Number of images processed: {0}", result.ImagesProcessed);
583-
foreach (SimilarImageConfig image in result.SimilarImages)
584-
{
585-
Console.WriteLine("file: {0} | id: {1} | score: {2}", image.ImageFile, image.ImageId, image.Score);
586-
587-
if (image.Metadata != null && image.Metadata.Count > 0)
588-
{
589-
foreach (KeyValuePair<string, string> kvp in image.Metadata)
590-
Console.WriteLine("\t{0} : {1}", kvp.Key, kvp.Value);
591-
}
592-
else
593-
{
594-
Console.WriteLine("There is no metadata for this image.");
595-
}
596-
}
597-
}
598-
else
599-
{
600-
Console.WriteLine("Result is null.");
601-
}
602-
}
603-
}
604-
605318
private void DeleteAllClassifiers()
606319
{
607320
Console.WriteLine("Getting classifiers");
@@ -643,36 +356,15 @@ private bool IsClassifierReady(string classifierId)
643356
return result.Status.ToLower() == "ready";
644357
}
645358

646-
private void DeleteDotnetCollections()
359+
#region Delay
360+
// Introducing a delay because of a known issue with Visual Recognition where newly created classifiers
361+
// will disappear without being deleted if a delete is attempted less than ~10 seconds after creation.
362+
private int _delayTime = 15000;
363+
private void Delay(int delayTime)
647364
{
648-
Console.WriteLine("\nGetting all collections");
649-
List<string> collectionIdsToDelete = new List<string>();
650-
651-
var collections = _visualRecognition.GetCollections();
652-
653-
foreach (CreateCollection collection in collections.Collections)
654-
{
655-
string name = collection.Name;
656-
string id = collection.CollectionId;
657-
Console.WriteLine(string.Format("name: {0} | id: {1}", name, id));
658-
659-
if (name == _collectionNameToCreate)
660-
collectionIdsToDelete.Add(id);
661-
}
662-
663-
if (collectionIdsToDelete.Count > 0)
664-
{
665-
foreach (string collectionIdToDelete in collectionIdsToDelete)
666-
{
667-
Console.WriteLine(string.Format("Deleting collection {0}.", collectionIdToDelete));
668-
_visualRecognition.DeleteCollection(collectionIdToDelete);
669-
Console.WriteLine(string.Format("\tCollection {0} deleted.", collectionIdToDelete));
670-
}
671-
}
672-
else
673-
{
674-
Console.WriteLine("There are no matching collections to delete.");
675-
}
365+
Console.WriteLine(string.Format("Delaying for {0} ms", delayTime));
366+
Thread.Sleep(delayTime);
676367
}
368+
#endregion
677369
}
678370
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@ public void t21_DeleteClassifier_Success()
390390
VisualRecognitionService service = new VisualRecognitionService();
391391
service.SetCredential(apikey);
392392

393+
#region Delay
394+
Delay(_delayTime);
395+
#endregion
396+
393397
var result = service.DeleteClassifier(_createdClassifierId);
394398

395399
Assert.IsNotNull(result);
@@ -456,5 +460,16 @@ private bool ContainsClass(GetClassifiersPerClassifierVerbose result, string cla
456460

457461
return containsClass;
458462
}
463+
464+
#region Delay
465+
// Introducing a delay because of a known issue with Visual Recognition where newly created classifiers
466+
// will disappear without being deleted if a delete is attempted less than ~10 seconds after creation.
467+
private int _delayTime = 15000;
468+
private void Delay(int delayTime)
469+
{
470+
Console.WriteLine(string.Format("Delaying for {0} ms", delayTime));
471+
Thread.Sleep(delayTime);
472+
}
473+
#endregion
459474
}
460475
}

0 commit comments

Comments
 (0)