AWS Boto3(Python):如何将多个图像输入的多个响应保存到单个文件?

AWS Boto3(Python): how to save multiple response from multiple image input to individual file?

以下代码用于 AWS Rekognition 图像标签检测。

问题1)由于AWS Recognition一次只能运行一张图片,我使用[List]一次添加多张图片。但是,如果我想要 运行 100 张图像怎么办?这意味着我必须手动为 [List] 中的 100 张图像写下名称,这将花费很长时间。假设我有 100 张名为 image1、image2、image3、... image100 的图像,解决此问题的最佳方法是什么?

问题 2) 下面的代码会将所有 3 张图像的响应保存到一个 JSON 文件中。如何将响应保存到单个文件中?

# List to record all the responses    
responselist=[]
list=['picture1.jpg','picture2.jpg','picture 3.jpg'] 
for image in list :
    response = client.detect_labels(
    Image={
    'S3Object': {
    'Bucket': 'test1',
    'Name': image
        }})
    responselist.append(response)

print(response)

# JASON file Save
json_file = json.dumps(response)
Path('rekognition_test.json').write_text(json_file)    

发布的代码与您认为的不一样。 具体来说,它 不保存 来自 三个图像。只有最后一个 response 是 已保存。

这是因为您正在创建一个responselist 根本没有使用过。也不要打电话给你 列出 list,因为它是 python 的实际 list 数据结构。

但是,如果你想为每张图片分别写出结果 您可以尝试以下操作:

my_list=['picture1.jpg','picture2.jpg','picture 3.jpg'] 

for image in my_list:

    response = client.detect_labels(
                  Image={
                  'S3Object': {
                  'Bucket': 'test1',
                  'Name': image
                }})

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