【发布时间】:2020-03-15 22:45:15
【问题描述】:
[
{
'topleft':{
'x':34,
'y':13
},
'confidence':0.17681329,
'bottomright':{
'x':398,
'y':347
},
'label':'person'
},
{
'topleft':{
'x':29,
'y':48
},
'confidence':0.107129775,
'bottomright':{
'x':399,
'y':351
},
'label':'car'
},
{
'topleft':{
'x':20,
'y':85
},
'confidence':0.22963998,
'bottomright':{
'x':376,
'y':350
},
'label':'cat'
},
{
'topleft':{
'x':0,
'y':2
},
'confidence':0.12423642,
'bottomright':{
'x':372,
'y':356
},
'label':'sheep'
},
{
'topleft':{
'x':20,
'y':12
},
'confidence':0.26517922,
'bottomright':{
'x':378,
'y':349
},
'label':'dog'
}
]
这是从 TensorFlow 返回的对象数组。但是,在尝试使用 json.dumps 转换为 JSON 对象时,我收到了此错误 TypeError: 0.17681329 is not JSON serializable
我尝试使用 simplejson 并得到 Float32 不可序列化。如何将此对象数组转换为 JSON 可序列化?
【问题讨论】:
-
看起来像这样:Convert numpy type to python。您的 float 值(大概)是 numpy float 或特定于 tensorflow 的类型 float,而不是 python float。
-
可能是那个或小数:在我的回答中是一种将它们转换为浮点数的方法。
-
如果答案有帮助,请考虑将其标记为正确。