我正在嘗試創建一個名為 Results 的嵌套 Python 字典。
我正在使用 AWS Rekognition 獲取影像并輸出結果。
results_dict 在編譯后只包含一個結果,我希望所有結果都在一個嵌套回圈中
我試圖得到:
{
"Results": [
{
"Name": "Human",
"Confidence": 98.87621307373047,
},
{
"Name": "Face",
"Confidence": 98.87621307373047,
},
{
"Name": "Person",
"Confidence": 98.87621307373047,
},
]
}
但我得到:
{
'Results':
{
'Name': 'Paper',
'Confidence': 57.299766540527344
}
}
代碼正在替換文本,我想添加另一組名稱和信心。
我的代碼是:
import boto3
import json
BUCKET = "*****"
FOLDER = 'testing/'
JOEY = FOLDER "Joey_30_Sept.png"
BEYONCE = FOLDER "beyonce_rekognition_moderation_testing.jpg"
MANBEARD = FOLDER "man_beard.jpg"
MEN = FOLDER "men_group.jpg"
client = boto3.client('rekognition')
response = client.detect_labels(Image=
{'S3Object': {
'Bucket': BUCKET,
'Name': JOEY
}},
MaxLabels = 10,
MinConfidence=0)
results_dict = {}
results_dict['Results'] = {}
results_dict['Results']['Name'] = ""
results_dict['Results']['Confidence'] = ""
for label in response['Labels']:
name = label['Name'] #to get the whole bounding box.
confidence = label['Confidence']
name_str = str(name)
conf_str = str(confidence)
results_dict["Results"]["Name"] = label['Name']
results_dict["Results"]["Confidence"] = label['Confidence']
print(results_dict)
uj5u.com熱心網友回復:
您將results_dict['Results']字典定義為 dict 而不是串列:
...
results_dict = {}
results_dict['Results'] = []
results_dict['Results']['Name'] = ""
results_dict['Results']['Confidence'] = ""
for label in response['Labels']:
name = label['Name'] #to get the whole bounding box.
confidence = label['Confidence']
name_str = str(name)
conf_str = str(confidence)
results_dict['Results'].append({["Name": name_str, "Confidence": conf_str })
print(results_dict)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/386382.html
