You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Rekognition批量处理S3图片及结果保存等技术咨询

AWS Rekognition批量处理S3图片及问题解答

1. 修改脚本批量处理S3桶内所有图片

要处理S3桶里的全部图片,核心是先遍历桶内的图片对象,再逐个调用Rekognition接口。以下是修改后的完整示例:

import boto3
import json
import csv
import os

# 初始化客户端
s3 = boto3.client('s3')
rekognition = boto3.client('rekognition')

# 配置参数
BUCKET_NAME = 'your-bucket-name'
OUTPUT_DIR = './rekognition_results/'  # 本地保存结果的目录

# 确保输出目录存在
os.makedirs(OUTPUT_DIR, exist_ok=True)

# 遍历S3桶内的所有图片对象
def list_s3_images(bucket):
    paginator = s3.get_paginator('list_objects_v2')
    for page in paginator.paginate(Bucket=bucket):
        if 'Contents' not in page:
            continue
        for obj in page['Contents']:
            # 过滤常见图片格式,可按需扩展
            if obj['Key'].lower().endswith(('.jpg', '.jpeg', '.png', '.bmp')):
                yield obj['Key']

# 批量处理逻辑
for image_key in list_s3_images(BUCKET_NAME):
    print(f"Processing image: {image_key}")
    try:
        # 调用Rekognition标签检测接口
        response = rekognition.detect_labels(
            Image={
                'S3Object': {
                    'Bucket': BUCKET_NAME,
                    'Name': image_key
                }
            },
            MaxLabels=10,
            MinConfidence=70
        )
        
        # 结果保存逻辑(见下方JSON/CSV代码)
        
    except Exception as e:
        print(f"Error processing {image_key}: {str(e)}")

2. 保存检测结果为同名JSON/CSV文件

保存为JSON文件

在批量处理循环中,添加以下代码即可将结果保存为与原图片同名的JSON文件:

# 生成结果文件名:替换图片后缀为.json
file_name = os.path.splitext(os.path.basename(image_key))[0] + '.json'
output_path = os.path.join(OUTPUT_DIR, file_name)

# 写入JSON文件
with open(output_path, 'w', encoding='utf-8') as f:
    json.dump(response, f, indent=4, ensure_ascii=False)
print(f"Result saved to: {output_path}")

保存为CSV文件

如果需要结构化的CSV格式,可以提取Labels中的关键信息(如标签名、置信度)进行保存:

# 生成CSV文件名
file_name = os.path.splitext(os.path.basename(image_key))[0] + '.csv'
output_path = os.path.join(OUTPUT_DIR, file_name)

# 整理标签数据
csv_data = [['LabelName', 'Confidence', 'InstancesCount']]
for label in response['Labels']:
    instances_count = len(label.get('Instances', []))
    csv_data.append([
        label['Name'],
        round(label['Confidence'], 2),
        instances_count
    ])

# 写入CSV文件
with open(output_path, 'w', newline='', encoding='utf-8') as f:
    writer = csv.writer(f)
    writer.writerows(csv_data)
print(f"Result saved to: {output_path}")

3. 关于Rekognition每日50张限额的说法

这个说法不准确。AWS Rekognition的免费套餐规则是:新注册用户在前12个月内,每月可享受5000次免费的DetectLabels(标签检测)调用,超出部分按按量付费标准计费。

对于付费用户,没有固定的每日/每月限额,只要AWS账户处于正常状态且有足够额度,就可以根据需求调用接口,仅受服务的默认并发限制(可通过AWS Support申请调整)。


内容的提问来源于stack exchange,提问作者RL-1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 09:25:08