1

I am trying to create one nested Python dictionary called Results.

I am using AWS Rekognition to get an image and output the results.

The results_dict only contains one result after it's complated, and I wish to have all the results in one nested loop

I'm trying to get:

{
    "Results": [
        {
            "Name": "Human",
            "Confidence": 98.87621307373047,
        },
                {
            "Name": "Face",
            "Confidence": 98.87621307373047,
        },
        {
            "Name": "Person",
            "Confidence": 98.87621307373047,
        },
        
    ]
}

But I'm getting:

{
    'Results': 
        {
          'Name': 'Paper', 
          'Confidence': 57.299766540527344
        }
}

The code is replacing the text, and I want to add another set of Name and Confidence.

My code is:

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)

1 Answer 1

1

You defined results_dict['Results'] as dictionary as dict not list:

...
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)
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thank you. I had been working on coding all day with no break. I needed another set of eyes to see the error. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.