2019-04-17 15:23:27 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-04-23 10:53:37 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using OpenCVForUnity.CoreModule;
|
2019-04-17 15:23:27 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Vuforia;
|
|
|
|
|
using Newtonsoft.Json;
|
2019-04-25 16:00:05 +00:00
|
|
|
|
using RestSharp;
|
2019-04-17 15:23:27 +00:00
|
|
|
|
using OpenCVForUnity.Calib3dModule;
|
|
|
|
|
using OpenCVForUnity.ImgprocModule;
|
2019-04-23 10:53:37 +00:00
|
|
|
|
using OpenCVForUnity.ImgcodecsModule;
|
2019-04-25 16:00:05 +00:00
|
|
|
|
using System.Collections;
|
|
|
|
|
|
2019-04-23 10:53:37 +00:00
|
|
|
|
public class HomographyAndState
|
2019-04-17 15:23:27 +00:00
|
|
|
|
{
|
2019-04-23 10:53:37 +00:00
|
|
|
|
public List<List<float>> homography { get; set; }
|
|
|
|
|
public List<List<int>> board { get; set; }
|
2019-04-17 15:23:27 +00:00
|
|
|
|
}
|
2019-04-23 10:53:37 +00:00
|
|
|
|
|
2019-04-17 15:23:27 +00:00
|
|
|
|
public class python_test : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
Mat camImageMat;
|
|
|
|
|
Texture2D outputTexture;
|
|
|
|
|
int baselineWidth = 1500;
|
|
|
|
|
int baselineHeight = 1500;
|
|
|
|
|
Mat loadedImage;
|
2019-04-29 12:16:20 +00:00
|
|
|
|
Mat loadedImage_pls;
|
2019-04-23 10:53:37 +00:00
|
|
|
|
GameObject rue;
|
|
|
|
|
bool displayWarped = false;
|
|
|
|
|
double rueX = 0.51;
|
|
|
|
|
double rueY = 0.71;
|
|
|
|
|
|
2019-04-25 16:00:05 +00:00
|
|
|
|
Mat camImgCopy;
|
2019-04-23 10:53:37 +00:00
|
|
|
|
|
2019-04-25 16:00:05 +00:00
|
|
|
|
WebCamTexture _webcamtex;
|
|
|
|
|
Mat camMatFromTexture;
|
2019-04-23 10:53:37 +00:00
|
|
|
|
|
2019-04-25 16:00:05 +00:00
|
|
|
|
Process process;
|
|
|
|
|
ProcessStartInfo start = new ProcessStartInfo();
|
|
|
|
|
Texture2D _TextureFromCamera;
|
2019-04-17 15:23:27 +00:00
|
|
|
|
|
2019-04-29 12:16:20 +00:00
|
|
|
|
bool displayLoaded = false;
|
2019-04-25 16:00:05 +00:00
|
|
|
|
RestClient restClient;
|
2019-04-17 15:23:27 +00:00
|
|
|
|
|
2019-04-29 12:16:20 +00:00
|
|
|
|
Dictionary<int, string> intToPiece = new Dictionary<int, string>();
|
|
|
|
|
|
|
|
|
|
|
2019-04-25 16:00:05 +00:00
|
|
|
|
int viewMarked = 0;
|
|
|
|
|
//StreamWriter sw;
|
|
|
|
|
//StreamReader sr;
|
2019-04-23 10:53:37 +00:00
|
|
|
|
|
2019-04-17 15:23:27 +00:00
|
|
|
|
|
2019-04-25 16:00:05 +00:00
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
void Start()
|
2019-04-23 10:53:37 +00:00
|
|
|
|
{
|
2019-04-17 15:23:27 +00:00
|
|
|
|
|
2019-04-29 12:16:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
loadedImage_pls = Imgcodecs.imread("/Users/alexandermunch-hansen/projects/chess_stuff/advancedskrald/whole_boards/boards_for_empty/board_1554285984.187497_rank_4.png");
|
2019-04-25 16:00:05 +00:00
|
|
|
|
|
2019-04-29 12:16:20 +00:00
|
|
|
|
|
|
|
|
|
intToPiece.Add(0, "RK");
|
|
|
|
|
intToPiece.Add(1, "KNT");
|
|
|
|
|
intToPiece.Add(2, "BSP");
|
|
|
|
|
intToPiece.Add(6, "");
|
2019-04-25 16:00:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
camMatFromTexture = new Mat(1080, 1920, CvType.CV_8UC3);
|
|
|
|
|
_TextureFromCamera = new Texture2D(1920, 1080);
|
2019-04-29 12:16:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-25 16:00:05 +00:00
|
|
|
|
|
|
|
|
|
restClient = new RestClient("http://localhost:5000");
|
2019-04-17 15:23:27 +00:00
|
|
|
|
|
|
|
|
|
|
2019-04-25 16:00:05 +00:00
|
|
|
|
//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(WebCamTexture.devices[0].name, 1920, 1080);
|
|
|
|
|
_webcamtex.Play();
|
2019-04-17 15:23:27 +00:00
|
|
|
|
|
2019-04-25 16:00:05 +00:00
|
|
|
|
|
|
|
|
|
|
2019-04-17 15:23:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 16:00:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2019-04-29 12:16:20 +00:00
|
|
|
|
print(response.Content);
|
2019-04-25 16:00:05 +00:00
|
|
|
|
return response.Content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
using (StreamWriter writer = process.StandardInput)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteLine(base64);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using (StreamReader reader = process.StandardOutput)
|
|
|
|
|
{
|
|
|
|
|
string result = reader.ReadLine();
|
|
|
|
|
return (string)result;
|
|
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-17 15:23:27 +00:00
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void Update()
|
2019-04-23 10:53:37 +00:00
|
|
|
|
{
|
2019-04-29 12:16:20 +00:00
|
|
|
|
if (displayLoaded)
|
|
|
|
|
{
|
|
|
|
|
camMatFromTexture = loadedImage_pls.clone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-04-25 16:00:05 +00:00
|
|
|
|
Image camImg = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGBA8888);
|
|
|
|
|
|
|
|
|
|
Mat textMat = null;
|
|
|
|
|
|
2019-04-17 15:23:27 +00:00
|
|
|
|
if (camImg != null)
|
2019-04-25 16:00:05 +00:00
|
|
|
|
{
|
|
|
|
|
_TextureFromCamera.SetPixels(_webcamtex.GetPixels());
|
|
|
|
|
_TextureFromCamera.Apply();
|
|
|
|
|
//byte[] bytes = _TextureFromCamera.EncodeToPNG();
|
|
|
|
|
//string filePath = "SavedScreen1.png";
|
|
|
|
|
//File.WriteAllBytes(filePath, bytes);
|
2019-04-29 12:16:20 +00:00
|
|
|
|
if (!displayLoaded)
|
|
|
|
|
{
|
|
|
|
|
OpenCVForUnity.UnityUtils.Utils.texture2DToMat(_TextureFromCamera, camMatFromTexture);
|
|
|
|
|
}
|
2019-04-25 16:00:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2019-04-23 10:53:37 +00:00
|
|
|
|
if (camImageMat == null)
|
|
|
|
|
{
|
|
|
|
|
camImageMat = new Mat(camImg.Height, camImg.Width, CvType.CV_8UC4);
|
|
|
|
|
}
|
2019-04-17 15:23:27 +00:00
|
|
|
|
camImageMat.put(0, 0, camImg.Pixels);
|
2019-04-25 16:00:05 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.A))
|
2019-04-17 15:23:27 +00:00
|
|
|
|
{
|
2019-04-25 16:00:05 +00:00
|
|
|
|
viewMarked = 0;
|
|
|
|
|
} else if (Input.GetKeyDown(KeyCode.S))
|
|
|
|
|
{
|
|
|
|
|
viewMarked = 1;
|
2019-04-29 12:16:20 +00:00
|
|
|
|
} else if (Input.GetKeyDown(KeyCode.D))
|
|
|
|
|
{
|
|
|
|
|
displayLoaded = true;
|
|
|
|
|
} else if (Input.GetKeyDown(KeyCode.F))
|
|
|
|
|
{
|
|
|
|
|
displayLoaded = false;
|
2019-04-25 16:00:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Input.GetKeyDown("space"))
|
|
|
|
|
{
|
2019-04-29 12:16:20 +00:00
|
|
|
|
//ScreenCapture.CaptureScreenshot("plspls.png");
|
2019-04-25 16:00:05 +00:00
|
|
|
|
|
2019-04-29 12:16:20 +00:00
|
|
|
|
// TODO: Set this back again
|
2019-04-17 15:23:27 +00:00
|
|
|
|
|
2019-04-29 12:16:20 +00:00
|
|
|
|
if (!displayLoaded)
|
|
|
|
|
{
|
|
|
|
|
loadedImage = camMatFromTexture.clone();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
loadedImage = loadedImage_pls.clone();
|
|
|
|
|
}
|
|
|
|
|
//camMatFromTexture = loadedImage.clone();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-25 16:00:05 +00:00
|
|
|
|
var pls = runCmd(camMatFromTexture);
|
|
|
|
|
|
2019-04-17 15:23:27 +00:00
|
|
|
|
|
2019-04-23 10:53:37 +00:00
|
|
|
|
Mat homography = new Mat(new Size(3, 3), CvType.CV_64F);
|
2019-04-17 15:23:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-23 10:53:37 +00:00
|
|
|
|
HomographyAndState homographyAndState = JsonConvert.DeserializeObject<HomographyAndState>(pls);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
|
{
|
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
|
{
|
|
|
|
|
homography.put(i, j, homographyAndState.homography[i][j]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MatOfPoint2f points = new MatOfPoint2f();
|
2019-04-23 12:25:46 +00:00
|
|
|
|
points.alloc(64);
|
2019-04-17 15:23:27 +00:00
|
|
|
|
|
2019-04-29 12:16:20 +00:00
|
|
|
|
|
2019-04-23 12:25:46 +00:00
|
|
|
|
var board = homographyAndState.board;
|
|
|
|
|
int indexer = 0;
|
|
|
|
|
for (int y = 1; y < 9; y++)
|
|
|
|
|
{
|
|
|
|
|
for (int x = 1; x < 9; x++)
|
|
|
|
|
{
|
|
|
|
|
var cur_pos = board[x - 1][y - 1];
|
2019-04-25 16:00:05 +00:00
|
|
|
|
|
2019-04-29 12:16:20 +00:00
|
|
|
|
|
2019-04-24 07:41:11 +00:00
|
|
|
|
points.put(indexer, 0, (cur_pos != 6 ? 1 : -200) * y * 165, (cur_pos != 6 ? 1 : -200) * x * 165);
|
2019-04-23 12:25:46 +00:00
|
|
|
|
indexer += 1;
|
|
|
|
|
}
|
2019-04-23 11:32:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MatOfPoint2f dstPoints = new MatOfPoint2f();
|
2019-04-23 10:53:37 +00:00
|
|
|
|
|
2019-04-23 11:32:57 +00:00
|
|
|
|
Core.perspectiveTransform(points, dstPoints, homography.inv());
|
|
|
|
|
|
|
|
|
|
List<Point> transformedPoints = dstPoints.toList();
|
2019-04-29 12:16:20 +00:00
|
|
|
|
|
|
|
|
|
indexer = 0;
|
|
|
|
|
for (int i = 0; i < 8; i++)
|
2019-04-23 11:32:57 +00:00
|
|
|
|
{
|
2019-04-29 12:16:20 +00:00
|
|
|
|
for (int j = 0; j < 8; j++) {
|
|
|
|
|
var point = transformedPoints[indexer];
|
|
|
|
|
//Imgproc.circle(loadedImage, point, 5, new Scalar(255, 255, 255), -1);
|
|
|
|
|
print($"({point.x}, {point.y}): {intToPiece[board[j][i]]}");
|
|
|
|
|
|
|
|
|
|
Imgproc.putText(loadedImage, $"{intToPiece[board[j][i]]}", new Point(point.x-30, point.y - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 1, new Scalar(255, 255, 255), 3);
|
|
|
|
|
indexer += 1;
|
|
|
|
|
}
|
2019-04-23 11:32:57 +00:00
|
|
|
|
}
|
2019-04-29 12:16:20 +00:00
|
|
|
|
|
2019-04-23 11:32:57 +00:00
|
|
|
|
|
2019-04-25 16:00:05 +00:00
|
|
|
|
// MatDisplay.DisplayMat(loadedImage, MatDisplaySettings.FULL_BACKGROUND);
|
|
|
|
|
|
|
|
|
|
//process.Close();
|
|
|
|
|
//process = Process.Start(start);
|
2019-04-23 10:53:37 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 16:00:05 +00:00
|
|
|
|
if (viewMarked == 0)
|
|
|
|
|
{
|
|
|
|
|
MatDisplay.DisplayMat(camMatFromTexture, MatDisplaySettings.FULL_BACKGROUND);
|
|
|
|
|
|
|
|
|
|
} else if (viewMarked == 1)
|
|
|
|
|
{
|
|
|
|
|
MatDisplay.DisplayMat(loadedImage, MatDisplaySettings.FULL_BACKGROUND);
|
|
|
|
|
}
|
2019-04-17 15:23:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2019-04-23 10:53:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 15:23:27 +00:00
|
|
|
|
|
|
|
|
|
}
|