/// <summary> /// Recognizes the emotions. /// </summary> /// <returns>The array of recognized emotions.</returns> /// <param name="imageBytes">Image bytes.</param> /// <param name="faceRects">Detected face rectangles, or null.</param> public Emotion[] RecognizeEmotions(byte[] imageBytes, FaceRectangle[] faceRects) { if (string.IsNullOrEmpty(emotionSubscriptionKey)) { throw new Exception("The emotion-subscription key is not set."); } StringBuilder faceRectsStr = new StringBuilder(); if (faceRects != null) { foreach (FaceRectangle rect in faceRects) { faceRectsStr.AppendFormat("{0},{1},{2},{3};", rect.left, rect.top, rect.width, rect.height); } if (faceRectsStr.Length > 0) { faceRectsStr.Remove(faceRectsStr.Length - 1, 1); // drop the last semicolon } } string requestUrl = string.Format("{0}/recognize??faceRectangles={1}", GetEmotionServiceUrl(), faceRectsStr); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("ocp-apim-subscription-key", emotionSubscriptionKey); HttpWebResponse response = CloudWebTools.DoWebRequest(requestUrl, "POST", "application/octet-stream", imageBytes, headers, true, false); Emotion[] emotions = null; if (!CloudWebTools.IsErrorStatus(response)) { StreamReader reader = new StreamReader(response.GetResponseStream()); //emotions = JsonConvert.DeserializeObject<Emotion[]>(reader.ReadToEnd(), jsonSettings); string newJson = "{ \"emotions\": " + reader.ReadToEnd() + "}"; EmotionCollection emotionCollection = JsonUtility.FromJson <EmotionCollection>(newJson); emotions = emotionCollection.emotions; } else { ProcessFaceError(response); } return(emotions); }
/// <summary> /// Detects the faces in the given image. /// </summary> /// <returns>List of detected faces.</returns> /// <param name="imageBytes">Image bytes.</param> public IEnumerator DetectFaces(byte[] imageBytes) { faces = null; if (string.IsNullOrEmpty(faceSubscriptionKey)) { throw new Exception("The face-subscription key is not set."); } // detect faces string faceServiceHost = FaceServiceHost.Replace("[location]", faceServiceLocation); string requestUrl = string.Format("{0}/detect?returnFaceId={1}&returnFaceLandmarks={2}&returnFaceAttributes={3}", faceServiceHost, true, false, "age,gender,smile,facialHair,glasses"); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("ocp-apim-subscription-key", faceSubscriptionKey); headers.Add("Content-Type", "application/octet-stream"); headers.Add("Content-Length", imageBytes.Length.ToString()); WWW www = new WWW(requestUrl, imageBytes, headers); yield return(www); // if (!string.IsNullOrEmpty(www.error)) // { // throw new Exception(www.error + " - " + requestUrl); // } if (!CloudWebTools.IsErrorStatus(www)) { //faces = JsonConvert.DeserializeObject<Face[]>(www.text, jsonSettings); string newJson = "{ \"faces\": " + www.text + "}"; FacesCollection facesCollection = JsonUtility.FromJson <FacesCollection>(newJson); faces = facesCollection.faces; } else { ProcessFaceError(www); } if (/**recognizeEmotions &&*/ !string.IsNullOrEmpty(emotionSubscriptionKey)) { // get face rectangles StringBuilder faceRectsStr = new StringBuilder(); if (faces != null) { foreach (Face face in faces) { FaceRectangle rect = face.faceRectangle; faceRectsStr.AppendFormat("{0},{1},{2},{3};", rect.left, rect.top, rect.width, rect.height); } if (faceRectsStr.Length > 0) { faceRectsStr.Remove(faceRectsStr.Length - 1, 1); // drop the last semicolon } } // recognize emotions string emotionServiceHost = EmotionServiceHost.Replace("[location]", emotionServiceLocation); requestUrl = string.Format("{0}/recognize??faceRectangles={1}", emotionServiceHost, faceRectsStr); headers = new Dictionary <string, string>(); headers.Add("ocp-apim-subscription-key", emotionSubscriptionKey); headers.Add("Content-Type", "application/octet-stream"); headers.Add("Content-Length", imageBytes.Length.ToString()); www = new WWW(requestUrl, imageBytes, headers); yield return(www); Emotion[] emotions = null; if (!CloudWebTools.IsErrorStatus(www)) { //emotions = JsonConvert.DeserializeObject<Emotion[]>(reader.ReadToEnd(), jsonSettings); string newJson = "{ \"emotions\": " + www.text + "}"; EmotionCollection emotionCollection = JsonUtility.FromJson <EmotionCollection>(newJson); emotions = emotionCollection.emotions; } else { ProcessFaceError(www); } if (emotions != null) { // match the emotions to faces int matched = MatchEmotionsToFaces(ref faces, ref emotions); if (matched != faces.Length) { Debug.Log(string.Format("Matched {0}/{1} emotions to {2} faces.", matched, emotions.Length, faces.Length)); } } } }