TypeError: Object of type ‘float32‘ is not JSON serializable

TypeError: Object of type ‘float32‘ is not JSON serializable,第1张

先将list转换成numpy.array,在将numpy.array转换成list

import numpy as np
import json
b=np.array(a).tolist()
json_str=json.dumps(b)

ok

示例:

def load_datum(filename):
    with open(osp.join(data_root,'pointlines',filename+'.pkl'),'rb') as handle:
        d = pickle.load(handle, encoding='latin1')
        h, w = d['img'].shape[:2]
        points = d['points']
        lines = d['lines']
        junctions = d ['junction']
        lsgs = np.array([[points[i][0], points[i][1], points[j][0], points[j][1]] for i, j in lines],
                        dtype=np.float32)
        image = d['img']
    
    return image, {'filename': filename+'.png', 
            'lines'   : lsgs.tolist(), 'junctions'   : np.array(junctions).tolist(), 'height':image.shape[0], 'width': image.shape[1]}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/578449.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-11
下一篇 2022-04-11

发表评论

登录后才能评论

评论列表(0条)

保存