Skip to content

Commit 3a68934

Browse files
authored
Merge pull request #164 from watson-developer-cloud/163-visual-recognition-parameters
Update Classify Parameters
2 parents ee49cde + bc5fbe3 commit 3a68934

File tree

3 files changed

+54
-221
lines changed

3 files changed

+54
-221
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Collections.Generic;
2323
using System.Threading;
2424
using System.Threading.Tasks;
25+
using Newtonsoft.Json;
2526

2627
namespace IBM.WatsonDeveloperCloud.VisualRecognition.v3.Example
2728
{
@@ -32,7 +33,6 @@ public class VisualRecognitionServiceExample
3233
private string _imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Kittyply_edit1.jpg/1200px-Kittyply_edit1.jpg";
3334
private string _faceUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/220px-President_Barack_Obama.jpg";
3435
private string _localGiraffeFilePath = @"VisualRecognitionTestData\giraffe_to_classify.jpg";
35-
private string _localImageMetadataPath = @"VisualRecognitionTestData\imageMetadata.json";
3636
private string _localFaceFilePath = @"VisualRecognitionTestData\obama.jpg";
3737
private string _localTurtleFilePath = @"VisualRecognitionTestData\turtle_to_classify.jpg";
3838
private string _localGiraffePositiveExamplesFilePath = @"VisualRecognitionTestData\giraffe_positive_examples.zip";
@@ -42,9 +42,6 @@ public class VisualRecognitionServiceExample
4242
private string _localNegativeExamplesFilePath = @"VisualRecognitionTestData\negative_examples.zip";
4343
private string _createdClassifierName = "dotnet-standard-test-classifier";
4444
private string _createdClassifierId = "";
45-
private string _collectionNameToCreate = "dotnet-standard-test-collection";
46-
private string _createdCollectionId = "";
47-
private string _addedImageId = "";
4845
AutoResetEvent autoEvent = new AutoResetEvent(false);
4946

5047
public VisualRecognitionServiceExample(string apikey)
@@ -62,6 +59,10 @@ public VisualRecognitionServiceExample(string apikey)
6259
IsClassifierReady(_createdClassifierId);
6360
autoEvent.WaitOne();
6461
UpdateClassifier();
62+
IsClassifierReady(_createdClassifierId);
63+
autoEvent.WaitOne();
64+
ClassifyWithClassifier();
65+
GetClassifiersVerbose();
6566
DeleteClassifier();
6667

6768
Console.WriteLine("\n\nOperation complete");
@@ -99,6 +100,18 @@ private void ClassifyPost()
99100
}
100101
}
101102

