jsonを生成するコードを作成しました。コードは:
import collections
d = collections.defaultdict(dict)
with open(res_file, 'w') as f:
for i, box in enumerate(boxes):
a=np.split(poly,4)
d[i][str(0)] = str(a[0])
d[i][str(1)] = str(a[1])
d[i][str(2)] = str(a[2])
d[i][str(3)] = str(a[3])
y = json.dumps(d)
print(y)
その出力は
{"0":{"0": "[429 44]"、 "1": "[436 44]"、 "2": "[436 56]"、 "3": "[429 56]"} 、 "1":{"0": "[345 41]"、 "1": "[431 44]"、 "2": "[430 69]"、 "3": "[344 66]"} 、 "2":{"0": "[453 42]"、 "1": "[554 42]"、 "2": "[554 68]"、 "3": "[453 68]"} 、 "3":{"0": "[654 45]"、 "1": "[710 45]"、 "2": "[710 76]"、 "3": "[654 76]"} 、 "4":{"0": "[436 48]"、 "1": "[449 48]"、 "2": "[449 66]"、 "3": "[436 66]"} 、 "5":{"0":"[153 58]"、 "1": "[287 61]"、 "2": "[286 97]"、 "3": "[152 93]"}、 "6":{"0": "[345 70]"、 "1": "[438 70]"、 "2": "[438 94]"、 "3": "[345 94]"}、 "7":{"0": "[442 69]"、 "1": "[477 69]"、 "2": "[477 94]"、 "3": "[442 94]"}、 "8":{"0": "[481 69]"、 "1": "[602 69]"、 "2": "[602 94]"、 "3": "[481 94]"}、 "9":{"0": "[638 76]"、 "1": "[724 76]"、 "2": "[724 94]"、 "3": "[638 94]"}、 "10":{"0": "[293 117]"、 "1 ":" [313 117] "、" 2 ":" [313 132] "、" 3 ":" [293 132] "}、" 11 ":{" 0 ":" [316 117] "、" 1 ":" [361 117] "、" 2 ":" [361 132] "、" 3 ":" [316 132] "}
これを実行したい:すべてのボックス(上記の例では11個のボックス)が正常に実行された場合は、結果に次のコマンドを追加します。
"Message":"success",
"Status":1,
最終的な出力jsonは次のようになります。
{
"Status":1,
"Message":"success",
"Result":{
"1":{
"1":"[431,44]",
"0":"[345,41]",
"3":"[344,66]",
"2":"[430,69]"
},
"0":{
"1":"[436,44]",
"0":"[429,44]",
"3":"[429,56]",
"2":"[436,56]"
},
...
}
}
それ、どうやったら出来るの?
必須フィールドを使用して、もう1つのdictを作成できます。
from collections import defaultdict
d = defaultdict(dict)
with open(res_file, 'w') as f:
for i, box in enumerate(boxes):
poly = np.array(box).astype(np.int32).reshape((-1))
a = np.split(poly, 4)
for j in range(4):
d[i][str(j)] = str(a[j])
result = {
'Status': int(bool(d)), # 0 - dict empty, 1 - dict not empty
'Message': 'success' if d else 'fail', # ('fail', 'success')[int(bool(d))]
'Result': d
}
この辞書をインデントして印刷する必要がある場合は、次を使用できますjson.dumps()
。
import json
print(json.dumps(result, indent=4))
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加