: Microsoft Cognitive Services + Unity |
private IEnumerator CheckEmotions()
{
EmotionService emoServ = new EmotionService(SubscriptionKeys.Instance.EmotionsApiKey);
while (true)
{
yield return new WaitForEndOfFrame();
yield return emoServ.GetEmoInfoCoroutine(_WebCam.Screenshot);
var emotions = emoServ.LastEmotions;
if (emotions != null)
{
_MaxEmotionValue.text = GetMaxEmotionOnScreenshot(emotions);
}
yield return new WaitForSeconds(DELAY);
}
}
private IEnumerator CreateRecognizeRequestAndSaveResponseCoroutine(
string contentHeader,
byte[] data)
{
Dictionary headers = new Dictionary();
headers.Add(Constants.SUB_KEY_HEADER, _SubscriptionKey);
headers.Add(Constants.CONTENT_TYPE_HEADER, contentHeader);
WWW request = new WWW(_RecognizeRequestUrl, data, headers);
yield return new WaitUntil(() => request.isDone);
ParseEmotionsFromJson(request.text);
}
private void CreateRecognizeRequest()
{
Clear();
_Thread = new Thread(Run);
_Thread.Start();
}
private void Run()
{
WebHeaderCollection headers = new WebHeaderCollection();
headers.Add(Constants.SUB_KEY_HEADER, _SubscriptionKey);
var request = HttpWebRequest.Create(_RecognizeRequestUrl);
request.ContentType = _ContentHeader;
request.Headers = headers;
request.ContentLength = _Data.Length;
request.Method = WebRequestMethods.Http.Post;
var dataStream = request.GetRequestStream();
dataStream.Write(_Data, 0, _Data.Length);
dataStream.Close();
var response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseString = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
Debug.Log(responseString);
if (!TryParseEmotionsFromJson(responseString))
{
Run();
}
else
{
_IsDataReady = true;
}
}