103+
private void ClassifyWithClassifier()
104+
{
105+
string[] classifierIDs = { _createdClassifierId };
106+
using (FileStream fs = File.OpenRead(_localTurtleFilePath))
107+
{
108+
Console.WriteLine(string.Format("\nCalling Classify(\"{0}\")...", _localTurtleFilePath));
109+
var result = _visualRecognition.Classify((fs as Stream).ReadAllBytes(), Path.GetFileName(_localTurtleFilePath), "image/jpeg", classifierIDs: classifierIDs);
110+
111+
Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
112+
}
113+
}
114+
102115
private void DetectFacesGet()
103116
{
104117
Console.WriteLine(string.Format("\nCalling DetectFaces(\"{0}\")...", _faceUrl));

src/IBM.WatsonDeveloperCloud.VisualRecognition.v3/VisualRecognitionService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ public ClassifyPost Classify(byte[] imageData = null, string imageDataName = nul
144144
{
145145
var imageContent = new ByteArrayContent(imageData);
146146
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse(imageDataMimeType);
147-
formData.Add(imageContent, imageDataName, imageDataName);
147+
formData.Add(imageContent, "images_file", imageDataName);
148148
}
149149

150150
if (!string.IsNullOrEmpty(parameters))
151151
{
152152
var parametersContent = new StringContent(parameters, Encoding.UTF8, HttpMediaType.TEXT_PLAIN);
153153
parametersContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
154-
formData.Add(parametersContent);
154+
formData.Add(parametersContent, "parameters");
155155
}
156156

157157
result = this.Client.PostAsync($"{ this.Endpoint}{PATH_CLASSIFY}")

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

Lines changed: 35 additions & 215 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,15 @@ public class VisualRecognitionServiceIntegrationTests
3434
private string _imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Kittyply_edit1.jpg/1200px-Kittyply_edit1.jpg";
3535
private string _faceUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/220px-President_Barack_Obama.jpg";
3636
private string _localGiraffeFilePath = @"VisualRecognitionTestData\giraffe_to_classify.jpg";
37-
//private string _localImageMetadataPath = @"VisualRecognitionTestData\imageMetadata.json";
3837
private string _localFaceFilePath = @"VisualRecognitionTestData\obama.jpg";
39-
//private string _localTurtleFilePath = @"VisualRecognitionTestData\turtle_to_classify.jpg";
38+
private string _localTurtleFilePath = @"VisualRecognitionTestData\turtle_to_classify.jpg";
4039
private string _localGiraffePositiveExamplesFilePath = @"VisualRecognitionTestData\giraffe_positive_examples.zip";
4140
private string _giraffeClassname = "giraffe";
4241
private string _localTurtlePositiveExamplesFilePath = @"VisualRecognitionTestData\turtle_positive_examples.zip";
4342
private string _turtleClassname = "turtle";
4443
private string _localNegativeExamplesFilePath = @"VisualRecognitionTestData\negative_examples.zip";
4544
private string _createdClassifierName = "dotnet-standard-test-classifier";
4645
private static string _createdClassifierId = "";
47-
//private string _collectionNameToCreate = "dotnet-standard-test-collection";
48-
//private static string _createdCollectionId = "";
49-
//private static string _addedImageId = "";
5046
AutoResetEvent autoEvent = new AutoResetEvent(false);
5147

5248
[TestInitialize]
@@ -192,197 +188,46 @@ public void t08_UpdateClassifier_Success()
192188
}
193189
}
194190

