如何使用AWS AI Service Cards – Amazon Rekognition ?

什么是 Amazon Rekognition?

Amazon Rekognition 提供预先训练和可自订的电脑视觉 (CV) 功能,可从影像和影片中撷取资料和分析。

为何要使用 AWS AI Service?

AI影像辨识需要大量运算资源跟训练资料,我们可以用AWS的AI Service使其在AWS Server做运算,省下不少麻烦。

怎么使用 Amazon Rekognition?

这次使用的AWS是教育方案的Learner Lab,差别在某些的功能限制以及IAM 角色只能使用"LabRole",接下来会展示Amazon Rekognition的文字辨识功能。

下面是接下来的範例架构图。
http://img2.58codes.com/2024/20166878vtH16rNGRa.png

功能说明:使用lambda的测试事件将S3的指定图片传到Lambda去做Amazon Rekognition的文字辨识,并将结果用Python的 OpenCV 将结果框起来并标籤。

输入图片
http://img2.58codes.com/2024/20166878IeluLtMIps.jpg

输出图片
http://img2.58codes.com/2024/20166878gQ8ScpHi2e.jpg

操作图

确认lambda的测试事件里的图片是否有存在于指定的S3贮存桶。点击 "测试"
http://img2.58codes.com/2024/20166878GsOPNZLxqZ.png

http://img2.58codes.com/2024/20166878jPCj5b78Ld.png

查看S3贮存桶
http://img2.58codes.com/2024/20166878l5ALBsebf5.png执行lambda

点击 "程式码"
http://img2.58codes.com/2024/20166878OzIUxzetME.png

点击 "Test"
http://img2.58codes.com/2024/201668784pRbeXs8aR.png
3.查看结果

回到S3查看是否回传结果图 "ress.jpg"
http://img2.58codes.com/2024/20166878DzJ6OuNSmQ.png

操作流程

建立lambda点击 "建立函式"
http://img2.58codes.com/2024/20166878lWD7l9t7qI.png

http://img2.58codes.com/2024/20166878su8nLrht5v.png
2. 新增层

关于step2 可以参考OpenCV Layer建立非常清楚 。
http://img2.58codes.com/2024/20166878KXr1OM6W5A.png

建立完毕后,到lambda 程式码最下面,点击 "新增层"。
http://img2.58codes.com/2024/20166878XXaRmuTst8.png
http://img2.58codes.com/2024/20166878Oap77lzwVV.png

编辑程式码回到上面lambda_function.py 将里面的程式码全部改掉,换成以下程式码。
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"。
http://img2.58codes.com/2024/20166878fuvNTxAeIu.png上传图片到S3
点击 "上传"。
http://img2.58codes.com/2024/20166878hdlgkwSHiw.png
点击 "新增档案"。
http://img2.58codes.com/2024/201668789eO3TRal74.png可以依你上传的图片的附档名去更改 lambda_function.py 的输出图片的副档名。新增lambda 测试事件

会到lambda 点击 "测试"
http://img2.58codes.com/2024/20166878S1rM6fy3ng.png
http://img2.58codes.com/2024/20166878DYrWiv3Zdt.png

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执行时间点击 "组态"
http://img2.58codes.com/2024/20166878VUoEykoSyO.png点击 "一般组态" 并 "编辑"。

http://img2.58codes.com/2024/20166878GNtu6KufNH.png

更改逾时时间

http://img2.58codes.com/2024/20166878JvKttytOIQ.png
7. 执行程式

回到程式码并点击 "Test"
http://img2.58codes.com/2024/20166878gpDCUYfBjU.png

查看step2的讯息是否跟下面的讯息一样
http://img2.58codes.com/2024/20166878mYAweuoDjJ.png

回到 S3 查看是否有 "ress.jpg" 的资料,有就代表成功了。
http://img2.58codes.com/2024/20166878nZb8gQOH7C.png
参考资料

OpenCV Layer建立什么是Amazon RekognitionOpenCV 範例函式Lambda 事件JSON範例

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章