From 554108bcaf5f52799c10e8a93e6d141ad6f5939a Mon Sep 17 00:00:00 2001 From: Gabe Hollombe Date: Mon, 6 May 2019 13:25:41 +0800 Subject: [PATCH] speak emotion with highest confidence (instead of first one in result set) --- ai.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ai.js b/ai.js index 51b7860..542d2c2 100644 --- a/ai.js +++ b/ai.js @@ -69,6 +69,10 @@ function toArrayBuffer(dataUrl) { }); } + function sortByProperty(list, property) { + return list.slice().sort((a, b) => (a[property] > b[property]) ? 1 : -1) + } + function generateSpeech() { var toSpeak = ""; @@ -82,7 +86,7 @@ function toArrayBuffer(dataUrl) { var face = faces.FaceDetails[i]; text += "The " + ordinal_suffix_of(i+1) + " face is a "; - if(face["Emotions"]) text += face["Emotions"][0].Type.toLowerCase() + " "; + if(face["Emotions"]) text += sortByProperty(face.Emotions, 'Confidence').reverse()[0].Type.toLowerCase() + " "; if(face["Gender"]) text += face.Gender.Value.toLowerCase() + " "; else text += "person "; if(face["AgeRange"]) text += "between " + face.AgeRange.Low + " and " + face.AgeRange.High + " years old";