Modular stuff

This commit is contained in:
Alexander Munch-Hansen 2019-04-29 14:16:20 +02:00
parent cc2c0843a0
commit 87845fd1a5
2 changed files with 63 additions and 35 deletions

View File

@ -25,6 +25,7 @@ public class python_test : MonoBehaviour
int baselineWidth = 1500; int baselineWidth = 1500;
int baselineHeight = 1500; int baselineHeight = 1500;
Mat loadedImage; Mat loadedImage;
Mat loadedImage_pls;
GameObject rue; GameObject rue;
bool displayWarped = false; bool displayWarped = false;
double rueX = 0.51; double rueX = 0.51;
@ -39,8 +40,12 @@ public class python_test : MonoBehaviour
ProcessStartInfo start = new ProcessStartInfo(); ProcessStartInfo start = new ProcessStartInfo();
Texture2D _TextureFromCamera; Texture2D _TextureFromCamera;
bool displayLoaded = false;
RestClient restClient; RestClient restClient;
Dictionary<int, string> intToPiece = new Dictionary<int, string>();
int viewMarked = 0; int viewMarked = 0;
//StreamWriter sw; //StreamWriter sw;
//StreamReader sr; //StreamReader sr;
@ -50,23 +55,23 @@ public class python_test : MonoBehaviour
void Start() void Start()
{ {
//loadedImage = Imgcodecs.imread("/Users/alexandermunch-hansen/projects/chess_stuff/advancedskrald/whole_boards/boards_for_empty/board_1554285984.187497_rank_4.png");
//camImgCopy = loadedImage.clone();
loadedImage_pls = Imgcodecs.imread("/Users/alexandermunch-hansen/projects/chess_stuff/advancedskrald/whole_boards/boards_for_empty/board_1554285984.187497_rank_4.png");
start.FileName = "/Users/alexandermunch-hansen/.virtualenvs/cv/bin/python3";
var cmd = "/Users/alexandermunch-hansen/projects/chess_stuff/advancedskrald/adapter.py"; intToPiece.Add(0, "RK");
var args = ""; intToPiece.Add(1, "KNT");
start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd, args); intToPiece.Add(2, "BSP");
print(start.Arguments); intToPiece.Add(6, "");
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)
camMatFromTexture = new Mat(1080, 1920, CvType.CV_8UC3); camMatFromTexture = new Mat(1080, 1920, CvType.CV_8UC3);
_TextureFromCamera = new Texture2D(1920, 1080); _TextureFromCamera = new Texture2D(1920, 1080);
restClient = new RestClient("http://localhost:5000"); restClient = new RestClient("http://localhost:5000");
@ -98,7 +103,7 @@ public class python_test : MonoBehaviour
request.AddParameter("application/json", json, ParameterType.RequestBody); request.AddParameter("application/json", json, ParameterType.RequestBody);
var response = restClient.Execute(request); var response = restClient.Execute(request);
print(response.Content);
return response.Content; return response.Content;
} }
@ -121,6 +126,12 @@ public class python_test : MonoBehaviour
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
if (displayLoaded)
{
camMatFromTexture = loadedImage_pls.clone();
}
Image camImg = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGBA8888); Image camImg = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGBA8888);
Mat textMat = null; Mat textMat = null;
@ -132,8 +143,10 @@ public class python_test : MonoBehaviour
//byte[] bytes = _TextureFromCamera.EncodeToPNG(); //byte[] bytes = _TextureFromCamera.EncodeToPNG();
//string filePath = "SavedScreen1.png"; //string filePath = "SavedScreen1.png";
//File.WriteAllBytes(filePath, bytes); //File.WriteAllBytes(filePath, bytes);
OpenCVForUnity.UnityUtils.Utils.texture2DToMat(_TextureFromCamera, camMatFromTexture); if (!displayLoaded)
{
OpenCVForUnity.UnityUtils.Utils.texture2DToMat(_TextureFromCamera, camMatFromTexture);
}
/* /*
@ -147,34 +160,37 @@ public class python_test : MonoBehaviour
if (Input.GetKeyDown(KeyCode.A)) if (Input.GetKeyDown(KeyCode.A))
{ {
print("fuck");
viewMarked = 0; viewMarked = 0;
} else if (Input.GetKeyDown(KeyCode.S)) } else if (Input.GetKeyDown(KeyCode.S))
{ {
print("cock");
viewMarked = 1; viewMarked = 1;
} else if (Input.GetKeyDown(KeyCode.D))
{
displayLoaded = true;
} else if (Input.GetKeyDown(KeyCode.F))
{
displayLoaded = false;
} }
if (Input.GetKeyDown("space")) if (Input.GetKeyDown("space"))
{ {
//ScreenCapture.CaptureScreenshot("plspls.png"); //ScreenCapture.CaptureScreenshot("plspls.png");
loadedImage = camMatFromTexture.clone();
// TODO: We should clone camImageMat again, instead of the loadedImage I use now // TODO: Set this back again
//loadedImage = camImageMat.clone(); if (!displayLoaded)
//camImgCopy = camImageMat.clone(); {
loadedImage = camMatFromTexture.clone();
print("------------------------------------"); }
else
//print(loadedImage.size()); {
camImageMat = loadedImage.clone(); loadedImage = loadedImage_pls.clone();
}
print("------------------------------------"); //camMatFromTexture = loadedImage.clone();
var pls = runCmd(camMatFromTexture); var pls = runCmd(camMatFromTexture);
@ -196,6 +212,7 @@ public class python_test : MonoBehaviour
MatOfPoint2f points = new MatOfPoint2f(); MatOfPoint2f points = new MatOfPoint2f();
points.alloc(64); points.alloc(64);
var board = homographyAndState.board; var board = homographyAndState.board;
int indexer = 0; int indexer = 0;
for (int y = 1; y < 9; y++) for (int y = 1; y < 9; y++)
@ -204,6 +221,7 @@ public class python_test : MonoBehaviour
{ {
var cur_pos = board[x - 1][y - 1]; var cur_pos = board[x - 1][y - 1];
points.put(indexer, 0, (cur_pos != 6 ? 1 : -200) * y * 165, (cur_pos != 6 ? 1 : -200) * x * 165); points.put(indexer, 0, (cur_pos != 6 ? 1 : -200) * y * 165, (cur_pos != 6 ? 1 : -200) * x * 165);
indexer += 1; indexer += 1;
} }
@ -214,11 +232,20 @@ public class python_test : MonoBehaviour
Core.perspectiveTransform(points, dstPoints, homography.inv()); Core.perspectiveTransform(points, dstPoints, homography.inv());
List<Point> transformedPoints = dstPoints.toList(); List<Point> transformedPoints = dstPoints.toList();
foreach (Point point in transformedPoints) indexer = 0;
for (int i = 0; i < 8; i++)
{ {
Imgproc.circle(loadedImage, point, 10, new Scalar(255, 0, 0), -1); 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;
}
} }
// MatDisplay.DisplayMat(loadedImage, MatDisplaySettings.FULL_BACKGROUND); // MatDisplay.DisplayMat(loadedImage, MatDisplaySettings.FULL_BACKGROUND);

View File

@ -3,5 +3,6 @@
<package id="DynamicLanguageRuntime" version="1.2.2" targetFramework="net471" /> <package id="DynamicLanguageRuntime" version="1.2.2" targetFramework="net471" />
<package id="IronPython" version="2.7.9" targetFramework="net471" /> <package id="IronPython" version="2.7.9" targetFramework="net471" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net471" /> <package id="Newtonsoft.Json" version="12.0.1" targetFramework="net471" />
<package id="RestSharp" version="106.6.9" targetFramework="net471" />
<package id="Unity.Newtonsoft.Json" version="7.0.0.0" targetFramework="net471" /> <package id="Unity.Newtonsoft.Json" version="7.0.0.0" targetFramework="net471" />
</packages> </packages>