1

I am trying to send Microsoft's Kinect v2 body data over MQTT to effectively map skeletal data without direct connection to the Kinect, but I can't seem to deserialize a Body[] correctly. I am publishing the a list of Bodys every frame in Update().

My current setup is using Newtonsoft's JSON.Net to serialize a List taken from Body[] and publish it to MQTT (using https://github.com/vovacooper/Unity3d_MQTT). I used this since the Body class isn't serializable (so I can't use JSONUtility?).

Essentially I have:

 void Update() {
     ...
     //trackedBodies is a List<Body> that contains the tracked Bodys
     //client is MQTTClient that is connected
     string bodyData = JsonConvert.SerializeObject(trackedBodies);
     client.Publish("test", System.Text.Encoding.UTF8.GetBytes(bodyData));
     ...
 }

And:

 void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) {
     //Check MQTT for data, then deserialize
    List<Body> bodyData = JsonConvert.DeserializeObject<List<Body>>(System.Text.Encoding.UTF8.GetString(e.Message));
     Debug.Log(bodyData);
 }

When the List is empty, I am able to receive an empty List of Bodys. When it is nonempty, the code halts for that method, and I am no longer able to receive messages at all. The Update() method still works properly.

I would appreciate it if anybody knows how to help with what I currently have, or suggest a better alternative to my problem.

4

2 回答 2

2

转换为 JSON 的数据可能太多。也许您应该 1) 寻找替代的 JSON 库或 2) 自己解码和编码对象,只传输您需要的数据。

就像是:

void Update() {
...
    //trackedBodies is a List<Body> that contains the tracked Bodys
    //client is MQTTClient that is connected
    string bodyData;
    foreach(Body body: trackedBodies)
        bodyData += "|" + body.X + ";" + body.Y + ";" + body.Z;

    client.Publish("test", System.Text.Encoding.UTF8.GetBytes(bodyData));
    ...
}

也看看这里:https ://www.newtonsoft.com/json/help/html/Performance.htm#ManuallySerialize

能否请您显示里面有什么样的数据trackedBodies

于 2018-02-07T11:26:34.447 回答
0

我看到有些人可能也有同样的问题。我终于为自己找到了一个可行的解决方案。我个人只是通过创建一个自定义的“伪”类来表示 Body 并保存 Joint 和 JointOrientation 数组来公开 Body 对象。通过使用 Json.NET 对它们进行序列化,我能够通过 MQTT 发送这些自定义对象的列表。

干杯。

于 2018-02-16T01:14:54.740 回答