首页 > 解决方案 > AWS Boto3(Python):使用路径保存多个响应

问题描述

从我从 AWS Rekognition (boto3 = python) 使用的以下脚本:

pics = [f'LP {num}.jpg' for num in range(1, 5)]
for pic in pics :
    request = {
        'S3Object': {
            'Bucket': 'test1',
            'Name': pic
        }
    }
 
response = client.detect_labels(Image=request)

json_file = json.dumps(response)
Path(f"{pic}.json").write_text(json_file)

我正在尝试运行 4 个名为 LP 1、LP 2、LP 3 和 LP 4 的图像,然后将每个响应保存到一个单独的 JSON 文件中。

问题 1:我不知道为什么,但我的脚本只保存了一个响应文件,而不是全部 4 个图像。例如,如果我将范围设置为 (1,5),则仅保存 LP 4 的响应。如果我将范围设置为 (1,4),则仅保存 LP 3 的响应。

问题 2:我正在尝试使用以下脚本以特定格式保存响应,但效果不佳:

    print(f"{label['Name']} - {label['Confidence']}")

谢谢!

标签: pythonamazon-web-servicesboto3amazon-rekognition

解决方案


pics = [f'LP {num}.jpg' for num in range(1, 5)]
for pic in pics :
    request = {
        'S3Object': {
            'Bucket': 'test1',
            'Name': pic
        }
    }
    response = client.detect_labels(Image=request)
    json_file = json.dumps(response)
    # what is the value of Key? - change it during the loop 
    Path(f"{Key}.json").write_text(json_file)

推荐阅读