Instantly share code, notes, and snippets.

Embed
What would you like to do?
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