195-
//[TestMethod]
196-
//public void t09_GetCollections_Success()
197-
//{
198-
// VisualRecognitionService service = new VisualRecognitionService();
199-
// service.SetCredential(apikey);
200-
201-
// var result = service.GetCollections();
202-
203-
// Assert.IsNotNull(result);
204-
//}
205-
206-
//[TestMethod]
207-
//public void t10_CreateCollection_Success()
208-
//{
209-
// DeleteDotnetCollections();
210-
211-
// VisualRecognitionService service = new VisualRecognitionService();
212-
// service.SetCredential(apikey);
213-
214-
// var result = service.CreateCollection(_collectionNameToCreate);
215-
216-
// _createdCollectionId = result.CollectionId;
217-
// Console.WriteLine(string.Format("Created collection {0}", _createdCollectionId));
218-
219-
// Assert.IsNotNull(result);
220-
// Assert.IsTrue(!string.IsNullOrEmpty(_createdCollectionId));
221-
//}
222-
223-
//[TestMethod]
224-
//public void t11_GetCollection_Success()
225-
//{
226-
// if (string.IsNullOrEmpty(_createdCollectionId))
227-
// Assert.Fail("Created collection ID is null or empty.");
228-
229-
// VisualRecognitionService service = new VisualRecognitionService();
230-
// service.SetCredential(apikey);
231-
232-
// var result = service.GetCollection(_createdCollectionId);
233-
234-
// Assert.IsNotNull(result);
235-
// Assert.IsTrue(result.CollectionId == _createdCollectionId);
236-
//}
237-
238-
//[TestMethod]
239-
//public void t12_GetCollectionImages_Success()
240-
//{
241-
// if (string.IsNullOrEmpty(_createdCollectionId))
242-
// Assert.Fail("Created collection ID is null or empty.");
243-
244-
// VisualRecognitionService service = new VisualRecognitionService();
245-
// service.SetCredential(apikey);
246-
247-
// var result = service.GetCollectionImages(_createdCollectionId);
248-
249-
// Assert.IsNotNull(result);
250-
//}
251-
252-
//[TestMethod]
253-
//public void t13_AddCollectionImages_Success()
254-
//{
255-
// VisualRecognitionService service = new VisualRecognitionService();
256-
// service.SetCredential(apikey);
257-
258-
// using (FileStream imageStream = File.OpenRead(_localGiraffeFilePath), metadataStream = File.OpenRead(_localImageMetadataPath))
259-
// {
260-
// var result = service.AddImage(_createdCollectionId, imageStream.ReadAllBytes(), Path.GetFileName(_localGiraffeFilePath), metadataStream.ReadAllBytes());
261-
262-
// _addedImageId = result.Images[0].ImageId;
263-
// Console.WriteLine(string.Format("Added image {0}", _addedImageId));
264-
265-
// Assert.IsNotNull(result);
266-
// Assert.IsTrue(result.ImagesProcessed > 0);
267-
// Assert.IsTrue(!string.IsNullOrEmpty(_addedImageId));
268-
// }
269-
//}
270-
271-
//[TestMethod]
272-
//public void t14_GetCollectionImage_Success()
273-
//{
274-
// if (string.IsNullOrEmpty(_createdCollectionId))
275-
// Assert.Fail("Created collection ID is null or empty.");
276-
277-
// if (string.IsNullOrEmpty(_addedImageId))
278-
// Assert.Fail("Added image ID is null or empty.");
279-
280-
// VisualRecognitionService service = new VisualRecognitionService();
281-
// service.SetCredential(apikey);
282-
283-
// var result = service.GetImage(_createdCollectionId, _addedImageId);
284-
285-
// Assert.IsNotNull(result);
286-
// Assert.IsTrue(result.ImageId == _addedImageId);
287-
//}
288-
289-
//[TestMethod]
290-
//public void t15_GetCollectionImageMetadata_Success()
291-
//{
292-
// if (string.IsNullOrEmpty(_createdCollectionId))
293-
// Assert.Fail("Created collection ID is null or empty.");
294-
295-
// if (string.IsNullOrEmpty(_addedImageId))
296-
// Assert.Fail("Added image ID is null or empty.");
297-
298-
// VisualRecognitionService service = new VisualRecognitionService();
299-
// service.SetCredential(apikey);
300-
301-
// var result = service.GetMetadata(_createdCollectionId, _addedImageId);
302-
303-
// Assert.IsNotNull(result);
304-
//}
305-
306-
//[TestMethod]
307-
//public void t16_AddCollectionImageMetadata_Success()
308-
//{
309-
// if (string.IsNullOrEmpty(_createdCollectionId))
310-
// Assert.Fail("Created collection ID is null or empty.");
311-
312-
// if (string.IsNullOrEmpty(_addedImageId))
313-
// Assert.Fail("Added image ID is null or empty.");
314-
315-
// VisualRecognitionService service = new VisualRecognitionService();
316-
// service.SetCredential(apikey);
317-
318-
// using (FileStream metadataStream = File.OpenRead(_localImageMetadataPath))
319-
// {
320-
// var result = service.AddImageMetadata(_createdCollectionId, _addedImageId, metadataStream.ReadAllBytes());
321-
322-
// Assert.IsNotNull(result);
323-
// Assert.IsTrue(result.Count > 0);
324-
// }
325-
//}
326-
327-
//[TestMethod]
328-
//public void t17_DeleteCollectionImageMetadata_Success()
329-
//{
330-
// VisualRecognitionService service = new VisualRecognitionService();
331-
// service.SetCredential(apikey);
332-
333-
// var result = service.DeleteImageMetadata(_createdCollectionId, _addedImageId);
334-
335-
// Assert.IsNotNull(result);
336-
//}
337-
338-
//[TestMethod]
339-
//public void t18_FindSimilar_Success()
340-
//{
341-
// VisualRecognitionService service = new VisualRecognitionService();
342-
// service.SetCredential(apikey);
343-
344-
// using (FileStream imageStream = File.OpenRead(_localTurtleFilePath))
345-
// {
346-
// var result = service.FindSimilar(_createdCollectionId, imageStream.ReadAllBytes(), Path.GetFileName(_localGiraffeFilePath));
347-
348-
// Assert.IsNotNull(result);
349-
// Assert.IsTrue(result.ImagesProcessed > 0);
350-
// }
351-
//}
352-
353-
//[TestMethod]
354-
//public void t19_DeleteCollectionImage_Success()
355-
//{
356-
// if (string.IsNullOrEmpty(_createdCollectionId))
357-
// Assert.Fail("Created collection ID is null or empty.");
358-
359-
// if (string.IsNullOrEmpty(_addedImageId))
360-
// Assert.Fail("Added image ID is null or empty.");
361-
362-
// VisualRecognitionService service = new VisualRecognitionService();
363-
// service.SetCredential(apikey);
364-
365-
// var result = service.DeleteImage(_createdCollectionId, _addedImageId);
366-
367-
// Assert.IsNotNull(result);
368-
//}
191+
[TestMethod]
192+
public void t09_WaitForClassifier()
193+
{
194+
IsClassifierReady(_createdClassifierId);
195+
autoEvent.WaitOne();
369196

370-
//[TestMethod]
371-
//public void t20_DeleteCollection_Success()
372-
//{
373-
// if (string.IsNullOrEmpty(_createdCollectionId))
374-
// Assert.Fail("Created collection ID is null or empty.");
197+
Assert.IsTrue(true);
198+
}
375199

