RestSharpy stuff
This commit is contained in:
parent
6fc555f8d8
commit
cc2c0843a0
BIN
AR-3/Assets/Plugins/RestSharp.dll
Normal file
BIN
AR-3/Assets/Plugins/RestSharp.dll
Normal file
Binary file not shown.
3516
AR-3/Assets/Plugins/RestSharp.xml
Normal file
3516
AR-3/Assets/Plugins/RestSharp.xml
Normal file
File diff suppressed because it is too large
Load Diff
|
@ -6,9 +6,11 @@ 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
|
||||
{
|
||||
|
@ -30,26 +32,27 @@ public class python_test : MonoBehaviour
|
|||
|
||||
Mat camImgCopy;
|
||||
|
||||
WebCamTexture _webcamtex;
|
||||
Mat camMatFromTexture;
|
||||
|
||||
Process process;
|
||||
ProcessStartInfo start = new ProcessStartInfo();
|
||||
Texture2D _TextureFromCamera;
|
||||
|
||||
RestClient restClient;
|
||||
|
||||
int viewMarked = 0;
|
||||
//StreamWriter sw;
|
||||
//StreamReader sr;
|
||||
|
||||
|
||||
// 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");
|
||||
camImgCopy = loadedImage.clone();
|
||||
//loadedImage = Imgcodecs.imread("/Users/alexandermunch-hansen/projects/chess_stuff/advancedskrald/whole_boards/boards_for_empty/board_1554285984.187497_rank_4.png");
|
||||
//camImgCopy = loadedImage.clone();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
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 args = "";
|
||||
|
@ -60,56 +63,120 @@ public class python_test : MonoBehaviour
|
|||
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))
|
||||
|
||||
|
||||
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(WebCamTexture.devices[0].name, 1920, 1080);
|
||||
_webcamtex.Play();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
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);
|
||||
|
||||
return response.Content;
|
||||
}
|
||||
|
||||
/*
|
||||
using (StreamWriter writer = process.StandardInput)
|
||||
{
|
||||
writer.WriteLine(base64);
|
||||
writer.Close();
|
||||
}
|
||||
|
||||
process.WaitForExit();
|
||||
|
||||
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;
|
||||
|
||||
Mat textMat = null;
|
||||
|
||||
if (camImg != null)
|
||||
{
|
||||
_TextureFromCamera.SetPixels(_webcamtex.GetPixels());
|
||||
_TextureFromCamera.Apply();
|
||||
//byte[] bytes = _TextureFromCamera.EncodeToPNG();
|
||||
//string filePath = "SavedScreen1.png";
|
||||
//File.WriteAllBytes(filePath, bytes);
|
||||
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))
|
||||
{
|
||||
print("fuck");
|
||||
viewMarked = 0;
|
||||
} else if (Input.GetKeyDown(KeyCode.S))
|
||||
{
|
||||
print("cock");
|
||||
viewMarked = 1;
|
||||
}
|
||||
|
||||
|
||||
if (Input.GetKeyDown("space"))
|
||||
{
|
||||
//ScreenCapture.CaptureScreenshot("plspls.png");
|
||||
|
||||
loadedImage = camMatFromTexture.clone();
|
||||
|
||||
// TODO: We should clone camImageMat again, instead of the loadedImage I use now
|
||||
/*
|
||||
var camImgCopy = camImageMat.clone();
|
||||
outputMat = camImageMat.clone();
|
||||
*/
|
||||
|
||||
//loadedImage = camImageMat.clone();
|
||||
//camImgCopy = camImageMat.clone();
|
||||
|
||||
print("------------------------------------");
|
||||
|
||||
print(loadedImage.size());
|
||||
//print(loadedImage.size());
|
||||
camImageMat = loadedImage.clone();
|
||||
|
||||
print("------------------------------------");
|
||||
|
||||
var pls = runCmd(camImageMat);
|
||||
|
||||
var pls = runCmd(camMatFromTexture);
|
||||
|
||||
|
||||
Mat homography = new Mat(new Size(3, 3), CvType.CV_64F);
|
||||
|
||||
|
@ -117,14 +184,12 @@ public class python_test : MonoBehaviour
|
|||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -138,12 +203,12 @@ public class python_test : MonoBehaviour
|
|||
for (int x = 1; x < 9; x++)
|
||||
{
|
||||
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);
|
||||
indexer += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
MatOfPoint2f dstPoints = new MatOfPoint2f();
|
||||
|
||||
Core.perspectiveTransform(points, dstPoints, homography.inv());
|
||||
|
@ -155,14 +220,22 @@ public class python_test : MonoBehaviour
|
|||
Imgproc.circle(loadedImage, point, 10, new Scalar(255, 0, 0), -1);
|
||||
}
|
||||
|
||||
// MatDisplay.DisplayMat(loadedImage, MatDisplaySettings.FULL_BACKGROUND);
|
||||
|
||||
//process.Close();
|
||||
//process = Process.Start(start);
|
||||
|
||||
}
|
||||
|
||||
if (viewMarked == 0)
|
||||
{
|
||||
MatDisplay.DisplayMat(camMatFromTexture, MatDisplaySettings.FULL_BACKGROUND);
|
||||
|
||||
} else if (viewMarked == 1)
|
||||
{
|
||||
MatDisplay.DisplayMat(loadedImage, MatDisplaySettings.FULL_BACKGROUND);
|
||||
displayWarped = true;
|
||||
|
||||
}
|
||||
|
||||
if (!displayWarped)
|
||||
MatDisplay.DisplayMat(camImgCopy, MatDisplaySettings.FULL_BACKGROUND);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user