arskrald/AR-3/Assets/python_test.cs
2019-05-18 17:19:21 +02:00

313 lines
8.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using OpenCVForUnity.CoreModule;
using UnityEngine;
using Vuforia;
using Newtonsoft.Json;
using RestSharp;
using OpenCVForUnity.Calib3dModule;
using OpenCVForUnity.ImgprocModule;
using OpenCVForUnity.ImgcodecsModule;
using System.Collections;
public class HomographyAndState
{
public List<List<float>> homography { get; set; }
public List<List<int>> board { get; set; }
}
public class python_test : MonoBehaviour
{
public GameObject FbxBishop;
public GameObject FbxKing;
public GameObject FbxKnight;
public GameObject FbxPawn;
public GameObject FbxQueen;
public GameObject FbxRook;
public List<GameObject> pieces;
Mat camImageMat;
Texture2D outputTexture;
int baselineWidth = 1500;
int baselineHeight = 1500;
Mat loadedImage;
Mat loadedImage_pls;
GameObject rue;
bool displayWarped = false;
double rueX = 0.51;
double rueY = 0.71;
Mat camImgCopy;
WebCamTexture _webcamtex;
Mat camMatFromTexture;
Process process;
ProcessStartInfo start = new ProcessStartInfo();
Texture2D _TextureFromCamera;
bool displayLoaded = false;
GameObject quad;
Renderer quadRend;
RestClient restClient;
Dictionary<int, GameObject> intToPiece = new Dictionary<int, GameObject>();
GameObject transObject;
List<List<int>> boardState;
GameObject baseObject;
int viewMarked = 0;
//StreamWriter sw;
//StreamReader sr;
// Start is called before the first frame update
void Start()
{
baseObject = GameObject.Find("base_object");
quad = GameObject.Find("Quad");
quadRend = quad.GetComponent<Renderer>();
transObject = GameObject.Find("fuck");
boardState = new List<List<int>>() {
new List<int>() {0, 1, 2, 4, 3, 2, 1, 0},
new List<int>() {5, 5, 5, 5, 5, 5, 5, 5},
new List<int>() {6, 6, 6, 6, 6, 6, 6, 6},
new List<int>() {6, 6, 6, 6, 6, 6, 6, 6},
new List<int>() {6, 6, 6, 6, 6, 6, 6, 6},
new List<int>() {6, 6, 6, 6, 6, 6, 6, 6},
new List<int>() {6, 6, 6, 6, 6, 6, 6, 6},
new List<int>() {6, 6, 6, 6, 6, 6, 6, 6},
};
loadedImage_pls = Imgcodecs.imread("/Users/alexandermunch-hansen/projects/chess_stuff/advancedskrald/whole_boards/boards_for_empty/board_1554288882.301468_rank_8.png");
intToPiece.Add(0, FbxRook);
intToPiece.Add(1, FbxKnight);
intToPiece.Add(2, FbxBishop);
intToPiece.Add(3, FbxKing);
intToPiece.Add(4, FbxQueen);
intToPiece.Add(5, FbxPawn);
camMatFromTexture = new Mat(1080, 1920, CvType.CV_8UC3);
_TextureFromCamera = new Texture2D(1920, 1080);
restClient = new RestClient("http://localhost:5000");
//process = Process.Start(start);
//sw = new StreamWriter(process.StandardInput.BaseStream, System.Text.Encoding.Default, 1024, true);
//sr = new StreamReader(process.StandardOutput.BaseStream, System.Text.Encoding.Default, true, 1024, true);
_webcamtex = new WebCamTexture("HD Pro Webcam C920", 1920, 1080);
_webcamtex.Play();
//DoStuff();
baseObject.SetActive(false);
}
string runCmd(Mat image)
{
var buffer = new MatOfByte();
OpenCVForUnity.ImgcodecsModule.Imgcodecs.imencode(".png", image, buffer);
string base64 = Convert.ToBase64String(buffer.toArray());
String json = $"{{\"img\": \"{base64}\"}}";
RestRequest request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddParameter("application/json", json, ParameterType.RequestBody);
var response = restClient.Execute(request);
print(response.Content);
return response.Content;
}
/*
using (StreamWriter writer = process.StandardInput)
{
writer.WriteLine(base64);
}
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadLine();
return (string)result;
}*/
void DoStuff()
{
Vector3 bp = baseObject.transform.position;
foreach (GameObject piece in pieces)
{
Destroy(piece);
}
pieces.Clear();
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
int intPiece = boardState[j][i];
if (intPiece == 6)
{
continue;
}
GameObject fbx = intToPiece[intPiece];
GameObject piece = Instantiate(fbx);
piece.transform.localScale = baseObject.transform.localScale;
piece.transform.position = new Vector3((float)(bp.x + i * 0.043),
bp.y,
(float)(bp.z + j * 0.040));
pieces.Add(piece);
}
}
var transMat = (Material)Resources.Load("trans_mat", typeof(Material));
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
int intPiece = boardState[7-j][i];
if (intPiece == 6 || boardState[j][i] != 6)
{
continue;
}
GameObject piece = Instantiate(transObject);
piece.transform.position = new Vector3((float)(bp.x + i * 0.04),
bp.y,
(float)(bp.z + j * 0.04));
pieces.Add(piece);
}
}
}
// Update is called once per frame
void Update()
{
if (displayLoaded)
{
camMatFromTexture = loadedImage_pls.clone();
}
Image camImg = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGBA8888);
Mat textMat = null;
if (camImg != null)
{
_TextureFromCamera.SetPixels(_webcamtex.GetPixels());
_TextureFromCamera.Apply();
//byte[] bytes = _TextureFromCamera.EncodeToPNG();
//string filePath = "SavedScreen1.png";
//File.WriteAllBytes(filePath, bytes);
if (!displayLoaded)
{
OpenCVForUnity.UnityUtils.Utils.texture2DToMat(_TextureFromCamera, camMatFromTexture);
}
/*
if (camImageMat == null)
{
camImageMat = new Mat(camImg.Height, camImg.Width, CvType.CV_8UC4);
}
camImageMat.put(0, 0, camImg.Pixels);
*/
if (Input.GetKeyDown(KeyCode.A))
{
viewMarked = 0;
} else if (Input.GetKeyDown(KeyCode.S))
{
viewMarked = 1;
} else if (Input.GetKeyDown(KeyCode.D))
{
displayLoaded = true;
} else if (Input.GetKeyDown(KeyCode.F))
{
displayLoaded = false;
}
if (Input.GetKeyDown("space"))
{
//ScreenCapture.CaptureScreenshot("plspls.png");
// TODO: Set this back again
if (!displayLoaded)
{
loadedImage = camMatFromTexture.clone();
}
else
{
loadedImage = loadedImage_pls.clone();
}
//camMatFromTexture = loadedImage.clone();
var pls = runCmd(camMatFromTexture);
HomographyAndState homographyAndState = JsonConvert.DeserializeObject<HomographyAndState>(pls);
boardState = homographyAndState.board;
DoStuff();
// MatDisplay.DisplayMat(loadedImage, MatDisplaySettings.FULL_BACKGROUND);
//process.Close();
//process = Process.Start(start);
}
if (viewMarked == 0)
{
//MatDisplay.DisplayMat(camMatFromTexture, MatDisplaySettings.FULL_BACKGROUND);
quadRend.sharedMaterial.mainTexture = _TextureFromCamera;
}
else if (viewMarked == 1)
{
MatDisplay.DisplayMat(loadedImage, MatDisplaySettings.FULL_BACKGROUND);
}
}
}
}