Created
December 24, 2016 12:37
-
-
Save yunojy/d222368e5fc36fd054eb2ad463c92416 to your computer and use it in GitHub Desktop.
Cognitive ServicesのEmotion APIhttp://2vr.jp/2016/12/24/oculus-vr-xmas2016/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.VR.WSA.WebCam; | |
using System.Linq; | |
public class YomEmotion : MonoBehaviour { | |
public string EmotionAPIKey = ""; | |
public float CaptureInterval = 60f; | |
PhotoCapture photoCapture = null; | |
void Start() { | |
Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First(); | |
PhotoCapture.CreateAsync(false, delegate(PhotoCapture captureObject) { | |
photoCapture = captureObject; | |
CameraParameters c = new CameraParameters(); | |
c.hologramOpacity = 0.0f; | |
c.cameraResolutionWidth = cameraResolution.width; | |
c.cameraResolutionHeight = cameraResolution.height; | |
c.pixelFormat = CapturePixelFormat.JPEG; | |
captureObject.StartPhotoModeAsync(c, delegate(PhotoCapture.PhotoCaptureResult result) { | |
Debug.Log("Photo Mode started"); | |
InvokeRepeating("ExecutePictureProcess", 0, CaptureInterval); | |
}); | |
}); | |
} | |
void ExecutePictureProcess() { | |
if (photoCapture == null) return; | |
photoCapture.TakePhotoAsync(delegate(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame) { | |
List<byte> photo = new List<byte>(); | |
photoCaptureFrame.CopyRawImageDataIntoBuffer(photo); | |
StartCoroutine(PostToEmotionAPI(photo.ToArray())); | |
}); | |
} | |
IEnumerator<object> PostToEmotionAPI(byte[] photo) { | |
var url = "https://api.projectoxford.ai/emotion/v1.0/recognize"; | |
var headers = new Dictionary<string, string>() { | |
{ "Ocp-Apim-Subscription-Key", EmotionAPIKey }, | |
{ "Content-Type", "application/octet-stream" } | |
}; | |
WWW www = new WWW(url, photo, headers); | |
yield return www; | |
if (www.error != null) { | |
Debug.Log(www.error); | |
} | |
string responseString = www.text; | |
JSONObject j = new JSONObject(responseString); | |
Debug.Log(j); | |
foreach (var result in j.list) { | |
var p = result.GetField("faceRectangle"); | |
string id = string.Format("{0},{1},{2},{3}", p.GetField("left"), p.GetField("top"), p.GetField("width"), p.GetField("height")); | |
var obj = result.GetField("scores"); | |
string highestEmote = "Unknown"; | |
float highestC = 0; | |
for (int i = 0; i < obj.list.Count; i++) | |
{ | |
string key = obj.keys[i]; | |
float c = obj.list[i].f; | |
if (c > highestC) { | |
highestEmote = key; | |
highestC = c; | |
} | |
} | |
Debug.Log("Emotion: " + highestEmote); | |
} | |
} | |
void OnDestroy() { | |
photoCapture.StopPhotoModeAsync(delegate(PhotoCapture.PhotoCaptureResult res) { | |
photoCapture.Dispose(); | |
photoCapture = null; | |
Debug.Log("Photo Mode stopped"); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment