arskrald/AR-3/Assets/python_test.cs

172 lines
5.4 KiB
C#
Raw Normal View History

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;
using OpenCVForUnity.Calib3dModule;
using OpenCVForUnity.ImgprocModule;
2019-04-23 10:53:37 +00:00
using OpenCVForUnity.ImgcodecsModule;
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-23 10:53:37 +00:00
GameObject rue;
bool displayWarped = false;
double rueX = 0.51;
double rueY = 0.71;
Mat camImgCopy;
2019-04-17 15:23:27 +00:00
// Start is called before the first frame update
void Start()
{
2019-04-23 10:53:37 +00:00
loadedImage = Imgcodecs.imread("/Users/alexandermunch-hansen/projects/chess_stuff/advancedskrald/whole_boards/boards_for_empty/board_1554285984.187497_rank_4.png");
camImgCopy = loadedImage.clone();
2019-04-17 15:23:27 +00:00
}
2019-04-23 10:53:37 +00:00
string runCmd(Mat image)
{
var buffer = new MatOfByte();
2019-04-17 15:23:27 +00:00
OpenCVForUnity.ImgcodecsModule.Imgcodecs.imencode(".png", camImageMat, buffer);
string base64 = Convert.ToBase64String(buffer.toArray());
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "/Users/alexandermunch-hansen/.virtualenvs/cv/bin/python3";
var cmd = "/Users/alexandermunch-hansen/projects/chess_stuff/advancedskrald/adapter.py";
var args = "";
start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd, args);
2019-04-23 10:53:37 +00:00
print(start.Arguments);
2019-04-17 15:23:27 +00:00
start.UseShellExecute = false;// Do not use OS shell
start.CreateNoWindow = true; // We don't need new window
start.RedirectStandardInput = true;
start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
using (Process process = Process.Start(start))
{
2019-04-23 10:53:37 +00:00
using (StreamWriter writer = process.StandardInput)
{
2019-04-17 15:23:27 +00:00
writer.WriteLine(base64);
2019-04-23 10:53:37 +00:00
writer.Close();
}
2019-04-17 15:23:27 +00:00
2019-04-23 10:53:37 +00:00
process.WaitForExit();
2019-04-17 15:23:27 +00:00
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadLine();
return (string) result;
}
2019-04-23 10:53:37 +00:00
}
2019-04-17 15:23:27 +00:00
}
// Update is called once per frame
void Update()
2019-04-23 10:53:37 +00:00
{
Image camImg = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGBA8888);
2019-04-17 15:23:27 +00:00
Mat outputMat = null;
if (camImg != null)
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-23 10:53:37 +00:00
if (Input.GetKeyDown("space"))
2019-04-17 15:23:27 +00:00
{
2019-04-24 07:41:11 +00:00
// TODO: We should clone camImageMat again, instead of the loadedImage I use now
2019-04-17 15:23:27 +00:00
/*
var camImgCopy = camImageMat.clone();
outputMat = camImageMat.clone();
*/
print("------------------------------------");
2019-04-24 07:41:11 +00:00
print(loadedImage.size());
2019-04-23 10:53:37 +00:00
camImageMat = loadedImage.clone();
print("------------------------------------");
2019-04-17 15:23:27 +00:00
var pls = runCmd(camImageMat);
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);
print(homographyAndState.homography);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
homography.put(i, j, homographyAndState.homography[i][j]);
print(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-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-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-17 15:23:27 +00:00
2019-04-23 11:32:57 +00:00
foreach (Point point in transformedPoints)
{
Imgproc.circle(loadedImage, point, 10, new Scalar(255, 0, 0), -1);
}
MatDisplay.DisplayMat(loadedImage, MatDisplaySettings.FULL_BACKGROUND);
displayWarped = true;
2019-04-23 10:53:37 +00:00
}
if (!displayWarped)
MatDisplay.DisplayMat(camImgCopy, 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
}