C#Unity-在Unity中从相机中提取像素阵列,并在python中显示
我试图做两件事:C#Unity-在Unity中从相机中提取像素阵列,并在python中显示,python,c#,arrays,unity3d,ml-agent,Python,C#,Arrays,Unity3d,Ml Agent,我试图做两件事: 从相机中检索84 x 84 x 3像素阵列 将此数组发送到python脚本,在那里可以显示它 我被1)卡住了,所以目前我只是将bytearray写入一个txt文件,然后用python打开该文件。然而,我不断得到这张图片,这让我相信bytearray实际上并没有用像素数据正确实例化。 [] 以下是C#unity代码: using System.IO; using UnityEngine; using MLAgents.Sensors; // using UnityEngine.
using System.IO;
using UnityEngine;
using MLAgents.Sensors;
// using UnityEngine.CoreModule;
public class TrainingAgent : Agent, IPrefab
{
public Camera cam;
private RenderTexture renderTexture;
public int bytesPerPixel;
private byte[] rawByteData;
private Texture2D texture2D;
private Rect rect;
public override void Initialize()
{
renderTexture = new RenderTexture(84, 84, 24);
rawByteData = new byte[84 * 84 * bytesPerPixel];
texture2D = new Texture2D(84, 84, TextureFormat.RGB24, false);
rect = new Rect(0, 0, 84, 84);
cam.targetTexture = renderTexture;
}
public override void CollectObservations(VectorSensor sensor)
{
run_cmd();
}
private void run_cmd()
{
cam.Render();
// Read pixels to texture
RenderTexture.active = renderTexture;
texture2D.ReadPixels(rect, 0, 0);
Array.Copy(texture2D.GetRawTextureData(), rawByteData, rawByteData.Length);
File.WriteAllBytes("/Path/to/byte/array/Foo.txt", rawByteData); // Requires System.IO
}
}
下面是python代码:
from PIL import Image
import numpy as np
fh = open('/Path/to/byte/array/foo.txt', 'rb')
ba = bytearray(fh.read())
data = np.array(list(ba)).reshape(84,84,3)
img = Image.fromarray(data, 'RGB')
img.show()
任何帮助都将不胜感激,因为我不知道哪里出了问题,我的调试尝试似乎是徒劳的。我不确定(不了解python的细节),但我不认为这是您希望在这里使用的
您更愿意使用(前者)导出实际的图像二进制文件,例如JPG
将此纹理编码为JPG格式
返回的字节数组是JPG“文件”。您可以将它们写入磁盘以获取JPG文件,通过网络发送它们,等等
此函数仅适用于未压缩的非HDR纹理格式。必须在纹理导入设置中启用纹理的读/写启用标志。编码的JPG数据将没有alpha通道
然后将其作为实际图像文件加载到python中
在您的代码中,这相当于将最后两行C#替换为:
rawByteData = ImageConversion.EncodeToJPG(texture2D);
File.WriteAllBytes("/Path/to/jpg/foo.jpg", rawByteData);
我不确定(不知道python的详细情况),但我不认为这是您希望在这里使用的
您更愿意使用(前者)导出实际的图像二进制文件,例如JPG
将此纹理编码为JPG格式
返回的字节数组是JPG“文件”。您可以将它们写入磁盘以获取JPG文件,通过网络发送它们,等等
此函数仅适用于未压缩的非HDR纹理格式。必须在纹理导入设置中启用纹理的读/写启用标志。编码的JPG数据将没有alpha通道
然后将其作为实际图像文件加载到python中
在您的代码中,这相当于将最后两行C#替换为:
rawByteData = ImageConversion.EncodeToJPG(texture2D);
File.WriteAllBytes("/Path/to/jpg/foo.jpg", rawByteData);
您尝试过Image.frombytes()吗?如果您能以某种方式发送指向.txt文件的链接,我可以检查它是否能正常工作。您尝试过Image.frombytes()吗?如果你能以某种方式发送一个指向.txt文件的链接,我可以检查它是否能正常工作。