Skip to content

Commit bf89c0a

Browse files
committed
feat(visual recognition v3): Added support for label language in DetectFaces
1 parent 3bf6924 commit bf89c0a

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

Examples/ServiceExamples/Scripts/ExampleVisualRecognition.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private IEnumerator Examples()
161161

162162
// Detect faces get
163163
Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to detect faces via URL");
164-
if (!_visualRecognition.DetectFaces(_imageURL, OnDetectFacesGet, OnFail))
164+
if (!_visualRecognition.DetectFaces(_imageURL, OnDetectFacesGet, OnFail, "es"))
165165
Log.Debug("ExampleVisualRecognition.DetectFaces()", "Detect faces failed!");
166166

167167
while (!_detectFacesGetTested)
@@ -170,7 +170,7 @@ private IEnumerator Examples()
170170
// Detect faces post image
171171
Log.Debug("ExampleVisualRecognition.Examples()", "Attempting to detect faces via image");
172172
string faceExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/obama.jpg";
173-
if (!_visualRecognition.DetectFaces(OnDetectFacesPost, OnFail, faceExamplePath))
173+
if (!_visualRecognition.DetectFaces(OnDetectFacesPost, OnFail, faceExamplePath, "es"))
174174
Log.Debug("ExampleVisualRecognition.DetectFaces()", "Detect faces failed!");
175175

176176
while (!_detectFacesPostTested)

Scripts/Services/VisualRecognition/v3/DataModels.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,21 @@ public class FaceAge
370370
public class FaceGender
371371
{
372372
/// <summary>
373-
/// The gener.
373+
/// Gender identified by the face. For example, `MALE` or `FEMALE`.
374374
/// </summary>
375-
public string gender { get; set; }
375+
[fsProperty("gender")]
376+
public string Gender { get; set; }
376377
/// <summary>
377-
/// The gender classification score.
378+
/// The word for \"male\" or \"female\" in the language defined by the **Accept-Language** request header.
378379
/// </summary>
379-
public double score { get; set; }
380+
[fsProperty("gender_label")]
381+
public string GenderLabel { get; set; }
382+
/// <summary>
383+
/// Confidence score in the range of 0 to 1. A higher score indicates greater confidence in the estimated value
384+
/// for the property.
385+
/// </summary>
386+
[fsProperty("score")]
387+
public float? Score { get; set; }
380388
}
381389

382390
/// <summary>

Scripts/Services/VisualRecognition/v3/VisualRecognition.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public VisualRecognition(Credentials credentials)
182182
}
183183
}
184184
req.OnResponse = OnClassifyResp;
185-
req.Headers["Accepted-Language"] = acceptLanguage;
185+
req.Headers["Accept-Language"] = acceptLanguage;
186186
if (Credentials.HasApiKey())
187187
req.Parameters["api_key"] = Credentials.ApiKey;
188188
req.Parameters["url"] = url;
@@ -282,6 +282,7 @@ public VisualRecognition(Credentials credentials)
282282
req.Parameters["version"] = VersionDate;
283283
req.Headers["Content-Type"] = "multipart/form-data";
284284
req.Headers["Accept"] = "application/json";
285+
req.Headers["Accept-Language"] = acceptLanguage;
285286