376-
// VisualRecognitionService service = new VisualRecognitionService();
377-
// service.SetCredential(apikey);
200+
[TestMethod]
201+
public void t10_ClassifyPost_Success()
202+
{
203+
VisualRecognitionService service = new VisualRecognitionService();
204+
service.SetCredential(apikey);
205+
string[] classifierIDs = { _createdClassifierId };
378206

379-
// var result = service.DeleteCollection(_createdCollectionId);
207+
using (FileStream fs = File.OpenRead(_localTurtleFilePath))
208+
{
209+
var result = service.Classify((fs as Stream).ReadAllBytes(), Path.GetFileName(_localTurtleFilePath), "image/jpeg", classifierIDs: classifierIDs);
380210

381-
// Assert.IsNotNull(result);
382-
//}
211+
Assert.IsNotNull(result);
212+
Assert.IsNotNull(result.Images);
213+
Assert.IsTrue(result.CustomClasses > 0);
214+
float giraffeScore = 0;
215+
float turtleScore = 0;
216+
List<ClassResult> classResults = result.Images[0]?._Classifiers[0]?.Classes;
217+
foreach(ClassResult classResult in classResults)
218+
{
219+
if (classResult._Class == _giraffeClassname)
220+
giraffeScore = classResult.Score;
221+
if (classResult._Class == _turtleClassname)
222+
turtleScore = classResult.Score;
223+
}
224+
Assert.IsTrue(giraffeScore < turtleScore);
225+
Assert.IsTrue(result.Images.Count > 0);
226+
}
227+
}
383228

384229
[TestMethod]
385-
public void t21_DeleteClassifier_Success()
230+
public void t11_DeleteClassifier_Success()
386231
{
387232
if (string.IsNullOrEmpty(_createdClassifierId))
388233
Assert.Fail("Created classsifier ID is null or empty.");
@@ -423,31 +268,6 @@ private bool IsClassifierReady(string classifierId)
423268
return result.Status.ToLower() == "ready";
424269
}
425270

426-
//private void DeleteDotnetCollections()
427-
//{
428-
// VisualRecognitionService service = new VisualRecognitionService();
429-
// service.SetCredential(apikey);
430-
431-
// List<string> collectionIdsToDelete = new List<string>();
432-
433-
// var collections = service.GetCollections();
434-
435-
// foreach (CreateCollection collection in collections.Collections)
436-
// {
437-
// string name = collection.Name;
438-
// string id = collection.CollectionId;
439-
440-
// if (name == _collectionNameToCreate)
441-
// collectionIdsToDelete.Add(id);
442-
// }
443-
444-
// if (collectionIdsToDelete.Count > 0)
445-
// {
446-
// foreach (string collectionIdToDelete in collectionIdsToDelete)
447-
// service.DeleteCollection(collectionIdToDelete);
448-
// }
449-
//}
450-
451271
private bool ContainsClass(GetClassifiersPerClassifierVerbose result, string classname)
452272
{
453273
bool containsClass = false;

0 commit comments

Comments
 (0)