什么是 Amazon Rekognition?
Amazon Rekognition 提供预先训练和可自订的电脑视觉 (CV) 功能,可从影像和影片中撷取资料和分析。
为何要使用 AWS AI Service?
AI影像辨识需要大量运算资源跟训练资料,我们可以用AWS的AI Service使其在AWS Server做运算,省下不少麻烦。
怎么使用 Amazon Rekognition?
这次使用的AWS是教育方案的Learner Lab,差别在某些的功能限制以及IAM 角色只能使用"LabRole",接下来会展示Amazon Rekognition的文字辨识功能。
下面是接下来的範例架构图。
功能说明:使用lambda的测试事件将S3的指定图片传到Lambda去做Amazon Rekognition的文字辨识,并将结果用Python的 OpenCV 将结果框起来并标籤。
输入图片
输出图片
操作图
确认lambda的测试事件里的图片是否有存在于指定的S3贮存桶。点击 "测试"查看S3贮存桶
执行lambda
点击 "程式码"
点击 "Test"
3.查看结果
回到S3查看是否回传结果图 "ress.jpg"
操作流程
建立lambda点击 "建立函式"
2. 新增层
关于step2 可以参考OpenCV Layer建立非常清楚 。
建立完毕后,到lambda 程式码最下面,点击 "新增层"。
import jsonimport boto3from datetime import datetimeimport cv2def lambda_handler(event, context): s3 = boto3.client('s3') # Get the bucket name and the uploaded file name bucket_name = event['Records'][0]['s3']['bucket']['name'] file_name = event['Records'][0]['s3']['object']['key'] print('Bucket name: {}'.format(bucket_name)) print('Upload file name: {}'.format(file_name)) #建立 Amazon Rekognition OCR client = boto3.client('rekognition') response = client.detect_text(Image={'S3Object':{'Bucket':bucket_name,'Name':file_name}}) print(json.dumps(response)) # 将辨识结果用OpenCV的函式框起来并标记号。 image_path = '/tmp/input_image.jpg' s3.download_file(bucket_name, file_name, image_path) output_image_path = '/tmp/output_image.jpg' draw_boxes_and_put_text_with_order(image_path, response['TextDetections'], output_image_path) # #将结果图放回该原图的S3位置,并命名为 "ress.jpg" (可以依照你输入的图片的副档名去更改)。 upload_image_to_s3(output_image_path, bucket_name, 'ress.jpg') return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') }def draw_boxes_and_put_text_with_order(image_path, text_detections, output_image_path): # Load the image image = cv2.imread(image_path) # Sort text detections by their top coordinate sorted_detections = sorted(text_detections, key=lambda x: x['Geometry']['BoundingBox']['Top']) # Draw bounding boxes and put text with order on the image for i, detection in enumerate(sorted_detections): box = detection['Geometry']['BoundingBox'] width, height = image.shape[1], image.shape[0] left = int(width * box['Left']) top = int(height * box['Top']) right = int(left + (width * box['Width'])) bottom = int(top + (height * box['Height'])) # Draw the bounding box cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2) # Put text with order on the image text = '{}: {}'.format(i+1, detection['DetectedText']) cv2.putText(image, str(i+1), (left, top-10), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 2) # Save the image with bounding boxes and text cv2.imwrite(output_image_path, image)def upload_image_to_s3(image_path, bucket_name, key_name): s3 = boto3.client('s3') with open(image_path, 'rb') as file: s3.upload_fileobj( file, bucket_name, key_name, ExtraArgs={'ACL': 'public-read'} )
编辑完成点击 "Deploy"。上传图片到S3
点击 "上传"。
点击 "新增档案"。
可以依你上传的图片的附档名去更改 lambda_function.py 的输出图片的副档名。新增lambda 测试事件
会到lambda 点击 "测试"
step2 的事件JSON请用以下程式码(需要替换成你的S3贮存桶名称跟图片名称)。
{ "Records": [ { "eventVersion": "2.1", "eventSource": "aws:s3", "awsRegion": "us-east-1", "eventTime": "2024-04-29T00:00:00.000Z", "eventName": "ObjectCreated:Put", "userIdentity": { "principalId": "EXAMPLE" }, "requestParameters": { "sourceIPAddress": "127.0.0.1" }, "responseElements": { "x-amz-request-id": "EXAMPLE123456789", "x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH" }, "s3": { "s3SchemaVersion": "1.0", "configurationId": "testConfigRule", "bucket": { "name": "你的S3贮存桶名称", "ownerIdentity": { "principalId": "EXAMPLE" }, "arn": "arn:aws:s3:::你的S3贮存桶名称" }, "object": { "key": "你的图片名称(包含副档名)", "size": 1024, "eTag": "0123456789abcdef0123456789abcdef", "versionId": "096fKKXTRTtl3on89fVO.nfljtsv6qko", "sequencer": "0A1B2C3D4E5F678901" } } } ]}
更改lamda执行时间点击 "组态"点击 "一般组态" 并 "编辑"。更改逾时时间
7. 执行程式
回到程式码并点击 "Test"
查看step2的讯息是否跟下面的讯息一样
回到 S3 查看是否有 "ress.jpg" 的资料,有就代表成功了。
参考资料