286287
if (owners != default(string[]))
287288
req.Parameters["owners"] = string.Join(",", owners);
@@ -369,8 +370,10 @@ private void OnClassifyResp(RESTConnector.Request req, RESTConnector.Response re
369370
/// <param name="url">URL.</param>
370371
/// <param name="successCallback">The success callback.</param>
371372
/// <param name="failCallback">The fail callback.</param>
373+
/// <param name="acceptLanguage">The language used for the value of `gender_label` in the response. (optional,
374+
/// default to en)</param>
372375
/// <param name="customData">Custom data.</param>
373-
public bool DetectFaces(string url, SuccessCallback<DetectedFaces> successCallback, FailCallback failCallback, Dictionary<string, object> customData = null)
376+
public bool DetectFaces(string url, SuccessCallback<DetectedFaces> successCallback, FailCallback failCallback, string acceptLanguage = null, Dictionary<string, object> customData = null)
374377
{
375378
if (successCallback == null)
376379
throw new ArgumentNullException("successCallback");
@@ -399,6 +402,8 @@ public bool DetectFaces(string url, SuccessCallback<DetectedFaces> successCallba
399402
req.OnResponse = OnDetectFacesResp;
400403
if (Credentials.HasApiKey())
401404
req.Parameters["api_key"] = Credentials.ApiKey;
405+
if (!string.IsNullOrEmpty(acceptLanguage))
406+
req.Headers["Accept-Language"] = acceptLanguage;
402407
req.Parameters["url"] = url;
403408
req.Parameters["version"] = VersionDate;
404409

@@ -412,8 +417,10 @@ public bool DetectFaces(string url, SuccessCallback<DetectedFaces> successCallba
412417
/// <param name="successCallback">The success callback.</param>
413418
/// <param name="failCallback">The fail callback.</param>
414419
/// <param name="imagePath">Image path.</param>
420+
/// <param name="acceptLanguage">The language used for the value of `gender_label` in the response. (optional,
421+
/// default to en)</param>
415422
/// <param name="customData">Custom data.</param>
416-
public bool DetectFaces(SuccessCallback<DetectedFaces> successCallback, FailCallback failCallback, string imagePath, Dictionary<string, object> customData = null)
423+
public bool DetectFaces(SuccessCallback<DetectedFaces> successCallback, FailCallback failCallback, string imagePath, string acceptLanguage, Dictionary<string, object> customData = null)
417424
{
418425
if (successCallback == null)
419426
throw new ArgumentNullException("successCallback");
@@ -440,7 +447,7 @@ public bool DetectFaces(SuccessCallback<DetectedFaces> successCallback, FailCall
440447
Log.Error("VisualRecognition.DetectFaces()", "Failed to upload {0}!", imagePath);
441448
}
442449

443-
return DetectFaces(successCallback, failCallback, imageData, customData);
450+
return DetectFaces(successCallback, failCallback, imageData, acceptLanguage, customData);
444451
}
445452

446453
/// <summary>
@@ -449,9 +456,11 @@ public bool DetectFaces(SuccessCallback<DetectedFaces> successCallback, FailCall
449456
/// <param name="successCallback">The success callback.</param>
450457
/// <param name="failCallback">The fail callback.</param>
451458
/// <param name="imageData">ByteArray of image data.</param>
459+
/// <param name="acceptLanguage">The language used for the value of `gender_label` in the response. (optional,
460+
/// default to en)</param>
452461
/// <param name="customData">Custom data.</param>
453462
/// <returns></returns>
454-
public bool DetectFaces(SuccessCallback<DetectedFaces> successCallback, FailCallback failCallback, byte[] imageData, Dictionary<string, object> customData = null)
463+
public bool DetectFaces(SuccessCallback<DetectedFaces> successCallback, FailCallback failCallback, byte[] imageData, string acceptLanguage = null, Dictionary<string, object> customData = null)
455464
{
456465
if (successCallback == null)
457466
throw new ArgumentNullException("successCallback");
@@ -479,6 +488,8 @@ public bool DetectFaces(SuccessCallback<DetectedFaces> successCallback, FailCall
479488
req.OnResponse = OnDetectFacesResp;
480489
if (Credentials.HasApiKey())
481490
req.Parameters["api_key"] = Credentials.ApiKey;
491+
if (!string.IsNullOrEmpty(acceptLanguage))
492+
req.Headers["Accept-Language"] = acceptLanguage;
482493
req.Parameters["version"] = VersionDate;
483494
req.Forms = new Dictionary<string, RESTConnector.Form>();
484495
req.Forms["images_file"] = new RESTConnector.Form(imageData);

Scripts/UnitTests/TestVisualRecognition.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public override IEnumerator RunTest()
163163

164164
// Detect faces get
165165
Log.Debug("TestVisualRecognition.RunTest()", "Attempting to detect faces via URL");
166-
if (!_visualRecognition.DetectFaces(_imageURL, OnDetectFacesGet, OnFail))
166+
if (!_visualRecognition.DetectFaces(_imageURL, OnDetectFacesGet, OnFail, "es"))
167167
Log.Debug("TestVisualRecognition.DetectFaces()", "Detect faces failed!");
168168

169169
while (!_detectFacesGetTested)
@@ -172,7 +172,7 @@ public override IEnumerator RunTest()
172172
// Detect faces post image
173173
Log.Debug("TestVisualRecognition.RunTest()", "Attempting to detect faces via image");
174174
string faceExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/visual-recognition-classifiers/obama.jpg";
175-
if (!_visualRecognition.DetectFaces(OnDetectFacesPost, OnFail, faceExamplePath))
175+
if (!_visualRecognition.DetectFaces(OnDetectFacesPost, OnFail, faceExamplePath, "es"))
176176
Log.Debug("TestVisualRecognition.DetectFaces()", "Detect faces failed!");
177177

178178
while (!_detectFacesPostTested)
@@ -256,13 +256,15 @@ private void OnDetectFacesGet(DetectedFaces multipleImages, Dictionary<string, o
256256
{
257257
Log.Debug("TestVisualRecognition.OnDetectFacesGet()", "VisualRecognition - DetectFacesGet Response: {0}", customData["json"].ToString());
258258
Test(multipleImages != null);
259+
Test(multipleImages.images[0].faces[0].gender.GenderLabel == "macho");
259260
_detectFacesGetTested = true;
260261
}
261262

262263
private void OnDetectFacesPost(DetectedFaces multipleImages, Dictionary<string, object> customData)
263264
{
264265
Log.Debug("TestVisualRecognition.OnDetectFacesPost()", "VisualRecognition - DetectFacesPost Response: {0}", customData["json"].ToString());
265266
Test(multipleImages != null);
267+
Test(multipleImages.images[0].faces[0].gender.GenderLabel == "macho");
266268
_detectFacesPostTested = true;
267269
}
268270

0 commit comments

Comments
 (0)