158 lines
4.2 KiB
C#
158 lines
4.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Networking;
|
|
using System.IO;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using ExtralityLab.API;
|
|
|
|
public class WebcamCapture : MonoBehaviour
|
|
{
|
|
|
|
private string HOST = "http://10.204.0.244";
|
|
private int PORT = 8003;
|
|
|
|
WebCamTexture webcam;
|
|
public RawImage captureShow;
|
|
|
|
public ExtralityLab.API.Image dataImage;
|
|
|
|
|
|
[System.Serializable]
|
|
public class DetectedObject
|
|
{
|
|
public string label;
|
|
public int x;
|
|
public int y;
|
|
public int width;
|
|
public int height;
|
|
public float confidence;
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class ServerResponse
|
|
{
|
|
public string image; // base64 image
|
|
public List<DetectedObject> objects; // list of detected objects
|
|
public int count; // number of objects detected
|
|
}
|
|
|
|
ControllerAPI controllerAPI;
|
|
|
|
void Start()
|
|
{
|
|
webcam = new WebCamTexture();
|
|
Renderer renderer = GetComponent<Renderer>();
|
|
renderer.material.mainTexture = webcam;
|
|
|
|
webcam.Play();
|
|
|
|
InvokeRepeating(nameof(CaptureImage), 0.1f, 0.3f);
|
|
|
|
controllerAPI = new ControllerAPI();
|
|
}
|
|
|
|
public void CaptureImage()
|
|
{
|
|
Texture2D snap = new Texture2D(webcam.width, webcam.height);
|
|
snap.SetPixels(webcam.GetPixels());
|
|
snap.Apply();
|
|
|
|
string path = Application.dataPath + "/capture.png";
|
|
byte[] bytes = snap.EncodeToPNG();
|
|
|
|
dataImage = new ExtralityLab.API.Image();
|
|
dataImage.data = bytes;
|
|
|
|
StartCoroutine(SendImage(bytes));
|
|
File.WriteAllBytes(path, bytes);
|
|
|
|
Debug.Log("Saved image!");
|
|
|
|
// Load from disk (simulate real workflow)
|
|
byte[] loadedBytes = File.ReadAllBytes(path);
|
|
|
|
|
|
// Texture2D loadedTexture = new Texture2D(2, 2);
|
|
// loadedTexture.LoadImage(loadedBytes);
|
|
|
|
// // Show in UI
|
|
// captureShow.texture = loadedTexture;
|
|
// Debug.Log("ShowCaputure");
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.Space)) RequestProcessingYoloImage();
|
|
}
|
|
|
|
public void RequestProcessingYoloImage()
|
|
{
|
|
WWWForm form = new WWWForm();
|
|
form.AddBinaryData("image", dataImage.data, "photo.png", "image/png");
|
|
|
|
Debug.Log("about to send image");
|
|
|
|
//controllerAPI.ProcessImageYOLO(ReceiveImage);
|
|
}
|
|
|
|
public void ReceiveImage(YoloServerResponse yoloResponse)
|
|
{
|
|
Debug.Log(yoloResponse.Stringify());
|
|
}
|
|
|
|
public IEnumerator SendImage(byte[] bytes)
|
|
{
|
|
Debug.Log("we are in SendImage");
|
|
WWWForm form = new WWWForm();
|
|
form.AddBinaryData("image", bytes, "photo.png", "image/png");
|
|
|
|
Debug.Log("about to send image");
|
|
|
|
using (UnityWebRequest request =
|
|
UnityWebRequest.Post("http://10.204.0.244:8003/detect-curl/", form))
|
|
{
|
|
// Expect image back
|
|
request.downloadHandler = new DownloadHandlerBuffer();
|
|
|
|
yield return request.SendWebRequest();
|
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
{
|
|
Debug.LogError(request.error);
|
|
yield break;
|
|
}
|
|
Debug.Log("should've received by now");
|
|
|
|
// Get JSON response
|
|
string jsonResponse = request.downloadHandler.text;
|
|
|
|
// Parse JSON
|
|
ServerResponse response = JsonUtility.FromJson<ServerResponse>(jsonResponse);
|
|
|
|
if (string.IsNullOrEmpty(response.image))
|
|
{
|
|
Debug.LogError("No image found in response!");
|
|
yield break;
|
|
}
|
|
|
|
// Decode Base64 to bytes
|
|
byte[] decodedBytes = System.Convert.FromBase64String(response.image);
|
|
|
|
// Create Texture2D
|
|
Texture2D tex = new Texture2D(2, 2);
|
|
tex.LoadImage(decodedBytes); // LoadImage auto-resizes texture
|
|
|
|
// Assign to RawImage
|
|
captureShow.texture = tex;
|
|
Debug.Log("Received processed image!");
|
|
|
|
// Access detected objects
|
|
foreach (var obj in response.objects)
|
|
{
|
|
// e.g., draw bounding boxes in UI
|
|
Debug.Log($"Detected {obj.label} at {obj.x},{obj.y}");
|
|
}
|
|
}
|
|
}
|
|
} |