Python 3.6
Unity 2019
我正在努力寻找将特征图数据传输到unity的最佳解决方案。
我想以字节为单位发送数据。然而,我没有找到如何将其编码为字节,然后统一解码。
基本上是一个4d阵列,需要根据我对的理解转换为字节
Python工件
for fmap in feature_maps:
bytes = []
bytes.append(fmap)
arrays_of_features.append(bytes)
data = np.array(arrays_of_features, dtype=float) # this is not working because of the fact is multidimensional array apparently.
print(fmap)
c.sendall(data.tobytes())
UNity工件:byte[]字节=新字节[4000];int idxUsedBytes=客户端。接收(字节(;
floatsReceived = new float[idxUsedBytes / 4];
Buffer.BlockCopy(bytes, 0, floatsReceived, 0, idxUsedBytes);
print(floatsReceived[0]);
启示:如何让Unity中的c#与Python 通信
一张功能图看起来像这样:
[[[[ 0. 0. 0. ... 0. 12.569366
0. ]
[ 0. 0. 0. ... 0. 4.421044
0. ]
[ 0. 0. 0. ... 0. 0.19193476
0. ]
...
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]]
[[ 0. 0. 0. ... 0. 12.910363
0. ]
[ 0. 0. 0. ... 0. 3.987629
0. ]
[ 0. 0. 0. ... 0. 1.6041028
0. ]
...
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]]
[[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
...
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]]
...
[[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
...
[ 0. 0. 0. ... 0. 0.
49.52598 ]
[ 0. 0. 0. ... 0. 0.
10.050183 ]
[ 0. 0. 0. ... 0. 9.6911745
0. ]]
[[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
...
[ 0. 0. 0. ... 0. 0.
29.483086 ]
[ 0. 0. 0. ... 0. 0.
24.422682 ]
[ 0. 0. 2.253025 ... 0. 0.
15.935954 ]]
[[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 18.458588 ... 15.824303 0.
0. ]
...
[ 0. 0. 0. ... 25.163502 56.87079
42.9939 ]
[ 0. 0. 11.397255 ... 36.644962 17.04247
44.108196 ]
[ 0. 0. 33.134758 ... 30.220499 8.817273
36.6427 ]]]]
你的问题很不清楚,我相信你对numpy的工作原理感到困惑。如果是,让我们解释一下。numpy中的数组不过是内存中的一个字节字符串。特别是,当为您显示这些字节时,它们将由数据类型进行解释。数据类型不是用来存储基础数据的,而只是用来显示它。因此,更改数据类型只会改变数据的外观,而不会改变数据本身。尺寸也是一样。数据的维度只会改变数据的显示和访问方式,python实际上不会移动数据或改变数据本身。例如,
import numpy as np
x = np.array([[1,2,3],[4,5,6]],dtype='int64') #48 bytes, each int takes up 8 bytes.
print(x)
x.dtype = 'int32'
print(x)
x.dtype = 'float'
print(x)
x.dtype = 'int16'
print(x)
请注意,我们可以更改dtype,并且绝对零计算由数组完成(因为底层数据已经是一个字节数组(。同样,我们可以改变形状,也可以进行绝对零计算。
x.shape = (2,2,6)
print(x)
形状和数据类型与内存中存储的数据无关。希望这能清楚地说明我们现在如何将数组作为字节进行处理。
x = np.array([[1,2,3],[4,5,6]],dtype='int64')
print(x)
y = x.tobytes()
# Send y somewhere. Save to a file. Etc.
z = np.frombuffer(y)
z.dtype = 'int64'
z.shape = (2,3)
print(z)