151 lines
5.0 KiB
C#
151 lines
5.0 KiB
C#
|
using System;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.IO;
|
|||
|
using OpenCVForUnity.CoreModule;
|
|||
|
using UnityEngine;
|
|||
|
using Vuforia;
|
|||
|
using Newtonsoft.Json;
|
|||
|
using OpenCVForUnity.Calib3dModule;
|
|||
|
using OpenCVForUnity.ImgprocModule;
|
|||
|
using OpenCVForUnity.ImgcodecsModule;
|
|||
|
|
|||
|
public class HomographyPoints
|
|||
|
{
|
|||
|
public List<List<float>> src_points { get; set; }
|
|||
|
public List<List<float>> dst_points { get; set; }
|
|||
|
}
|
|||
|
|
|||
|
public class python_test : MonoBehaviour
|
|||
|
{
|
|||
|
Mat camImageMat;
|
|||
|
readonly int baselineWidth = 1500;
|
|||
|
readonly int baselineHeight = 1500;
|
|||
|
Mat loadedImage;
|
|||
|
|
|||
|
// Start is called before the first frame update
|
|||
|
void Start()
|
|||
|
{
|
|||
|
|
|||
|
loadedImage = Imgcodecs.imread("/Users/alexandermunch-hansen/projects/chess_stuff/advancedskrald/whole_boards/boards_for_empty/board_1554285984.187497_rank_4.png");
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
string runCmd(Mat image)
|
|||
|
{
|
|||
|
var buffer = new MatOfByte();
|
|||
|
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 cmd = "/Users/alexandermunch-hansen/projects/FuckCSharp/AR-Course/AR-3/Assets/tester.py";
|
|||
|
var args = "";
|
|||
|
start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd, args);
|
|||
|
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))
|
|||
|
{
|
|||
|
using (StreamWriter writer = process.StandardInput)
|
|||
|
{
|
|||
|
writer.WriteLine(base64);
|
|||
|
//writer.WriteLine("lol" + writer.NewLine);
|
|||
|
writer.Close();
|
|||
|
}
|
|||
|
|
|||
|
process.WaitForExit();
|
|||
|
|
|||
|
string err = process.StandardError.ReadToEnd();
|
|||
|
print(err);
|
|||
|
|
|||
|
using (StreamReader reader = process.StandardOutput)
|
|||
|
{
|
|||
|
string result = reader.ReadLine();
|
|||
|
|
|||
|
return (string) result;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Update is called once per frame
|
|||
|
void Update()
|
|||
|
{
|
|||
|
Image camImg = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGBA8888);
|
|||
|
Mat outputMat = null;
|
|||
|
if (camImg != null)
|
|||
|
{
|
|||
|
if (camImageMat == null)
|
|||
|
{
|
|||
|
camImageMat = new Mat(camImg.Height, camImg.Width, CvType.CV_8UC4);
|
|||
|
}
|
|||
|
camImageMat.put(0, 0, camImg.Pixels);
|
|||
|
if (Input.GetKeyDown("space"))
|
|||
|
{
|
|||
|
|
|||
|
/*
|
|||
|
var camImgCopy = camImageMat.clone();
|
|||
|
outputMat = camImageMat.clone();
|
|||
|
*/
|
|||
|
|
|||
|
print("------------------------------------");
|
|||
|
print(loadedImage.size());
|
|||
|
|
|||
|
var camImgCopy = loadedImage.clone();
|
|||
|
outputMat = camImgCopy.clone();
|
|||
|
camImageMat = loadedImage.clone();
|
|||
|
|
|||
|
print("------------------------------------");
|
|||
|
|
|||
|
|
|||
|
var pls = runCmd(camImageMat);
|
|||
|
|
|||
|
print(pls);
|
|||
|
|
|||
|
HomographyPoints homographyPoints = JsonConvert.DeserializeObject<HomographyPoints>(pls);
|
|||
|
|
|||
|
print(homographyPoints.src_points);
|
|||
|
|
|||
|
MatOfPoint2f imagePoints = new MatOfPoint2f();
|
|||
|
imagePoints.alloc(homographyPoints.src_points.Count);
|
|||
|
print(homographyPoints.src_points.Count);
|
|||
|
|
|||
|
|
|||
|
MatOfPoint2f dstPoints = new MatOfPoint2f();
|
|||
|
dstPoints.alloc(homographyPoints.dst_points.Count);
|
|||
|
|
|||
|
|
|||
|
for (int i = 0; i < homographyPoints.src_points.Count; i++)
|
|||
|
{
|
|||
|
imagePoints.put(i, 0, (int) homographyPoints.src_points[i][0], (int) homographyPoints.src_points[i][1]);
|
|||
|
}
|
|||
|
|
|||
|
for (int i = 0; i < homographyPoints.dst_points.Count; i++)
|
|||
|
{
|
|||
|
|
|||
|
dstPoints.put(i, 0, (int) homographyPoints.dst_points[i][0], (int) homographyPoints.dst_points[i][1]);
|
|||
|
}
|
|||
|
|
|||
|
var homography = Calib3d.findHomography(imagePoints, dstPoints, Calib3d.RANSAC);
|
|||
|
|
|||
|
Imgproc.warpPerspective(camImgCopy, outputMat, homography, new Size(baselineWidth, baselineHeight));
|
|||
|
|
|||
|
imagePoints.Dispose();
|
|||
|
dstPoints.Dispose();
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
if (outputMat != null)
|
|||
|
MatDisplay.DisplayMat(outputMat, MatDisplaySettings.FULL_BACKGROUND);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|