我有以下的类:
命名空间为Message;
struct BBox {
xmin:float;
xmax:float;
ymin:float;
ymax:float;
}
table msg {
key:string;
boxes: [BBox];
}
root_type Message;
为了创建对象,我会执行如下操作:
b = flatbuffers.Builder(0)
msg.msgStart(b)
msg.msgAddKey(b, b.CreateString(key))
v = flatbuffers.Builder(0)
size = len(boxes)
msg.msgBoxesVector(v, size)
for elem in boxes:
xmin, ymin, xmax, ymax = elem
BBox.CreateBBox(v, xmin, xmax, ymin, ymax)
boxes = v.EndVector(size)
msg.msgAddBoxes(b, boxes)
obj = msg.msgEnd(b)
b.Finish(obj)
并且不会抛出任何错误
然而,当我尝试显示结果时,键是正确的,但向量的大小和内容却是错误的。
rep = msg.msg.GetRootAsmsg(bytearray(b.Output()), 0)
print rep.BoxesLength() # give me 4 instead of 1
for i in range(rep.BoxesLength()):
print rep.Boxes(i).Xmin(), rep.Boxes(i).Ymin()
print rep.Boxes(i).Xmax(), rep.Boxes(i).Ymax()