gpt4 book ai didi

python - 使用 FlatBuffers 编写结构向量

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:52 24 4
gpt4 key购买 nike

我有以下类(class):

命名空间消息;

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()

最佳答案

我们有一个关于 Python 端口没有做足够的错误检查的未决问题:https://github.com/google/flatbuffers/issues/299

字符串和向量的创建应该发生在 msgStart 之前。另外,您应该只使用一个 Builder 对象(只使用 b,而不是 v),因为上面的代码从一个缓冲区引用到另一个缓冲区,这是行不通的。

编辑:当您尝试嵌套向量/字符串/表格生成时,Python 实现现在可以正确地发出错误信号。但是,它仍然无法检测到跨缓冲区偏移量。

关于python - 使用 FlatBuffers 编写结构向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33482163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com