Created
August 1, 2018 15:04
-
-
Save akeller/b865717cd5163679503653ced99c4554 to your computer and use it in GitHub Desktop.
Unity + MAX (Model Asset Exchange) Image Caption Generator
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; | |
//ignore the counter for now, its only setup for take on picture and turn the camera off | |
int counter = 0; | |
WebCamTexture webCamTexture = null; | |
// Use this for initialization | |
void Start () { | |
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.EncodeToJPG(); | |
File.WriteAllBytes("/Users/amarakeller/Documents/UnityTest/" + counter + "testphoto.png", bytes); | |
//counter++; | |
webCamTexture.Stop(); | |
WWWForm form = new WWWForm(); | |
form.AddBinaryData("image", bytes); | |
var w = UnityWebRequest.Post("http://{your-kube-model-public-url}/model/predict", form); | |
yield return w.SendWebRequest(); | |
if (w.isNetworkError || w.isHttpError) | |
print(w.error); | |
else | |
print(w.downloadHandler.text); | |
//counter++; | |
yield return null; | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment