Last active
November 2, 2019 06:52
-
-
Save akeller/8d2db3b4fce7f4faef3e93644029ff2c to your computer and use it in GitHub Desktop.
Unity + MAX (Model Asset Exchange) now with 100% more JSON parsing!
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.UI; | |
using UnityEngine.Networking; | |
using System.IO; | |
using System; | |
public class TakePicture : MonoBehaviour { | |
public Button takepicbutton; | |
public RawImage rawImage; | |
public Text generatedCaption; | |
WebCamTexture webCamTexture = null; | |
// Use this for initialization | |
void Start () { | |
//need to grab the camera name not hardcode it | |
webCamTexture = new WebCamTexture("FaceTime HD Camera", 400, 300); | |
rawImage.texture = webCamTexture; | |
rawImage.material.mainTexture = webCamTexture; | |
webCamTexture.Play(); | |
Button btn1 = takepicbutton.GetComponent<Button>(); | |
btn1.onClick.AddListener(WorkPlease); | |
} | |
//wrapper to call my coroutine | |
public void WorkPlease(){ | |
StartCoroutine(PicCapture()); | |
} | |
private IEnumerator PicCapture() | |
{ | |
yield return new WaitForEndOfFrame(); | |
Texture2D photo = new Texture2D((int)rawImage.material.mainTexture.width, (int)rawImage.material.mainTexture.height, TextureFormat.RGBA32, false); | |
photo.SetPixels(webCamTexture.GetPixels()); | |
photo.Apply(); | |
byte[] bytes = photo.EncodeToPNG(); | |
//can remove below line | |
File.WriteAllBytes("/Users/amarakeller/Documents/UnityTest/testphoto.PNG", bytes); | |
webCamTexture.Stop(); | |
WWWForm form = new WWWForm(); | |
form.AddBinaryData("image", bytes); | |
UnityWebRequest w = UnityWebRequest.Post("http://localhost:5000/model/predict", form); | |
yield return w.SendWebRequest(); | |
if (w.isNetworkError || w.isHttpError) | |
//print(w.error); | |
print(w.downloadHandler.text); | |
else { | |
var MaxResponseObj = MaxResults.CreateFromJSON(w.downloadHandler.text).predictions[0].caption; | |
Debug.Log(MaxResponseObj); | |
generatedCaption.text = MaxResponseObj; | |
} | |
yield return null; | |
} | |
} | |
[System.Serializable] | |
public class MaxResults | |
{ | |
public string status; | |
public List<Prediction> predictions; | |
[System.Serializable] | |
public class Prediction | |
{ | |
public string index; | |
public string caption; | |
public double probability; | |
} | |
public static MaxResults CreateFromJSON(string jsonString) | |
{ | |
return JsonUtility.FromJson<MaxResults>(jsonString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment