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