np数组:
box
array([[838, 108],
[838, 0],
[995, 0],
[995, 108]], dtype=int64)
过程 先将np数组转成一维列表,然后将2个列表合成字典,然后输出json
import numpy
import json
box = numpy.array([[838,108],[838,0],[995,0],[995,108]])
dic_red = {}
dic_red["red"] = []
xy_key = ['x1','y1','x2','y2','x3','y3','x4','y4']
dic = box.tolist()
dic = [i for item in dic for i in item]
dic = dict(zip(xy_key,dic))
dic_red["red"].append(dic)
dicJson_red = json.dumps(dic_red,sort_keys=True, indent=4, separators=(',', ': '))
print(dicJson_red)
结果:
{
"red": [
{
"x1": 838,
"x2": 838,
"x3": 995,
"x4": 995,
"y1": 108,
"y2": 0,
"y3": 0,
"y4": 108
}
]
}