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

先将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]}

猜你喜欢

转载自blog.csdn.net/qq_35831906/article/details/124087949
今日推荐